diff options
Diffstat (limited to 'src/main/java')
7 files changed, 204 insertions, 162 deletions
diff --git a/src/main/java/org/onap/clamp/clds/config/ClampProperties.java b/src/main/java/org/onap/clamp/clds/config/ClampProperties.java index 8eae9066d..0b5c951bf 100644 --- a/src/main/java/org/onap/clamp/clds/config/ClampProperties.java +++ b/src/main/java/org/onap/clamp/clds/config/ClampProperties.java @@ -23,14 +23,10 @@ package org.onap.clamp.clds.config; -import com.google.gson.JsonElement; - import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; - import org.apache.commons.io.IOUtils; -import org.onap.clamp.clds.util.JsonUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; @@ -75,38 +71,6 @@ public class ClampProperties { } /** - * Return json as objects that can be updated. The value obtained from the - * clds-reference file will be used as a filename. - * - * @param key The key that will be used to access the clds-reference file - * @return A jsonNode - * @throws IOException In case of issues with the JSON parser - */ - public JsonElement getJsonTemplate(String key) throws IOException { - String fileReference = getStringValue(key); - return (fileReference != null) - ? JsonUtils.GSON.fromJson(getFileContentFromPath(fileReference), JsonElement.class) - : null; - } - - /** - * Return json as objects that can be updated. First try with combo key (key1 + - * "." + key2), otherwise default to just key1. The value obtained from the - * clds-reference file will be used as a filename. - * - * @param key1 The first key - * @param key2 The second key after a dot - * @return A JsonNode - * @throws IOException In case of issues with the JSON parser - */ - public JsonElement getJsonTemplate(String key1, String key2) throws IOException { - String fileReference = getStringValue(key1, key2); - return (fileReference != null) - ? JsonUtils.GSON.fromJson(getFileContentFromPath(fileReference), JsonElement.class) - : null; - } - - /** * Return the file content. The value obtained from the clds-reference file will * be used as a filename. * diff --git a/src/main/java/org/onap/clamp/clds/config/LegacyOperationalPolicy.java b/src/main/java/org/onap/clamp/clds/config/LegacyOperationalPolicy.java new file mode 100644 index 000000000..7d4b5f8fe --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/config/LegacyOperationalPolicy.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 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.clamp.clds.config; + +import javax.annotation.PostConstruct; +import org.onap.clamp.loop.template.PolicyModel; +import org.onap.clamp.loop.template.PolicyModelsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +@Configuration +@Profile("legacy-operational-policy") +public class LegacyOperationalPolicy { + + @Autowired + PolicyModelsService policyModelService; + + public static final String OPERATIONAL_POLICY_LEGACY = "onap.policies.operational.legacy"; + + @PostConstruct + public void init() { + policyModelService.saveOrUpdatePolicyModel(new PolicyModel(OPERATIONAL_POLICY_LEGACY, "", "1.0.0", + "OperationalPolicyLegacy")); + } +} + diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java index 122b4c77f..f185460cf 100644 --- a/src/main/java/org/onap/clamp/loop/Loop.java +++ b/src/main/java/org/onap/clamp/loop/Loop.java @@ -48,6 +48,7 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; +import org.apache.commons.lang3.RandomStringUtils; import org.hibernate.annotations.SortNatural; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; @@ -62,6 +63,7 @@ import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.loop.service.Service; import org.onap.clamp.loop.template.LoopElementModel; import org.onap.clamp.loop.template.LoopTemplate; +import org.onap.clamp.policy.Policy; import org.onap.clamp.policy.microservice.MicroServicePolicy; import org.onap.clamp.policy.operational.OperationalPolicy; @@ -168,12 +170,18 @@ public class Loop extends AuditEntity implements Serializable { this.setModelService(loopTemplate.getModelService()); loopTemplate.getLoopElementModelsUsed().forEach(element -> { if (LoopElementModel.MICRO_SERVICE_TYPE.equals(element.getLoopElementModel().getLoopElementType())) { - this.addMicroServicePolicy(new MicroServicePolicy(name, + this.addMicroServicePolicy(new MicroServicePolicy(Policy.generatePolicyName("MICROSERVICE_", + loopTemplate.getModelService().getName(),loopTemplate.getModelService().getVersion(), + RandomStringUtils.randomAlphanumeric(3),RandomStringUtils.randomAlphanumeric(3)), element.getLoopElementModel().getPolicyModels().first(), false, element.getLoopElementModel())); } else if (LoopElementModel.OPERATIONAL_POLICY_TYPE .equals(element.getLoopElementModel().getLoopElementType())) { - this.addOperationalPolicy(new OperationalPolicy(name, null, new JsonObject(), - element.getLoopElementModel().getPolicyModels().first(), element.getLoopElementModel(), null, null)); + this.addOperationalPolicy(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL_", + loopTemplate.getModelService().getName(),loopTemplate.getModelService().getVersion(), + RandomStringUtils.randomAlphanumeric(3),RandomStringUtils.randomAlphanumeric(3)), null, + new JsonObject(), + element.getLoopElementModel().getPolicyModels().first(), element.getLoopElementModel(), + null,null)); } }); } diff --git a/src/main/java/org/onap/clamp/loop/LoopController.java b/src/main/java/org/onap/clamp/loop/LoopController.java index 59b97e1a2..1a67455e8 100644 --- a/src/main/java/org/onap/clamp/loop/LoopController.java +++ b/src/main/java/org/onap/clamp/loop/LoopController.java @@ -38,11 +38,9 @@ import org.springframework.stereotype.Controller; public class LoopController { private final LoopService loopService; - private static final Type OPERATIONAL_POLICY_TYPE = new TypeToken<List<OperationalPolicy>>() { - }.getType(); + private static final Type OPERATIONAL_POLICY_TYPE = new TypeToken<List<OperationalPolicy>>() {}.getType(); - private static final Type MICROSERVICE_POLICY_TYPE = new TypeToken<List<MicroServicePolicy>>() { - }.getType(); + private static final Type MICROSERVICE_POLICY_TYPE = new TypeToken<List<MicroServicePolicy>>() {}.getType(); @Autowired public LoopController(LoopService loopService) { diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java index 47dee1ae9..ebeb84fd6 100644 --- a/src/main/java/org/onap/clamp/policy/Policy.java +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -23,28 +23,40 @@ package org.onap.clamp.policy; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; import java.io.UnsupportedEncodingException; - +import java.util.Map; import javax.persistence.Column; import javax.persistence.FetchType; import javax.persistence.JoinColumn; +import javax.persistence.JoinColumns; import javax.persistence.ManyToOne; import javax.persistence.MappedSuperclass; - +import javax.persistence.Transient; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; +import org.json.JSONObject; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.common.AuditEntity; import org.onap.clamp.loop.template.LoopElementModel; +import org.onap.clamp.loop.template.PolicyModel; +import org.yaml.snakeyaml.Yaml; @MappedSuperclass @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)}) public abstract class Policy extends AuditEntity { + @Transient + private static final EELFLogger logger = EELFManager.getInstance().getLogger(Policy.class); + @Expose @Type(type = "json") @Column(columnDefinition = "json", name = "json_representation", nullable = false) @@ -72,7 +84,67 @@ public abstract class Policy extends AuditEntity { @Column(name = "pdp_sub_group") private String pdpSubgroup; - public abstract String createPolicyPayload() throws UnsupportedEncodingException; + @Expose + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"), + @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); + } + + private String getModelPropertyNameFromTosca(JsonObject object, String policyModelType) { + return object.getAsJsonObject("policy_types").getAsJsonObject(policyModelType) + .getAsJsonObject( + "properties") + .keySet().toArray(new String[1])[0]; + } + + /** + * 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 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("version", this.getPolicyModel().getVersion()); + + JsonObject policyMetadata = new JsonObject(); + policyDetails.add("metadata", policyMetadata); + policyMetadata.addProperty("policy-id", this.getName()); + + JsonObject policyProperties = new JsonObject(); + policyDetails.add("properties", policyProperties); + policyProperties + .add(this.getModelPropertyNameFromTosca(toscaJson, this.getPolicyModel().getPolicyModelType()), + this.getConfigurationsJson()); + String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult); + logger.info("Policy payload: " + policyPayload); + return policyPayload; + } + /** * Name getter. @@ -105,6 +177,24 @@ public abstract class Policy extends AuditEntity { } /** + * policyModel getter. + * + * @return the policyModel + */ + public PolicyModel getPolicyModel() { + return policyModel; + } + + /** + * policyModel setter. + * + * @param policyModel The new policyModel + */ + public void setPolicyModel(PolicyModel policyModel) { + this.policyModel = policyModel; + } + + /** * configurationsJson getter. * * @return The configurationsJson @@ -160,7 +250,7 @@ public abstract class Policy extends AuditEntity { /** * pdpSubgroup getter. - * + * * @return the pdpSubgroup */ public String getPdpSubgroup() { @@ -169,7 +259,7 @@ public abstract class Policy extends AuditEntity { /** * pdpSubgroup setter. - * + * * @param pdpSubgroup the pdpSubgroup to set */ public void setPdpSubgroup(String pdpSubgroup) { diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java index 7fd752c31..c4037ffb6 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -25,28 +25,20 @@ package org.onap.clamp.policy.microservice; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; import java.io.Serializable; import java.util.HashSet; -import java.util.Map; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.JoinColumns; import javax.persistence.ManyToMany; -import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; -import org.json.JSONObject; import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor; import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; @@ -54,7 +46,6 @@ import org.onap.clamp.loop.Loop; import org.onap.clamp.loop.template.LoopElementModel; import org.onap.clamp.loop.template.PolicyModel; import org.onap.clamp.policy.Policy; -import org.yaml.snakeyaml.Yaml; @Entity @Table(name = "micro_service_policies") @@ -100,12 +91,6 @@ public class MicroServicePolicy extends Policy implements Serializable { @Column(name = "dcae_blueprint_id") private String dcaeBlueprintId; - @Expose - @ManyToOne(fetch = FetchType.EAGER) - @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"), - @JoinColumn(name = "policy_model_version", referencedColumnName = "version")}) - private PolicyModel policyModel; - public MicroServicePolicy() { // serialization } @@ -119,20 +104,15 @@ public class MicroServicePolicy extends Policy implements Serializable { * @param shared The flag indicate whether the MicroService is shared */ public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared, LoopElementModel loopElementModel) { - this(name,policyModel,shared,JsonUtils.GSON_JPA_MODEL + this(name, policyModel, shared, JsonUtils.GSON_JPA_MODEL .fromJson(new ToscaYamlToJsonConvertor().parseToscaYaml(policyModel.getPolicyModelTosca(), - policyModel.getPolicyModelType()), JsonObject.class),loopElementModel, null, null); - } - - private JsonObject createJsonFromPolicyTosca() { - Map<String, Object> map = new Yaml().load(this.getPolicyModel().getPolicyModelTosca()); - JSONObject jsonObject = new JSONObject(map); - return new Gson().fromJson(jsonObject.toString(), JsonObject.class); + policyModel.getPolicyModelType()), JsonObject.class), loopElementModel,null,null); } /** * The constructor that does not make use of ToscaYamlToJsonConvertor but take * the jsonRepresentation instead. + * * @param name The name of the MicroService * @param policyModel The policy model type of the MicroService * @param shared The flag indicate whether the MicroService is @@ -145,7 +125,7 @@ public class MicroServicePolicy extends Policy implements Serializable { public MicroServicePolicy(String name, PolicyModel policyModel, Boolean shared, JsonObject jsonRepresentation, LoopElementModel loopElementModel, String pdpGroup, String pdpSubgroup) { this.name = name; - this.policyModel = policyModel; + this.setPolicyModel(policyModel); this.shared = shared; this.setJsonRepresentation(jsonRepresentation); this.setLoopElementModel(loopElementModel); @@ -200,14 +180,6 @@ public class MicroServicePolicy extends Policy implements Serializable { this.deviceTypeScope = deviceTypeScope; } - public PolicyModel getPolicyModel() { - return policyModel; - } - - public void setPolicyModel(PolicyModel policyModel) { - this.policyModel = policyModel; - } - /** * dcaeDeploymentId getter. * @@ -288,48 +260,7 @@ public class MicroServicePolicy extends Policy implements Serializable { } } else if (!name.equals(other.name)) { return false; - } return true; - } - - private String getMicroServicePropertyNameFromTosca(JsonObject object) { - return object.getAsJsonObject("policy_types").getAsJsonObject(this.getPolicyModel().getPolicyModelType()) - .getAsJsonObject( - "properties") - .keySet().toArray(new String[1])[0]; - } - - @Override - public String createPolicyPayload() { - 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("version", this.getPolicyModel().getVersion()); - - JsonObject policyMetadata = new JsonObject(); - policyDetails.add("metadata", policyMetadata); - policyMetadata.addProperty("policy-id", this.getName()); - - JsonObject policyProperties = new JsonObject(); - policyDetails.add("properties", policyProperties); - policyProperties.add(this.getMicroServicePropertyNameFromTosca(toscaJson), this.getConfigurationsJson()); - String policyPayload = new GsonBuilder().setPrettyPrinting().create().toJson(policyPayloadResult); - logger.info("Micro service policy payload: " + policyPayload); - return policyPayload; + } + return true; } - } diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java index 7cf06dcfc..b0e8b0c3d 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java @@ -51,6 +51,8 @@ import javax.persistence.Table; import javax.persistence.Transient; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor; +import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.Loop; import org.onap.clamp.loop.template.LoopElementModel; @@ -80,12 +82,6 @@ public class OperationalPolicy extends Policy implements Serializable { @JoinColumn(name = "loop_id", nullable = false) private Loop loop; - @Expose - @ManyToOne(fetch = FetchType.EAGER) - @JoinColumns({@JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"), - @JoinColumn(name = "policy_model_version", referencedColumnName = "version")}) - private PolicyModel policyModel; - public OperationalPolicy() { // Serialization } @@ -111,16 +107,32 @@ public class OperationalPolicy extends Policy implements Serializable { this.setPdpGroup(pdpGroup); this.setPdpSubgroup(pdpSubgroup); this.setLoopElementModel(loopElementModel); - if (policyModel != null && policyModel.getPolicyModelType().contains("legacy")) { - LegacyOperationalPolicy.preloadConfiguration(configurationsJson, loop); + this.setJsonRepresentation(this.generateJsonRepresentation(policyModel)); + + } + + private JsonObject generateJsonRepresentation(PolicyModel policyModel) { + JsonObject jsonReturned = new JsonObject(); + if (policyModel == null) { + return new JsonObject(); } try { - this.setJsonRepresentation( - OperationalPolicyRepresentationBuilder.generateOperationalPolicySchema(loop.getModelService())); + if (isLegacy()) { + // Op policy Legacy case + LegacyOperationalPolicy.preloadConfiguration(jsonReturned, loop); + this.setJsonRepresentation( + OperationalPolicyRepresentationBuilder.generateOperationalPolicySchema(loop.getModelService())); + } else { + // Generic Case + this.setJsonRepresentation(JsonUtils.GSON + .fromJson(new ToscaYamlToJsonConvertor().parseToscaYaml(policyModel.getPolicyModelTosca(), + policyModel.getPolicyModelType()), JsonObject.class)); + } } catch (JsonSyntaxException | IOException | NullPointerException e) { logger.error("Unable to generate the operational policy Schema ... ", e); this.setJsonRepresentation(new JsonObject()); } + return jsonReturned; } public void setLoop(Loop loopName) { @@ -146,24 +158,6 @@ public class OperationalPolicy extends Policy implements Serializable { this.name = name; } - /** - * policyModel getter. - * - * @return the policyModel - */ - public PolicyModel getPolicyModel() { - return policyModel; - } - - /** - * policyModel setter. - * - * @param policyModel the policyModel to set - */ - public void setPolicyModel(PolicyModel policyModel) { - this.policyModel = policyModel; - } - @Override public int hashCode() { final int prime = 31; @@ -194,6 +188,10 @@ public class OperationalPolicy extends Policy implements Serializable { return true; } + public Boolean isLegacy() { + return (this.getPolicyModel() != null) && this.getPolicyModel().getPolicyModelType().contains("legacy"); + } + /** * Create policy Yaml from json defined here. * @@ -236,17 +234,22 @@ public class OperationalPolicy extends Policy implements Serializable { @Override public String createPolicyPayload() throws UnsupportedEncodingException { - // Now using the legacy payload fo Dublin - JsonObject payload = new JsonObject(); - payload.addProperty("policy-id", this.getName()); - payload.addProperty("content", - URLEncoder.encode( - LegacyOperationalPolicy - .createPolicyPayloadYamlLegacy(this.getConfigurationsJson().get("operational_policy")), - StandardCharsets.UTF_8.toString())); - String opPayload = new GsonBuilder().setPrettyPrinting().create().toJson(payload); - logger.info("Operational policy payload: " + opPayload); - return opPayload; + if (isLegacy()) { + // Now using the legacy payload fo Dublin + JsonObject payload = new JsonObject(); + payload.addProperty("policy-id", this.getName()); + payload.addProperty("content", + URLEncoder.encode( + LegacyOperationalPolicy + .createPolicyPayloadYamlLegacy( + this.getConfigurationsJson().get("operational_policy")), + StandardCharsets.UTF_8.toString())); + String opPayload = new GsonBuilder().setPrettyPrinting().create().toJson(payload); + logger.info("Operational policy payload: " + opPayload); + return opPayload; + } else { + return super.createPolicyPayload(); + } } /** |