Skip to content

Commit

Permalink
Implement Labels Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dakshina99 committed Jan 24, 2025
1 parent 364ea98 commit c602250
Show file tree
Hide file tree
Showing 55 changed files with 3,606 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO;
import org.wso2.carbon.apimgt.api.dto.KeyManagerPermissionConfigurationDTO;
import org.wso2.carbon.apimgt.api.model.APICategory;
import org.wso2.carbon.apimgt.api.model.ApiResult;
import org.wso2.carbon.apimgt.api.model.Application;
import org.wso2.carbon.apimgt.api.model.ApplicationInfo;
import org.wso2.carbon.apimgt.api.model.Environment;
import org.wso2.carbon.apimgt.api.model.LLMProvider;
import org.wso2.carbon.apimgt.api.model.Label;
import org.wso2.carbon.apimgt.api.model.Monetization;
import org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo;
import org.wso2.carbon.apimgt.api.model.Workflow;
Expand Down Expand Up @@ -266,6 +268,81 @@ void updateMonetizationUsagePublishInfo(MonetizationUsagePublishInfo monetizatio
*/
APICategory getAPICategoryByID(String apiCategoryId) throws APIManagementException;

/**
* Adds a new label for the tenant
*
* @param label label to add
* @param tenantDomain tenant domain
* @throws APIManagementException if failed add label
*/
Label addLabel(Label label, String tenantDomain) throws APIManagementException;

/**
* Updates a label
*
* @param label label to update
* @throws APIManagementException if failed update label
*/
void updateLabel(Label label) throws APIManagementException;

/**
* Delete a label
*
* @param labelID label ID to delete
* @throws APIManagementException if failed delete label
*/
void deleteLabel(String labelID) throws APIManagementException;

/**
* Checks whether a label exists by the given name when updating the label
*
* @param labelName label name to check
* @param uuid label UUID
* @param tenantDomain tenant domain
* @return true if a label exists by the given label name
* @throws APIManagementException if failed
*/
boolean isLabelNameExists(String labelName, String uuid, String tenantDomain) throws APIManagementException;

/**
* Checks whether a label exists by the given name when creating the label
*
* @param labelName label name to check
* @param tenantDomain tenant domain
* @return true if a label exists by the given label name
* @throws APIManagementException if failed
*/
boolean isLabelNameExists(String labelName, String tenantDomain) throws APIManagementException;


/**
* Returns all labels of the tenant
*
* @param tenantDomain tenant domain
* @return List<Label> list of Label objects
* @throws APIManagementException if failed to get labels
*/
List<Label> getAllLabelsOfTenant(String tenantDomain) throws APIManagementException;

/**
* Get label identified by the given uuid
*
* @param labelID label UUID
* @param tenantDomain tenant domain
* @return Label Label object
* @throws APIManagementException
*/
Label getLabelByIdAndTenantDomain(String labelID, String tenantDomain) throws APIManagementException;

/**
* Get mapped APIs for the given label
*
* @param labelID label UUID
* @return List<ApiResult> list of ApiResult objects
* @throws APIManagementException
*/
List<ApiResult> getMappedApisForLabel(String labelID) throws APIManagementException;

/**
* The method converts the date into timestamp
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,44 @@ EnvironmentPropertiesDTO getEnvironmentSpecificAPIProperties(String apiUuid, Str
*/
Environment getEnvironment(String organization, String uuid) throws APIManagementException;

/**
* Returns all labels of the tenant
*
* @param tenantDomain tenant domain
* @return List<Label> list of Label objects
* @throws APIManagementException if failed to get labels
*/
List<Label> getAllLabels(String tenantDomain) throws APIManagementException;

/**
* Returns all attached labels of the API
*
* @param apiID API UUID
* @return List<Label> list of Label objects
* @throws APIManagementException if failed to get labels
*/
List<Label> getAllLabelsOfApi(String apiID) throws APIManagementException;

/**
* Attach labels to an API
*
* @param apiID API UUID
* @param labelList List of Labels
* @return List<Label> list of Label objects
* @throws APIManagementException if failed to get labels
*/
List<Label> attachApiLabels(String apiID, List<Label> labelList) throws APIManagementException;

/**
* Detach labels from an API
*
* @param apiID API UUID
* @param labelList List of Labels
* @return List<Label> list of Label objects
* @throws APIManagementException if failed to get labels
*/
List<Label> detachApiLabels(String apiID, List<Label> labelList) throws APIManagementException;

/**
* Set existing operation policy mapping to the URI Templates
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.apimgt.api.model;


import java.util.Objects;

/**
* This class represents the API result object.
*/
public class ApiResult {

private String provider = null;
private String name = null;
private String version = null;
private String id = null;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}

public String getProvider() {
return provider;
}
public void setProvider(String provider) {
this.provider = provider;
}

@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (this == obj) return true;
ApiResult other = (ApiResult) obj;
if (!Objects.equals(this.id, other.id)) return false;
if (!Objects.equals(this.name, other.name)) return false;
if (!Objects.equals(this.version, other.version)) return false;
if (!Objects.equals(this.provider, other.provider)) return false;
return true;
}

