Skip to content

Commit

Permalink
Add: Configuration to enable/disable Policy Loading
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Jan 2, 2024
1 parent a049e86 commit cf9ace4
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ public API getApi() throws DataNotFoundException {
api.setApiCreator(apiObj.getApiProvider());
api.setApiCreatorTenantDomain(MultitenantUtils.getTenantDomain(api.getApiCreator()));
List<URITemplate> uriTemplates = new ArrayList<>();

for (URLMapping uriTemplate : apiObj.getUrlMappings()) {
org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate uriTemplateObj
= new org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public final class APIConstants {
//governance registry apimgt root location
public static final String APIMGT_REGISTRY_LOCATION = "/apimgt";

public static final String POLICY_ENABLED_FOR_ANALYTICS = "policyEnabled";

public static final String API_CONTEXT_ID = "api.context.id";
//This is the resource name of API
public static final String API_RESOURCE_NAME = "/api";
Expand Down Expand Up @@ -973,6 +975,7 @@ private Permissions() {

public static final String API_ANALYTICS = "Analytics.";
public static final String API_USAGE_ENABLED = API_ANALYTICS + "Enabled";
public static final String API_ANALYTICS_POLICY_ENABLED = API_ANALYTICS + "PolicyEnabled";
public static final String API_ANALYTICS_REPORTER_CLASS = API_ANALYTICS + "ReporterClass";
public static final String API_ANALYTICS_PROPERTIES = API_ANALYTICS + "Properties";
public static final String API_ANALYTICS_RESPONSE_SCHEMA_NAME = API_ANALYTICS + "ResponseSchemaName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ private void readChildElements(OMElement serverConfig,

OMElement analyticsType = element.getFirstChildWithName(new QName("Type"));
analyticsProps.put("type", analyticsType.getText());

OMElement enablePolicy = element.getFirstChildWithName(new QName("PolicyEnabled"));
analyticsProps.put("policyEnabled", enablePolicy.getText());

analyticsProperties = analyticsProps;
} else if ("PersistenceConfigs".equals(localName)) {
OMElement properties = element.getFirstChildWithName(new QName("Properties"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy;
import org.wso2.carbon.apimgt.api.model.subscription.URLMapping;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.apimgt.impl.ThrottlePolicyConstants;
import org.wso2.carbon.apimgt.impl.dao.constants.SQLConstants;
import org.wso2.carbon.apimgt.impl.dao.constants.SubscriptionValidationSQLConstants;
Expand All @@ -62,13 +63,16 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.wso2.carbon.apimgt.impl.APIConstants.POLICY_ENABLED_FOR_ANALYTICS;

/**
* This Class used to handle DAO access for subscription Validation.
*/
public class SubscriptionValidationDAO {

private static Log log = LogFactory.getLog(SubscriptionValidationDAO.class);
private static String OPERATION_POLICY_ENABLE_WITH_ANALYTICS_EVENT = "operationPolicyEnableWithAnalyticsEvent";
private static Map<String,String> configs = APIManagerConfiguration.getAnalyticsProperties();

/*
* This method can be used to retrieve all the Subscriptions in the database
Expand Down Expand Up @@ -1149,8 +1153,11 @@ private void attachURlMappingDetailsOfApiProduct(Connection connection, API api,
}
}

if (Boolean.parseBoolean(System.getProperty(OPERATION_POLICY_ENABLE_WITH_ANALYTICS_EVENT))) {
attachPolicies(connection, revisionId, api);
if(configs.containsKey(POLICY_ENABLED_FOR_ANALYTICS)) {
boolean isPolicyEnabled = Boolean.parseBoolean(configs.get(POLICY_ENABLED_FOR_ANALYTICS));
if (isPolicyEnabled) {
attachPolicies(connection, revisionId, api);
}
}
}

Expand Down Expand Up @@ -1232,9 +1239,11 @@ private void attachURLMappingDetails(Connection connection, String revisionId, A
}
}

if (Boolean.parseBoolean(System.getProperty(OPERATION_POLICY_ENABLE_WITH_ANALYTICS_EVENT))) {
// Attach the relevant operation policies and api to the resources and to the API.
attachPolicies(connection, revisionId, api);
if(configs.containsKey(POLICY_ENABLED_FOR_ANALYTICS)) {
boolean isPolicyEnabled = Boolean.parseBoolean(configs.get(POLICY_ENABLED_FOR_ANALYTICS));
if (isPolicyEnabled) {
attachPolicies(connection, revisionId, api);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,12 @@
"example" : false,
"description" : "Whether this is the default version of the API."
},
"apiPolicies" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/OperationPolicy"
}
},
"urlMappings" : {
"type" : "array",
"items" : {
Expand Down Expand Up @@ -1242,8 +1248,8 @@
"type" : "string",
"example" : "EXCHANGED",
"description" : "The type of the tokens to be used (exchanged or without exchanged). Accepted values are EXCHANGED, DIRECT or BOTH.",
"enum" : [ "EXCHANGED", "DIRECT", "BOTH" ],
"default" : "DIRECT"
"default" : "DIRECT",
"enum" : [ "EXCHANGED", "DIRECT", "BOTH" ]
}
}
},
Expand Down Expand Up @@ -1703,9 +1709,42 @@
"items" : {
"type" : "string"
}
},
"operationPolicies" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/OperationPolicy"
}
}
}
},
"OperationPolicy" : {
"properties" : {
"policyName" : {
"type" : "string"
},
"policyVersion" : {
"type" : "string"
},
"direction" : {
"type" : "string"
},
"policyId" : {
"type" : "string"
},
"order" : {
"type" : "integer"
},
"parameters" : {
"type" : "object",
"additionalProperties" : {
"type" : "object",
"properties" : { }
}
}
},
"title" : "Operation Policy"
},
"Pagination" : {
"properties" : {
"offset" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,40 @@ paths:
source: 'curl -k -X POST -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8"
"https://127.0.0.1:9443/api/am/publisher/v4/apis/890a4f4d-09eb-4877-a323-57f6ce2ed79b/restore-revision?revisionId=e0824883-3e86-403a-aec1-22bbc454eb7c"'

/apis/{apiId}/cancel-revision-workflow/{revisionId}/{envName}:
#--------------------------------------------------------
# Cancel a pending revision deployment workflow task given revision id and environment name
#--------------------------------------------------------
delete:
tags:
- API Revisions
summary: Delete Pending Revision Deployment Workflow Tasks
description: |
This operation can be used to remove pending revision deployment requests that are in pending state
parameters:
- $ref: '#/components/parameters/apiId'
- $ref: '#/components/parameters/revisionId'
- $ref: '#/components/parameters/envName'
responses:
200:
description: |
OK.
Revision deployment pending task removed successfully.
content: { }
404:
$ref: '#/components/responses/NotFound'
412:
$ref: '#/components/responses/PreconditionFailed'
security:
- OAuth2Security:
- apim:api_publish
- apim:api_manage
x-code-samples:
- lang: Curl
source: 'curl -k -X DELETE -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8"
"https://127.0.0.1:9443/api/am/publisher/v4/apis/890a4f4d-09eb-4877-a323-57f6ce2ed79b/cancel-revision-workflow/e0824883-3e86-403a-aec1-22bbc454eb7c/Default"'
operationId: deleteAPIRevisionDeploymentPendingTask

/apis/import-service:
post:
tags:
Expand Down Expand Up @@ -9156,6 +9190,14 @@ components:
minLength: 1
type: string
example: Default
status:
type: string
example: CREATED
default: CREATED
enum:
- CREATED
- APPROVED
- REJECTED
vhost:
maxLength: 255
minLength: 1
Expand Down Expand Up @@ -12287,6 +12329,17 @@ components:
schema:
type: string

# API Revision Environment
# Specified as part of the path expression
envName:
name: envName
in: path
description: |
Environment name of an Revision
required: true
schema:
type: string

# API Revision Identifier
# Specified as part of the query string
revisionId-Q:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@
<!-- Enable Analytics for API Manager -->
<Enabled>{{apim.analytics.enable}}</Enabled>

<PolicyEnabled>{{apim.analytics.enablePolicy}}</PolicyEnabled>

<Type>{{apim.analytics.type}}</Type>

<AuthToken>{{apim.analytics.auth_token}}</AuthToken>
Expand Down

0 comments on commit cf9ace4

Please sign in to comment.