diff options
author | sebdet <sebastien.determe@intl.att.com> | 2021-02-26 19:14:03 +0100 |
---|---|---|
committer | S�bastien Determe <sebastien.determe@intl.att.com> | 2021-03-11 15:55:31 +0000 |
commit | 1e2760ea1ee4baa9db0e921907f3491fda795136 (patch) | |
tree | 5f2e508e3c26fb1ba06b3db019c2257c69392367 /src/main/java | |
parent | ea2969fd3bbfe52cbe4f41546dd40d68321c233b (diff) |
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 <sebastien.determe@intl.att.com>
Change-Id: I18ab3a6034cac719525774f11b2c17f0a14bc2aa
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Diffstat (limited to 'src/main/java')
4 files changed, 102 insertions, 43 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 |