diff options
author | Liam Fallon <liam.fallon@est.tech> | 2021-09-28 07:35:26 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2021-09-28 07:35:26 +0000 |
commit | 2e1d9ec7bf612998b7316d2c3395e824b372044e (patch) | |
tree | 194be98e093288954ea63d84f2b5f2d264d89f66 /runtime-controlloop/src | |
parent | ccb9d7505052fa1e18969982e0c509d018840db0 (diff) | |
parent | d426686c7bda61c7cb6872e1d7bbd527289174a4 (diff) |
Merge "Add validation participants registered in issue ControlLoop Command"
Diffstat (limited to 'runtime-controlloop/src')
8 files changed, 183 insertions, 179 deletions
diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java index 472348ac3..d2d57ed83 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java @@ -37,10 +37,8 @@ import org.apache.commons.collections4.MapUtils; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse; import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler; -import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityType; diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java index d2e8be24d..5ec6158bd 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java @@ -32,6 +32,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.function.Function; import java.util.function.UnaryOperator; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -44,7 +45,9 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop 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.controlloop.concepts.Participant; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; import org.onap.policy.clamp.controlloop.models.messages.rest.GenericNameVersion; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopOrderStateResponse; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopPrimed; @@ -84,6 +87,7 @@ public class ControlLoopInstantiationProvider { private final ControlLoopProvider controlLoopProvider; private final CommissioningProvider commissioningProvider; private final SupervisionHandler supervisionHandler; + private final ParticipantProvider participantProvider; private static final Object lockit = new Object(); @@ -95,20 +99,18 @@ public class ControlLoopInstantiationProvider { * @throws PfModelException on creation errors */ public InstancePropertiesResponse createInstanceProperties(ToscaServiceTemplate serviceTemplate) - throws PfModelException { + throws PfModelException { String instanceName = generateSequentialInstanceName(); ControlLoop controlLoop = new ControlLoop(); Map<UUID, ControlLoopElement> controlLoopElements = new HashMap<>(); - ToscaServiceTemplate toscaServiceTemplate = commissioningProvider - .getToscaServiceTemplate(null, null); + ToscaServiceTemplate toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(null, null); - Map<String, ToscaNodeTemplate> persistedNodeTemplateMap = toscaServiceTemplate - .getToscaTopologyTemplate().getNodeTemplates(); + Map<String, ToscaNodeTemplate> persistedNodeTemplateMap = + toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); - Map<String, ToscaNodeTemplate> nodeTemplates = - deepCloneNodeTemplate(serviceTemplate); + Map<String, ToscaNodeTemplate> nodeTemplates = deepCloneNodeTemplate(serviceTemplate); nodeTemplates.forEach((key, template) -> { ToscaNodeTemplate newNodeTemplate = new ToscaNodeTemplate(); @@ -154,15 +156,14 @@ public class ControlLoopInstantiationProvider { ToscaServiceTemplate toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(name, version); - toscaServiceTemplate.getToscaTopologyTemplate() - .getNodeTemplates().forEach((key, nodeTemplate) -> { - if (!nodeTemplate.getName().contains(instanceName)) { - filteredToscaNodeTemplateMap.put(key, nodeTemplate); - } - }); + toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().forEach((key, nodeTemplate) -> { + if (!nodeTemplate.getName().contains(instanceName)) { + filteredToscaNodeTemplateMap.put(key, nodeTemplate); + } + }); - List<ToscaNodeTemplate> filteredToscaNodeTemplateList = - toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().values().stream() + List<ToscaNodeTemplate> filteredToscaNodeTemplateList = toscaServiceTemplate.getToscaTopologyTemplate() + .getNodeTemplates().values().stream() .filter(nodeTemplate -> nodeTemplate.getName().contains(instanceName)).collect(Collectors.toList()); InstantiationResponse response = this.deleteControlLoop(name, version); @@ -183,8 +184,7 @@ public class ControlLoopInstantiationProvider { synchronized (lockit) { for (ControlLoop controlLoop : controlLoops.getControlLoopList()) { - var checkControlLoop = controlLoopProvider - .getControlLoop(controlLoop.getKey().asIdentifier()); + var checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier()); if (checkControlLoop != null) { throw new PfModelException(Response.Status.BAD_REQUEST, controlLoop.getKey().asIdentifier() + " already defined"); @@ -349,12 +349,20 @@ public class ControlLoopInstantiationProvider { } synchronized (lockit) { + var participants = participantProvider.getParticipants(null, null); + if (participants.isEmpty()) { + throw new ControlLoopException(Status.BAD_REQUEST, "No participants registered"); + } List<ControlLoop> controlLoops = new ArrayList<>(command.getControlLoopIdentifierList().size()); for (ToscaConceptIdentifier id : command.getControlLoopIdentifierList()) { var controlLoop = controlLoopProvider.getControlLoop(id); controlLoop.setCascadedOrderedState(command.getOrderedState()); controlLoops.add(controlLoop); } + BeanValidationResult validationResult = validateIssueControlLoops(controlLoops, participants); + if (!validationResult.isValid()) { + throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult()); + } controlLoopProvider.updateControlLoops(controlLoops); } @@ -365,6 +373,37 @@ public class ControlLoopInstantiationProvider { return response; } + private BeanValidationResult validateIssueControlLoops(List<ControlLoop> controlLoops, + List<Participant> participants) { + var result = new BeanValidationResult("ControlLoops", controlLoops); + + Map<ToscaConceptIdentifier, Participant> participantMap = participants.stream() + .collect(Collectors.toMap(participant -> participant.getKey().asIdentifier(), Function.identity())); + + for (ControlLoop controlLoop : controlLoops) { + + for (var element : controlLoop.getElements().values()) { + var subResult = new BeanValidationResult("entry " + element.getDefinition().getName(), element); + + Participant p = participantMap.get(element.getParticipantId()); + if (p == null) { + subResult.addResult(new ObjectValidationResult(CONTROL_LOOP_NODE_ELEMENT_TYPE, + element.getDefinition().getName(), ValidationStatus.INVALID, + "Participant with ID " + element.getParticipantId() + " is not registered")); + } else if (!p.getParticipantType().equals(element.getParticipantType())) { + subResult.addResult(new ObjectValidationResult(CONTROL_LOOP_NODE_ELEMENT_TYPE, + element.getDefinition().getName(), ValidationStatus.INVALID, + "Participant with ID " + element.getParticipantType() + " - " + element.getParticipantId() + + " is not registered")); + } + result.addResult(subResult); + } + + } + + return result; + } + /** * Gets a list of control loops with it's ordered state. * @@ -374,7 +413,7 @@ public class ControlLoopInstantiationProvider { * @throws PfModelException on errors getting control loops */ public ControlLoopOrderStateResponse getInstantiationOrderState(String name, String version) - throws PfModelException { + throws PfModelException { List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version); @@ -399,8 +438,7 @@ public class ControlLoopInstantiationProvider { * @return a list of Instantiation Command * @throws PfModelException on errors getting control loops */ - public ControlLoopPrimedResponse getControlLoopPriming(String name, String version) - throws PfModelException { + public ControlLoopPrimedResponse getControlLoopPriming(String name, String version) throws PfModelException { List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version); @@ -425,8 +463,8 @@ public class ControlLoopInstantiationProvider { * @return the result of the instance properties and instantiation operation * @throws PfModelException on creation errors */ - private InstancePropertiesResponse saveInstancePropertiesAndControlLoop( - ToscaServiceTemplate serviceTemplate, ControlLoops controlLoops) throws PfModelException { + private InstancePropertiesResponse saveInstancePropertiesAndControlLoop(ToscaServiceTemplate serviceTemplate, + ControlLoops controlLoops) throws PfModelException { var response = new InstancePropertiesResponse(); @@ -437,7 +475,7 @@ public class ControlLoopInstantiationProvider { var checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier()); if (checkControlLoop != null) { throw new PfModelException(Response.Status.BAD_REQUEST, - controlLoop.getKey().asIdentifier() + " already defined"); + "Control loop with id " + controlLoop.getKey().asIdentifier() + " already defined"); } } @@ -448,19 +486,20 @@ public class ControlLoopInstantiationProvider { } List<ToscaConceptIdentifier> affectedControlLoops = controlLoops.getControlLoopList().stream() - .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()); + .map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()); List<ToscaConceptIdentifier> toscaAffectedProperties = toscaSavedNodeTemplate.values().stream() - .map(template -> template.getKey().asIdentifier()).collect(Collectors.toList()); + .map(template -> template.getKey().asIdentifier()).collect(Collectors.toList()); response.setAffectedInstanceProperties(Stream.of(affectedControlLoops, toscaAffectedProperties) - .flatMap(Collection::stream).collect(Collectors.toList())); + .flatMap(Collection::stream).collect(Collectors.toList())); return response; } /** * Crates a new Control Loop instance. + * * @param instanceName Control Loop Instance name * @param controlLoop empty Control Loop * @param controlLoopElements new Control Loop Element map @@ -468,9 +507,8 @@ public class ControlLoopInstantiationProvider { * @param newNodeTemplate new Tosca Node Template */ private void crateNewControlLoopInstance(String instanceName, ControlLoop controlLoop, - Map<UUID, ControlLoopElement> controlLoopElements, - ToscaNodeTemplate template, - ToscaNodeTemplate newNodeTemplate) { + Map<UUID, ControlLoopElement> controlLoopElements, ToscaNodeTemplate template, + ToscaNodeTemplate newNodeTemplate) { if (template.getType().equals(CONTROL_LOOP_NODE_TYPE)) { controlLoop.setDefinition(getControlLoopDefinition(newNodeTemplate)); } @@ -487,7 +525,6 @@ public class ControlLoopInstantiationProvider { controlLoop.setOrderedState(ControlLoopOrderedState.UNINITIALISED); } - /** * Get's the instance property name of the control loop. * @@ -497,9 +534,8 @@ public class ControlLoopInstantiationProvider { * @throws PfModelException on errors getting control loops */ private String getInstancePropertyName(String name, String version) throws PfModelException { - List<String> toscaDefinitionsNames = - controlLoopProvider.getControlLoops(name, version).stream().map(ControlLoop::getDefinition) - .map(ToscaNameVersion::getName).collect(Collectors.toList()); + List<String> toscaDefinitionsNames = controlLoopProvider.getControlLoops(name, version).stream() + .map(ControlLoop::getDefinition).map(ToscaNameVersion::getName).collect(Collectors.toList()); return toscaDefinitionsNames.stream().reduce("", (s1, s2) -> { @@ -521,8 +557,7 @@ public class ControlLoopInstantiationProvider { private String generateSequentialInstanceName() { List<ToscaNodeTemplate> nodeTemplates = controlLoopProvider.getNodeTemplates(null, null); - int instanceNumber = - nodeTemplates.stream().map(ToscaNodeTemplate::getName) + int instanceNumber = nodeTemplates.stream().map(ToscaNodeTemplate::getName) .filter(name -> name.contains(INSTANCE_TEXT)).map(n -> { String[] defNameArr = n.split(INSTANCE_TEXT); @@ -560,8 +595,8 @@ public class ControlLoopInstantiationProvider { definition.setVersion(template.getVersion()); controlLoopElement.setDefinition(definition); - LinkedTreeMap<String, Object> participantId = (LinkedTreeMap<String, Object>) template.getProperties() - .get(PARTICIPANT_ID_PROPERTY_KEY); + LinkedTreeMap<String, Object> participantId = + (LinkedTreeMap<String, Object>) template.getProperties().get(PARTICIPANT_ID_PROPERTY_KEY); if (participantId != null) { ToscaConceptIdentifier participantIdProperty = new ToscaConceptIdentifier(); @@ -570,8 +605,8 @@ public class ControlLoopInstantiationProvider { controlLoopElement.setParticipantId(participantIdProperty); } - LinkedTreeMap<String, Object> participantType = (LinkedTreeMap<String, Object>) template.getProperties() - .get(PARTICIPANT_TYPE_PROPERTY_KEY); + LinkedTreeMap<String, Object> participantType = + (LinkedTreeMap<String, Object>) template.getProperties().get(PARTICIPANT_TYPE_PROPERTY_KEY); if (participantType != null) { ToscaConceptIdentifier participantTypeProperty = new ToscaConceptIdentifier(); diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java index 1747bbd3c..20292633b 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java @@ -21,6 +21,7 @@ package org.onap.policy.clamp.controlloop.runtime.supervision; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; @@ -42,7 +43,6 @@ import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.Contr import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck; import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher; @@ -143,14 +143,11 @@ public class SupervisionHandler { * * @param participantRegisterMessage the ParticipantRegister message received from a participant */ - @MessageIntercept public void handleParticipantMessage(ParticipantRegister participantRegisterMessage) { LOGGER.debug("Participant Register received {}", participantRegisterMessage); participantRegisterAckPublisher.send(participantRegisterMessage.getMessageId(), participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType()); - - participantUpdatePublisher.send(null, true); } /** @@ -221,7 +218,7 @@ public class SupervisionHandler { */ public void handleSendDeCommissionMessage() { LOGGER.debug("Participant update message being sent"); - participantUpdatePublisher.send(null, false); + participantUpdatePublisher.send(Collections.emptyMap(), false); } /** diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java index 7f04056cf..24272e308 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java @@ -26,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.IOException; import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -107,7 +108,7 @@ class ControlLoopInstantiationProviderTest { @BeforeAll public static void setUpBeforeClass() throws Exception { serviceTemplate = yamlTranslator.fromYaml(ResourceUtils.getResourceAsString(TOSCA_TEMPLATE_YAML), - ToscaServiceTemplate.class); + ToscaServiceTemplate.class); } /** @@ -119,8 +120,7 @@ class ControlLoopInstantiationProviderTest { public static void setupDbProviderParameters() throws PfModelException { ClRuntimeParameterGroup controlLoopParameters = CommonTestData.geParameterGroup("instantproviderdb"); - modelsProvider = - CommonTestData.getPolicyModelsProvider(controlLoopParameters.getDatabaseProviderParameters()); + modelsProvider = CommonTestData.getPolicyModelsProvider(controlLoopParameters.getDatabaseProviderParameters()); clProvider = new ControlLoopProvider(controlLoopParameters.getDatabaseProviderParameters()); participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters()); @@ -138,8 +138,8 @@ class ControlLoopInstantiationProviderTest { var participantDeregisterAckPublisher = Mockito.mock(ParticipantDeregisterAckPublisher.class); var participantUpdatePublisher = Mockito.mock(ParticipantUpdatePublisher.class); supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider, - controlLoopUpdatePublisher, controlLoopStateChangePublisher, - participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher); + controlLoopUpdatePublisher, controlLoopStateChangePublisher, participantRegisterAckPublisher, + participantDeregisterAckPublisher, participantUpdatePublisher); } @BeforeEach @@ -160,13 +160,14 @@ class ControlLoopInstantiationProviderTest { @Test void testInstantiationCrud() throws Exception { + participantProvider.createParticipants(CommonTestData.createParticipants()); ControlLoops controlLoopsCreate = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Crud"); ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); assertThat(controlLoopsDb.getControlLoopList()).isEmpty(); - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate); InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate); @@ -222,8 +223,8 @@ class ControlLoopInstantiationProviderTest { ControlLoops controlLoopsDb = new ControlLoops(); controlLoopsDb.setControlLoopList(new ArrayList<>()); - var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, - commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); for (ControlLoop controlLoop : controlLoopsSource.getControlLoopList()) { ControlLoops controlLoopsFromDb = @@ -242,12 +243,12 @@ class ControlLoopInstantiationProviderTest { ControlLoop controlLoop0 = controlLoops.getControlLoopList().get(0); - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); assertThatThrownBy( - () -> instantiationProvider.deleteControlLoop(controlLoop0.getName(), controlLoop0.getVersion())) - .hasMessageMatching(CONTROL_LOOP_NOT_FOUND); + () -> instantiationProvider.deleteControlLoop(controlLoop0.getName(), controlLoop0.getVersion())) + .hasMessageMatching(CONTROL_LOOP_NOT_FOUND); InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoops), controlLoops); @@ -277,13 +278,13 @@ class ControlLoopInstantiationProviderTest { controlLoop.setState(state); - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); instantiationProvider.updateControlLoops(controlLoops); assertThatThrownBy( - () -> instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion())) - .hasMessageMatching(String.format(DELETE_BAD_REQUEST, state)); + () -> instantiationProvider.deleteControlLoop(controlLoop.getName(), controlLoop.getVersion())) + .hasMessageMatching(String.format(DELETE_BAD_REQUEST, state)); } @Test @@ -295,8 +296,8 @@ class ControlLoopInstantiationProviderTest { ControlLoops controlLoopsDb = getControlLoopsFromDb(controlLoopsCreate); assertThat(controlLoopsDb.getControlLoopList()).isEmpty(); - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); InstantiationResponse instantiationResponse = instantiationProvider.createControlLoops(controlLoopsCreate); InstantiationUtils.assertInstantiationResponse(instantiationResponse, controlLoopsCreate); @@ -315,10 +316,11 @@ class ControlLoopInstantiationProviderTest { ControlLoops controlLoops = InstantiationUtils .getControlLoopsFromResource(CL_INSTANTIATION_DEFINITION_NAME_NOT_FOUND_JSON, "ClElementNotFound"); - var provider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var provider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler, + participantProvider); // to validate control Loop, it needs to define ToscaServiceTemplate - // InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider); + // InstantiationUtils.storeToscaServiceTemplate(TOSCA_TEMPLATE_YAML, commissioningProvider); assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty(); @@ -333,15 +335,16 @@ class ControlLoopInstantiationProviderTest { assertThat(getControlLoopsFromDb(controlLoops).getControlLoopList()).isEmpty(); - var provider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var provider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler, + participantProvider); assertThatThrownBy(() -> provider.createControlLoops(controlLoops)) .hasMessageMatching(CONTROLLOOP_DEFINITION_NOT_FOUND); } @Test void testIssueControlLoopCommand_OrderedStateInvalid() throws ControlLoopRuntimeException, IOException { - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); assertThatThrownBy(() -> instantiationProvider.issueControlLoopCommand(new InstantiationCommand())) .hasMessageMatching(ORDERED_STATE_INVALID); } @@ -353,8 +356,8 @@ class ControlLoopInstantiationProviderTest { InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "V1"); assertThat(getControlLoopsFromDb(controlLoopsV1).getControlLoopList()).isEmpty(); - var instantiationProvider = - new ControlLoopInstantiationProvider(clProvider, commissioningProvider, supervisionHandler); + var instantiationProvider = new ControlLoopInstantiationProvider(clProvider, commissioningProvider, + supervisionHandler, participantProvider); InstantiationUtils.assertInstantiationResponse(instantiationProvider.createControlLoops(controlLoopsV1), controlLoopsV1); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java index 42e4ddee2..5a15687c5 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java @@ -36,6 +36,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopPrimedResponse; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse; @@ -43,6 +44,7 @@ import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstan import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationUtils; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.main.rest.InstantiationController; +import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; import org.onap.policy.common.utils.coder.YamlJsonTranslator; import org.onap.policy.common.utils.resources.ResourceUtils; @@ -94,6 +96,9 @@ class InstantiationControllerTest extends CommonRestController { @Autowired private ControlLoopInstantiationProvider instantiationProvider; + @Autowired + private ParticipantProvider participantProvider; + @LocalServerPort private int randomServerPort; @@ -326,10 +331,11 @@ class InstantiationControllerTest extends CommonRestController { @Test void testCommand() throws Exception { - var controlLoops = InstantiationUtils.getControlLoopsFromResource(CL_INSTANTIATION_CREATE_JSON, "Command"); instantiationProvider.createControlLoops(controlLoops); + participantProvider.createParticipants(CommonTestData.createParticipants()); + InstantiationCommand command = InstantiationUtils.getInstantiationCommandFromResource(CL_INSTANTIATION_CHANGE_STATE_JSON, "Command"); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java index bcc3a701c..70d115e47 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java @@ -24,29 +24,20 @@ package org.onap.policy.clamp.controlloop.runtime.supervision.comm; import static org.assertj.core.api.Assertions.assertThatCode; import java.time.Instant; -import java.util.ArrayList; import java.util.Collections; -import java.util.List; -import java.util.Map; import java.util.UUID; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck; -import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister; 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.models.messages.dmaap.participant.ParticipantUpdateAck; -import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider; import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler; @@ -54,27 +45,18 @@ import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.TopicSink; -import org.onap.policy.common.utils.coder.YamlJsonTranslator; -import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; 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; class SupervisionMessagesTest extends CommonRestController { - private static final String TOSCA_SERVICE_TEMPLATE_YAML = - "src/test/resources/rest/servicetemplates/pmsh_multiple_cl_tosca.yaml"; private static final Object lockit = new Object(); private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; private static final String TOPIC = "my-topic"; private static SupervisionHandler supervisionHandler; - private static CommissioningProvider commissioningProvider; private static ControlLoopProvider clProvider; private static PolicyModelsProvider modelsProvider; - private static ParticipantProvider participantProvider; - private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); /** * setup Db Provider Parameters. @@ -87,13 +69,11 @@ class SupervisionMessagesTest extends CommonRestController { modelsProvider = CommonTestData.getPolicyModelsProvider(controlLoopParameters.getDatabaseProviderParameters()); clProvider = new ControlLoopProvider(controlLoopParameters.getDatabaseProviderParameters()); - participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters()); var participantStatisticsProvider = new ParticipantStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters()); var clElementStatisticsProvider = new ClElementStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters()); - commissioningProvider = new CommissioningProvider(modelsProvider, clProvider, null, participantProvider); var monitoringProvider = new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider); var participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters()); @@ -114,26 +94,6 @@ class SupervisionMessagesTest extends CommonRestController { } @Test - void testReceiveParticipantRegister() throws Exception { - final ParticipantRegister participantRegisterMsg = new ParticipantRegister(); - participantRegisterMsg.setParticipantId(getParticipantId()); - participantRegisterMsg.setTimestamp(Instant.now()); - participantRegisterMsg.setParticipantType(getParticipantType()); - - synchronized (lockit) { - ParticipantRegisterListener participantRegisterListener = - new ParticipantRegisterListener(supervisionHandler); - ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml( - ResourceUtils.getResourceAsString(TOSCA_SERVICE_TEMPLATE_YAML), ToscaServiceTemplate.class); - - // List<ToscaNodeTemplate> listOfTemplates = commissioningProvider.getControlLoopDefinitions(null, null); - commissioningProvider.createControlLoopDefinitions(serviceTemplate); - assertThatCode(() -> participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterMsg)) - .doesNotThrowAnyException(); - } - } - - @Test void testSendParticipantRegisterAck() throws Exception { final ParticipantRegisterAck participantRegisterAckMsg = new ParticipantRegisterAck(); participantRegisterAckMsg.setMessage("ParticipantRegisterAck message"); @@ -178,72 +138,6 @@ class SupervisionMessagesTest extends CommonRestController { } @Test - void testSendParticipantUpdate() throws Exception { - final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate(); - participantUpdateMsg.setParticipantId(getParticipantId()); - participantUpdateMsg.setTimestamp(Instant.now()); - participantUpdateMsg.setParticipantType(getParticipantType()); - participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000)); - participantUpdateMsg.setMessageId(UUID.randomUUID()); - - ToscaServiceTemplate toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(null, null); - List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); - for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate() - .getNodeTemplates().entrySet()) { - if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), - toscaServiceTemplate)) { - var clParticipantType = - ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()); - prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(), - toscaInputEntry.getValue(), participantDefinitionUpdates); - } - } - - participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates); - synchronized (lockit) { - ParticipantUpdatePublisher participantUpdatePublisher = new ParticipantUpdatePublisher(modelsProvider); - participantUpdatePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class))); - assertThatCode(() -> participantUpdatePublisher.send(participantUpdateMsg)).doesNotThrowAnyException(); - } - } - - private 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 ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition, - ToscaConceptIdentifier clParticipantType, - List<ControlLoopElementDefinition> controlLoopElementDefinitionList) { - ParticipantDefinition participantDefinition = new ParticipantDefinition(); - participantDefinition.setParticipantType(clParticipantType); - controlLoopElementDefinitionList.add(clDefinition); - participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList); - return participantDefinition; - } - - @Test void testReceiveParticipantUpdateAckMessage() throws Exception { final ParticipantUpdateAck participantUpdateAckMsg = new ParticipantUpdateAck(); participantUpdateAckMsg.setMessage("ParticipantUpdateAck message"); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java index 300c662ce..13d30e401 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/util/CommonTestData.java @@ -20,8 +20,10 @@ package org.onap.policy.clamp.controlloop.runtime.util; +import java.util.List; import javax.ws.rs.core.Response.Status; import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; @@ -32,6 +34,7 @@ import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; /** * Class to hold/create all parameters for test cases. @@ -81,4 +84,40 @@ public class CommonTestData { throw new PfModelRuntimeException(e); } } + + /** + * Create a List of Participants. + * + * @return a List of Participants + */ + public static List<Participant> createParticipants() { + var participant1 = createParticipant( + new ToscaConceptIdentifier("org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", "2.3.4"), + new ToscaConceptIdentifier("org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", + "2.3.4")); + var participant2 = createParticipant( + new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1"), + new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1")); + var participant3 = createParticipant( + new ToscaConceptIdentifier("org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", "2.2.1"), + new ToscaConceptIdentifier("org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", "2.2.1")); + return List.of(participant1, participant2, participant3); + } + + /** + * Create a new Participant. + * + * @param participantType the participant Type + * @param participantId the participant id + * @return a new Participant + */ + public static Participant createParticipant(ToscaConceptIdentifier participantType, + ToscaConceptIdentifier participantId) { + var participant = new Participant(); + participant.setDefinition(participantId); + participant.setParticipantType(participantType); + participant.setName(participantId.getName()); + participant.setVersion(participantId.getVersion()); + return participant; + } } diff --git a/runtime-controlloop/src/test/resources/rest/controlloops/ControlLoopsUpdate.json b/runtime-controlloop/src/test/resources/rest/controlloops/ControlLoopsUpdate.json index 025e2a1fb..b09e46fe7 100644 --- a/runtime-controlloop/src/test/resources/rest/controlloops/ControlLoopsUpdate.json +++ b/runtime-controlloop/src/test/resources/rest/controlloops/ControlLoopsUpdate.json @@ -17,6 +17,10 @@ "name": "org.onap.domain.pmsh.PMSH_DCAEMicroservice", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", + "version": "2.3.4" + }, "participantType": { "name": "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", "version": "2.3.4" @@ -31,6 +35,10 @@ "name": "org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, "participantType": { "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", "version": "2.3.1" @@ -45,6 +53,10 @@ "name": "org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, "participantType": { "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", "version": "2.3.1" @@ -59,6 +71,10 @@ "name": "org.onap.domain.pmsh.PMSH_CDS_ControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", + "version": "2.2.1" + }, "participantType": { "name": "org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", "version": "2.2.1" @@ -86,6 +102,10 @@ "name": "org.onap.domain.pmsh.PMSH_DCAEMicroservice", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", + "version": "2.3.4" + }, "participantType": { "name": "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", "version": "2.3.4" @@ -100,6 +120,10 @@ "name": "org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, "participantType": { "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", "version": "2.3.1" @@ -114,6 +138,10 @@ "name": "org.onap.domain.pmsh.PMSH_MonitoringPolicyControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", + "version": "2.3.1" + }, "participantType": { "name": "org.onap.policy.controlloop.PolicyControlLoopParticipant", "version": "2.3.1" @@ -128,6 +156,10 @@ "name": "org.onap.domain.pmsh.PMSH_CDS_ControlLoopElement", "version": "1.2.3" }, + "participantId": { + "name": "org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", + "version": "2.2.1" + }, "participantType": { "name": "org.onap.ccsdk.cds.controlloop.CdsControlLoopParticipant", "version": "2.2.1" |