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 clParticipantType;
154 clParticipantType = CODER.decode(
155 toscaInputEntry.getValue().getProperties().get("participantType").toString(),
156 ToscaConceptIdentifier.class);
157 } catch (CoderException e) {
158 throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e);
161 clElement.setParticipantId(clParticipantType);
162 clElement.setParticipantType(clParticipantType);
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 clParticipantType;
234 clParticipantType = CODER.decode(
235 toscaInputEntry.getValue().getProperties().get("participantType").toString(),
236 ToscaConceptIdentifier.class);
237 } catch (CoderException e) {
238 throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e);
240 prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
241 toscaInputEntry.getValue(), participantDefinitionUpdates);
245 participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
246 return participantUpdateMsg;
249 private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
250 ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
252 var clDefinition = new ControlLoopElementDefinition();
253 clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(
254 entryKey, entryValue.getVersion()));
255 clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
256 List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
258 if (participantDefinitionUpdates.isEmpty()) {
259 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType,
260 controlLoopElementDefinitionList));
262 boolean participantExists = false;
263 for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
264 if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) {
265 participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
266 participantExists = true;
269 if (!participantExists) {
270 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType,
271 controlLoopElementDefinitionList));
276 private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
277 ToscaConceptIdentifier clParticipantType,
278 List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
279 ParticipantDefinition participantDefinition = new ParticipantDefinition();
280 participantDefinition.setParticipantType(clParticipantType);
281 controlLoopElementDefinitionList.add(clDefinition);
282 participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
283 return participantDefinition;
287 * Method to create ControlLoopUpdate using the arguments passed.
289 * @param jsonFilePath the path of the controlloop content
291 * @return ControlLoopUpdate message
292 * @throws CoderException exception while reading the file to object
294 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
295 throws CoderException {
296 ControlLoopUpdate controlLoopUpdateMsg =
297 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
298 return controlLoopUpdateMsg;
301 private static ToscaServiceTemplate testControlLoopRead() {
302 Set<String> controlLoopDirectoryContents =
303 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
305 boolean atLeastOneControlLoopTested = false;
306 ToscaServiceTemplate toscaServiceTemplate = null;
308 for (String controlLoopFilePath : controlLoopDirectoryContents) {
309 if (!controlLoopFilePath.endsWith(".yaml")) {
312 atLeastOneControlLoopTested = true;
313 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
316 assertTrue(atLeastOneControlLoopTested);
317 return toscaServiceTemplate;
320 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
322 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
323 if (controlLoopString == null) {
324 throw new FileNotFoundException(controlLoopFilePath);
327 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
328 controlLoopString, ToscaServiceTemplate.class);
329 return serviceTemplate;
330 } catch (FileNotFoundException e) {
331 LOGGER.error("cannot find YAML file", controlLoopFilePath);
332 throw new IllegalArgumentException(e);