public int hashCode() {
return Objects.hash(super.hashCode(), id, name, version, provider);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package org.wso2.carbon.apimgt.api.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -28,9 +26,7 @@
public class Label {
private String labelId;
private String name;
private String tenantId;
private String description;
private List<String> accessUrls = new ArrayList<>();

public Label() {
}
Expand All @@ -51,22 +47,6 @@ public void setName(String name) {
this.name = name;
}

public String getTenantId() {
return tenantId;
}

public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}

public List<String> getAccessUrls() {
return accessUrls;
}

public void setAccessUrls(List<String> accessUrls) {
this.accessUrls = accessUrls;
}

public String getDescription() {
return description;
}
Expand All @@ -83,12 +63,10 @@ public boolean equals(Object obj) {
if (!Objects.equals(this.name, other.name)) return false;
if (!Objects.equals(this.labelId, other.labelId)) return false;
if (!Objects.equals(this.description, other.description)) return false;
if (!Objects.equals(this.tenantId, other.tenantId)) return false;
if (!Objects.equals(this.accessUrls, other.accessUrls)) return false;
return true;
}

public int hashCode() {
return Objects.hash(super.hashCode(), labelId, name, tenantId, description, accessUrls);
return Objects.hash(super.hashCode(), labelId, name, description);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.wso2.carbon.apimgt.api.dto.KeyManagerPermissionConfigurationDTO;
import org.wso2.carbon.apimgt.api.model.APICategory;
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
import org.wso2.carbon.apimgt.api.model.ApiResult;
import org.wso2.carbon.apimgt.api.model.Application;
import org.wso2.carbon.apimgt.api.model.ApplicationInfo;
import org.wso2.carbon.apimgt.api.model.ApplicationInfoKeyManager;
Expand All @@ -53,6 +54,7 @@
import org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration;
import org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration;
import org.wso2.carbon.apimgt.api.model.LLMProvider;
import org.wso2.carbon.apimgt.api.model.Label;
import org.wso2.carbon.apimgt.api.model.Monetization;
import org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo;
import org.wso2.carbon.apimgt.api.model.VHost;
Expand All @@ -63,6 +65,7 @@
import org.wso2.carbon.apimgt.api.model.policy.PolicyConstants;
import org.wso2.carbon.apimgt.impl.alertmgt.AlertMgtConstants;
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
import org.wso2.carbon.apimgt.impl.dao.LabelsDAO;
import org.wso2.carbon.apimgt.impl.dao.constants.SQLConstants;
import org.wso2.carbon.apimgt.impl.dto.ThrottleProperties;
import org.wso2.carbon.apimgt.impl.dto.WorkflowProperties;
Expand Down Expand Up @@ -133,9 +136,11 @@ public class APIAdminImpl implements APIAdmin {

private static final Log log = LogFactory.getLog(APIAdminImpl.class);
protected ApiMgtDAO apiMgtDAO;
protected LabelsDAO labelsDAO;

public APIAdminImpl() {
apiMgtDAO = ApiMgtDAO.getInstance();
labelsDAO = LabelsDAO.getInstance();
}

@Override
Expand Down Expand Up @@ -1220,6 +1225,50 @@ private int isCategoryAttached(APICategory category, String username) throws API
return (int) (Integer) result.get("length");
}

public Label addLabel(Label label, String tenantDomain) throws APIManagementException {

label.setLabelId(UUID.randomUUID().toString());
return labelsDAO.addLabel(label, tenantDomain);
}

public void updateLabel(Label label) throws APIManagementException {

labelsDAO.updateLabel(label);
}

public void deleteLabel(String labelID) throws APIManagementException {

if (labelsDAO.hasAPIsForLabel(labelID)) {
APIUtil.handleException("Label is attached to APIs and cannot be deleted.");
}
labelsDAO.deleteLabel(labelID);
}

public List<Label> getAllLabelsOfTenant(String tenantDomain) throws APIManagementException {

return labelsDAO.getAllLabels(tenantDomain);
}

public boolean isLabelNameExists(String labelName, String uuid, String tenantDomain) throws APIManagementException {

return labelsDAO.isLabelNameExists(labelName, uuid, tenantDomain);
}

public boolean isLabelNameExists(String labelName, String tenantDomain) throws APIManagementException {

return labelsDAO.isLabelNameExists(labelName, tenantDomain);
}

public Label getLabelByIdAndTenantDomain(String labelID, String tenantDomain) throws APIManagementException {

return labelsDAO.getLabelByIdAndTenantDomain(labelID, tenantDomain);
}

public List<ApiResult> getMappedApisForLabel(String labelID) throws APIManagementException {

return labelsDAO.getMappedApisForLabel(labelID);
}

private void validateKeyManagerConfiguration(KeyManagerConfigurationDTO keyManagerConfigurationDTO)
throws APIManagementException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,7 @@ public static class AuditLogConstants {
public static final String TENANT_CONFIG_INFO = "User updated Tenant Config";

public static final String API_CATEGORIES = "APICategories";
public static final String LABELS = "Labels";
public static final String APPLICATIONS = "Applications";
public static final String GATEWAY_ENVIRONMENTS = "GatewayEnvironments";
public static final String ROLES_FOR_SCOPE = "RolesForScope";
Expand Down
Loading

0 comments on commit c602250

Please sign in to comment.