2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.simulator.main.rest;
23 import static org.junit.Assert.assertTrue;
26 import java.io.FileNotFoundException;
27 import java.time.Instant;
28 import java.util.ArrayList;
29 import java.util.List;
32 import java.util.UUID;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
43 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
44 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.ParticipantSimulatorParameters;
45 import org.onap.policy.clamp.controlloop.participant.simulator.simulation.SimulationProvider;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
50 import org.onap.policy.common.utils.resources.ResourceUtils;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public class TestListenerUtils {
59 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
60 private static final Coder CODER = new StandardCoder();
61 static CommonTestData commonTestData = new CommonTestData();
62 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
64 private TestListenerUtils() {}
67 private static ParticipantHandler participantHandler;
70 * Method to initialize participantHandler.
72 public static void initParticipantHandler() {
74 final ParticipantSimulatorParameters participantParameters = commonTestData.toObject(
75 commonTestData.getParticipantParameterGroupMap(CommonTestData.PARTICIPANT_GROUP_NAME),
76 ParticipantSimulatorParameters.class);
78 SimulationProvider simulationProvider =
79 new SimulationProvider(participantParameters.getIntermediaryParameters());
81 participantHandler = simulationProvider.getIntermediaryApi().getParticipantHandler();
85 * Method to create a controlLoop from a yaml file.
87 * @return ControlLoop controlloop
89 public static ControlLoop createControlLoop() {
90 ControlLoop controlLoop = new ControlLoop();
91 List<ControlLoopElement> elements = new ArrayList<>();
92 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
93 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
94 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
95 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
96 ControlLoopElement clElement = new ControlLoopElement();
97 clElement.setId(UUID.randomUUID());
99 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
100 clElementParticipantId.setName(toscaInputEntry.getKey());
101 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
102 clElement.setParticipantId(clElementParticipantId);
104 clElement.setDefinition(clElementParticipantId);
105 clElement.setState(ControlLoopState.UNINITIALISED);
106 clElement.setDescription(toscaInputEntry.getValue().getDescription());
107 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
108 elements.add(clElement);
110 controlLoop.setElements(elements);
111 controlLoop.setName("PMSHInstance0");
112 controlLoop.setVersion("1.0.0");
114 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
115 definition.setName("PMSHInstance0");
116 definition.setVersion("1.0.0");
117 controlLoop.setDefinition(definition);
123 * Method to create ParticipantStateChange message from the arguments passed.
125 * @param participantState participant State
127 * @return ParticipantStateChange message
129 public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
130 final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
131 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
133 participantStateChangeMsg.setParticipantId(participantId);
134 participantStateChangeMsg.setTimestamp(Instant.now());
135 participantStateChangeMsg.setState(participantState);
137 return participantStateChangeMsg;
141 * Method to create ControlLoopStateChange message from the arguments passed.
143 * @param controlLoopOrderedState controlLoopOrderedState
145 * @return ParticipantControlLoopStateChange message
147 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
148 final ControlLoopOrderedState controlLoopOrderedState) {
149 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
151 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
152 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
154 participantClStateChangeMsg.setControlLoopId(controlLoopId);
155 participantClStateChangeMsg.setParticipantId(participantId);
156 participantClStateChangeMsg.setTimestamp(Instant.now());
157 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
159 return participantClStateChangeMsg;
163 * Method to create ControlLoopUpdateMsg.
165 * @return ParticipantControlLoopUpdate message
167 public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
168 final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
169 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0.");
170 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
172 clUpdateMsg.setControlLoopId(controlLoopId);
173 clUpdateMsg.setParticipantId(participantId);
175 ControlLoop controlLoop = new ControlLoop();
176 List<ControlLoopElement> elements = new ArrayList<>();
177 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
178 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
179 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
180 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
181 ControlLoopElement clElement = new ControlLoopElement();
182 clElement.setId(UUID.randomUUID());
184 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
185 clElementParticipantId.setName(toscaInputEntry.getKey());
186 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
187 clElement.setParticipantId(clElementParticipantId);
189 clElement.setDefinition(clElementParticipantId);
190 clElement.setState(ControlLoopState.UNINITIALISED);
191 clElement.setDescription(toscaInputEntry.getValue().getDescription());
192 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
193 elements.add(clElement);
195 controlLoop.setElements(elements);
196 controlLoop.setName("PMSHInstance0");
197 controlLoop.setVersion("1.0.0");
198 controlLoop.setDefinition(controlLoopId);
199 clUpdateMsg.setControlLoop(controlLoop);
200 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
206 * Method to create ParticipantControlLoopUpdate using the arguments passed.
208 * @param jsonFilePath the path of the controlloop content
210 * @return ParticipantControlLoopUpdate message
211 * @throws CoderException exception while reading the file to object
213 public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
214 throws CoderException {
215 ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
216 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
217 return participantControlLoopUpdateMsg;
220 private static ToscaServiceTemplate testControlLoopRead() {
221 Set<String> controlLoopDirectoryContents =
222 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
224 boolean atLeastOneControlLoopTested = false;
225 ToscaServiceTemplate toscaServiceTemplate = null;
227 for (String controlLoopFilePath : controlLoopDirectoryContents) {
228 if (!controlLoopFilePath.endsWith(".yaml")) {
231 atLeastOneControlLoopTested = true;
232 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
235 assertTrue(atLeastOneControlLoopTested);
236 return toscaServiceTemplate;
239 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
241 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
242 if (controlLoopString == null) {
243 throw new FileNotFoundException(controlLoopFilePath);
246 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
247 controlLoopString, ToscaServiceTemplate.class);
248 return serviceTemplate;
249 } catch (FileNotFoundException e) {
250 LOGGER.error("cannot find YAML file", controlLoopFilePath);
251 throw new IllegalArgumentException(e);