diff options
Diffstat (limited to 'participant/participant-impl/participant-impl-dcae/src/test/java/org')
3 files changed, 185 insertions, 32 deletions
diff --git a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/endtoend/ParticipantDcaeTest.java b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/endtoend/ParticipantDcaeTest.java index a3fcfdd9e..6e7f28735 100644 --- a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/endtoend/ParticipantDcaeTest.java +++ b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/endtoend/ParticipantDcaeTest.java @@ -34,10 +34,12 @@ import org.mockserver.integration.ClientAndServer; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState; 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.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData; import org.onap.policy.clamp.controlloop.participant.dcae.main.rest.TestListenerUtils; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener; import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener; +import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener; import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.utils.coder.CoderException; @@ -54,6 +56,7 @@ class ParticipantDcaeTest { private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; private static final String TOPIC = "my-topic"; + private static final Object lockit = new Object(); private static final String LOOP = "pmsh_loop"; private static final String BLUEPRINT_DEPLOYED = "BLUEPRINT_DEPLOYED"; @@ -68,7 +71,7 @@ class ParticipantDcaeTest { * start Servers. */ @BeforeAll - public static void startServers() { + public void startServers() { // Clamp mockClampServer = ClientAndServer.startClientAndServer(8443); @@ -88,6 +91,12 @@ class ParticipantDcaeTest { mockConsulServer.when(request().withMethod("PUT").withPath("/v1/kv/dcae-pmsh:policy")) .respond(response().withStatusCode(200)); + ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg(); + + synchronized (lockit) { + ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler); + participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg); + } } /** @@ -126,7 +135,6 @@ class ParticipantDcaeTest { void testControlLoopUpdateListener_ParticipantIdNoMatch() throws CoderException { ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); controlLoopUpdateMsg.getParticipantId().setName("DummyName"); - controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler); clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg); @@ -139,7 +147,6 @@ class ParticipantDcaeTest { @Test void testControlLoopUpdateListenerPassive() throws CoderException { ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); - controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.PASSIVE); ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler); clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg); @@ -152,7 +159,6 @@ class ParticipantDcaeTest { @Test void testControlLoopUpdateListenerUninitialised() throws CoderException { ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); - controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED); ControlLoopUpdateListener clUpdateListener = new ControlLoopUpdateListener(participantHandler); clUpdateListener.onTopicEvent(INFRA, TOPIC, null, controlLoopUpdateMsg); @@ -165,7 +171,6 @@ class ParticipantDcaeTest { @Test void testControlLoopUpdateListenerString() throws CoderException { ControlLoopUpdate controlLoopUpdateMsg = TestListenerUtils.createControlLoopUpdateMsg(); - controlLoopUpdateMsg.getControlLoop().setOrderedState(ControlLoopOrderedState.UNINITIALISED); assertThat(controlLoopUpdateMsg.toString()).contains("state=UNINITIALISED"); ControlLoopUpdate copyControlLoopUpdateMsg = diff --git a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/handler/ControlLoopElementHandlerTest.java b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/handler/ControlLoopElementHandlerTest.java index 40f2f5f7b..8a7f403f4 100644 --- a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/handler/ControlLoopElementHandlerTest.java +++ b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/handler/ControlLoopElementHandlerTest.java @@ -31,21 +31,28 @@ import static org.mockito.Mockito.when; import java.util.UUID; import org.json.JSONException; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; 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.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ClampHttpClient; import org.onap.policy.clamp.controlloop.participant.dcae.httpclient.ConsulDcaeHttpClient; import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData; +import org.onap.policy.clamp.controlloop.participant.dcae.main.rest.TestListenerUtils; import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop; import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener; +import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; +import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; 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.models.base.PfModelException; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.junit.jupiter.SpringExtension; /** @@ -61,8 +68,27 @@ class ControlLoopElementHandlerTest { private static final String MICROSERVICE_INSTALLED_SUCCESSFULLY = "MICROSERVICE_INSTALLED_SUCCESSFULLY"; public static final Coder CODER = new StandardCoder(); + private static final Object lockit = new Object(); + private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; + private static final String TOPIC = "my-topic"; private CommonTestData commonTestData = new CommonTestData(); + @Autowired + private ParticipantHandler participantHandler; + + /** + * Initial ParticipantUpdate message. + */ + @BeforeAll + public void sendParticipantUpdate() { + ParticipantUpdate participantUpdateMsg = TestListenerUtils.createParticipantUpdateMsg(); + + synchronized (lockit) { + ParticipantUpdateListener participantUpdateListener = new ParticipantUpdateListener(participantHandler); + participantUpdateListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateMsg); + } + } + @Test void test_ControlLoopElementStateChange() { ClampHttpClient clampClient = spy(mock(ClampHttpClient.class)); @@ -109,7 +135,9 @@ class ControlLoopElementHandlerTest { element.setOrderedState(ControlLoopOrderedState.PASSIVE); final ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate(); - controlLoopElementHandler.controlLoopElementUpdate(element, controlLoopDefinition); + controlLoopElementHandler.controlLoopElementUpdate(element, + controlLoopDefinition.getToscaTopologyTemplate().getNodeTemplates() + .get("org.onap.domain.pmsh.PMSH_DCAEMicroservice")); verify(clampClient).create(eq(LOOP), eq(TEMPLATE)); verify(consulClient).deploy(any(String.class), any(String.class)); @@ -139,7 +167,9 @@ class ControlLoopElementHandlerTest { element.setOrderedState(ControlLoopOrderedState.PASSIVE); ToscaServiceTemplate controlLoopDefinition = new ToscaServiceTemplate(); - controlLoopElementHandler.controlLoopElementUpdate(element, controlLoopDefinition); + controlLoopElementHandler.controlLoopElementUpdate(element, + controlLoopDefinition.getToscaTopologyTemplate().getNodeTemplates() + .get("org.onap.domain.pmsh.PMSH_DCAEMicroservice")); verify(clampClient, times(0)).create(eq(LOOP), eq(TEMPLATE)); verify(consulClient).deploy(any(String.class), any(String.class)); diff --git a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/rest/TestListenerUtils.java b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/rest/TestListenerUtils.java index 136d0e500..c6dd927da 100644 --- a/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/rest/TestListenerUtils.java +++ b/participant/participant-impl/participant-impl-dcae/src/test/java/org/onap/policy/clamp/controlloop/participant/dcae/main/rest/TestListenerUtils.java @@ -22,16 +22,21 @@ package org.onap.policy.clamp.controlloop.participant.dcae.main.rest; import java.io.File; import java.time.Instant; +import java.util.ArrayList; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.UUID; 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.ParticipantState; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates; 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.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; @@ -46,6 +51,7 @@ public class TestListenerUtils { private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); private static final Coder CODER = new StandardCoder(); private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml"; + private static final String CONTROL_LOOP_ELEMENT = "ControlLoopElement"; /** * Method to create a controlLoop from a yaml file. @@ -119,46 +125,158 @@ public class TestListenerUtils { */ public static ControlLoopUpdate createControlLoopUpdateMsg() { final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate(); - ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(); - controlLoopId.setName("PMSHInstance0"); - controlLoopId.setVersion("1.0.0"); - - ToscaConceptIdentifier participantId = new ToscaConceptIdentifier(); - participantId.setName("DCAEParticipant0"); - participantId.setVersion("1.0.0"); + ToscaConceptIdentifier controlLoopId = + new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); + ToscaConceptIdentifier participantId = + new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0"); clUpdateMsg.setControlLoopId(controlLoopId); clUpdateMsg.setParticipantId(participantId); + clUpdateMsg.setMessageId(UUID.randomUUID()); + clUpdateMsg.setTimestamp(Instant.now()); - ControlLoop controlLoop = new ControlLoop(); Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>(); ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead(); Map<String, ToscaNodeTemplate> nodeTemplatesMap = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) { - ControlLoopElement clElement = new ControlLoopElement(); - clElement.setId(UUID.randomUUID()); + if (toscaInputEntry.getValue().getType().contains(CONTROL_LOOP_ELEMENT)) { + ControlLoopElement clElement = new ControlLoopElement(); + clElement.setId(UUID.randomUUID()); + ToscaConceptIdentifier clParticipantId; + try { + clParticipantId = CODER.decode( + toscaInputEntry.getValue().getProperties().get("participant_id").toString(), + ToscaConceptIdentifier.class); + } catch (CoderException e) { + throw new RuntimeException("cannot get ParticipantId from toscaNodeTemplate", e); + } - ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier(); - clElementParticipantId.setName(toscaInputEntry.getKey()); - clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion()); - clElement.setParticipantId(clElementParticipantId); + clElement.setParticipantId(clParticipantId); + clElement.setParticipantType(clParticipantId); - clElement.setDefinition(clElementParticipantId); - clElement.setState(ControlLoopState.UNINITIALISED); - clElement.setDescription(toscaInputEntry.getValue().getDescription()); - clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED); - elements.put(clElement.getId(), clElement); + clElement.setDefinition(new ToscaConceptIdentifier(toscaInputEntry.getKey(), + toscaInputEntry.getValue().getVersion())); + clElement.setState(ControlLoopState.UNINITIALISED); + clElement.setDescription(toscaInputEntry.getValue().getDescription()); + clElement.setOrderedState(ControlLoopOrderedState.PASSIVE); + elements.put(clElement.getId(), clElement); + } } - controlLoop.setElements(elements); - controlLoop.setName("PMSHInstance0"); - controlLoop.setVersion("1.0.0"); - controlLoop.setDefinition(controlLoopId); - clUpdateMsg.setControlLoop(controlLoop); + List<ParticipantUpdates> participantUpdates = new ArrayList<>(); + for (ControlLoopElement element : elements.values()) { + prepareParticipantUpdateForControlLoop(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. + * + * @return ParticipantUpdate message + */ + public static ParticipantUpdate createParticipantUpdateMsg() { + final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate(); + ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0"); + ToscaConceptIdentifier participantType = new ToscaConceptIdentifier( + "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1"); + + participantUpdateMsg.setParticipantId(participantId); + participantUpdateMsg.setTimestamp(Instant.now()); + participantUpdateMsg.setParticipantType(participantType); + participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000)); + participantUpdateMsg.setMessageId(UUID.randomUUID()); + + ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead(); + + List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); + for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : + toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) { + if (toscaInputEntry.getValue().getType().contains(CONTROL_LOOP_ELEMENT)) { + ToscaConceptIdentifier clParticipantId; + try { + clParticipantId = CODER.decode( + toscaInputEntry.getValue().getProperties().get("participant_id").toString(), + ToscaConceptIdentifier.class); + } catch (CoderException e) { + throw new RuntimeException("cannot get ParticipantId from toscaNodeTemplate", e); + } + prepareParticipantDefinitionUpdate(clParticipantId, toscaInputEntry.getKey(), + toscaInputEntry.getValue(), participantDefinitionUpdates); + } + } + + participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates); + participantUpdateMsg.setToscaServiceTemplate(toscaServiceTemplate); + return participantUpdateMsg; + } + + private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantId, 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, clParticipantId, + controlLoopElementDefinitionList)); + } else { + boolean participantExists = false; + for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) { + if (participantDefinitionUpdate.getParticipantId().equals(clParticipantId)) { + participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition); + participantExists = true; + } + } + if (!participantExists) { + participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId, + controlLoopElementDefinitionList)); + } + } + } + + private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition, + ToscaConceptIdentifier clParticipantId, + List<ControlLoopElementDefinition> controlLoopElementDefinitionList) { + ParticipantDefinition participantDefinition = new ParticipantDefinition(); + participantDefinition.setParticipantId(clParticipantId); + controlLoopElementDefinitionList.add(clDefinition); + participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList); + return participantDefinition; + } + /** * Method to create a deep copy of ControlLoopUpdateMsg. * |