From e916ac28ba46ff7cad64f1a3150b128ba4772c70 Mon Sep 17 00:00:00 2001 From: sebdet Date: Mon, 16 Mar 2020 11:04:34 -0700 Subject: [PATCH] Rework the policy refresh Rework the policy refresh for the new unique dialog policyModel Issue-ID: CLAMP-578 Signed-off-by: sebdet Change-Id: Ie8c91223e92c1e344d7ead5784ffea33d4f6a00f --- src/main/java/org/onap/clamp/loop/Loop.java | 24 ++----- .../java/org/onap/clamp/loop/LoopController.java | 64 ++++++++++++++++-- src/main/java/org/onap/clamp/loop/LoopService.java | 27 ++------ .../onap/clamp/loop/template/LoopElementModel.java | 8 +-- src/main/java/org/onap/clamp/policy/Policy.java | 8 +++ .../policy/microservice/MicroServicePolicy.java | 23 ++++--- .../microservice/MicroServicePolicyService.java | 30 +++++++-- .../policy/operational/OperationalPolicy.java | 75 ++++++++-------------- .../OperationalPolicyRepresentationBuilder.java | 60 +++++++++-------- .../operational/OperationalPolicyService.java | 19 +++++- .../resources/clds/camel/rest/clamp-api-v2.xml | 53 ++++++++++++--- ...OperationalPolicyRepresentationBuilderTest.java | 2 - ui-react/src/api/LoopCache.js | 4 -- ui-react/src/api/LoopCache.test.js | 11 ---- ui-react/src/api/LoopService.js | 27 +++++++- .../OperationalPolicy/OperationalPolicyModal.js | 2 +- 16 files changed, 259 insertions(+), 178 deletions(-) diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java index dd6fbf05..605e42fd 100644 --- a/src/main/java/org/onap/clamp/loop/Loop.java +++ b/src/main/java/org/onap/clamp/loop/Loop.java @@ -23,11 +23,8 @@ package org.onap.clamp.loop; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; -import java.io.IOException; import java.io.Serializable; import java.util.HashMap; import java.util.HashSet; @@ -77,9 +74,6 @@ public class Loop extends AuditEntity implements Serializable { */ private static final long serialVersionUID = -286522707701388642L; - @Transient - private static final EELFLogger logger = EELFManager.getInstance().getLogger(Loop.class); - @Id @Expose @Column(nullable = false, name = "name", unique = true) @@ -170,23 +164,13 @@ public class Loop extends AuditEntity implements Serializable { this.setModelService(loopTemplate.getModelService()); loopTemplate.getLoopElementModelsUsed().forEach(element -> { if (LoopElementModel.MICRO_SERVICE_TYPE.equals(element.getLoopElementModel().getLoopElementType())) { - try { - this.addMicroServicePolicy((MicroServicePolicy) element.getLoopElementModel() - .createPolicyInstance(this, toscaConverter)); - } catch (IOException e) { - logger.error("Exception caught when creating the microservice policy instance of the loop " - + "instance", e); - } + this.addMicroServicePolicy((MicroServicePolicy) element.getLoopElementModel() + .createPolicyInstance(this, toscaConverter)); } else if (LoopElementModel.OPERATIONAL_POLICY_TYPE .equals(element.getLoopElementModel().getLoopElementType())) { - try { - this.addOperationalPolicy((OperationalPolicy) element.getLoopElementModel() - .createPolicyInstance(this, toscaConverter)); - } catch (IOException e) { - logger.error("Exception caught when creating the operational policy instance of the loop instance", - e); - } + this.addOperationalPolicy((OperationalPolicy) element.getLoopElementModel() + .createPolicyInstance(this, toscaConverter)); } }); } diff --git a/src/main/java/org/onap/clamp/loop/LoopController.java b/src/main/java/org/onap/clamp/loop/LoopController.java index d230eb97..9c2c71f5 100644 --- a/src/main/java/org/onap/clamp/loop/LoopController.java +++ b/src/main/java/org/onap/clamp/loop/LoopController.java @@ -29,9 +29,12 @@ import com.google.gson.reflect.TypeToken; import java.io.IOException; import java.lang.reflect.Type; import java.util.List; +import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport; import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.microservice.MicroServicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; +import org.onap.clamp.policy.operational.OperationalPolicyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -39,13 +42,36 @@ import org.springframework.stereotype.Controller; public class LoopController { private final LoopService loopService; - private static final Type OPERATIONAL_POLICY_TYPE = new TypeToken>() {}.getType(); - private static final Type MICROSERVICE_POLICY_TYPE = new TypeToken>() {}.getType(); + private final ToscaConverterWithDictionarySupport toscaConverter; + private final OperationalPolicyService operationalPolicyService; + + private final MicroServicePolicyService microServicePolicyService; + + private static final Type OPERATIONAL_POLICY_TYPE = new TypeToken>() { + }.getType(); + + private static final Type MICROSERVICE_POLICY_TYPE = new TypeToken>() { + }.getType(); + + + /** + * Constructor. + * + * @param loopService loopService + * @param operationalPolicyService operationalPolicyService + * @param microServicePolicyService microServicePolicyService + * @param toscaConverter toscaConverter + */ @Autowired - public LoopController(LoopService loopService) { + public LoopController(LoopService loopService, OperationalPolicyService operationalPolicyService, + MicroServicePolicyService microServicePolicyService, + ToscaConverterWithDictionarySupport toscaConverter) { this.loopService = loopService; + this.toscaConverter = toscaConverter; + this.operationalPolicyService = operationalPolicyService; + this.microServicePolicyService = microServicePolicyService; } public Loop createLoop(String loopName, String templateName) { @@ -156,10 +182,34 @@ public class LoopController { /** * Refresh the Operational Policy Json representation of the loop. * - * @param loopName The loop name - * @return The refreshed Loop + * @param loop The loop + * @param operationalPolicyName The operational policy name that needs a refresh + * @return The loop object + */ + public Loop refreshOperationalPolicyJsonRepresentation(Loop loop, String operationalPolicyName) { + for (OperationalPolicy operationalPolicy : loop.getOperationalPolicies()) { + if (operationalPolicy.getName().equals(operationalPolicyName)) { + this.operationalPolicyService + .refreshOperationalPolicyJsonRepresentation(operationalPolicy, toscaConverter); + } + } + return loop; + } + + /** + * Refresh the Config Policy Json representation of the loop. + * + * @param loop The loop + * @param microServicePolicyName The microservice policy name that needs a refresh + * @return The loop object */ - public Loop refreshOpPolicyJsonRepresentation(String loopName) { - return loopService.refreshOpPolicyJsonRepresentation(loopName); + public Loop refreshMicroServicePolicyJsonRepresentation(Loop loop, String microServicePolicyName) { + for (MicroServicePolicy microServicePolicy : loop.getMicroServicePolicies()) { + if (microServicePolicy.getName().equals(microServicePolicyName)) { + this.microServicePolicyService + .refreshMicroServicePolicyJsonRepresentation(microServicePolicy, toscaConverter); + } + } + return loop; } } diff --git a/src/main/java/org/onap/clamp/loop/LoopService.java b/src/main/java/org/onap/clamp/loop/LoopService.java index acd125b7..af1f58ba 100644 --- a/src/main/java/org/onap/clamp/loop/LoopService.java +++ b/src/main/java/org/onap/clamp/loop/LoopService.java @@ -122,15 +122,15 @@ public class LoopService { return null; } loop.addOperationalPolicy( - new OperationalPolicy(loop,loop.getModelService(), policyModel, toscaConverter)); + new OperationalPolicy(loop, loop.getModelService(), policyModel, toscaConverter)); return loopsRepository.saveAndFlush(loop); } /** * This method remove an operational policy to a loop instance. * - * @param loopName The loop name - * @param policyType The policy model type + * @param loopName The loop name + * @param policyType The policy model type * @param policyVersion The policy model version * @return The loop modified */ @@ -141,8 +141,8 @@ public class LoopService { return null; } for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) { - if (opPolicy.getPolicyModel().getPolicyModelType().equals(policyType) && - opPolicy.getPolicyModel().getVersion().equals(policyVersion)) { + if (opPolicy.getPolicyModel().getPolicyModelType().equals(policyType) + && opPolicy.getPolicyModel().getVersion().equals(policyVersion)) { loop.removeOperationalPolicy(opPolicy); break; } @@ -179,20 +179,5 @@ public class LoopService { return loopsRepository.findById(loopName) .orElseThrow(() -> new EntityNotFoundException("Couldn't find closed loop named: " + loopName)); } - - /** - * Api to refresh the Operational Policy UI window. - * - * @param loopName The loop Name - * @return The refreshed loop object - */ - public Loop refreshOpPolicyJsonRepresentation(String loopName) { - Loop loop = findClosedLoopByName(loopName); - Set policyList = loop.getOperationalPolicies(); - for (OperationalPolicy policy : policyList) { - policy.updateJsonRepresentation(); - } - loop.setOperationalPolicies(policyList); - return loopsRepository.save(loop); - } } + diff --git a/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java b/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java index dfdfc42b..4a46a954 100644 --- a/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java +++ b/src/main/java/org/onap/clamp/loop/template/LoopElementModel.java @@ -24,7 +24,6 @@ package org.onap.clamp.loop.template; import com.google.gson.annotations.Expose; -import java.io.IOException; import java.io.Serializable; import java.util.HashSet; import java.util.Set; @@ -251,16 +250,15 @@ public class LoopElementModel extends AuditEntity implements Serializable { * Create a policy instance from the current loop element model. * * @return A Policy object. - * @throws IOException in case of failure when creating an operational policy */ - public Policy createPolicyInstance(Loop loop, ToscaConverterWithDictionarySupport toscaConverter) - throws IOException { + public Policy createPolicyInstance(Loop loop, ToscaConverterWithDictionarySupport toscaConverter) { if (LoopElementModel.MICRO_SERVICE_TYPE.equals(this.getLoopElementType())) { return new MicroServicePolicy(loop, loop.getModelService(), this, toscaConverter); } else if (LoopElementModel.OPERATIONAL_POLICY_TYPE.equals(this.getLoopElementType())) { return new OperationalPolicy(loop, loop.getModelService(), this, toscaConverter); - } else { + } + else { return null; } } diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java index 3b220646..87d36f3d 100644 --- a/src/main/java/org/onap/clamp/policy/Policy.java +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -43,6 +43,7 @@ import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; import org.json.JSONObject; +import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.common.AuditEntity; import org.onap.clamp.loop.template.LoopElementModel; @@ -175,6 +176,13 @@ public abstract class Policy extends AuditEntity { this.jsonRepresentation = jsonRepresentation; } + /** + * Regenerate the Policy Json Representation. + * + * @param toscaConverter The tosca converter required to regenerate the json schema + */ + public abstract void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter); + /** * policyModel getter. * 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 321c12f6..47b3a4ff 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -23,8 +23,6 @@ package org.onap.clamp.policy.microservice; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; import java.io.Serializable; @@ -36,7 +34,6 @@ import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; -import javax.persistence.Transient; import org.apache.commons.lang3.RandomStringUtils; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; @@ -57,9 +54,6 @@ public class MicroServicePolicy extends Policy implements Serializable { */ private static final long serialVersionUID = 6271238288583332616L; - @Transient - private static final EELFLogger logger = EELFManager.getInstance().getLogger(MicroServicePolicy.class); - @Expose @Id @Column(nullable = false, name = "name", unique = true) @@ -126,20 +120,19 @@ public class MicroServicePolicy extends Policy implements Serializable { /** * Constructor with tosca converter. * - * @param loop The loop instance - * @param service The service model object + * @param loop The loop instance + * @param service The service model object * @param loopElementModel The loop element model from which this microservice instance is created - * @param toscaConverter The tosca converter that will used to convert the tosca policy model + * @param toscaConverter The tosca converter that will used to convert the tosca policy model */ public MicroServicePolicy(Loop loop, Service service, LoopElementModel loopElementModel, ToscaConverterWithDictionarySupport toscaConverter) { this(Policy.generatePolicyName("MICROSERVICE", service.getName(), service.getVersion(), RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)), loopElementModel.getPolicyModels().first(), false, - toscaConverter.convertToscaToJsonSchemaObject( - loopElementModel.getPolicyModels().first().getPolicyModelTosca(), - loopElementModel.getPolicyModels().first().getPolicyModelType()), + new JsonObject(), loopElementModel, null, null); + this.updateJsonRepresentation(toscaConverter); } @Override @@ -157,6 +150,12 @@ public class MicroServicePolicy extends Policy implements Serializable { this.name = name; } + @Override + public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter) { + toscaConverter.convertToscaToJsonSchemaObject(this.getPolicyModel().getPolicyModelTosca(), + this.getPolicyModel().getPolicyModelType()); + } + public Boolean getShared() { return shared; } diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java index 9bc641c6..0631380f 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java @@ -26,6 +26,7 @@ package org.onap.clamp.policy.microservice; import java.util.List; import java.util.Set; import java.util.stream.Collectors; +import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport; import org.onap.clamp.loop.Loop; import org.onap.clamp.policy.PolicyService; import org.springframework.beans.factory.annotation.Autowired; @@ -34,11 +35,11 @@ import org.springframework.stereotype.Service; @Service public class MicroServicePolicyService implements PolicyService { - private final MicroServicePolicyRepository repository; + private final MicroServicePolicyRepository microServiceRepository; @Autowired - public MicroServicePolicyService(MicroServicePolicyRepository repository) { - this.repository = repository; + public MicroServicePolicyService(MicroServicePolicyRepository microServiceRepository) { + this.microServiceRepository = microServiceRepository; } @Override @@ -49,7 +50,7 @@ public class MicroServicePolicyService implements PolicyService updateMicroservicePolicyProperties(p, policy, loop)) + return microServiceRepository.save( + microServiceRepository + .findById(policy.getName()).map(p -> updateMicroservicePolicyProperties(p, policy, loop)) .orElse(new MicroServicePolicy(policy.getName(), policy.getPolicyModel(), policy.getShared(), policy.getJsonRepresentation(), null, policy.getPdpGroup(), policy.getPdpSubgroup()))); @@ -89,6 +91,20 @@ public class MicroServicePolicyService implements PolicyService setConfiguration(p, policy)) - .orElse(initializeMissingFields(loop,policy))) + .orElse(initializeMissingFields(loop, policy))) .collect(Collectors.toSet()); } @@ -74,4 +79,16 @@ public class OperationalPolicyService implements PolicyService - + + + + + + + + + + + + + java.lang.Exception + + false + + + + + + + + + - + + message="Refresh Operational Policy UI for loop: ${header.loopName} and ${header.operationalPolicyName}" /> + uri="bean:org.onap.clamp.flow.log.FlowLogOperation?method=startLog(*, 'REFRESH Operational Policy UI request')" /> + uri="bean:org.onap.clamp.loop.LoopController?method=refreshOperationalPolicyJsonRepresentation(${exchangeProperty[loopObject]},${header.operationalPolicyName}})" /> + message="REFRESH operational policy request successfully executed for loop: ${header.loopName}" /> + uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('REFRESH operational policy request successfully executed','INFO',${exchangeProperty[loopObject]})" /> @@ -235,9 +270,9 @@ + message="REFRESH operational policy request failed for loop: ${header.loopName}" /> + uri="bean:org.onap.clamp.loop.log.LoopLogService?method=addLog('REFRESH operational policy request failed, Error reported: ${exception} - Body: ${exception.responseBody}','ERROR',${exchangeProperty[loopObject]})" /> diff --git a/src/test/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilderTest.java b/src/test/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilderTest.java index 12ddbaa7..4e9b5620 100644 --- a/src/test/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilderTest.java +++ b/src/test/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilderTest.java @@ -27,9 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; - import java.io.IOException; - import org.junit.Test; import org.onap.clamp.clds.util.ResourceFileUtil; import org.onap.clamp.loop.service.Service; diff --git a/ui-react/src/api/LoopCache.js b/ui-react/src/api/LoopCache.js index 5eaa79a6..c54337f2 100644 --- a/ui-react/src/api/LoopCache.js +++ b/ui-react/src/api/LoopCache.js @@ -70,10 +70,6 @@ export default class LoopCache { return this.loopJsonCache["name"]; } - getOperationalPolicyConfigurationJson() { - return this.loopJsonCache["operationalPolicies"]["0"]["configurationsJson"]; - } - getOperationalPolicyJsonSchema() { return this.loopJsonCache["operationalPolicies"]["0"]["jsonRepresentation"]; } diff --git a/ui-react/src/api/LoopCache.test.js b/ui-react/src/api/LoopCache.test.js index fc756814..4642ff52 100644 --- a/ui-react/src/api/LoopCache.test.js +++ b/ui-react/src/api/LoopCache.test.js @@ -30,17 +30,6 @@ describe('Verify LoopCache functions', () => { expect(loopCache.getLoopName()).toBe("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); }); - it('getOperationalPolicyConfigurationJson', () => { - const opPolicyConfig = { - "guard_policies": {}, - "operational_policy": { - "controlLoop": {}, - "policies": [] - } - }; - expect(loopCache.getOperationalPolicyConfigurationJson()).toStrictEqual(opPolicyConfig); - }); - it('getOperationalPolicies', () => { const opPolicy = [{ "name": "OPERATIONAL_h2NMX_v1_0_ResourceInstanceName1_tca", diff --git a/ui-react/src/api/LoopService.js b/ui-react/src/api/LoopService.js index 2750763d..698ee28c 100644 --- a/ui-react/src/api/LoopService.js +++ b/ui-react/src/api/LoopService.js @@ -171,8 +171,8 @@ export default class LoopService { }); } - static refreshOpPolicyJson(loopName) { - return fetch('/restservices/clds/v2/loop/refreshOpPolicyJsonSchema/' + loopName, { + static refreshOperationalPolicyJson(loopName,operationalPolicyName) { + return fetch('/restservices/clds/v2/loop/refreshOperationalPolicyJsonSchema/' + loopName + '/' + operationalPolicyName, { method: 'PUT', headers: { "Content-Type": "application/json" @@ -194,6 +194,29 @@ export default class LoopService { }); } + static refreshMicroServicePolicyJson(loopName,microServicePolicyName) { + return fetch('/restservices/clds/v2/loop/refreshMicroServicePolicyJsonSchema/' + loopName + '/' + microServicePolicyName, { + method: 'PUT', + headers: { + "Content-Type": "application/json" + }, + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("Refresh Operational Policy Json Schema response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("Refresh Operational Policy Json Schema query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("Refresh Operational Policy Json Schema error received", error); + return {}; + }); + } + static addOperationalPolicyType(loopName, policyType, policyVersion) { return fetch('/restservices/clds/v2/loop/addOperationaPolicy/' + loopName + '/policyModel/' + policyType +'/' + policyVersion , { method: 'PUT', diff --git a/ui-react/src/components/dialogs/OperationalPolicy/OperationalPolicyModal.js b/ui-react/src/components/dialogs/OperationalPolicy/OperationalPolicyModal.js index 7ed8ba6f..89e70795 100644 --- a/ui-react/src/components/dialogs/OperationalPolicy/OperationalPolicyModal.js +++ b/ui-react/src/components/dialogs/OperationalPolicy/OperationalPolicyModal.js @@ -127,7 +127,7 @@ export default class OperationalPolicyModal extends React.Component { } handleRefresh() { - LoopService.refreshOpPolicyJson(this.state.loopCache.getLoopName()).then(data => { + LoopService.refreshOperationalPolicyJson(this.state.loopCache.getLoopName(), this.state.loopCache.getOperationalPolicies()[0]).then(data => { var newLoopCache = new LoopCache(data); var schema_json = newLoopCache.getOperationalPolicyJsonSchema(); var operationalPoliciesData = newLoopCache.getOperationalPoliciesNoJsonSchema(); -- 2.16.6