From 3142c70fb7530c444c891674fe55c6a9b236dfb3 Mon Sep 17 00:00:00 2001 From: sebdet Date: Fri, 23 Aug 2019 07:45:27 -0700 Subject: Fix the log status Fix the way the code (camel) computes the global loop state + introduce level in component state for that Issue-ID: CLAMP-384 Change-Id: I83a9306eabacfc1f118df6b828e521ee59d19b5f Signed-off-by: sebdet --- .../components/external/ExternalComponent.java | 6 +- .../external/ExternalComponentState.java | 60 +++++++- .../loop/components/external/PolicyComponent.java | 60 +++++--- .../resources/clds/camel/routes/loop-flows.xml | 157 ++++++++------------- .../resources/clds/camel/routes/policy-flows.xml | 31 ++++ .../java/org/onap/clamp/loop/LoopToJsonTest.java | 34 ++--- 6 files changed, 208 insertions(+), 140 deletions(-) diff --git a/src/main/java/org/onap/clamp/loop/components/external/ExternalComponent.java b/src/main/java/org/onap/clamp/loop/components/external/ExternalComponent.java index a8aae2038..2be707fe4 100644 --- a/src/main/java/org/onap/clamp/loop/components/external/ExternalComponent.java +++ b/src/main/java/org/onap/clamp/loop/components/external/ExternalComponent.java @@ -28,9 +28,7 @@ import com.google.gson.annotations.Expose; import org.apache.camel.Exchange; /** - * - * SHould be abstract but Gson can't instantiate it if it's an abstract - * + * Should be abstract but Gson can't instantiate it if it's an abstract. */ public class ExternalComponent { @Expose @@ -49,7 +47,7 @@ public class ExternalComponent { } public ExternalComponentState computeState(Exchange camelExchange) { - return new ExternalComponentState("INIT", "no desc"); + return new ExternalComponentState("INIT", "no desc", 0); } public ExternalComponent(ExternalComponentState initialState) { diff --git a/src/main/java/org/onap/clamp/loop/components/external/ExternalComponentState.java b/src/main/java/org/onap/clamp/loop/components/external/ExternalComponentState.java index 6a723c24e..a220ee1d4 100644 --- a/src/main/java/org/onap/clamp/loop/components/external/ExternalComponentState.java +++ b/src/main/java/org/onap/clamp/loop/components/external/ExternalComponentState.java @@ -29,18 +29,26 @@ import com.google.gson.annotations.Expose; * This is a transient state reflecting the deployment status of a component. It * can be Policy, DCAE, or whatever... This is object is generic. Clamp is now * stateless, so it triggers the different components at runtime, the status per - * component is stored here. + * component is stored here. The state level is used to re-compute the global + * state when multiple sub states are required for that computation (generally + * provided sequentially to the method computeState from the camel routes. * */ -public class ExternalComponentState { +public class ExternalComponentState implements Comparable { @Expose private String stateName; @Expose private String description; + private int stateLevel; - public ExternalComponentState(String stateName, String description) { + public ExternalComponentState(String stateName, String description, int level) { this.stateName = stateName; this.description = description; + this.stateLevel = level; + } + + public ExternalComponentState(String stateName, String description) { + this(stateName, description, 0); } public ExternalComponentState() { @@ -58,4 +66,50 @@ public class ExternalComponentState { public String toString() { return stateName; } + + public int getLevel() { + return stateLevel; + } + + public void setLevel(int priority) { + this.stateLevel = priority; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((stateName == null) ? 0 : stateName.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ExternalComponentState other = (ExternalComponentState) obj; + if (stateName == null) { + if (other.stateName != null) + return false; + } else if (!stateName.equals(other.stateName)) + return false; + return true; + } + + /** + * This method compares this object by using the level of them. + * + * @param stateToCompare The state to compare to the current object + * @return If the one given in input has a higher level than the current object + * it returns -1, 1 otherwise and 0 if equals. + */ + @Override + public int compareTo(ExternalComponentState stateToCompare) { + return Integer.compare(this.getLevel(), stateToCompare.getLevel()); + } + } diff --git a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java index acd6115fe..68d598b5f 100644 --- a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java +++ b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java @@ -44,17 +44,22 @@ public class PolicyComponent extends ExternalComponent { @Transient private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyComponent.class); + public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR", + "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent", 100); public static final ExternalComponentState NOT_SENT = new ExternalComponentState("NOT_SENT", - "The policies defined have NOT yet been created on the policy engine"); + "The policies defined have NOT yet been created on the policy engine", 90); public static final ExternalComponentState SENT = new ExternalComponentState("SENT", - "The policies defined have been created but NOT deployed on the policy engine"); + "The policies defined have been created but NOT deployed on the policy engine", 50); public static final ExternalComponentState SENT_AND_DEPLOYED = new ExternalComponentState("SENT_AND_DEPLOYED", - "The policies defined have been created and deployed on the policy engine"); - public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR", - "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent"); + "The policies defined have been created and deployed on the policy engine", 10); public PolicyComponent() { - super(NOT_SENT); + /* + * We assume it's good by default as we will receive the state for each policy + * on by one, each time we increase the level we can't decrease it anymore. + * That's why it starts with the lowest one SENT_AND_DEPLOYED. + */ + super(SENT_AND_DEPLOYED); } @Override @@ -103,21 +108,38 @@ public class PolicyComponent extends ExternalComponent { return policyNamesList; } + private static ExternalComponentState findNewState(boolean found, boolean deployed) { + + ExternalComponentState newState = NOT_SENT; + if (found && deployed) { + newState = SENT_AND_DEPLOYED; + } else if (found) { + newState = SENT; + } else if (deployed) { + newState = IN_ERROR; + } + return newState; + } + + private static ExternalComponentState mergeStates(ExternalComponentState oldState, + ExternalComponentState newState) { + return (oldState.compareTo(newState) < 0) ? newState : oldState; + } + + /** + * This is a method that expect the results of the queries getPolicy and + * getPolicyDeployed for a unique policy (op,guard, config, etc ...). It + * re-computes the global policy state for each policy results given. Therefore + * this method is called multiple times from the camel route and must be reset + * for a new global policy state retrieval. The state to compute the global + * policy state is stored in this class. + * + */ @Override public ExternalComponentState computeState(Exchange camelExchange) { - boolean oneNotFound = (boolean) camelExchange.getIn().getExchange().getProperty("atLeastOnePolicyNotFound"); - boolean oneNotDeployed = (boolean) camelExchange.getIn().getExchange() - .getProperty("atLeastOnePolicyNotDeployed"); - - if (oneNotFound && oneNotDeployed) { - this.setState(NOT_SENT); - } else if (!oneNotFound && oneNotDeployed) { - this.setState(SENT); - } else if (!oneNotFound && !oneNotDeployed) { - this.setState(SENT_AND_DEPLOYED); - } else { - this.setState(IN_ERROR); - } + this.setState(mergeStates(this.getState(), + findNewState((boolean) camelExchange.getIn().getExchange().getProperty("policyFound"), + (boolean) camelExchange.getIn().getExchange().getProperty("policyDeployed")))); return this.getState(); } } diff --git a/src/main/resources/clds/camel/routes/loop-flows.xml b/src/main/resources/clds/camel/routes/loop-flows.xml index ede899e71..922d5f164 100644 --- a/src/main/resources/clds/camel/routes/loop-flows.xml +++ b/src/main/resources/clds/camel/routes/loop-flows.xml @@ -5,9 +5,7 @@ ${header.loopName} - + @@ -15,26 +13,24 @@ 404 - + + - - false - - - false - ${exchangeProperty[loopObject].getComponent('POLICY')} - + true + + + true + + ${exchangeProperty[loopObject].getMicroServicePolicies()} @@ -48,29 +44,11 @@ null - - - false - - - - ${header.CamelHttpResponseCode} != 200 - - true - - - - - ${header.CamelHttpResponseCode} != 200 - - true - - + - ${exchangeProperty[loopObject].getOperationalPolicies()} @@ -87,29 +65,10 @@ null - - - false - - - - ${header.CamelHttpResponseCode} != 200 - - true - - - - - ${header.CamelHttpResponseCode} != 200 - - true - - - - + ${exchangeProperty[operationalPolicy].createGuardPolicyPayloads().entrySet()} @@ -123,42 +82,23 @@ null - - - false - - - - ${header.CamelHttpResponseCode} != 200 - - true - - - - - ${header.CamelHttpResponseCode} != 200 - - true - - + - ${exchangeProperty[policyComponent].computeState(*)} + ${exchangeProperty[policyComponent].getState()} - - ${exchangeProperty[loopObject].getComponent('DCAE')} @@ -175,8 +115,7 @@ ${header.CamelHttpResponseCode} == 200 - @@ -186,8 +125,7 @@ ${exchangeProperty[dcaeComponent].computeState(*)} - @@ -195,53 +133,78 @@ - - ${exchangeProperty['dcaeState'].getStateName()} == 'BLUEPRINT_DEPLOYED' and ${exchangeProperty['policyState'].getStateName()} == 'NOT_SENT' + ${exchangeProperty['dcaeState'].getStateName()} == + 'BLUEPRINT_DEPLOYED' and ${exchangeProperty['policyState'].getStateName()} + == 'NOT_SENT' + - ${exchangeProperty['dcaeState'].getStateName()} == 'IN_ERROR' or ${exchangeProperty['dcaeState'].getStateName()} == 'MICROSERVICE_INSTALLATION_FAILED' + ${exchangeProperty['dcaeState'].getStateName()} == 'IN_ERROR' or + ${exchangeProperty['dcaeState'].getStateName()} == + 'MICROSERVICE_INSTALLATION_FAILED' + - ${exchangeProperty['dcaeState'].getStateName()} == 'MICROSERVICE_UNINSTALLATION_FAILED' or ${exchangeProperty['policyState'].getStateName()} == 'IN_ERROR' + ${exchangeProperty['dcaeState'].getStateName()} == + 'MICROSERVICE_UNINSTALLATION_FAILED' or + ${exchangeProperty['policyState'].getStateName()} == 'IN_ERROR' + - ${exchangeProperty['dcaeState'].getStateName()} == 'MICROSERVICE_INSTALLED_SUCCESSFULLY' and ${exchangeProperty['policyState'].getStateName()} == 'SENT_AND_DEPLOYED' + ${exchangeProperty['dcaeState'].getStateName()} == + 'MICROSERVICE_INSTALLED_SUCCESSFULLY' and + ${exchangeProperty['policyState'].getStateName()} == 'SENT_AND_DEPLOYED' + - ${exchangeProperty['dcaeState'].getStateName()} == 'MICROSERVICE_INSTALLED_SUCCESSFULLY' and ${exchangeProperty['policyState'].getStateName()} == 'SENT' + ${exchangeProperty['dcaeState'].getStateName()} == + 'MICROSERVICE_INSTALLED_SUCCESSFULLY' and + ${exchangeProperty['policyState'].getStateName()} == 'SENT' + - ${exchangeProperty['dcaeState'].getStateName()} == 'BLUEPRINT_DEPLOYED' or ${exchangeProperty['dcaeState'].getStateName()} == 'MICROSERVICE_UNINSTALLED_SUCCESSFULLY' and ${exchangeProperty['policyState'].getStateName()} == 'SENT_AND_DEPLOYED' + ${exchangeProperty['dcaeState'].getStateName()} == + 'BLUEPRINT_DEPLOYED' or ${exchangeProperty['dcaeState'].getStateName()} == + 'MICROSERVICE_UNINSTALLED_SUCCESSFULLY' and + ${exchangeProperty['policyState'].getStateName()} == 'SENT_AND_DEPLOYED' + - ${exchangeProperty['dcaeState'].getStateName()} == 'PROCESSING_MICROSERVICE_INSTALLATION' or ${exchangeProperty['dcaeState'].getStateName()} == 'PROCESSING_MICROSERVICE_UNINSTALLATION' and ${exchangeProperty['policyState'].getStateName()} == 'SENT_AND_DEPLOYED' + ${exchangeProperty['dcaeState'].getStateName()} == + 'PROCESSING_MICROSERVICE_INSTALLATION' or + ${exchangeProperty['dcaeState'].getStateName()} == + 'PROCESSING_MICROSERVICE_UNINSTALLATION' and + ${exchangeProperty['policyState'].getStateName()} == 'SENT_AND_DEPLOYED' + - ${exchangeProperty['dcaeState'].getStateName()} == 'MICROSERVICE_INSTALLED_SUCCESSFULLY' and ${exchangeProperty['policyState'].getStateName()} != 'NOT_SENT' + ${exchangeProperty['dcaeState'].getStateName()} == + 'MICROSERVICE_INSTALLED_SUCCESSFULLY' and + ${exchangeProperty['policyState'].getStateName()} != 'NOT_SENT' + - diff --git a/src/main/resources/clds/camel/routes/policy-flows.xml b/src/main/resources/clds/camel/routes/policy-flows.xml index 5d5861547..b6f30c37c 100644 --- a/src/main/resources/clds/camel/routes/policy-flows.xml +++ b/src/main/resources/clds/camel/routes/policy-flows.xml @@ -1,5 +1,36 @@ + + + + false + + + + ${header.CamelHttpResponseCode} != 200 + + false + + + + + false + + + + ${header.CamelHttpResponseCode} != 200 + + false + + + + + ${exchangeProperty[policyComponent].computeState(*)} + + + diff --git a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java index c9350c610..3010fe50b 100644 --- a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java +++ b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java @@ -54,7 +54,7 @@ public class LoopToJsonTest { } private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, - String dcaeId, String dcaeUrl, String dcaeBlueprintId) { + String dcaeId, String dcaeUrl, String dcaeBlueprintId) { Loop loop = new Loop(name, blueprint, svgRepresentation); loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); loop.setLastComputedState(LoopState.DESIGN); @@ -65,9 +65,9 @@ public class LoopToJsonTest { } private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation, - String policyTosca, String jsonProperties, boolean shared) { + String policyTosca, String jsonProperties, boolean shared) { MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared, - gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); + gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); return microService; @@ -82,13 +82,13 @@ public class LoopToJsonTest { @Test public void loopGsonTest() throws IOException { Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", - "123456789", "https://dcaetest.org", "UUID-blueprint"); + "123456789", "https://dcaetest.org", "UUID-blueprint"); OperationalPolicy opPolicy = this.getOperationalPolicy( - ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest"); + ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest"); loopTest.addOperationalPolicy(opPolicy); MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "", - "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0", - "{\"param1\":\"value1\"}", true); + "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0", + "{\"param1\":\"value1\"}", true); loopTest.addMicroServicePolicy(microServicePolicy); LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest); loopTest.addLog(loopLog); @@ -99,11 +99,11 @@ public class LoopToJsonTest { Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class); assertNotNull(loopTestDeserialized); assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint", - "components"); + "components"); assertThat(loopTestDeserialized.getComponent("DCAE").getState()) - .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState()); - assertThat(loopTestDeserialized.getComponent("POLICY").getState()) - .isEqualToComparingFieldByField(loopTest.getComponent("POLICY").getState()); + .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState()); + assertThat(loopTestDeserialized.getComponent("POLICY").getState()).isEqualToComparingOnlyGivenFields( + loopTest.getComponent("POLICY").getState(), "stateName", "description"); // svg and blueprint not exposed so wont be deserialized assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null); assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null); @@ -112,22 +112,22 @@ public class LoopToJsonTest { assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy); assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog); assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog, - "loop"); + "loop"); } @Test public void createPoliciesPayloadPdpGroupTest() throws IOException { Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", - "123456789", "https://dcaetest.org", "UUID-blueprint"); + "123456789", "https://dcaetest.org", "UUID-blueprint"); OperationalPolicy opPolicy = this.getOperationalPolicy( - ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest"); + ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest"); loopTest.addOperationalPolicy(opPolicy); MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "", - "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0", - "{\"param1\":\"value1\"}", true); + "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0", + "{\"param1\":\"value1\"}", true); loopTest.addMicroServicePolicy(microServicePolicy); JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"), - PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false); + PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false); } } -- cgit 1.2.3-korg