diff options
Diffstat (limited to 'participant')
29 files changed, 677 insertions, 407 deletions
diff --git a/participant/participant-impl/participant-impl-cds/pom.xml b/participant/participant-impl/participant-impl-cds/pom.xml index 170a800da..c52c8f5cb 100644 --- a/participant/participant-impl/participant-impl-cds/pom.xml +++ b/participant/participant-impl/participant-impl-cds/pom.xml @@ -25,7 +25,7 @@ <parent> <groupId>org.onap.policy.clamp.participant</groupId> <artifactId>policy-clamp-participant-impl</artifactId> - <version>6.1.3-SNAPSHOT</version> + <version>6.2.0-SNAPSHOT</version> </parent> <artifactId>policy-clamp-participant-impl-cds</artifactId> diff --git a/participant/participant-impl/participant-impl-http/pom.xml b/participant/participant-impl/participant-impl-http/pom.xml index 5cfb30f6a..668588f63 100644 --- a/participant/participant-impl/participant-impl-http/pom.xml +++ b/participant/participant-impl/participant-impl-http/pom.xml @@ -25,7 +25,7 @@ <parent> <groupId>org.onap.policy.clamp.participant</groupId> <artifactId>policy-clamp-participant-impl</artifactId> - <version>6.1.3-SNAPSHOT</version> + <version>6.2.0-SNAPSHOT</version> </parent> <artifactId>policy-clamp-participant-impl-http</artifactId> diff --git a/participant/participant-impl/participant-impl-kubernetes/pom.xml b/participant/participant-impl/participant-impl-kubernetes/pom.xml index 68c1a1418..fb4620621 100644 --- a/participant/participant-impl/participant-impl-kubernetes/pom.xml +++ b/participant/participant-impl/participant-impl-kubernetes/pom.xml @@ -25,7 +25,7 @@ <parent> <groupId>org.onap.policy.clamp.participant</groupId> <artifactId>policy-clamp-participant-impl</artifactId> - <version>6.1.3-SNAPSHOT</version> + <version>6.2.0-SNAPSHOT</version> </parent> <artifactId>policy-clamp-participant-impl-kubernetes</artifactId> diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandler.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandler.java index e1e9195eb..3f2113d98 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandler.java +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/handler/ControlLoopElementHandler.java @@ -20,9 +20,9 @@ package org.onap.policy.clamp.controlloop.participant.kubernetes.handler; - import java.io.IOException; import java.lang.invoke.MethodHandles; +import java.time.Instant; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -30,6 +30,7 @@ import java.util.concurrent.ConcurrentHashMap; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; @@ -51,7 +52,6 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; - /** * This class handles implementation of controlLoopElement updates. */ @@ -170,10 +170,16 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { * Overridden method. * * @param controlLoopElementId controlLoopElement id - * @throws PfModelException incase of error + * @throws PfModelException in case of error */ @Override public synchronized void handleStatistics(UUID controlLoopElementId) throws PfModelException { - // TODO Implement statistics functionality + var clElement = intermediaryApi.getControlLoopElement(controlLoopElementId); + if (clElement != null) { + var clElementStatistics = new ClElementStatistics(); + clElementStatistics.setControlLoopState(clElement.getState()); + clElementStatistics.setTimeStamp(Instant.now()); + intermediaryApi.updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics); + } } } diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClient.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClient.java index 1c405539b..31fa62f1f 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClient.java +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClient.java @@ -47,6 +47,7 @@ public class HelmClient { private ChartStore chartStore; private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private static final String PATH_DELIMITER = "/"; /** * Install a chart. @@ -100,7 +101,7 @@ public class HelmClient { } var localHelmChartDir = chartStore.getAppPath(chart.getChartId()).toString(); logger.info("Chart not found in helm repositories, verifying local repo {} ", localHelmChartDir); - if (verifyLocalHelmRepo(new File(localHelmChartDir + "/" + chart.getChartId().getName()))) { + if (verifyLocalHelmRepo(new File(localHelmChartDir + PATH_DELIMITER + chart.getChartId().getName()))) { repository = localHelmChartDir; } return repository; diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClientTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClientTest.java index 79a37f8e6..41b1fbeb5 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClientTest.java +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/helm/HelmClientTest.java @@ -89,7 +89,10 @@ class HelmClientTest { .thenReturn("success"); doReturn(new File("/target/tmp/override.yaml")).when(chartStore) .getOverrideFile(any()); - assertDoesNotThrow(() -> helmClient.installChart(charts.get(0))); + var chartinfo = charts.get(0); + assertDoesNotThrow(() -> helmClient.installChart(chartinfo)); + chartinfo.setNamespace(""); + assertDoesNotThrow(() -> helmClient.installChart(chartinfo)); } @Test diff --git a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ChartControllerTest.java b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ChartControllerTest.java index bbb57c320..2af2a0171 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ChartControllerTest.java +++ b/participant/participant-impl/participant-impl-kubernetes/src/test/java/org/onap/policy/clamp/controlloop/participant/kubernetes/rest/ChartControllerTest.java @@ -73,6 +73,7 @@ class ChartControllerTest { private static String UNINSTALL_CHART_URL = "/helm/uninstall/"; private static String ONBOARD_CHART_URL = "/helm/onboard/chart"; private static String DELETE_CHART_URL = "/helm/chart"; + private static String CONFIGURE_REPO_URL = "/helm/repo"; @Autowired private MockMvc mockMvc; @@ -217,6 +218,23 @@ class ChartControllerTest { } + /** + * Test endpoint for configuring a helm repository. + * @throws Exception in case of error. + */ + @Test + void testConfigureRepo() throws Exception { + RequestBuilder requestBuilder; + + requestBuilder = MockMvcRequestBuilders.post(CONFIGURE_REPO_URL).accept(MediaType.APPLICATION_JSON_VALUE) + .content(getInstallationJson(charts.get(0).getChartId().getName(), charts.get(0).getChartId().getVersion())) + .contentType(MediaType.APPLICATION_JSON_VALUE); + + mockMvc.perform(requestBuilder).andExpect(status().isCreated()); + + } + + private String getInstallationJson(String name, String version) { JSONObject jsonObj = new JSONObject(); jsonObj.put("name", name); diff --git a/participant/participant-impl/participant-impl-policy/pom.xml b/participant/participant-impl/participant-impl-policy/pom.xml index bed42b081..ed0667ffd 100644 --- a/participant/participant-impl/participant-impl-policy/pom.xml +++ b/participant/participant-impl/participant-impl-policy/pom.xml @@ -25,7 +25,7 @@ <parent> <groupId>org.onap.policy.clamp.participant</groupId> <artifactId>policy-clamp-participant-impl</artifactId> - <version>6.1.3-SNAPSHOT</version> + <version>6.2.0-SNAPSHOT</version> </parent> <artifactId>policy-clamp-participant-impl-policy</artifactId> diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyPapHttpClient.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyPapHttpClient.java index 3cce3bd8a..f835c6e04 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyPapHttpClient.java +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/client/PolicyPapHttpClient.java @@ -20,12 +20,16 @@ package org.onap.policy.clamp.controlloop.participant.policy.client; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; +import java.util.LinkedList; +import java.util.List; import javax.ws.rs.client.Entity; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters; +import org.onap.policy.models.pdp.concepts.DeploymentGroup; +import org.onap.policy.models.pdp.concepts.DeploymentGroups; +import org.onap.policy.models.pdp.concepts.DeploymentSubGroup; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.springframework.stereotype.Component; @Component @@ -55,33 +59,27 @@ public class PolicyPapHttpClient extends AbstractHttpClient { * @return Response */ public Response handlePolicyDeployOrUndeploy(final String policyName, final String policyVersion, - final String action) { - // policies - JsonObject policyArrayBody = new JsonObject(); - policyArrayBody.addProperty("name", policyName); - policyArrayBody.addProperty("version", policyVersion); - JsonArray policyArr = new JsonArray(); - policyArr.add(policyArrayBody); + final DeploymentSubGroup.Action action) { - // deploymentSubgroups - JsonObject deploymentSubGrpBody = new JsonObject(); - deploymentSubGrpBody.addProperty("pdpType", pdpType); - deploymentSubGrpBody.addProperty("action", action); - deploymentSubGrpBody.add("policies", policyArr); - JsonArray deployArr = new JsonArray(); - deployArr.add(deploymentSubGrpBody); + List<ToscaConceptIdentifier> policies = new LinkedList<ToscaConceptIdentifier>(); + policies.add(new ToscaConceptIdentifier(policyName, policyVersion)); - // groups - JsonObject groupArrayBody = new JsonObject(); - groupArrayBody.addProperty("name", pdpGroup); - groupArrayBody.add("deploymentSubgroups", deployArr); - JsonArray groupArr = new JsonArray(); - groupArr.add(groupArrayBody); + DeploymentSubGroup subGroup = new DeploymentSubGroup(); + subGroup.setPolicies(policies); + subGroup.setPdpType(pdpType); + subGroup.setAction(action); - // main json - JsonObject mainJson = new JsonObject(); - mainJson.add("groups", groupArr); + DeploymentGroup group = new DeploymentGroup(); + List<DeploymentSubGroup> subGroups = new LinkedList<DeploymentSubGroup>(); + subGroups.add(subGroup); + group.setDeploymentSubgroups(subGroups); + group.setName(pdpGroup); - return executePost(PAP_URI + "pdps/deployments/batch", Entity.entity(mainJson, MediaType.APPLICATION_JSON)); + DeploymentGroups groups = new DeploymentGroups(); + List<DeploymentGroup> groupsArr = new LinkedList<DeploymentGroup>(); + groupsArr.add(group); + groups.setGroups(groupsArr); + + return executePost(PAP_URI + "pdps/deployments/batch", Entity.entity(groups, MediaType.APPLICATION_JSON)); } } diff --git a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java index 29a7852f1..901fb682b 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java +++ b/participant/participant-impl/participant-impl-policy/src/main/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandler.java @@ -39,6 +39,7 @@ import org.onap.policy.clamp.controlloop.participant.policy.client.PolicyApiHttp import org.onap.policy.clamp.controlloop.participant.policy.client.PolicyPapHttpClient; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.pdp.concepts.DeploymentSubGroup; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; @@ -101,7 +102,7 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { break; case PASSIVE: try { - undeployPolicies(controlLoopElementId, orderedState); + undeployPolicies(controlLoopElementId); } catch (PfModelRuntimeException e) { LOGGER.debug("Undeploying policies failed - no policies to undeploy {}", controlLoopElementId); } @@ -144,7 +145,8 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { // Deploy all policies of this controlLoop from Policy Framework if (policyMap.entrySet() != null) { for (Entry<String, String> policy : policyMap.entrySet()) { - papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(), "POST"); + papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(), + DeploymentSubGroup.Action.POST); } LOGGER.debug("Policies deployed to {} successfully", controlLoopElementId); } else { @@ -155,11 +157,12 @@ public class ControlLoopElementHandler implements ControlLoopElementListener { ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE); } - private void undeployPolicies(UUID controlLoopElementId, ControlLoopOrderedState newState) { + private void undeployPolicies(UUID controlLoopElementId) { // Undeploy all policies of this controlloop from Policy Framework if (policyMap.entrySet() != null) { for (Entry<String, String> policy : policyMap.entrySet()) { - papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(), "DELETE"); + papHttpClient.handlePolicyDeployOrUndeploy(policy.getKey(), policy.getValue(), + DeploymentSubGroup.Action.DELETE); } LOGGER.debug("Undeployed policies from {} successfully", controlLoopElementId); } else { diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java index 25da5a3e9..d517ef61e 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java @@ -33,9 +33,9 @@ import java.util.Set; import java.util.UUID; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.onap.policy.clamp.controlloop.common.utils.CommonUtils; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; @@ -53,7 +53,6 @@ import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; -import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,8 +63,6 @@ public final class TestListenerUtils { private static final Coder CODER = new StandardCoder(); static CommonTestData commonTestData = new CommonTestData(); private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class); - private static final String POLICY_TYPE_ID = "policy_type_id"; - private static final String POLICY_ID = "policy_id"; /** * Method to create a controlLoop from a yaml file. @@ -174,65 +171,13 @@ public final class TestListenerUtils { List<ParticipantUpdates> participantUpdates = new ArrayList<>(); for (ControlLoopElement element : elements.values()) { - populateToscaNodeTemplateFragment(element, toscaServiceTemplate); - prepareParticipantUpdateForControlLoop(element, participantUpdates); + CommonUtils.setServiceTemplatePolicyInfo(element, toscaServiceTemplate); + CommonUtils.prepareParticipantUpdate(element, participantUpdates); } clUpdateMsg.setParticipantUpdatesList(participantUpdates); return clUpdateMsg; } - private static void populateToscaNodeTemplateFragment(ControlLoopElement clElement, - ToscaServiceTemplate toscaServiceTemplate) { - ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates() - .get(clElement.getDefinition().getName()); - // If the ControlLoopElement has policy_type_id or policy_id, identify it as a PolicyControlLoopElement - // and pass respective PolicyTypes or Policies as part of toscaServiceTemplateFragment - if ((toscaNodeTemplate.getProperties().get(POLICY_TYPE_ID) != null) - || (toscaNodeTemplate.getProperties().get(POLICY_ID) != null)) { - // ControlLoopElement for policy framework, send policies and policyTypes to participants - if ((toscaServiceTemplate.getPolicyTypes() != null) - || (toscaServiceTemplate.getToscaTopologyTemplate().getPolicies() != null)) { - ToscaServiceTemplate toscaServiceTemplateFragment = new ToscaServiceTemplate(); - toscaServiceTemplateFragment.setPolicyTypes(toscaServiceTemplate.getPolicyTypes()); - - ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate(); - toscaTopologyTemplate.setPolicies(toscaServiceTemplate.getToscaTopologyTemplate().getPolicies()); - toscaServiceTemplateFragment.setToscaTopologyTemplate(toscaTopologyTemplate); - - toscaServiceTemplateFragment.setDataTypes(toscaServiceTemplate.getDataTypes()); - - clElement.setToscaServiceTemplateFragment(toscaServiceTemplateFragment); - } - } - } - - private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement, - List<ParticipantUpdates> participantUpdates) { - if (participantUpdates.isEmpty()) { - participantUpdates.add(getControlLoopElementList(clElement)); - } else { - boolean participantExists = false; - for (ParticipantUpdates participantUpdate : participantUpdates) { - if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) { - participantUpdate.getControlLoopElementList().add(clElement); - participantExists = true; - } - } - if (!participantExists) { - participantUpdates.add(getControlLoopElementList(clElement)); - } - } - } - - private static ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) { - ParticipantUpdates participantUpdate = new ParticipantUpdates(); - List<ControlLoopElement> controlLoopElementList = new ArrayList<>(); - participantUpdate.setParticipantId(clElement.getParticipantId()); - controlLoopElementList.add(clElement); - participantUpdate.setControlLoopElementList(controlLoopElementList); - return participantUpdate; - } - /** * Method to create participantUpdateMsg. * @@ -259,10 +204,10 @@ public final class TestListenerUtils { .getNodeTemplates().entrySet()) { if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) { - var clParticipantType = - ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()); - prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(), - toscaInputEntry.getValue(), participantDefinitionUpdates); + CommonUtils.prepareParticipantDefinitionUpdate( + ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()), + toscaInputEntry.getKey(), toscaInputEntry.getValue(), + participantDefinitionUpdates, null); } } @@ -270,42 +215,6 @@ public final class TestListenerUtils { return participantUpdateMsg; } - private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey, - ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) { - - var clDefinition = new ControlLoopElementDefinition(); - clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); - clDefinition.setControlLoopElementToscaNodeTemplate(entryValue); - List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>(); - - if (participantDefinitionUpdates.isEmpty()) { - participantDefinitionUpdates - .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); - } else { - boolean participantExists = false; - for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) { - if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) { - participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition); - participantExists = true; - } - } - if (!participantExists) { - participantDefinitionUpdates.add( - getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); - } - } - } - - private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition, - ToscaConceptIdentifier clParticipantType, - List<ControlLoopElementDefinition> controlLoopElementDefinitionList) { - ParticipantDefinition participantDefinition = new ParticipantDefinition(); - participantDefinition.setParticipantType(clParticipantType); - controlLoopElementDefinitionList.add(clDefinition); - participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList); - return participantDefinition; - } - /** * Method to create ControlLoopUpdate using the arguments passed. * diff --git a/participant/participant-impl/participant-impl-simulator/pom.xml b/participant/participant-impl/participant-impl-simulator/pom.xml index d0d364605..324abea0e 100644 --- a/participant/participant-impl/participant-impl-simulator/pom.xml +++ b/participant/participant-impl/participant-impl-simulator/pom.xml @@ -26,7 +26,7 @@ <parent> <groupId>org.onap.policy.clamp.participant</groupId> <artifactId>policy-clamp-participant-impl</artifactId> - <version>6.1.3-SNAPSHOT</version> + <version>6.2.0-SNAPSHOT</version> </parent> <artifactId>policy-clamp-participant-impl-simulator</artifactId> diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/ParticipantSimulatorApplication.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/ParticipantSimulatorApplication.java index 580bffa80..5e72d9479 100644 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/ParticipantSimulatorApplication.java +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/ParticipantSimulatorApplication.java @@ -34,7 +34,8 @@ import org.springframework.context.annotation.ComponentScan; @ConfigurationPropertiesScan("org.onap.policy.clamp.controlloop.participant.simulator.main.parameters")
@ComponentScan({
"org.onap.policy.clamp.controlloop.participant.simulator",
- "org.onap.policy.clamp.controlloop.participant.intermediary"
+ "org.onap.policy.clamp.controlloop.participant.intermediary",
+ "org.onap.policy.clamp.controlloop.common.rest"
})
//@formatter:on
public class ParticipantSimulatorApplication {
diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/ParticipantConfig.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/ParticipantConfig.java index d28ddf9dc..f2079edf5 100644 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/ParticipantConfig.java +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/ParticipantConfig.java @@ -20,9 +20,9 @@ package org.onap.policy.clamp.controlloop.participant.simulator.config; +import org.onap.policy.clamp.controlloop.common.rest.RequestResponseLoggingFilter; import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; import org.onap.policy.clamp.controlloop.participant.simulator.main.handler.ControlLoopElementHandler; -import org.onap.policy.clamp.controlloop.participant.simulator.main.rest.RequestResponseLoggingFilter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlConfiguration.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlConfiguration.java index 16da5cf7f..28dd2f9bc 100644 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlConfiguration.java +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlConfiguration.java @@ -21,6 +21,7 @@ package org.onap.policy.clamp.controlloop.participant.simulator.config; import java.util.List; +import org.onap.policy.clamp.controlloop.common.rest.CoderHttpMesageConverter; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; @@ -32,8 +33,8 @@ public class YamlConfiguration implements WebMvcConfigurer { @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { - converters.add(new YamlHttpMessageConverter<>("yaml")); - converters.add(new YamlHttpMessageConverter<>("json")); + converters.add(new CoderHttpMesageConverter<>("yaml")); + converters.add(new CoderHttpMesageConverter<>("json")); StringHttpMessageConverter converter = new StringHttpMessageConverter(); converter.setSupportedMediaTypes(List.of(MediaType.TEXT_PLAIN)); diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlHttpMessageConverter.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlHttpMessageConverter.java deleted file mode 100644 index d9a72ce10..000000000 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/config/YamlHttpMessageConverter.java +++ /dev/null @@ -1,75 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * ================================================================================ - * Modifications 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.controlloop.participant.simulator.config; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.nio.charset.StandardCharsets; -import javax.ws.rs.core.Response; -import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.common.utils.coder.StandardYamlCoder; -import org.springframework.http.HttpInputMessage; -import org.springframework.http.HttpOutputMessage; -import org.springframework.http.MediaType; -import org.springframework.http.converter.AbstractHttpMessageConverter; -import org.springframework.http.converter.HttpMessageNotReadableException; -import org.springframework.http.converter.HttpMessageNotWritableException; - -public class YamlHttpMessageConverter<T> extends AbstractHttpMessageConverter<T> { - - private Coder coder; - - public YamlHttpMessageConverter(String type) { - super(new MediaType("application", type, StandardCharsets.UTF_8)); - this.coder = "json".equals(type) ? new StandardCoder() : new StandardYamlCoder(); - } - - @Override - protected boolean supports(Class<?> clazz) { - return true; - } - - @Override - protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) - throws IOException, HttpMessageNotReadableException { - try (var is = new InputStreamReader(inputMessage.getBody(), StandardCharsets.UTF_8)) { - return coder.decode(is, clazz); - } catch (CoderException e) { - throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e); - } - } - - @Override - protected void writeInternal(T t, HttpOutputMessage outputMessage) - throws IOException, HttpMessageNotWritableException { - try (var writer = new OutputStreamWriter(outputMessage.getBody(), StandardCharsets.UTF_8)) { - coder.encode(writer, t); - } catch (CoderException e) { - throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e); - } - } -} diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/GlobalControllerExceptionHandler.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/GlobalControllerExceptionHandler.java index 34b212305..8648c253e 100644 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/GlobalControllerExceptionHandler.java +++ b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/GlobalControllerExceptionHandler.java @@ -23,11 +23,10 @@ package org.onap.policy.clamp.controlloop.participant.simulator.main.rest; import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; -import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse; -import org.springframework.http.HttpStatus; +import org.onap.policy.clamp.controlloop.models.messages.rest.SimpleResponse; +import org.onap.policy.clamp.controlloop.models.rest.RestUtils; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; @RestControllerAdvice @@ -40,12 +39,7 @@ public class GlobalControllerExceptionHandler { * @return ResponseEntity */ @ExceptionHandler(ControlLoopException.class) - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public ResponseEntity<CommissioningResponse> handleBadRequest(ControlLoopException ex) { - - var resp = new CommissioningResponse(); - resp.setErrorDetails(ex.getErrorResponse().getErrorMessage()); - - return ResponseEntity.status(ex.getErrorResponse().getResponseCode().getStatusCode()).body(resp); + public ResponseEntity<SimpleResponse> handleBadRequest(ControlLoopException ex) { + return RestUtils.toSimpleResponse(ex); } } diff --git a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/RequestResponseLoggingFilter.java b/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/RequestResponseLoggingFilter.java deleted file mode 100644 index 9626421e8..000000000 --- a/participant/participant-impl/participant-impl-simulator/src/main/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/RequestResponseLoggingFilter.java +++ /dev/null @@ -1,69 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * ================================================================================ - * Modifications 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.clamp.controlloop.participant.simulator.main.rest; - -import java.io.IOException; -import java.util.UUID; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.springframework.core.annotation.Order; -import org.springframework.stereotype.Component; - -@Component -@Order(2) -public class RequestResponseLoggingFilter implements Filter { - - private static final String VERSION_MINOR_NAME = "X-MinorVersion"; - private static final String VERSION_PATCH_NAME = "X-PatchVersion"; - private static final String VERSION_LATEST_NAME = "X-LatestVersion"; - public static final String API_VERSION = "1.0.0"; - public static final String REQUEST_ID_NAME = "X-ONAP-RequestID"; - - @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) - throws IOException, ServletException { - - - HttpServletResponse res = (HttpServletResponse) response; - HttpServletRequest req = (HttpServletRequest) request; - - /* - * Disabling sonar because of ONAP requires the request ID to be copied from the request - * to the response, and just a simulator used during testing. - */ - String requestId = req.getHeader(REQUEST_ID_NAME); - res.addHeader(REQUEST_ID_NAME, requestId != null ? requestId : UUID.randomUUID().toString()); // NOSONAR - - res.addHeader(VERSION_MINOR_NAME, "0"); - res.addHeader(VERSION_PATCH_NAME, "0"); - res.addHeader(VERSION_LATEST_NAME, API_VERSION); - - chain.doFilter(request, response); - } - -} diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java index bd3316abb..c9848248e 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java @@ -33,9 +33,9 @@ import java.util.Set; import java.util.UUID; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.onap.policy.clamp.controlloop.common.utils.CommonUtils; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; @@ -166,39 +166,12 @@ public final class TestListenerUtils { List<ParticipantUpdates> participantUpdates = new ArrayList<>(); for (ControlLoopElement element : elements.values()) { - prepareParticipantUpdateForControlLoop(element, participantUpdates); + CommonUtils.prepareParticipantUpdate(element, participantUpdates); } clUpdateMsg.setParticipantUpdatesList(participantUpdates); return clUpdateMsg; } - private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement, - List<ParticipantUpdates> participantUpdates) { - if (participantUpdates.isEmpty()) { - participantUpdates.add(getControlLoopElementList(clElement)); - } else { - boolean participantExists = false; - for (ParticipantUpdates participantUpdate : participantUpdates) { - if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) { - participantUpdate.getControlLoopElementList().add(clElement); - participantExists = true; - } - } - if (!participantExists) { - participantUpdates.add(getControlLoopElementList(clElement)); - } - } - } - - private static ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) { - ParticipantUpdates participantUpdate = new ParticipantUpdates(); - List<ControlLoopElement> controlLoopElementList = new ArrayList<>(); - participantUpdate.setParticipantId(clElement.getParticipantId()); - controlLoopElementList.add(clElement); - participantUpdate.setControlLoopElementList(controlLoopElementList); - return participantUpdate; - } - /** * Method to create participantUpdateMsg. * @@ -226,8 +199,8 @@ public final class TestListenerUtils { toscaServiceTemplate)) { var clParticipantType = ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()); - prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(), - toscaInputEntry.getValue(), participantDefinitionUpdates); + CommonUtils.prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(), + toscaInputEntry.getValue(), participantDefinitionUpdates, null); } } @@ -235,42 +208,6 @@ public final class TestListenerUtils { return participantUpdateMsg; } - private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey, - ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) { - - var clDefinition = new ControlLoopElementDefinition(); - clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion())); - clDefinition.setControlLoopElementToscaNodeTemplate(entryValue); - List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>(); - - if (participantDefinitionUpdates.isEmpty()) { - participantDefinitionUpdates - .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); - } else { - boolean participantExists = false; - for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) { - if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) { - participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition); - participantExists = true; - } - } - if (!participantExists) { - participantDefinitionUpdates.add( - getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList)); - } - } - } - - private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition, - ToscaConceptIdentifier clParticipantType, - List<ControlLoopElementDefinition> controlLoopElementDefinitionList) { - ParticipantDefinition participantDefinition = new ParticipantDefinition(); - participantDefinition.setParticipantType(clParticipantType); - controlLoopElementDefinitionList.add(clDefinition); - participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList); - return participantDefinition; - } - /** * Method to create ControlLoopUpdate using the arguments passed. * diff --git a/participant/participant-impl/pom.xml b/participant/participant-impl/pom.xml index 3d1acb0f0..646692b43 100644 --- a/participant/participant-impl/pom.xml +++ b/participant/participant-impl/pom.xml @@ -27,7 +27,7 @@ <parent> <groupId>org.onap.policy.clamp.participant</groupId> <artifactId>policy-clamp-participant</artifactId> - <version>6.1.3-SNAPSHOT</version> + <version>6.2.0-SNAPSHOT</version> </parent> <artifactId>policy-clamp-participant-impl</artifactId> diff --git a/participant/participant-intermediary/pom.xml b/participant/participant-intermediary/pom.xml index 0b111fbcc..a22826d28 100644 --- a/participant/participant-intermediary/pom.xml +++ b/participant/participant-intermediary/pom.xml @@ -25,7 +25,7 @@ <parent> <groupId>org.onap.policy.clamp.participant</groupId> <artifactId>policy-clamp-participant</artifactId> - <version>6.1.3-SNAPSHOT</version> + <version>6.2.0-SNAPSHOT</version> </parent> <artifactId>policy-clamp-participant-intermediary</artifactId> diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/impl/ParticipantIntermediaryApiImpl.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/impl/ParticipantIntermediaryApiImpl.java index 43ac3464c..bbafc4678 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/impl/ParticipantIntermediaryApiImpl.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/api/impl/ParticipantIntermediaryApiImpl.java @@ -89,7 +89,7 @@ public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryAp @Override public void updateParticipantStatistics(ParticipantStatistics participantStatistics) { - // TODO Auto-generated method stub + participantHandler.updateParticipantStatistics(participantStatistics); } @Override diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java index ac135b0d3..5a0f4989f 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandler.java @@ -93,7 +93,8 @@ public class ControlLoopHandler { /** * Handle a control loop element state change message. * - * @param id controlloop element id + * @param controlLoopId the controlLoop Id + * @param id the controlLoop UUID * @param orderedState the current state * @param newState the ordered state * @return controlLoopElement the updated controlloop element @@ -160,8 +161,10 @@ public class ControlLoopHandler { * Handle a control loop state change message. * * @param stateChangeMsg the state change message + * @param clElementDefinitions the list of ControlLoopElementDefinition */ - public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg) { + public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg, + List<ControlLoopElementDefinition> clElementDefinitions) { if (stateChangeMsg.getControlLoopId() == null) { return; } @@ -182,7 +185,8 @@ public class ControlLoopHandler { return; } - handleState(controlLoop, stateChangeMsg.getOrderedState()); + handleState(controlLoop, stateChangeMsg.getOrderedState(), stateChangeMsg.getStartPhase(), + clElementDefinitions); } /** @@ -190,17 +194,20 @@ public class ControlLoopHandler { * * @param controlLoop participant response * @param orderedState controlloop ordered state + * @param startPhaseMsg startPhase from message + * @param clElementDefinitions the list of ControlLoopElementDefinition */ - private void handleState(final ControlLoop controlLoop, ControlLoopOrderedState orderedState) { + private void handleState(final ControlLoop controlLoop, ControlLoopOrderedState orderedState, Integer startPhaseMsg, + List<ControlLoopElementDefinition> clElementDefinitions) { switch (orderedState) { case UNINITIALISED: - handleUninitialisedState(controlLoop, orderedState); + handleUninitialisedState(controlLoop, orderedState, startPhaseMsg, clElementDefinitions); break; case PASSIVE: - handlePassiveState(controlLoop, orderedState); + handlePassiveState(controlLoop, orderedState, startPhaseMsg, clElementDefinitions); break; case RUNNING: - handleRunningState(controlLoop, orderedState); + handleRunningState(controlLoop, orderedState, startPhaseMsg, clElementDefinitions); break; default: LOGGER.debug("StateChange message has no state, state is null {}", controlLoop.getDefinition()); @@ -212,6 +219,7 @@ public class ControlLoopHandler { * Handle a control loop update message. * * @param updateMsg the update message + * @param clElementDefinitions the list of ControlLoopElementDefinition */ public void handleControlLoopUpdate(ControlLoopUpdate updateMsg, List<ControlLoopElementDefinition> clElementDefinitions) { @@ -300,8 +308,7 @@ public class ControlLoopHandler { ToscaConceptIdentifier clElementDefId) { for (var clElementDefinition : clElementDefinitions) { - if (clElementDefId.getName().contains( - clElementDefinition.getClElementDefinitionId().getName())) { + if (clElementDefId.getName().contains(clElementDefinition.getClElementDefinitionId().getName())) { return clElementDefinition.getControlLoopElementToscaNodeTemplate(); } } @@ -332,11 +339,18 @@ public class ControlLoopHandler { * * @param controlLoop participant response * @param orderedState orderedState + * @param startPhaseMsg startPhase from message + * @param clElementDefinitions the list of ControlLoopElementDefinition */ - private void handleUninitialisedState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState) { - handleStateChange(controlLoop, orderedState); - controlLoopMap.remove(controlLoop.getDefinition()); - controlLoop.getElements().values().forEach(element -> elementsOnThisParticipant.remove(element.getId())); + private void handleUninitialisedState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState, + Integer startPhaseMsg, List<ControlLoopElementDefinition> clElementDefinitions) { + handleStateChange(controlLoop, orderedState, startPhaseMsg, clElementDefinitions); + boolean isAllUninitialised = controlLoop.getElements().values().stream() + .filter(element -> !ControlLoopState.UNINITIALISED.equals(element.getState())).findAny().isEmpty(); + if (isAllUninitialised) { + controlLoopMap.remove(controlLoop.getDefinition()); + controlLoop.getElements().values().forEach(element -> elementsOnThisParticipant.remove(element.getId())); + } } /** @@ -344,9 +358,12 @@ public class ControlLoopHandler { * * @param controlLoop participant response * @param orderedState orderedState + * @param startPhaseMsg startPhase from message + * @param clElementDefinitions the list of ControlLoopElementDefinition */ - private void handlePassiveState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState) { - handleStateChange(controlLoop, orderedState); + private void handlePassiveState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState, + Integer startPhaseMsg, List<ControlLoopElementDefinition> clElementDefinitions) { + handleStateChange(controlLoop, orderedState, startPhaseMsg, clElementDefinitions); } /** @@ -354,9 +371,12 @@ public class ControlLoopHandler { * * @param controlLoop participant response * @param orderedState orderedState + * @param startPhaseMsg startPhase from message + * @param clElementDefinitions the list of ControlLoopElementDefinition */ - private void handleRunningState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState) { - handleStateChange(controlLoop, orderedState); + private void handleRunningState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState, + Integer startPhaseMsg, List<ControlLoopElementDefinition> clElementDefinitions) { + handleStateChange(controlLoop, orderedState, startPhaseMsg, clElementDefinitions); } /** @@ -364,8 +384,11 @@ public class ControlLoopHandler { * * @param controlLoop participant status in memory * @param orderedState orderedState the new ordered state the participant should have + * @param startPhaseMsg startPhase from message + * @param clElementDefinitions the list of ControlLoopElementDefinition */ - private void handleStateChange(ControlLoop controlLoop, final ControlLoopOrderedState orderedState) { + private void handleStateChange(ControlLoop controlLoop, final ControlLoopOrderedState orderedState, + Integer startPhaseMsg, List<ControlLoopElementDefinition> clElementDefinitions) { if (orderedState.equals(controlLoop.getOrderedState())) { var controlLoopAck = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK); @@ -378,16 +401,27 @@ public class ControlLoopHandler { return; } - controlLoop.getElements().values().stream().forEach(clElement -> { - for (var clElementListener : listeners) { - try { - clElementListener.controlLoopElementStateChange(controlLoop.getDefinition(), clElement.getId(), - clElement.getState(), orderedState); - } catch (PfModelException e) { - LOGGER.debug("Control loop element update failed {}", controlLoop.getDefinition()); + controlLoop.getElements().values().stream().forEach(clElement -> controlLoopElementStateChange(controlLoop, + orderedState, clElement, startPhaseMsg, clElementDefinitions)); + } + + private void controlLoopElementStateChange(ControlLoop controlLoop, ControlLoopOrderedState orderedState, + ControlLoopElement clElement, Integer startPhaseMsg, + List<ControlLoopElementDefinition> clElementDefinitions) { + var clElementNodeTemplate = getClElementNodeTemplate(clElementDefinitions, clElement.getDefinition()); + if (clElementNodeTemplate != null) { + int startPhase = ParticipantUtils.findStartPhase(clElementNodeTemplate.getProperties()); + if (startPhaseMsg.equals(startPhase)) { + for (var clElementListener : listeners) { + try { + clElementListener.controlLoopElementStateChange(controlLoop.getDefinition(), clElement.getId(), + clElement.getState(), orderedState); + } catch (PfModelException e) { + LOGGER.debug("Control loop element update failed {}", controlLoop.getDefinition()); + } } } - }); + } } /** diff --git a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java index 0734cb9c7..89a13a84b 100644 --- a/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java +++ b/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandler.java @@ -156,7 +156,7 @@ public class ParticipantHandler { * @param stateChangeMsg the state change message */ public void handleControlLoopStateChange(ControlLoopStateChange stateChangeMsg) { - controlLoopHandler.handleControlLoopStateChange(stateChangeMsg); + controlLoopHandler.handleControlLoopStateChange(stateChangeMsg, clElementDefsOnThisParticipant); } private void handleStateChange(ParticipantState newParticipantState, ParticipantUpdateAck response) { @@ -190,6 +190,19 @@ public class ParticipantHandler { } /** + * Method to update participant statistics. + * + * @param statistics participant statistics + */ + public void updateParticipantStatistics(ParticipantStatistics statistics) { + participantStatistics.setState(statistics.getState()); + participantStatistics.setHealthStatus(statistics.getHealthStatus()); + participantStatistics.setTimeStamp(statistics.getTimeStamp()); + participantStatistics.setAverageExecutionTime(statistics.getAverageExecutionTime()); + participantStatistics.setEventCount(statistics.getEventCount()); + } + + /** * Get participants as a {@link Participant} class. * * @param name the participant name to get diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandlerTest.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandlerTest.java index 29387959b..676747d04 100644 --- a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandlerTest.java +++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ControlLoopHandlerTest.java @@ -21,20 +21,29 @@ package org.onap.policy.clamp.controlloop.participant.intermediary.handler; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.mockito.Mockito.mock; import java.time.Instant; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener; import org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters.CommonTestData; +import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -45,14 +54,14 @@ class ControlLoopHandlerTest { @Test void controlLoopHandlerTest() { - ControlLoopHandler clh = commonTestData.getMockControlLoopHandler(); + var clh = commonTestData.getMockControlLoopHandler(); assertNotNull(clh.getControlLoops()); assertNotNull(clh.getControlLoopMap()); assertNotNull(clh.getElementsOnThisParticipant()); - UUID elementId1 = UUID.randomUUID(); - ControlLoopElement element = new ControlLoopElement(); + var elementId1 = UUID.randomUUID(); + var element = new ControlLoopElement(); element.setId(elementId1); element.setDefinition(new ToscaConceptIdentifier( "org.onap.policy.controlloop.PolicyControlLoopParticipant", "1.0.1")); @@ -62,22 +71,21 @@ class ControlLoopHandlerTest { ControlLoopElementListener listener = mock(ControlLoopElementListener.class); clh.registerControlLoopElementListener(listener); assertThat(clh.getListeners()).contains(listener); - } @Test void updateNullControlLoopHandlerTest() { - UUID id = UUID.randomUUID(); + var id = UUID.randomUUID(); - ControlLoopHandler clh = commonTestData.getMockControlLoopHandler(); + var clh = commonTestData.getMockControlLoopHandler(); assertNull(clh.updateControlLoopElementState(null, null, ControlLoopOrderedState.UNINITIALISED, ControlLoopState.PASSIVE)); assertNull(clh.updateControlLoopElementState(null, id, ControlLoopOrderedState.UNINITIALISED, ControlLoopState.PASSIVE)); - ClElementStatistics clElementStatistics = new ClElementStatistics(); - ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("defName", "0.0.1"); + var clElementStatistics = new ClElementStatistics(); + var controlLoopId = new ToscaConceptIdentifier("defName", "0.0.1"); clElementStatistics.setParticipantId(controlLoopId); clElementStatistics.setControlLoopState(ControlLoopState.RUNNING); clElementStatistics.setTimeStamp(Instant.now()); @@ -85,9 +93,77 @@ class ControlLoopHandlerTest { clh.updateControlLoopElementStatistics(id, clElementStatistics); assertNull(clh.updateControlLoopElementState(controlLoopId, id, ControlLoopOrderedState.UNINITIALISED, ControlLoopState.PASSIVE)); + } + + @Test + void updateControlLoopHandlerTest() throws CoderException { + var uuid = UUID.randomUUID(); + var id = CommonTestData.getParticipantId(); + + var clh = setTestControlLoopHandler(id, uuid); + var key = clh.getElementsOnThisParticipant().keySet().iterator().next(); + var value = clh.getElementsOnThisParticipant().get(key); + assertEquals(ControlLoopState.UNINITIALISED, value.getState()); + clh.updateControlLoopElementState(id, uuid, ControlLoopOrderedState.UNINITIALISED, + ControlLoopState.PASSIVE); + assertEquals(ControlLoopState.PASSIVE, value.getState()); + + var clElementStatistics = new ClElementStatistics(); + clElementStatistics.setParticipantId(id); + clElementStatistics.setControlLoopState(ControlLoopState.RUNNING); + clElementStatistics.setTimeStamp(Instant.now()); + + assertNotEquals(uuid, value.getClElementStatistics().getId()); + clh.updateControlLoopElementStatistics(uuid, clElementStatistics); + assertEquals(uuid, value.getClElementStatistics().getId()); + } + + @Test + void handleControlLoopUpdateExceptionTest() throws CoderException { + var uuid = UUID.randomUUID(); + var id = CommonTestData.getParticipantId(); + + var stateChange = new ControlLoopStateChange(); + stateChange.setControlLoopId(id); + stateChange.setParticipantId(id); + stateChange.setMessageId(uuid); + stateChange.setOrderedState(ControlLoopOrderedState.RUNNING); + stateChange.setCurrentState(ControlLoopState.UNINITIALISED); + stateChange.setTimestamp(Instant.ofEpochMilli(3000)); + + var clh = setTestControlLoopHandler(id, uuid); + clh.handleControlLoopStateChange(stateChange, List.of()); + var newid = new ToscaConceptIdentifier("id", "1.2.3"); + stateChange.setControlLoopId(newid); + stateChange.setParticipantId(newid); + assertDoesNotThrow(() -> clh.handleControlLoopStateChange(stateChange, List.of())); + + List<ControlLoopElementDefinition> clElementDefinitions = new ArrayList<>(); + var cld = new ControlLoopElementDefinition(); + cld.setClElementDefinitionId(id); + clElementDefinitions.add(cld); + var updateMsg = new ControlLoopUpdate(); + updateMsg.setControlLoopId(id); + updateMsg.setMessageId(uuid); + updateMsg.setParticipantId(id); + updateMsg.setStartPhase(0); + assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, clElementDefinitions)); + updateMsg.setStartPhase(1); + assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, clElementDefinitions)); + } + + private ControlLoopHandler setTestControlLoopHandler(ToscaConceptIdentifier id, UUID uuid) throws CoderException { + var clh = commonTestData.getMockControlLoopHandler(); + var key = commonTestData.getTestControlLoopMap().keySet().iterator().next(); + var value = commonTestData.getTestControlLoopMap().get(key); + clh.getControlLoopMap().put(key, value); + var keyElem = commonTestData.setControlLoopElementTest(uuid, id).keySet().iterator().next(); + var valueElem = commonTestData.setControlLoopElementTest(uuid, id).get(keyElem); + clh.getElementsOnThisParticipant().put(keyElem, valueElem); + return clh; } } diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandlerTest.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandlerTest.java new file mode 100644 index 000000000..d00697521 --- /dev/null +++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/handler/ParticipantHandlerTest.java @@ -0,0 +1,176 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.participant.intermediary.handler; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantAckMessage; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; +import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher; +import org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters.CommonTestData; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; + +class ParticipantHandlerTest { + + private CommonTestData commonTestData = new CommonTestData(); + private static final String ID_NAME = "org.onap.PM_CDS_Blueprint"; + private static final String ID_VERSION = "1.0.1"; + + @Test + void mockParticipantHandlerTest() { + var participantHandler = commonTestData.getMockParticipantHandler(); + assertNull(participantHandler.getParticipant(null, null)); + assertEquals("org.onap.PM_CDS_Blueprint 1.0.1", participantHandler.getParticipantId().toString()); + + var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION); + assertEquals(id, participantHandler.getParticipantId()); + assertEquals(id, participantHandler.getParticipantType()); + assertThat(participantHandler.getClElementDefinitionCommonProperties(id)).isEmpty(); + + } + + @Test + void handleUpdateTest() { + var parameters = CommonTestData.getParticipantParameters(); + var controlLoopHander = commonTestData.getMockControlLoopHandler(); + var publisher = new ParticipantMessagePublisher(); + var emptyParticipantHandler = + new ParticipantHandler(parameters, publisher, controlLoopHander); + var participantUpdateMsg = new ParticipantUpdate(); + + assertThatThrownBy(() -> + emptyParticipantHandler.handleParticipantUpdate(participantUpdateMsg)) + .isInstanceOf(RuntimeException.class); + + var participantHandler = commonTestData.getMockParticipantHandler(); + + var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION); + participantUpdateMsg.setControlLoopId(id); + participantUpdateMsg.setParticipantId(id); + participantUpdateMsg.setParticipantType(id); + participantUpdateMsg.setMessageId(UUID.randomUUID()); + participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000)); + + var heartbeatF = participantHandler.makeHeartbeat(false); + assertEquals(id, heartbeatF.getParticipantId()); + assertEquals(ParticipantState.UNKNOWN, heartbeatF.getParticipantStatistics().getState()); + assertThat(heartbeatF.getControlLoopInfoList()).isEmpty(); + + participantHandler.handleParticipantUpdate(participantUpdateMsg); + assertThat(participantHandler.getClElementDefinitionCommonProperties(id)).isEmpty(); + + var heartbeatT = participantHandler.makeHeartbeat(true); + assertEquals(id, heartbeatT.getParticipantId()); + assertEquals(ParticipantState.TERMINATED, heartbeatT.getParticipantStatistics().getState()); + assertThat(heartbeatT.getParticipantDefinitionUpdates()).isNotEmpty(); + assertEquals(id, heartbeatT.getParticipantDefinitionUpdates().get(0).getParticipantId()); + + var pum = setListParticipantDefinition(participantUpdateMsg); + participantHandler.handleParticipantUpdate(pum); + var heartbeatTAfterUpdate = participantHandler.makeHeartbeat(true); + assertEquals(id, heartbeatTAfterUpdate.getParticipantId()); + assertEquals(ParticipantState.PASSIVE, heartbeatTAfterUpdate.getParticipantStatistics().getState()); + + } + + private ParticipantUpdate setListParticipantDefinition(ParticipantUpdate participantUpdateMsg) { + var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION); + List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); + var def = new ParticipantDefinition(); + def.setParticipantId(id); + def.setParticipantType(id); + participantDefinitionUpdates.add(def); + participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates); + return participantUpdateMsg; + } + + @Test + void handleParticipantTest() { + var participantHandler = commonTestData.getMockParticipantHandler(); + var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION); + var p = participantHandler.getParticipant(id.getName(), id.getVersion()); + assertEquals(ParticipantState.UNKNOWN, p.getParticipantState()); + + participantHandler.updateParticipantState(id, ParticipantState.PASSIVE); + var p2 = participantHandler.getParticipant(id.getName(), id.getVersion()); + assertEquals(ParticipantState.PASSIVE, p2.getParticipantState()); + + var participantRegisterAckMsg = new ParticipantRegisterAck(); + participantRegisterAckMsg.setState(ParticipantState.TERMINATED); + participantHandler.handleParticipantRegisterAck(participantRegisterAckMsg); + assertEquals(ParticipantHealthStatus.HEALTHY, participantHandler.makeHeartbeat(false).getHealthStatus()); + + var emptyid = new ToscaConceptIdentifier("", ID_VERSION); + assertNull(participantHandler.updateParticipantState(emptyid, ParticipantState.PASSIVE)); + + var sameid = new ToscaConceptIdentifier(ID_NAME, ID_VERSION); + var participant = participantHandler.updateParticipantState(sameid, ParticipantState.PASSIVE); + assertEquals(participant.getDefinition(), sameid); + + } + + @Test + void checkAppliesTo() { + var participantHandler = commonTestData.getMockParticipantHandler(); + var participantAckMsg = + new ParticipantAckMessage(ParticipantMessageType.CONTROL_LOOP_UPDATE); + assertTrue(participantHandler.appliesTo(participantAckMsg)); + + var participantMsg = + new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS); + assertTrue(participantHandler.appliesTo(participantMsg)); + + var emptyid = new ToscaConceptIdentifier("", ID_VERSION); + participantMsg.setParticipantType(emptyid); + assertFalse(participantHandler.appliesTo(participantMsg)); + + } + + @Test + void getControlLoopInfoListTest() throws CoderException { + var participantHandler = commonTestData.getParticipantHandlerControlLoops(); + var id = new ToscaConceptIdentifier(ID_NAME, ID_VERSION); + participantHandler.sendHeartbeat(); + assertEquals(id, participantHandler.makeHeartbeat(false) + .getControlLoopInfoList() + .get(0) + .getControlLoopId()); + + } + +} diff --git a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java index ae20f4b7f..43f3c8e2b 100644 --- a/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java +++ b/participant/participant-intermediary/src/test/java/org/onap/policy/clamp/controlloop/participant/intermediary/main/parameters/CommonTestData.java @@ -20,15 +20,27 @@ package org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters; +import java.io.File; +import java.time.Instant; import java.util.Arrays; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; +import java.util.UUID; import org.mockito.Mockito; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher; import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ControlLoopHandler; import org.onap.policy.clamp.controlloop.participant.intermediary.handler.DummyParticipantParameters; +import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.common.endpoints.parameters.TopicParameters; @@ -45,7 +57,6 @@ public class CommonTestData { public static final String DESCRIPTION = "Participant description"; public static final long TIME_INTERVAL = 2000; public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams()); - public static final Coder CODER = new StandardCoder(); private static final Object lockit = new Object(); @@ -128,7 +139,7 @@ public class CommonTestData { * @return topic parameters */ public static TopicParameters getTopicParams() { - final TopicParameters topicParams = new TopicParameters(); + final var topicParams = new TopicParameters(); topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT"); topicParams.setTopicCommInfrastructure("dmaap"); topicParams.setServers(Arrays.asList("localhost")); @@ -151,8 +162,7 @@ public class CommonTestData { */ private ParticipantMessagePublisher getParticipantMessagePublisher() { synchronized (lockit) { - ParticipantMessagePublisher participantMessagePublisher = - new ParticipantMessagePublisher(); + var participantMessagePublisher = new ParticipantMessagePublisher(); participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class))); return participantMessagePublisher; } @@ -168,4 +178,96 @@ public class CommonTestData { getParticipantParameters(), getParticipantMessagePublisher()); } + + /** + * Returns a mocked ParticipantHandler for test cases. + * + * @return participant Handler + */ + public ParticipantHandler getMockParticipantHandler() { + var parameters = getParticipantParameters(); + var controlLoopHandler = getMockControlLoopHandler(); + var publisher = new ParticipantMessagePublisher(); + publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class))); + var participantHandler = new ParticipantHandler(parameters, publisher, controlLoopHandler); + return participantHandler; + } + + /** + * Returns a mocked ParticipantHandler for test cases. + * + * @return participant Handler + * + * @throws CoderException if there is an error with .json file. + */ + public ParticipantHandler getParticipantHandlerControlLoops() throws CoderException { + var controlLoopHandler = Mockito.mock(ControlLoopHandler.class); + Mockito.doReturn(getTestControlLoops()).when(controlLoopHandler).getControlLoops(); + Mockito.doReturn(getTestControlLoopMap()).when(controlLoopHandler).getControlLoopMap(); + var publisher = new ParticipantMessagePublisher(); + publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class))); + var parameters = getParticipantParameters(); + var participantHandler = new ParticipantHandler(parameters, publisher, controlLoopHandler); + participantHandler.sendParticipantRegister(); + participantHandler.handleParticipantStatusReq(null); + participantHandler.sendParticipantDeregister(); + var participantDeregisterAckMsg = new ParticipantDeregisterAck(); + participantDeregisterAckMsg.setResponseTo(UUID.randomUUID()); + participantHandler.handleParticipantDeregisterAck(participantDeregisterAckMsg); + return participantHandler; + } + + /** + * Returns a Map of ToscaConceptIdentifier and ControlLoop for test cases. + * + * @return controlLoopMap + * + * @throws CoderException if there is an error with .json file. + */ + public Map<ToscaConceptIdentifier, ControlLoop> getTestControlLoopMap() throws CoderException { + var controlLoops = getTestControlLoops(); + var controlLoop = controlLoops.getControlLoopList().get(1); + var id = getParticipantId(); + Map<ToscaConceptIdentifier, ControlLoop> controlLoopMap = new LinkedHashMap<>(); + controlLoopMap.put(id, controlLoop); + return controlLoopMap; + } + + /** + * Returns List of ControlLoop for test cases. + * + * @return ControlLoops + * + * @throws CoderException if there is an error with .json file. + */ + public ControlLoops getTestControlLoops() throws CoderException { + return new StandardCoder() + .decode(new File("src/test/resources/providers/TestControlLoops.json"), ControlLoops.class); + } + + /** + * Returns a map for a elementsOnThisParticipant for test cases. + * + * @param uuid UUID and id ToscaConceptIdentifier + * @return a map suitable for elementsOnThisParticipant + */ + public Map<UUID, ControlLoopElement> setControlLoopElementTest(UUID uuid, ToscaConceptIdentifier id) { + var clElement = new ControlLoopElement(); + clElement.setId(uuid); + clElement.setParticipantId(id); + clElement.setDefinition(id); + clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED); + + var clElementStatistics = new ClElementStatistics(); + clElementStatistics.setParticipantId(id); + clElementStatistics.setControlLoopState(ControlLoopState.UNINITIALISED); + clElementStatistics.setTimeStamp(Instant.now()); + + clElement.setClElementStatistics(clElementStatistics); + + Map<UUID, ControlLoopElement> elementsOnThisParticipant = new LinkedHashMap<>(); + elementsOnThisParticipant.put(uuid, clElement); + return elementsOnThisParticipant; + } + } diff --git a/participant/participant-intermediary/src/test/resources/providers/TestControlLoops.json b/participant/participant-intermediary/src/test/resources/providers/TestControlLoops.json new file mode 100644 index 000000000..fedda9600 --- /dev/null +++ b/participant/participant-intermediary/src/test/resources/providers/TestControlLoops.json @@ -0,0 +1,142 @@ +{ + "controlLoopList": [ + { + "definition": { + "name": "org.onap.domain.pmsh.PMSHControlLoopDefinition", + "version": "1.0.0" + }, + "state": "UNINITIALISED", + "orderedState": "UNINITIALISED", + "elements": { + "709c62b3-8918-41b9-a747-e21eb79c6c20": { + "id": "709c62b3-8918-41b9-a747-d21eb79c6c20", + "definition": { + "name": "org.onap.domain.pmsh.PMSH_DCAEMicroservice", + "version": "1.2.3" + }, + "participantType": { + "name": "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", + "version": "2.3.4" + }, + "state": "UNINITIALISED", + "orderedState": "UNINITIALISED", + "description": "DCAE Control Loop Element for the PMSH instance 0 control loop" + }, + "709c62b3-8918-41b9-a747-e21eb79c6c21": { + "id": "709c62b3-8918-41b9-a747-d21eb79c6c21", + "definition": { + "name": "org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement", + "version": "1.2.3" + }, + "participantType": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, + "state": "UNINITIALISED", + "orderedState": "UNINITIALISED", + "description": "Monitoring Policy Control Loop Element for the PMSH instance 0 control loop" + }, + "709c62b3-8918-41b9-a747-e21eb79c6c22": { + "id": "709c62b3-8918-41b9-a747-d21eb79c6c22", + "definition": { + "name": "org.onap.domain.pmsh.PMSH_OperationalPolicyControlLoopElement", + "version": "1.2.3" + }, + "participantType": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, + "state": "UNINITIALISED", + "orderedState": "UNINITIALISED", + "description": "Operational Policy Control Loop Element for the PMSH instance 0 control loop" + }, + "709c62b3-8918-41b9-a747-e21eb79c6c23": { + "id": "709c62b3-8918-41b9-a747-d21eb79c6c23", + "definition": { + "name": "org.onap.domain.pmsh.PMSH_CDS_ControlLoopElement", + "version": "1.2.3" + }, + "participantType": { + "name": "org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", + "version": "2.2.1" + }, + "state": "UNINITIALISED", + "orderedState": "UNINITIALISED", + "description": "CDS Control Loop Element for the PMSH instance 0 control loop" + } + }, + "name": "PMSHInstance0", + "version": "1.0.1", + "description": "PMSH control loop instance 0" + }, + { + "definition": { + "name": "org.onap.domain.pmsh.PMSHControlLoopDefinition", + "version": "1.0.0" + }, + "state": "UNINITIALISED", + "orderedState": "UNINITIALISED", + "elements": { + "709c62b3-8918-41b9-a747-e21eb79c6c24": { + "id": "709c62b3-8918-41b9-a747-e21eb79c6c24", + "definition": { + "name": "org.onap.domain.pmsh.PMSH_DCAEMicroservice", + "version": "1.2.3" + }, + "participantType": { + "name": "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", + "version": "2.3.4" + }, + "state": "UNINITIALISED", + "orderedState": "UNINITIALISED", + "description": "DCAE Control Loop Element for the PMSH instance 1 control loop" + }, + "709c62b3-8918-41b9-a747-e21eb79c6c25": { + "id": "709c62b3-8918-41b9-a747-e21eb79c6c25", + "definition": { + "name": "org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement", + "version": "1.2.3" + }, + "participantType": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, + "state": "UNINITIALISED", + "orderedState": "UNINITIALISED", + "description": "Monitoring Policy Control Loop Element for the PMSH instance 1 control loop" + }, + "709c62b3-8918-41b9-a747-e21eb79c6c26": { + "id": "709c62b3-8918-41b9-a747-e21eb79c6c26", + "definition": { + "name": "org.onap.domain.pmsh.PMSH_OperationalPolicyControlLoopElement", + "version": "1.2.3" + }, + "participantType": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, + "state": "UNINITIALISED", + "orderedState": "UNINITIALISED", + "description": "Operational Policy Control Loop Element for the PMSH instance 1 control loop" + }, + "709c62b3-8918-41b9-a747-e21eb79c6c27": { + "id": "709c62b3-8918-41b9-a747-e21eb79c6c27", + "definition": { + "name": "org.onap.domain.pmsh.PMSH_CDS_ControlLoopElement", + "version": "1.2.3" + }, + "participantType": { + "name": "org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", + "version": "2.2.1" + }, + "state": "UNINITIALISED", + "orderedState": "UNINITIALISED", + "description": "CDS Control Loop Element for the PMSH instance 1 control loop" + } + }, + "name": "PMSHInstance1", + "version": "1.0.1", + "description": "PMSH control loop instance 1" + } + ] +} diff --git a/participant/pom.xml b/participant/pom.xml index 77108fbf5..fcfbe0c90 100644 --- a/participant/pom.xml +++ b/participant/pom.xml @@ -26,7 +26,7 @@ <parent> <groupId>org.onap.policy.clamp</groupId> <artifactId>policy-clamp</artifactId> - <version>6.1.3-SNAPSHOT</version> + <version>6.2.0-SNAPSHOT</version> </parent> <groupId>org.onap.policy.clamp.participant</groupId> |