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;
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.ControlLoopElementDefinition;
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.ParticipantDefinition;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
44 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
45 import org.onap.policy.common.utils.coder.Coder;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
49 import org.onap.policy.common.utils.resources.ResourceUtils;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class TestListenerUtils {
58 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
59 private static final Coder CODER = new StandardCoder();
60 static CommonTestData commonTestData = new CommonTestData();
61 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
62 private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement";
64 private TestListenerUtils() {}
67 * Method to create a controlLoop from a yaml file.
69 * @return ControlLoop controlloop
71 public static ControlLoop createControlLoop() {
72 ControlLoop controlLoop = new ControlLoop();
73 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
74 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
75 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
76 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
77 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
78 ControlLoopElement clElement = new ControlLoopElement();
79 clElement.setId(UUID.randomUUID());
81 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
82 clElementParticipantId.setName(toscaInputEntry.getKey());
83 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
84 clElement.setParticipantId(clElementParticipantId);
85 clElement.setParticipantType(clElementParticipantId);
87 clElement.setDefinition(clElementParticipantId);
88 clElement.setState(ControlLoopState.UNINITIALISED);
89 clElement.setDescription(toscaInputEntry.getValue().getDescription());
90 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
91 elements.put(clElement.getId(), clElement);
93 controlLoop.setElements(elements);
94 controlLoop.setName("PMSHInstance0");
95 controlLoop.setVersion("1.0.0");
97 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
98 definition.setName("PMSHInstance0");
99 definition.setVersion("1.0.0");
100 controlLoop.setDefinition(definition);
106 * Method to create ControlLoopStateChange message from the arguments passed.
108 * @param controlLoopOrderedState controlLoopOrderedState
110 * @return ControlLoopStateChange message
112 public static ControlLoopStateChange createControlLoopStateChangeMsg(
113 final ControlLoopOrderedState controlLoopOrderedState) {
114 final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
116 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
117 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
119 clStateChangeMsg.setControlLoopId(controlLoopId);
120 clStateChangeMsg.setParticipantId(participantId);
121 clStateChangeMsg.setTimestamp(Instant.now());
122 clStateChangeMsg.setOrderedState(controlLoopOrderedState);
124 return clStateChangeMsg;
128 * Method to create ControlLoopUpdateMsg.
130 * @return ControlLoopUpdate message
132 public static ControlLoopUpdate createControlLoopUpdateMsg() {
133 final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
134 ToscaConceptIdentifier controlLoopId =
135 new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
136 ToscaConceptIdentifier participantId =
137 new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
139 clUpdateMsg.setControlLoopId(controlLoopId);
140 clUpdateMsg.setParticipantId(participantId);
141 clUpdateMsg.setMessageId(UUID.randomUUID());
142 clUpdateMsg.setTimestamp(Instant.now());
144 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
145 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
146 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
147 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
148 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
149 if (toscaInputEntry.getValue().getType().contains(CONTROL_LOOP_ELEMENT)) {
150 ControlLoopElement clElement = new ControlLoopElement();
151 clElement.setId(UUID.randomUUID());
152 ToscaConceptIdentifier clParticipantId;
154 clParticipantId = CODER.decode(
155 toscaInputEntry.getValue().getProperties().get("participant_id").toString(),
156 ToscaConceptIdentifier.class);
157 } catch (CoderException e) {
158 throw new RuntimeException("cannot get ParticipantId from toscaNodeTemplate", e);
161 clElement.setParticipantId(clParticipantId);
162 clElement.setParticipantType(clParticipantId);
164 clElement.setDefinition(new ToscaConceptIdentifier(toscaInputEntry.getKey(),
165 toscaInputEntry.getValue().getVersion()));
166 clElement.setState(ControlLoopState.UNINITIALISED);
167 clElement.setDescription(toscaInputEntry.getValue().getDescription());
168 clElement.setOrderedState(ControlLoopOrderedState.PASSIVE);
169 elements.put(clElement.getId(), clElement);
173 List<ParticipantUpdates> participantUpdates = new ArrayList<>();
174 for (ControlLoopElement element : elements.values()) {
175 prepareParticipantUpdateForControlLoop(element, participantUpdates);
177 clUpdateMsg.setParticipantUpdatesList(participantUpdates);
181 private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement,
182 List<ParticipantUpdates> participantUpdates) {
183 if (participantUpdates.isEmpty()) {
184 participantUpdates.add(getControlLoopElementList(clElement));
186 boolean participantExists = false;
187 for (ParticipantUpdates participantUpdate : participantUpdates) {
188 if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
189 participantUpdate.getControlLoopElementList().add(clElement);
190 participantExists = true;
193 if (!participantExists) {
194 participantUpdates.add(getControlLoopElementList(clElement));
199 private static ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) {
200 ParticipantUpdates participantUpdate = new ParticipantUpdates();
201 List<ControlLoopElement> controlLoopElementList = new ArrayList<>();
202 participantUpdate.setParticipantId(clElement.getParticipantId());
203 controlLoopElementList.add(clElement);
204 participantUpdate.setControlLoopElementList(controlLoopElementList);
205 return participantUpdate;
209 * Method to create participantUpdateMsg.
211 * @return ParticipantUpdate message
213 public static ParticipantUpdate createParticipantUpdateMsg() {
214 final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
215 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
216 ToscaConceptIdentifier participantType = new ToscaConceptIdentifier(
217 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
219 participantUpdateMsg.setParticipantId(participantId);
220 participantUpdateMsg.setTimestamp(Instant.now());
221 participantUpdateMsg.setParticipantType(participantType);
222 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
223 participantUpdateMsg.setMessageId(UUID.randomUUID());
225 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
226 // Add policies to the toscaServiceTemplate
228 List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
229 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry :
230 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
231 if (toscaInputEntry.getValue().getType().contains(CONTROL_LOOP_ELEMENT)) {
232 ToscaConceptIdentifier clParticipantId;
234 clParticipantId = CODER.decode(
235 toscaInputEntry.getValue().getProperties().get("participant_id").toString(),
236 ToscaConceptIdentifier.class);
237 } catch (CoderException e) {
238 throw new RuntimeException("cannot get ParticipantId from toscaNodeTemplate", e);
240 prepareParticipantDefinitionUpdate(clParticipantId, toscaInputEntry.getKey(),
241 toscaInputEntry.getValue(), participantDefinitionUpdates);
245 participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
246 participantUpdateMsg.setToscaServiceTemplate(toscaServiceTemplate);
247 return participantUpdateMsg;
250 private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantId, String entryKey,
251 ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
253 var clDefinition = new ControlLoopElementDefinition();
254 clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(
255 entryKey, entryValue.getVersion()));
256 clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
257 List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
259 if (participantDefinitionUpdates.isEmpty()) {
260 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
261 controlLoopElementDefinitionList));
263 boolean participantExists = false;
264 for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
265 if (participantDefinitionUpdate.getParticipantId().equals(clParticipantId)) {
266 participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
267 participantExists = true;
270 if (!participantExists) {
271 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
272 controlLoopElementDefinitionList));
277 private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
278 ToscaConceptIdentifier clParticipantId,
279 List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
280 ParticipantDefinition participantDefinition = new ParticipantDefinition();
281 participantDefinition.setParticipantId(clParticipantId);
282 controlLoopElementDefinitionList.add(clDefinition);
283 participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
284 return participantDefinition;
288 * Method to create ControlLoopUpdate using the arguments passed.
290 * @param jsonFilePath the path of the controlloop content
292 * @return ControlLoopUpdate message
293 * @throws CoderException exception while reading the file to object
295 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
296 throws CoderException {
297 ControlLoopUpdate controlLoopUpdateMsg =
298 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
299 return controlLoopUpdateMsg;
302 private static ToscaServiceTemplate testControlLoopRead() {
303 Set<String> controlLoopDirectoryContents =
304 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
306 boolean atLeastOneControlLoopTested = false;
307 ToscaServiceTemplate toscaServiceTemplate = null;
309 for (String controlLoopFilePath : controlLoopDirectoryContents) {
310 if (!controlLoopFilePath.endsWith(".yaml")) {
313 atLeastOneControlLoopTested = true;
314 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
317 assertTrue(atLeastOneControlLoopTested);
318 return toscaServiceTemplate;
321 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
323 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
324 if (controlLoopString == null) {
325 throw new FileNotFoundException(controlLoopFilePath);
328 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
329 controlLoopString, ToscaServiceTemplate.class);
330 return serviceTemplate;
331 } catch (FileNotFoundException e) {
332 LOGGER.error("cannot find YAML file", controlLoopFilePath);
333 throw new IllegalArgumentException(e);