diff options
author | liamfallon <liam.fallon@est.tech> | 2022-01-25 19:55:43 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2022-02-18 15:54:40 +0000 |
commit | 43098043c4ef31d9d5dead66568d7d9482a6b165 (patch) | |
tree | 6f6ea4812ff93d65e7c64e12a3ec6ab4462a64e2 /participant/participant-impl/participant-impl-policy/src/test/java | |
parent | f401b5099bcb64f3e21de608d0207dd69d8043cd (diff) |
Rename TOSCA Control Loop to ACM
This commit renames the TOSCA Control Loop functionality in CLAMP to
Automation Composition Management.
This review is a direct renaming review and, as everything is renamed
together it is large.
Issue-ID: POLICY-3939
Change-Id: I28f0a6dd889bf3570a4c1365ae9e71fc58db6d6c
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'participant/participant-impl/participant-impl-policy/src/test/java')
9 files changed, 453 insertions, 447 deletions
diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantMessagesTest.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/endtoend/ParticipantMessagesTest.java index ce368eba1..c17d2c53d 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/endtoend/ParticipantMessagesTest.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/endtoend/ParticipantMessagesTest.java @@ -19,7 +19,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.clamp.controlloop.participant.policy.endtoend; +package org.onap.policy.clamp.acm.participant.policy.endtoend; import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; @@ -30,19 +30,19 @@ import java.util.UUID; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; -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.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.participant.intermediary.comm.ParticipantDeregisterAckListener; -import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher; -import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantRegisterAckListener; -import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantUpdateListener; -import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler; -import org.onap.policy.clamp.controlloop.participant.policy.main.utils.TestListenerUtils; +import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantDeregisterAckListener; +import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher; +import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantRegisterAckListener; +import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantUpdateListener; +import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler; +import org.onap.policy.clamp.acm.participant.policy.main.utils.TestListenerUtils; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdateAck; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; @@ -169,6 +169,6 @@ class ParticipantMessagesTest { } private ToscaConceptIdentifier getParticipantType() { - return new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1"); + return new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1"); } } diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandlerTest.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandlerTest.java new file mode 100644 index 000000000..0d114db8d --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/handler/AutomationCompositionElementHandlerTest.java @@ -0,0 +1,102 @@ +/*- + * ============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.acm.participant.policy.main.handler; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.Mockito.when; + +import java.util.UUID; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi; +import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient; +import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState; +import org.onap.policy.models.base.PfModelException; +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 AutomationCompositionElementHandlerTest { + + private static final String ID_NAME = "org.onap.PM_CDS_Blueprint"; + private static final String ID_VERSION = "1.0.1"; + private static final UUID automationCompositionElementId = UUID.randomUUID(); + private static final ToscaConceptIdentifier automationCompositionId = + new ToscaConceptIdentifier(ID_NAME, ID_VERSION); + + @Test + void testHandlerExceptions() { + AutomationCompositionElementHandler handler = getTestingHandler(); + + assertDoesNotThrow(() -> handler + .automationCompositionElementStateChange(automationCompositionId, + automationCompositionElementId, + AutomationCompositionState.UNINITIALISED, + AutomationCompositionOrderedState.PASSIVE)); + + assertDoesNotThrow(() -> handler + .automationCompositionElementStateChange(automationCompositionId, + automationCompositionElementId, + AutomationCompositionState.RUNNING, + AutomationCompositionOrderedState.UNINITIALISED)); + + assertDoesNotThrow(() -> handler + .automationCompositionElementStateChange(automationCompositionId, + automationCompositionElementId, + AutomationCompositionState.PASSIVE, + AutomationCompositionOrderedState.RUNNING)); + var element = getTestingAcElement(); + var acElementDefinition = Mockito.mock(ToscaNodeTemplate.class); + + assertDoesNotThrow(() -> handler + .automationCompositionElementUpdate(automationCompositionId, element, acElementDefinition)); + + assertDoesNotThrow(() -> handler + .handleStatistics(automationCompositionElementId)); + } + + AutomationCompositionElementHandler getTestingHandler() { + var api = Mockito.mock(PolicyApiHttpClient.class); + var pap = Mockito.mock(PolicyPapHttpClient.class); + var handler = new AutomationCompositionElementHandler(api, pap); + var intermediaryApi = Mockito.mock(ParticipantIntermediaryApi.class); + var element = getTestingAcElement(); + when(intermediaryApi.getAutomationCompositionElement(automationCompositionElementId)).thenReturn(element); + handler.setIntermediaryApi(intermediaryApi); + return handler; + } + + AutomationCompositionElement getTestingAcElement() { + var element = new AutomationCompositionElement(); + element.setDefinition(automationCompositionId); + element.setDescription("Description"); + element.setId(automationCompositionElementId); + element.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED); + element.setParticipantId(automationCompositionId); + element.setState(AutomationCompositionState.UNINITIALISED); + var template = Mockito.mock(ToscaServiceTemplate.class); + element.setToscaServiceTemplateFragment(template); + return element; + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/parameters/CommonTestData.java index e965370cc..b5cd9d442 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/CommonTestData.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/parameters/CommonTestData.java @@ -18,12 +18,13 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.clamp.controlloop.participant.policy.main.parameters; +package org.onap.policy.clamp.acm.participant.policy.main.parameters; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.TreeMap; +import org.onap.policy.clamp.acm.participant.policy.main.parameters.ParticipantPolicyParameters; import org.onap.policy.common.endpoints.parameters.TopicParameters; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; @@ -34,7 +35,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; * Class to hold/create all parameters for test cases. */ public class CommonTestData { - public static final String PARTICIPANT_GROUP_NAME = "ControlLoopParticipantGroup"; + public static final String PARTICIPANT_GROUP_NAME = "AutomationCompositionParticipantGroup"; public static final String DESCRIPTION = "Participant description"; public static final long TIME_INTERVAL = 2000; public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams()); @@ -123,7 +124,7 @@ public class CommonTestData { map.put("description", DESCRIPTION); map.put("participantId", getParticipantId()); map.put("participantType", getParticipantId()); - map.put("clampControlLoopTopics", getTopicParametersMap(false)); + map.put("clampAutomationCompositionTopics", getTopicParametersMap(false)); } return map; @@ -151,7 +152,7 @@ public class CommonTestData { */ public static TopicParameters getTopicParams() { final TopicParameters topicParams = new TopicParameters(); - topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT"); + topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT"); topicParams.setTopicCommInfrastructure("dmaap"); topicParams.setServers(Arrays.asList("localhost")); return topicParams; diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParametersTest.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/parameters/ParticipantPolicyParametersTest.java index 74e7cb1bc..89b2bce58 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/parameters/ParticipantPolicyParametersTest.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/parameters/ParticipantPolicyParametersTest.java @@ -18,17 +18,18 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.clamp.controlloop.participant.policy.main.parameters; +package org.onap.policy.clamp.acm.participant.policy.main.parameters; import static org.assertj.core.api.Assertions.assertThat; import javax.validation.Validation; import javax.validation.ValidatorFactory; import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.acm.participant.policy.main.parameters.ParticipantPolicyParameters; class ParticipantPolicyParametersTest { - private CommonTestData commonTestData = new CommonTestData(); - private ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); + private final CommonTestData commonTestData = new CommonTestData(); + private final ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); @Test void testParticipantPolicyParameters() { @@ -39,14 +40,14 @@ class ParticipantPolicyParametersTest { @Test void testParticipantPolicyParameters_NullTopicSinks() { final ParticipantPolicyParameters participantParameters = commonTestData.getParticipantPolicyParameters(); - participantParameters.getIntermediaryParameters().getClampControlLoopTopics().setTopicSinks(null); + participantParameters.getIntermediaryParameters().getClampAutomationCompositionTopics().setTopicSinks(null); assertThat(validatorFactory.getValidator().validate(participantParameters)).isNotEmpty(); } @Test void testParticipantPolicyParameters_NullTopicSources() { final ParticipantPolicyParameters participantParameters = commonTestData.getParticipantPolicyParameters(); - participantParameters.getIntermediaryParameters().getClampControlLoopTopics().setTopicSources(null); + participantParameters.getIntermediaryParameters().getClampAutomationCompositionTopics().setTopicSources(null); assertThat(validatorFactory.getValidator().validate(participantParameters)).isNotEmpty(); } diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/rest/ActuatorControllerTest.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/rest/ActuatorControllerTest.java index 9cc16e287..8543bf98a 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/rest/ActuatorControllerTest.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/rest/ActuatorControllerTest.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.clamp.controlloop.participant.policy.main.rest; +package org.onap.policy.clamp.acm.participant.policy.main.rest; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -27,7 +27,7 @@ import javax.ws.rs.core.Response; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.policy.clamp.controlloop.participant.policy.main.utils.CommonActuatorController; +import org.onap.policy.clamp.acm.participant.policy.main.utils.CommonActuatorController; import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/CommonActuatorController.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/utils/CommonActuatorController.java index ec6e30918..689977e3c 100644 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/CommonActuatorController.java +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/utils/CommonActuatorController.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.clamp.controlloop.participant.policy.main.utils; +package org.onap.policy.clamp.acm.participant.policy.main.utils; import static org.junit.Assert.assertEquals; @@ -40,7 +40,7 @@ import org.onap.policy.common.utils.network.NetworkUtil; public class CommonActuatorController { public static final String SELF = NetworkUtil.getHostname(); - public static final String CONTEXT_PATH = "onap/policyparticipant"; + public static final String CONTEXT_PATH = "onap/policy/clamp/acm/policyparticipant"; public static final String ACTUATOR_ENDPOINT = CONTEXT_PATH + "/actuator/"; private static String httpPrefix; diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/utils/TestListenerUtils.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/utils/TestListenerUtils.java new file mode 100644 index 000000000..a29831d56 --- /dev/null +++ b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/acm/participant/policy/main/utils/TestListenerUtils.java @@ -0,0 +1,321 @@ +/*- + * ============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.acm.participant.policy.main.utils; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileNotFoundException; +import java.time.Instant; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.onap.policy.clamp.models.acm.concepts.AutomationComposition; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState; +import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState; +import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition; +import org.onap.policy.clamp.models.acm.concepts.ParticipantUpdates; +import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionUpdate; +import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate; +import org.onap.policy.clamp.models.acm.utils.AcmUtils; +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.YamlJsonTranslator; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class TestListenerUtils { + + private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); + private static final Coder CODER = new StandardCoder(); + private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class); + + /** + * Method to create a automationComposition from a yaml file. + * + * @return AutomationComposition automation composition + */ + public static AutomationComposition createAutomationComposition() { + AutomationComposition automationComposition = new AutomationComposition(); + Map<UUID, AutomationCompositionElement> elements = new LinkedHashMap<>(); + ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead(); + Map<String, ToscaNodeTemplate> nodeTemplatesMap = + toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); + for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) { + AutomationCompositionElement acElement = new AutomationCompositionElement(); + acElement.setId(UUID.randomUUID()); + + ToscaConceptIdentifier acElementParticipantId = new ToscaConceptIdentifier(); + acElementParticipantId.setName(toscaInputEntry.getKey()); + acElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion()); + acElement.setParticipantId(acElementParticipantId); + + acElement.setDefinition(acElementParticipantId); + acElement.setState(AutomationCompositionState.UNINITIALISED); + acElement.setDescription(toscaInputEntry.getValue().getDescription()); + acElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED); + elements.put(acElement.getId(), acElement); + } + automationComposition.setElements(elements); + automationComposition.setName("PMSHInstance0"); + automationComposition.setVersion("1.0.0"); + + ToscaConceptIdentifier definition = new ToscaConceptIdentifier(); + definition.setName("PMSHInstance0"); + definition.setVersion("1.0.0"); + automationComposition.setDefinition(definition); + + return automationComposition; + } + + /** + * Method to create AutomationCompositionStateChange message from the arguments passed. + * + * @param automationCompositionOrderedState automationCompositionOrderedState + * @return AutomationCompositionStateChange message + */ + public static AutomationCompositionStateChange createAutomationCompositionStateChangeMsg( + final AutomationCompositionOrderedState automationCompositionOrderedState) { + final AutomationCompositionStateChange acStateChangeMsg = new AutomationCompositionStateChange(); + + ToscaConceptIdentifier automationCompositionId = new ToscaConceptIdentifier(); + automationCompositionId.setName("PMSHInstance0"); + automationCompositionId.setVersion("1.0.0"); + + ToscaConceptIdentifier participantId = new ToscaConceptIdentifier(); + participantId.setName("org.onap.PM_Policy"); + participantId.setVersion("0.0.0"); + + acStateChangeMsg.setAutomationCompositionId(automationCompositionId); + acStateChangeMsg.setParticipantId(participantId); + acStateChangeMsg.setTimestamp(Instant.now()); + acStateChangeMsg.setOrderedState(automationCompositionOrderedState); + + return acStateChangeMsg; + } + + /** + * Method to create AutomationCompositionUpdateMsg. + * + * @return AutomationCompositionUpdate message + */ + public static AutomationCompositionUpdate createAutomationCompositionUpdateMsg() { + final AutomationCompositionUpdate acUpdateMsg = new AutomationCompositionUpdate(); + ToscaConceptIdentifier automationCompositionId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0"); + ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0"); + + acUpdateMsg.setAutomationCompositionId(automationCompositionId); + acUpdateMsg.setParticipantId(participantId); + acUpdateMsg.setMessageId(UUID.randomUUID()); + acUpdateMsg.setTimestamp(Instant.now()); + + Map<UUID, AutomationCompositionElement> elements = new LinkedHashMap<>(); + ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead(); + TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate); + Map<String, ToscaNodeTemplate> nodeTemplatesMap = + toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); + for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) { + if (ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(toscaInputEntry.getValue(), + toscaServiceTemplate)) { + AutomationCompositionElement acElement = new AutomationCompositionElement(); + acElement.setId(UUID.randomUUID()); + var acParticipantType = + ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()); + + acElement.setParticipantId(acParticipantType); + acElement.setParticipantType(acParticipantType); + + acElement.setDefinition( + new ToscaConceptIdentifier(toscaInputEntry.getKey(), toscaInputEntry.getValue().getVersion())); + acElement.setState(AutomationCompositionState.UNINITIALISED); + acElement.setDescription(toscaInputEntry.getValue().getDescription()); + acElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE); + elements.put(acElement.getId(), acElement); + } + } + + List<ParticipantUpdates> participantUpdates = new ArrayList<>(); + for (AutomationCompositionElement element : elements.values()) { + AcmUtils.setServiceTemplatePolicyInfo(element, toscaServiceTemplate); + AcmUtils.prepareParticipantUpdate(element, participantUpdates); + } + acUpdateMsg.setParticipantUpdatesList(participantUpdates); + return acUpdateMsg; + } + + /** + * 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.acm.PolicyAutomationCompositionParticipant", "2.3.1"); + + participantUpdateMsg.setParticipantId(participantId); + participantUpdateMsg.setTimestamp(Instant.now()); + participantUpdateMsg.setParticipantType(participantType); + participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000)); + participantUpdateMsg.setMessageId(UUID.randomUUID()); + + ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead(); + // Add policies to the toscaServiceTemplate + TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate); + + List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); + for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate() + .getNodeTemplates().entrySet()) { + if (ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(toscaInputEntry.getValue(), + toscaServiceTemplate)) { + AcmUtils.prepareParticipantDefinitionUpdate( + ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()), + toscaInputEntry.getKey(), toscaInputEntry.getValue(), + participantDefinitionUpdates, null); + } + } + + participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates); + return participantUpdateMsg; + } + + /** + * Method to create AutomationCompositionUpdate using the arguments passed. + * + * @param jsonFilePath the path of the automation composition content + * @return AutomationCompositionUpdate message + * @throws CoderException exception while reading the file to object + */ + public static AutomationCompositionUpdate createParticipantAcUpdateMsgFromJson(String jsonFilePath) + throws CoderException { + AutomationCompositionUpdate automationCompositionUpdateMsg = + CODER.decode(new File(jsonFilePath), AutomationCompositionUpdate.class); + return automationCompositionUpdateMsg; + } + + private static ToscaServiceTemplate testAutomationCompositionRead() { + Set<String> automationCompositionDirectoryContents = + ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates"); + + boolean atLeastOneAutomationCompositionTested = false; + ToscaServiceTemplate toscaServiceTemplate = null; + + for (String automationCompositionFilePath : automationCompositionDirectoryContents) { + if (!automationCompositionFilePath.endsWith(".yaml")) { + continue; + } + atLeastOneAutomationCompositionTested = true; + toscaServiceTemplate = testAutomationCompositionYamlSerialization(automationCompositionFilePath); + } + + // Add policy_types to the toscaServiceTemplate + addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate); + + assertTrue(atLeastOneAutomationCompositionTested); + return toscaServiceTemplate; + } + + private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) { + Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes"); + + for (String policyTypeFilePath : policyTypeDirectoryContents) { + String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath); + + ToscaServiceTemplate foundPolicyTypeSt = + yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class); + + toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom()); + toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription()); + toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata()); + toscaServiceTemplate.setName(foundPolicyTypeSt.getName()); + toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion()); + toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion()); + + if (foundPolicyTypeSt.getDataTypes() != null) { + if (toscaServiceTemplate.getDataTypes() == null) { + toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes()); + } else { + toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes()); + } + } + + if (toscaServiceTemplate.getPolicyTypes() == null) { + toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes()); + } else { + toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes()); + } + } + } + + /** + * Method to add polcies to the toscaServiceTemplate. + * + * @param toscaServiceTemplate to add policies + */ + public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) { + Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies"); + + for (String policiesFilePath : policiesDirectoryContents) { + if (!policiesFilePath.endsWith("yaml")) { + continue; + } + + String policiesString = ResourceUtils.getResourceAsString(policiesFilePath); + + ToscaServiceTemplate foundPoliciesSt = + yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class); + toscaServiceTemplate.getToscaTopologyTemplate() + .setPolicies(foundPoliciesSt.getToscaTopologyTemplate().getPolicies()); + } + } + + private static ToscaServiceTemplate testAutomationCompositionYamlSerialization( + String automationCompositionFilePath) { + try { + String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath); + if (automationCompositionString == null) { + throw new FileNotFoundException(automationCompositionFilePath); + } + + ToscaServiceTemplate serviceTemplate = + yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class); + return serviceTemplate; + } catch (FileNotFoundException e) { + LOGGER.error("cannot find YAML file", automationCompositionFilePath); + throw new IllegalArgumentException(e); + } + } +} diff --git a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandlerTest.java b/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandlerTest.java deleted file mode 100644 index b5c5e19cb..000000000 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/handler/ControlLoopElementHandlerTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/*- - * ============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.policy.main.handler; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.mockito.Mockito.when; - -import java.util.UUID; -import org.junit.jupiter.api.Test; -import org.mockito.Mockito; -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.participant.intermediary.api.ParticipantIntermediaryApi; -import org.onap.policy.clamp.controlloop.participant.policy.client.PolicyApiHttpClient; -import org.onap.policy.clamp.controlloop.participant.policy.client.PolicyPapHttpClient; -import org.onap.policy.models.base.PfModelException; -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 ControlLoopElementHandlerTest { - - private static final String ID_NAME = "org.onap.PM_CDS_Blueprint"; - private static final String ID_VERSION = "1.0.1"; - private static final UUID controlLoopElementId = UUID.randomUUID(); - private static final ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(ID_NAME, ID_VERSION); - - @Test - void testHandlerExceptions() throws PfModelException { - ControlLoopElementHandler handler = getTestingHandler(); - - assertDoesNotThrow(() -> handler - .controlLoopElementStateChange(controlLoopId, - controlLoopElementId, - ControlLoopState.UNINITIALISED, - ControlLoopOrderedState.PASSIVE)); - - assertDoesNotThrow(() -> handler - .controlLoopElementStateChange(controlLoopId, - controlLoopElementId, - ControlLoopState.RUNNING, - ControlLoopOrderedState.UNINITIALISED)); - - assertDoesNotThrow(() -> handler - .controlLoopElementStateChange(controlLoopId, - controlLoopElementId, - ControlLoopState.PASSIVE, - ControlLoopOrderedState.RUNNING)); - var element = getTestingClElement(); - var clElementDefinition = Mockito.mock(ToscaNodeTemplate.class); - - assertDoesNotThrow(() -> handler - .controlLoopElementUpdate(controlLoopId, element, clElementDefinition)); - - assertDoesNotThrow(() -> handler - .handleStatistics(controlLoopElementId)); - } - - ControlLoopElementHandler getTestingHandler() { - var api = Mockito.mock(PolicyApiHttpClient.class); - var pap = Mockito.mock(PolicyPapHttpClient.class); - var handler = new ControlLoopElementHandler(api, pap); - var intermediaryApi = Mockito.mock(ParticipantIntermediaryApi.class); - var element = getTestingClElement(); - when(intermediaryApi.getControlLoopElement(controlLoopElementId)).thenReturn(element); - handler.setIntermediaryApi(intermediaryApi); - return handler; - } - - ControlLoopElement getTestingClElement() { - var element = new ControlLoopElement(); - element.setDefinition(controlLoopId); - element.setDescription("Description"); - element.setId(controlLoopElementId); - element.setOrderedState(ControlLoopOrderedState.UNINITIALISED); - element.setParticipantId(controlLoopId); - element.setState(ControlLoopState.UNINITIALISED); - var template = Mockito.mock(ToscaServiceTemplate.class); - element.setToscaServiceTemplateFragment(template); - return element; - } - -} 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 deleted file mode 100644 index d517ef61e..000000000 --- a/participant/participant-impl/participant-impl-policy/src/test/java/org/onap/policy/clamp/controlloop/participant/policy/main/utils/TestListenerUtils.java +++ /dev/null @@ -1,317 +0,0 @@ -/*- - * ============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.policy.main.utils; - -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.FileNotFoundException; -import java.time.Instant; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -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.ControlLoopOrderedState; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState; -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.controlloop.concepts.ParticipantUtils; -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.policy.main.parameters.CommonTestData; -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.YamlJsonTranslator; -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.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class TestListenerUtils { - - private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator(); - private static final Coder CODER = new StandardCoder(); - static CommonTestData commonTestData = new CommonTestData(); - private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class); - - /** - * Method to create a controlLoop from a yaml file. - * - * @return ControlLoop controlloop - */ - public static ControlLoop createControlLoop() { - 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()); - - ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier(); - clElementParticipantId.setName(toscaInputEntry.getKey()); - clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion()); - clElement.setParticipantId(clElementParticipantId); - - clElement.setDefinition(clElementParticipantId); - clElement.setState(ControlLoopState.UNINITIALISED); - clElement.setDescription(toscaInputEntry.getValue().getDescription()); - clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED); - elements.put(clElement.getId(), clElement); - } - controlLoop.setElements(elements); - controlLoop.setName("PMSHInstance0"); - controlLoop.setVersion("1.0.0"); - - ToscaConceptIdentifier definition = new ToscaConceptIdentifier(); - definition.setName("PMSHInstance0"); - definition.setVersion("1.0.0"); - controlLoop.setDefinition(definition); - - return controlLoop; - } - - /** - * Method to create ControlLoopStateChange message from the arguments passed. - * - * @param controlLoopOrderedState controlLoopOrderedState - * - * @return ControlLoopStateChange message - */ - public static ControlLoopStateChange createControlLoopStateChangeMsg( - final ControlLoopOrderedState controlLoopOrderedState) { - final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange(); - - ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier(); - controlLoopId.setName("PMSHInstance0"); - controlLoopId.setVersion("1.0.0"); - - ToscaConceptIdentifier participantId = new ToscaConceptIdentifier(); - participantId.setName("org.onap.PM_Policy"); - participantId.setVersion("0.0.0"); - - clStateChangeMsg.setControlLoopId(controlLoopId); - clStateChangeMsg.setParticipantId(participantId); - clStateChangeMsg.setTimestamp(Instant.now()); - clStateChangeMsg.setOrderedState(controlLoopOrderedState); - - return clStateChangeMsg; - } - - /** - * Method to create ControlLoopUpdateMsg. - * - * @return ControlLoopUpdate message - */ - public static ControlLoopUpdate createControlLoopUpdateMsg() { - final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate(); - 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()); - - Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>(); - ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead(); - TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate); - Map<String, ToscaNodeTemplate> nodeTemplatesMap = - toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); - for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) { - if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), - toscaServiceTemplate)) { - ControlLoopElement clElement = new ControlLoopElement(); - clElement.setId(UUID.randomUUID()); - var clParticipantType = - ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()); - - clElement.setParticipantId(clParticipantType); - clElement.setParticipantType(clParticipantType); - - 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); - } - } - - List<ParticipantUpdates> participantUpdates = new ArrayList<>(); - for (ControlLoopElement element : elements.values()) { - CommonUtils.setServiceTemplatePolicyInfo(element, toscaServiceTemplate); - CommonUtils.prepareParticipantUpdate(element, participantUpdates); - } - clUpdateMsg.setParticipantUpdatesList(participantUpdates); - return clUpdateMsg; - } - - /** - * 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(); - // Add policies to the toscaServiceTemplate - TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate); - - List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); - for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate() - .getNodeTemplates().entrySet()) { - if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), - toscaServiceTemplate)) { - CommonUtils.prepareParticipantDefinitionUpdate( - ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()), - toscaInputEntry.getKey(), toscaInputEntry.getValue(), - participantDefinitionUpdates, null); - } - } - - participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates); - return participantUpdateMsg; - } - - /** - * Method to create ControlLoopUpdate using the arguments passed. - * - * @param jsonFilePath the path of the controlloop content - * - * @return ControlLoopUpdate message - * @throws CoderException exception while reading the file to object - */ - public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) throws CoderException { - ControlLoopUpdate controlLoopUpdateMsg = CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class); - return controlLoopUpdateMsg; - } - - private static ToscaServiceTemplate testControlLoopRead() { - Set<String> controlLoopDirectoryContents = - ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates"); - - boolean atLeastOneControlLoopTested = false; - ToscaServiceTemplate toscaServiceTemplate = null; - - for (String controlLoopFilePath : controlLoopDirectoryContents) { - if (!controlLoopFilePath.endsWith(".yaml")) { - continue; - } - atLeastOneControlLoopTested = true; - toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath); - } - - // Add policy_types to the toscaServiceTemplate - addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate); - - assertTrue(atLeastOneControlLoopTested); - return toscaServiceTemplate; - } - - private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) { - Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes"); - - for (String policyTypeFilePath : policyTypeDirectoryContents) { - String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath); - - ToscaServiceTemplate foundPolicyTypeSt = - yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class); - - toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom()); - toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription()); - toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata()); - toscaServiceTemplate.setName(foundPolicyTypeSt.getName()); - toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion()); - toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion()); - - if (foundPolicyTypeSt.getDataTypes() != null) { - if (toscaServiceTemplate.getDataTypes() == null) { - toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes()); - } else { - toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes()); - } - } - - if (toscaServiceTemplate.getPolicyTypes() == null) { - toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes()); - } else { - toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes()); - } - } - } - - /** - * Method to add polcies to the toscaServiceTemplate. - * - * @param toscaServiceTemplate to add policies - */ - public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) { - Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies"); - - for (String policiesFilePath : policiesDirectoryContents) { - String policiesString = ResourceUtils.getResourceAsString(policiesFilePath); - - ToscaServiceTemplate foundPoliciesSt = yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class); - toscaServiceTemplate.getToscaTopologyTemplate() - .setPolicies(foundPoliciesSt.getToscaTopologyTemplate().getPolicies()); - } - } - - private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) { - try { - String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath); - if (controlLoopString == null) { - throw new FileNotFoundException(controlLoopFilePath); - } - - ToscaServiceTemplate serviceTemplate = - yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class); - return serviceTemplate; - } catch (FileNotFoundException e) { - LOGGER.error("cannot find YAML file", controlLoopFilePath); - throw new IllegalArgumentException(e); - } - } -} |