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.LinkedHashMap;
31 import java.util.UUID;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
40 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
41 import org.onap.policy.common.utils.coder.Coder;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
45 import org.onap.policy.common.utils.resources.ResourceUtils;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class TestListenerUtils {
54 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
55 private static final Coder CODER = new StandardCoder();
56 static CommonTestData commonTestData = new CommonTestData();
57 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
59 private TestListenerUtils() {}
62 * Method to create a controlLoop from a yaml file.
64 * @return ControlLoop controlloop
66 public static ControlLoop createControlLoop() {
67 ControlLoop controlLoop = new ControlLoop();
68 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
69 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
70 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
71 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
72 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
73 ControlLoopElement clElement = new ControlLoopElement();
74 clElement.setId(UUID.randomUUID());
76 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
77 clElementParticipantId.setName(toscaInputEntry.getKey());
78 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
79 clElement.setParticipantId(clElementParticipantId);
80 clElement.setParticipantType(clElementParticipantId);
82 clElement.setDefinition(clElementParticipantId);
83 clElement.setState(ControlLoopState.UNINITIALISED);
84 clElement.setDescription(toscaInputEntry.getValue().getDescription());
85 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
86 elements.put(clElement.getId(), clElement);
88 controlLoop.setElements(elements);
89 controlLoop.setName("PMSHInstance0");
90 controlLoop.setVersion("1.0.0");
92 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
93 definition.setName("PMSHInstance0");
94 definition.setVersion("1.0.0");
95 controlLoop.setDefinition(definition);
101 * Method to create ParticipantStateChange message from the arguments passed.
103 * @param participantState participant State
105 * @return ParticipantStateChange message
107 public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
108 final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
109 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
111 participantStateChangeMsg.setParticipantId(participantId);
112 participantStateChangeMsg.setTimestamp(Instant.now());
113 participantStateChangeMsg.setState(participantState);
115 return participantStateChangeMsg;
119 * Method to create ControlLoopStateChange message from the arguments passed.
121 * @param controlLoopOrderedState controlLoopOrderedState
123 * @return ParticipantControlLoopStateChange message
125 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
126 final ControlLoopOrderedState controlLoopOrderedState) {
127 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
129 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
130 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
132 participantClStateChangeMsg.setControlLoopId(controlLoopId);
133 participantClStateChangeMsg.setParticipantId(participantId);
134 participantClStateChangeMsg.setTimestamp(Instant.now());
135 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
137 return participantClStateChangeMsg;
141 * Method to create ControlLoopUpdateMsg.
143 * @return ControlLoopUpdate message
145 public static ControlLoopUpdate createControlLoopUpdateMsg() {
146 final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
147 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
148 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
150 clUpdateMsg.setControlLoopId(controlLoopId);
151 clUpdateMsg.setParticipantId(participantId);
152 clUpdateMsg.setParticipantType(participantId);
154 ControlLoop controlLoop = new ControlLoop();
155 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
156 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
157 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
158 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
159 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
160 ControlLoopElement clElement = new ControlLoopElement();
161 clElement.setId(UUID.randomUUID());
163 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
164 clElementParticipantId.setName(toscaInputEntry.getKey());
165 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
166 clElement.setParticipantId(clElementParticipantId);
167 clElement.setParticipantType(clElementParticipantId);
169 clElement.setDefinition(clElementParticipantId);
170 clElement.setState(ControlLoopState.UNINITIALISED);
171 clElement.setDescription(toscaInputEntry.getValue().getDescription());
172 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
173 elements.put(clElement.getId(), clElement);
175 controlLoop.setElements(elements);
176 controlLoop.setName("PMSHInstance0");
177 controlLoop.setVersion("1.0.0");
178 controlLoop.setDefinition(controlLoopId);
179 clUpdateMsg.setControlLoop(controlLoop);
180 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
186 * Method to create ControlLoopUpdate using the arguments passed.
188 * @param jsonFilePath the path of the controlloop content
190 * @return ControlLoopUpdate message
191 * @throws CoderException exception while reading the file to object
193 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
194 throws CoderException {
195 ControlLoopUpdate controlLoopUpdateMsg =
196 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
197 return controlLoopUpdateMsg;
200 private static ToscaServiceTemplate testControlLoopRead() {
201 Set<String> controlLoopDirectoryContents =
202 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
204 boolean atLeastOneControlLoopTested = false;
205 ToscaServiceTemplate toscaServiceTemplate = null;
207 for (String controlLoopFilePath : controlLoopDirectoryContents) {
208 if (!controlLoopFilePath.endsWith(".yaml")) {
211 atLeastOneControlLoopTested = true;
212 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
215 assertTrue(atLeastOneControlLoopTested);
216 return toscaServiceTemplate;
219 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
221 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
222 if (controlLoopString == null) {
223 throw new FileNotFoundException(controlLoopFilePath);
226 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
227 controlLoopString, ToscaServiceTemplate.class);
228 return serviceTemplate;
229 } catch (FileNotFoundException e) {
230 LOGGER.error("cannot find YAML file", controlLoopFilePath);
231 throw new IllegalArgumentException(e);