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 12, 2025
1 parent bf7e34d commit 442a3c8
Show file tree
Hide file tree
Showing 59 changed files with 3,125 additions and 68 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,75 @@ 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
*
* 1. in case uuid is null : checks whether the labelName is already taken in the tenantDomain for the given type
* (this flow is used when adding a new label)
* 2. in case uuid is not null: checks whether the labelName is already taken by any label other than the one
* defined by the passed uuid in the given tenant and type
*
* @param labelName label name to check
* @param type label type
* @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 type, String uuid, 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
* @return Label Label object
* @throws APIManagementException
*/
Label getLabelByID(String labelID) 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 @@ -95,6 +95,8 @@ public enum ExceptionCodes implements ErrorHandler {
409, "Cannot update API: %s:%s, due to the resources to remove are used by one or more API Products"),
API_CATEGORY_INVALID(
900345, "The API category is invalid.", 400, " The API category is invalid for API: %s"),
API_LABEL_INVALID(
900345, "The label is invalid.", 400, " The label is invalid for API: %s"),
INVALID_ADDITIONAL_PROPERTIES(900346, "Invalid additional properties", 400,
"Invalid additional properties for API: %s:%s"),
INVALID_CONTEXT(900346, "Invalid context provided", 400, "Invalid context provided for API: %s:%s"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ public void setSoapToRestSequences(List<SOAPToRestSequence> soapToRestSequences)

private List<APICategory> apiCategories;

private List<Label> apiLabels;

/**
* Property to hold enable/disable status of the store visibility.
*/
Expand Down Expand Up @@ -1417,6 +1419,14 @@ public List<APICategory> getApiCategories() {
return apiCategories;
}

public void setApiLabels(List<Label> apiLabels) {
this.apiLabels = apiLabels;
}

public List<Label> getApiLabels() {
return apiLabels;
}

public List<String> getKeyManagers() {

return keyManagers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class APIProduct implements Serializable {
private String accessControlRoles;

private List<APICategory> apiCategories;
private List<Label> apiLabels;

private Date lastUpdated;
private Date createdTime;
Expand Down Expand Up @@ -634,6 +635,14 @@ public List<APICategory> getApiCategories() {
return apiCategories;
}

public void setApiLabels(List<Label> apiLabels) {
this.apiLabels = apiLabels;
}

public List<Label> getApiLabels() {
return apiLabels;
}

public boolean isAsync() {
return getType().equals("WS") || getType().equals("WEBSUB") || getType().equals("SSE")
|| getType().equals("ASYNC");
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,9 @@
public class Label {
private String labelId;
private String name;
private String tenantId;
private String type;
private String description;
private List<String> accessUrls = new ArrayList<>();
private String tenantDomain;

public Label() {
}
Expand All @@ -51,20 +49,12 @@ public void setName(String name) {
this.name = name;
}

public String getTenantId() {
return tenantId;
public String getType() {
return type;
}

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

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

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

public String getDescription() {
Expand All @@ -75,20 +65,28 @@ public void setDescription(String description) {
this.description = description;
}

public String getTenantDomain() {
return tenantDomain;
}

public void setTenantDomain(String tenantDomain) {
this.tenantDomain = tenantDomain;
}

@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (this == obj) return true;
Label other = (Label) obj;
if (!Objects.equals(this.name, other.name)) return false;
if (!Objects.equals(this.labelId, other.labelId)) return false;
if (!Objects.equals(this.type, other.type)) 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;
if (!Objects.equals(this.tenantDomain, other.tenantDomain)) return false;
return true;
}

public int hashCode() {
return Objects.hash(super.hashCode(), labelId, name, tenantId, description, accessUrls);
return Objects.hash(super.hashCode(), labelId, name, type, description, tenantDomain);
}
}
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 Down Expand Up @@ -1220,6 +1222,44 @@ private int isCategoryAttached(APICategory category, String username) throws API
return (int) (Integer) result.get("length");
}

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

return apiMgtDAO.addLabel(label, tenantDomain);
}

public void updateLabel(Label label) throws APIManagementException {

apiMgtDAO.updateLabel(label);
}

public void deleteLabel(String labelID) throws APIManagementException {

if (apiMgtDAO.hasAPIsForLabel(labelID)) {
APIUtil.handleException("Unable to delete the label. It is attached to API(s)");
}
apiMgtDAO.deleteLabel(labelID);
}

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

return apiMgtDAO.getAllLabels(tenantDomain);
}

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

return apiMgtDAO.isLabelNameExists(labelName, type, uuid, tenantDomain);
}

public Label getLabelByID(String labelID) throws APIManagementException {

return apiMgtDAO.getLabelByID(labelID);
}

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

return apiMgtDAO.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 442a3c8

Please sign in to comment.