diff options
Diffstat (limited to 'src')
6 files changed, 120 insertions, 58 deletions
diff --git a/src/main/java/org/onap/policy/clamp/clds/util/JsonUtils.java b/src/main/java/org/onap/policy/clamp/clds/util/JsonUtils.java index d57ce2095..fd5079c47 100644 --- a/src/main/java/org/onap/policy/clamp/clds/util/JsonUtils.java +++ b/src/main/java/org/onap/policy/clamp/clds/util/JsonUtils.java @@ -40,10 +40,10 @@ public class JsonUtils { protected static final EELFLogger logger = EELFManager.getInstance().getLogger(JsonUtils.class); - public static final Gson GSON = new GsonBuilder() + public static final Gson GSON = new GsonBuilder().setPrettyPrinting() .registerTypeAdapter(SecureServicePermission.class, new SecureServicePermissionDeserializer()).create(); - public static final Gson GSON_JPA_MODEL = new GsonBuilder() + public static final Gson GSON_JPA_MODEL = new GsonBuilder().setPrettyPrinting() .registerTypeAdapter(Instant.class, new InstantSerializer()) .registerTypeAdapter(Instant.class, new InstantDeserializer()).setPrettyPrinting() .excludeFieldsWithoutExposeAnnotation().create(); diff --git a/src/main/java/org/onap/policy/clamp/policy/Policy.java b/src/main/java/org/onap/policy/clamp/policy/Policy.java index 801183cab..f8bdab6c2 100644 --- a/src/main/java/org/onap/policy/clamp/policy/Policy.java +++ b/src/main/java/org/onap/policy/clamp/policy/Policy.java @@ -1,10 +1,12 @@ /*- * ============LICENSE_START======================================================= - * ONAP CLAMP + * ONAP POLICY-CLAMP * ================================================================================ * Copyright (C) 2019 Nokia Intellectual Property. All rights * reserved. * ================================================================================ + * Modifications Copyright (C) 2021 AT&T + * ================================================================================ * Licensed 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 @@ -91,13 +93,6 @@ public abstract class Policy extends AuditEntity { @JoinColumn(name = "policy_model_version", referencedColumnName = "version")}) private PolicyModel policyModel; - private JsonObject createJsonFromPolicyTosca() { - Map<String, Object> map = - new Yaml().load(this.getPolicyModel() != null ? this.getPolicyModel().getPolicyModelTosca() : ""); - JSONObject jsonObject = new JSONObject(map); - return new Gson().fromJson(jsonObject.toString(), JsonObject.class); - } - /** * This method create the policy payload that must be sent to PEF. * @@ -105,39 +100,12 @@ public abstract class Policy extends AuditEntity { * @throws UnsupportedEncodingException In case of failure */ public String createPolicyPayload() throws UnsupportedEncodingException { - JsonObject toscaJson = createJsonFromPolicyTosca(); - - JsonObject policyPayloadResult = new JsonObject(); - - policyPayloadResult.add("tosca_definitions_version", toscaJson.get("tosca_definitions_version")); - - JsonObject topologyTemplateNode = new JsonObject(); - policyPayloadResult.add("topology_template", topologyTemplateNode); - - JsonArray policiesArray = new JsonArray(); - topologyTemplateNode.add("policies", policiesArray); - - JsonObject thisPolicy = new JsonObject(); - policiesArray.add(thisPolicy); - - JsonObject policyDetails = new JsonObject(); - thisPolicy.add(this.getName(), policyDetails); - policyDetails.addProperty("type", this.getPolicyModel().getPolicyModelType()); - policyDetails.addProperty("type_version", this.getPolicyModel().getVersion()); - policyDetails.addProperty("version", this.getPolicyModel().getVersion()); - - JsonObject policyMetadata = new JsonObject(); - policyDetails.add("metadata", policyMetadata); - policyMetadata.addProperty("policy-id", this.getName()); - - policyDetails.add("properties", this.getConfigurationsJson()); - - String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult); - logger.info("Policy payload: " + policyPayload); - return policyPayload; + return PolicyPayload + .createPolicyPayload(this.getPolicyModel().getPolicyModelType(), this.getPolicyModel().getVersion(), + this.getName(), this.getPolicyModel().getVersion(), this.getConfigurationsJson(), + this.getPolicyModel() != null ? this.getPolicyModel().getPolicyModelTosca() : ""); } - /** * Name getter. * @@ -172,7 +140,7 @@ public abstract class Policy extends AuditEntity { * Regenerate the Policy Json Representation. * * @param toscaConverter The tosca converter required to regenerate the json schema - * @param serviceModel The service model associated + * @param serviceModel The service model associated */ public abstract void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter, Service serviceModel); diff --git a/src/main/java/org/onap/policy/clamp/policy/PolicyEngineServices.java b/src/main/java/org/onap/policy/clamp/policy/PolicyEngineServices.java index 196642589..bdd77cb41 100644 --- a/src/main/java/org/onap/policy/clamp/policy/PolicyEngineServices.java +++ b/src/main/java/org/onap/policy/clamp/policy/PolicyEngineServices.java @@ -165,7 +165,7 @@ public class PolicyEngineServices { options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yamlParser = new Yaml(options); String responseBody = callCamelRoute( - ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType) + ExchangeBuilder.anExchange(camelContext).withProperty("policyModelType", policyType) .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-tosca-model", "Get one policy"); diff --git a/src/main/java/org/onap/policy/clamp/policy/PolicyPayload.java b/src/main/java/org/onap/policy/clamp/policy/PolicyPayload.java new file mode 100644 index 000000000..d2c860150 --- /dev/null +++ b/src/main/java/org/onap/policy/clamp/policy/PolicyPayload.java @@ -0,0 +1,91 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP POLICY-CLAMP + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.policy.clamp.policy; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import java.io.UnsupportedEncodingException; +import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.json.JSONObject; +import org.onap.policy.clamp.clds.util.JsonUtils; +import org.yaml.snakeyaml.Yaml; + +/** + * This class is a utility class to create the policy payload. + */ +public class PolicyPayload { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyPayload.class); + + private static JsonObject createJsonFromPolicyTosca(String toscaContent) { + Map<String, Object> map = + new Yaml().load(!StringUtils.isEmpty(toscaContent) ? toscaContent : ""); + return JsonUtils.GSON.fromJson(new JSONObject(map).toString(), JsonObject.class); + } + + /** + * This method create the policy payload that must be sent to PEF. + * + * @return A String containing the payload + * @throws UnsupportedEncodingException In case of failure + */ + public static String createPolicyPayload(String policyModelType, String policyModelVersion, String policyName, + String policyVersion, JsonObject policyProperties, String toscaContent) + throws UnsupportedEncodingException { + JsonObject policyPayloadResult = new JsonObject(); + + policyPayloadResult.add("tosca_definitions_version", + createJsonFromPolicyTosca(toscaContent).get("tosca_definitions_version")); + + JsonObject topologyTemplateNode = new JsonObject(); + policyPayloadResult.add("topology_template", topologyTemplateNode); + + JsonArray policiesArray = new JsonArray(); + topologyTemplateNode.add("policies", policiesArray); + + JsonObject thisPolicy = new JsonObject(); + policiesArray.add(thisPolicy); + + JsonObject policyDetails = new JsonObject(); + thisPolicy.add(policyName, policyDetails); + policyDetails.addProperty("type", policyModelType); + policyDetails.addProperty("type_version", policyModelVersion); + policyDetails.addProperty("version", policyVersion); + policyDetails.addProperty("name", policyName); + + JsonObject policyMetadata = new JsonObject(); + policyDetails.add("metadata", policyMetadata); + policyMetadata.addProperty("policy-id", policyName); + policyMetadata.addProperty("policy-version", policyVersion); + + policyDetails.add("properties", policyProperties); + + String policyPayload = JsonUtils.GSON.toJson(policyPayloadResult); + logger.info("Policy payload: " + policyPayload); + return policyPayload; + } +}
\ No newline at end of file diff --git a/src/main/resources/clds/camel/rest/clamp-api-v2.xml b/src/main/resources/clds/camel/rest/clamp-api-v2.xml index 01ad8a8a3..7de2de1c9 100644 --- a/src/main/resources/clds/camel/rest/clamp-api-v2.xml +++ b/src/main/resources/clds/camel/rest/clamp-api-v2.xml @@ -500,7 +500,7 @@ <simple resultType="java.lang.Boolean">false</simple> </setProperty> <to uri="direct:delete-policy-from-loop-object"/> - <to uri="direct:create-policy"/> + <to uri="direct:create-policy-from-loop-object"/> </split> <log loggingLevel="INFO" message="Processing all OPERATIONAL policies defined in loop ${exchangeProperty[loopObject].getName()}"/> @@ -517,7 +517,7 @@ </setProperty> <to uri="direct:delete-policy-from-loop-object"/> - <to uri="direct:create-policy"/> + <to uri="direct:create-policy-from-loop-object"/> </split> <delay> @@ -1363,32 +1363,35 @@ </get> <!-- Create a new policy --> - <post uri="/v2/policies/{policyModelName}/{policyModelVersion}" + <post uri="/v2/policies/{policyModelType}/{policyModelVersion}/{policyName}/{policyVersion}" type="com.google.gson.JsonElement" consumes="application/json" outType="com.google.gson.JsonObject" produces="application/json"> <route> <removeHeaders pattern="*" - excludePattern="policyModelName|policyModelVersion"/> + excludePattern="policyModelType|policyModelVersion|policyName|policyVersion"/> <doTry> <to uri="bean:org.onap.policy.clamp.flow.log.FlowLogOperation?method=startLog(*, 'CREATE a new Policy ')"/> <to uri="bean:org.onap.policy.clamp.authorization.AuthorizationController?method=authorize(*,'policies','','update')"/> - <setProperty propertyName="policyModelName"> - <simple>${header.policyModelName}</simple> + <setProperty propertyName="policyModelType"> + <simple>${header.policyModelType}</simple> </setProperty> <setProperty propertyName="policyModelVersion"> <simple>${header.policyModelVersion}</simple> </setProperty> + <setProperty propertyName="policyProperties"> + <simple>${body}</simple> + </setProperty> + <to + uri="bean:org.onap.policy.clamp.loop.template.PolicyModelsService?method=getPolicyModelTosca(${header.policyModelType},${header.policyModelVersion})"/> <setBody> - <method ref="org.onap.policy.clamp.policy.pdpgroup.PoliciesPdpMerger" - method="removePdpStatesOnePolicy(${body})"/> + <method ref="org.onap.policy.clamp.policy.PolicyPayload" + method="createPolicyPayload(${header.policyModelType}, ${header.policyModelVersion}, + ${header.policyName}, ${header.policyVersion}, ${exchangeProperty[policyProperties]}, ${body})"/> </setBody> - <setBody> - <simple>${body.toString()}</simple> - </setBody> <to uri="direct:create-policy"/> <to uri="bean:org.onap.policy.clamp.flow.log.FlowLogOperation?method=endLog()"/> diff --git a/src/main/resources/clds/camel/routes/policy-flows.xml b/src/main/resources/clds/camel/routes/policy-flows.xml index f9f2b1d72..c92617856 100644 --- a/src/main/resources/clds/camel/routes/policy-flows.xml +++ b/src/main/resources/clds/camel/routes/policy-flows.xml @@ -183,7 +183,7 @@ <simple>${exchangeProperty[policy].getPolicyModel().getPolicyModelType()}</simple> </setProperty> <setProperty propertyName="policyModelVersion"> - <simple>${exchangeProperty[policy].getPolicyModel().getVersion()</simple> + <simple>${exchangeProperty[policy].getPolicyModel().getVersion()}</simple> </setProperty> <setProperty propertyName="policyName"> <simple>${exchangeProperty[policy].getName()}</simple> @@ -386,7 +386,7 @@ <from uri="direct:get-policy-tosca-model"/> <doTry> <log loggingLevel="INFO" - message="Getting the policy tosca model: ${exchangeProperty[policyModelName]}/${exchangeProperty[policyModelVersion]}"/> + message="Getting the policy tosca model: ${exchangeProperty[policyModelType]}/${exchangeProperty[policyModelVersion]}"/> <to uri="bean:org.onap.policy.clamp.flow.log.FlowLogOperation?method=invokeLog('Policy', 'Getting the policy model')"/> <setHeader headerName="CamelHttpMethod"> @@ -405,9 +405,9 @@ </simple> </setHeader> <log loggingLevel="INFO" - message="Endpoint to get policy model: {{clamp.config.policy.api.url}}/policy/api/v1/policytypes/${exchangeProperty[policyModelName]}/versions/${exchangeProperty[policyModelVersion]}"></log> + message="Endpoint to get policy model: {{clamp.config.policy.api.url}}/policy/api/v1/policytypes/${exchangeProperty[policyModelType]}/versions/${exchangeProperty[policyModelVersion]}"></log> <toD - uri="{{clamp.config.policy.api.url}}/policy/api/v1/policytypes/${exchangeProperty[policyModelName]}/versions/${exchangeProperty[policyModelVersion]}?bridgeEndpoint=true&useSystemProperties=true&throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&authMethod=Basic&authUsername={{clamp.config.policy.api.userName}}&authPassword={{clamp.config.policy.api.password}}&httpClient.connectTimeout=10000&httpClient.socketTimeout=20000&authenticationPreemptive=true&connectionClose=true"/> + uri="{{clamp.config.policy.api.url}}/policy/api/v1/policytypes/${exchangeProperty[policyModelType]}/versions/${exchangeProperty[policyModelVersion]}?bridgeEndpoint=true&useSystemProperties=true&throwExceptionOnFailure=${exchangeProperty[raiseHttpExceptionFlag]}&authMethod=Basic&authUsername={{clamp.config.policy.api.userName}}&authPassword={{clamp.config.policy.api.password}}&httpClient.connectTimeout=10000&httpClient.socketTimeout=20000&authenticationPreemptive=true&connectionClose=true"/> <convertBodyTo type="java.lang.String"/> <doFinally> <to uri="direct:reset-raise-http-exception-flag"/> |