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.LinkedHashMap;
30 import java.util.List;
33 import java.util.UUID;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
44 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
45 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.ParticipantSimulatorParameters;
46 import org.onap.policy.clamp.controlloop.participant.simulator.simulation.SimulationProvider;
47 import org.onap.policy.common.utils.coder.Coder;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
51 import org.onap.policy.common.utils.resources.ResourceUtils;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 public class TestListenerUtils {
60 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
61 private static final Coder CODER = new StandardCoder();
62 static CommonTestData commonTestData = new CommonTestData();
63 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
65 private TestListenerUtils() {}
68 private static ParticipantHandler participantHandler;
71 * Method to initialize participantHandler.
73 public static void initParticipantHandler() {
75 final ParticipantSimulatorParameters participantParameters = commonTestData.toObject(
76 commonTestData.getParticipantParameterGroupMap(CommonTestData.PARTICIPANT_GROUP_NAME),
77 ParticipantSimulatorParameters.class);
79 SimulationProvider simulationProvider =
80 new SimulationProvider(participantParameters.getIntermediaryParameters());
82 participantHandler = simulationProvider.getIntermediaryApi().getParticipantHandler();
86 * Method to create a controlLoop from a yaml file.
88 * @return ControlLoop controlloop
90 public static ControlLoop createControlLoop() {
91 ControlLoop controlLoop = new ControlLoop();
92 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
93 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
94 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
95 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
96 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
97 ControlLoopElement clElement = new ControlLoopElement();
98 clElement.setId(UUID.randomUUID());
100 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
101 clElementParticipantId.setName(toscaInputEntry.getKey());
102 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
103 clElement.setParticipantId(clElementParticipantId);
104 clElement.setParticipantType(clElementParticipantId);
106 clElement.setDefinition(clElementParticipantId);
107 clElement.setState(ControlLoopState.UNINITIALISED);
108 clElement.setDescription(toscaInputEntry.getValue().getDescription());
109 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
110 elements.put(clElement.getId(), clElement);
112 controlLoop.setElements(elements);
113 controlLoop.setName("PMSHInstance0");
114 controlLoop.setVersion("1.0.0");
116 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
117 definition.setName("PMSHInstance0");
118 definition.setVersion("1.0.0");
119 controlLoop.setDefinition(definition);
125 * Method to create ParticipantStateChange message from the arguments passed.
127 * @param participantState participant State
129 * @return ParticipantStateChange message
131 public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
132 final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
133 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
135 participantStateChangeMsg.setParticipantId(participantId);
136 participantStateChangeMsg.setTimestamp(Instant.now());
137 participantStateChangeMsg.setState(participantState);
139 return participantStateChangeMsg;
143 * Method to create ControlLoopStateChange message from the arguments passed.
145 * @param controlLoopOrderedState controlLoopOrderedState
147 * @return ParticipantControlLoopStateChange message
149 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
150 final ControlLoopOrderedState controlLoopOrderedState) {
151 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
153 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
154 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
156 participantClStateChangeMsg.setControlLoopId(controlLoopId);
157 participantClStateChangeMsg.setParticipantId(participantId);
158 participantClStateChangeMsg.setTimestamp(Instant.now());
159 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
161 return participantClStateChangeMsg;
165 * Method to create ControlLoopUpdateMsg.
167 * @return ParticipantControlLoopUpdate message
169 public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
170 final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
171 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
172 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
174 clUpdateMsg.setControlLoopId(controlLoopId);
175 clUpdateMsg.setParticipantId(participantId);
176 clUpdateMsg.setParticipantType(participantId);
178 ControlLoop controlLoop = new ControlLoop();
179 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
180 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
181 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
182 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
183 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
184 ControlLoopElement clElement = new ControlLoopElement();
185 clElement.setId(UUID.randomUUID());
187 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
188 clElementParticipantId.setName(toscaInputEntry.getKey());
189 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
190 clElement.setParticipantId(clElementParticipantId);
191 clElement.setParticipantType(clElementParticipantId);
193 clElement.setDefinition(clElementParticipantId);
194 clElement.setState(ControlLoopState.UNINITIALISED);
195 clElement.setDescription(toscaInputEntry.getValue().getDescription());
196 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
197 elements.put(clElement.getId(), clElement);
199 controlLoop.setElements(elements);
200 controlLoop.setName("PMSHInstance0");
201 controlLoop.setVersion("1.0.0");
202 controlLoop.setDefinition(controlLoopId);
203 clUpdateMsg.setControlLoop(controlLoop);
204 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
210 * Method to create ParticipantControlLoopUpdate using the arguments passed.
212 * @param jsonFilePath the path of the controlloop content
214 * @return ParticipantControlLoopUpdate message
215 * @throws CoderException exception while reading the file to object
217 public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
218 throws CoderException {
219 ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
220 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
221 return participantControlLoopUpdateMsg;
224 private static ToscaServiceTemplate testControlLoopRead() {
225 Set<String> controlLoopDirectoryContents =
226 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
228 boolean atLeastOneControlLoopTested = false;
229 ToscaServiceTemplate toscaServiceTemplate = null;
231 for (String controlLoopFilePath : controlLoopDirectoryContents) {
232 if (!controlLoopFilePath.endsWith(".yaml")) {
235 atLeastOneControlLoopTested = true;
236 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
239 assertTrue(atLeastOneControlLoopTested);
240 return toscaServiceTemplate;
243 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
245 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
246 if (controlLoopString == null) {
247 throw new FileNotFoundException(controlLoopFilePath);
250 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
251 controlLoopString, ToscaServiceTemplate.class);
252 return serviceTemplate;
253 } catch (FileNotFoundException e) {
254 LOGGER.error("cannot find YAML file", controlLoopFilePath);
255 throw new IllegalArgumentException(e);