From 1e2760ea1ee4baa9db0e921907f3491fda795136 Mon Sep 17 00:00:00 2001 From: sebdet Date: Fri, 26 Feb 2021 19:14:03 +0100 Subject: Introduce a new json editor component Add this a new react component so that the json editor can be called within the policies table + Add the tosca tab + Fix for Submit operation in clamp-api-v2.xml Issue-ID: POLICY-3106 Issue-ID: POLICY-3124 Signed-off-by: sebdet Change-Id: I18ab3a6034cac719525774f11b2c17f0a14bc2aa Signed-off-by: sebdet --- .../org/onap/policy/clamp/clds/util/JsonUtils.java | 4 +- .../java/org/onap/policy/clamp/policy/Policy.java | 48 ++---------- .../policy/clamp/policy/PolicyEngineServices.java | 2 +- .../onap/policy/clamp/policy/PolicyPayload.java | 91 ++++++++++++++++++++++ .../resources/clds/camel/rest/clamp-api-v2.xml | 25 +++--- .../resources/clds/camel/routes/policy-flows.xml | 8 +- 6 files changed, 120 insertions(+), 58 deletions(-) create mode 100644 src/main/java/org/onap/policy/clamp/policy/PolicyPayload.java (limited to 'src') 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 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 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 @@ false - + @@ -517,7 +517,7 @@ - + @@ -1363,32 +1363,35 @@ - + excludePattern="policyModelType|policyModelVersion|policyName|policyVersion"/> - - ${header.policyModelName} + + ${header.policyModelType} ${header.policyModelVersion} + + ${body} + + - + - - ${body.toString()} - 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 @@ ${exchangeProperty[policy].getPolicyModel().getPolicyModelType()} - ${exchangeProperty[policy].getPolicyModel().getVersion() + ${exchangeProperty[policy].getPolicyModel().getVersion()} ${exchangeProperty[policy].getName()} @@ -386,7 +386,7 @@ + message="Getting the policy tosca model: ${exchangeProperty[policyModelType]}/${exchangeProperty[policyModelVersion]}"/> @@ -405,9 +405,9 @@ + message="Endpoint to get policy model: {{clamp.config.policy.api.url}}/policy/api/v1/policytypes/${exchangeProperty[policyModelType]}/versions/${exchangeProperty[policyModelVersion]}"> + 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"/> -- cgit 1.2.3-korg