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.policy.main.utils;
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.ControlLoopElementDefinition;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
43 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData;
44 import org.onap.policy.common.utils.coder.Coder;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.common.utils.coder.StandardCoder;
47 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
48 import org.onap.policy.common.utils.resources.ResourceUtils;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class TestListenerUtils {
57 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
58 private static final Coder CODER = new StandardCoder();
59 static CommonTestData commonTestData = new CommonTestData();
60 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
62 private TestListenerUtils() {}
65 * Method to create a controlLoop from a yaml file.
67 * @return ControlLoop controlloop
69 public static ControlLoop createControlLoop() {
70 ControlLoop controlLoop = new ControlLoop();
71 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
72 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
73 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
74 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
75 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
76 ControlLoopElement clElement = new ControlLoopElement();
77 clElement.setId(UUID.randomUUID());
79 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
80 clElementParticipantId.setName(toscaInputEntry.getKey());
81 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
82 clElement.setParticipantId(clElementParticipantId);
84 clElement.setDefinition(clElementParticipantId);
85 clElement.setState(ControlLoopState.UNINITIALISED);
86 clElement.setDescription(toscaInputEntry.getValue().getDescription());
87 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
88 elements.put(clElement.getId(), clElement);
90 controlLoop.setElements(elements);
91 controlLoop.setName("PMSHInstance0");
92 controlLoop.setVersion("1.0.0");
94 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
95 definition.setName("PMSHInstance0");
96 definition.setVersion("1.0.0");
97 controlLoop.setDefinition(definition);
103 * Method to create ParticipantStateChange message from the arguments passed.
105 * @param participantState participant State
107 * @return ParticipantStateChange message
109 public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
110 final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
111 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
112 participantId.setName("org.onap.PM_Policy");
113 participantId.setVersion("0.0.0");
115 participantStateChangeMsg.setParticipantId(participantId);
116 participantStateChangeMsg.setTimestamp(Instant.now());
117 participantStateChangeMsg.setState(participantState);
119 return participantStateChangeMsg;
123 * Method to create ControlLoopStateChange message from the arguments passed.
125 * @param controlLoopOrderedState controlLoopOrderedState
127 * @return ParticipantControlLoopStateChange message
129 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
130 final ControlLoopOrderedState controlLoopOrderedState) {
131 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
133 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
134 controlLoopId.setName("PMSHInstance0");
135 controlLoopId.setVersion("1.0.0");
137 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
138 participantId.setName("org.onap.PM_Policy");
139 participantId.setVersion("0.0.0");
141 participantClStateChangeMsg.setControlLoopId(controlLoopId);
142 participantClStateChangeMsg.setParticipantId(participantId);
143 participantClStateChangeMsg.setTimestamp(Instant.now());
144 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
146 return participantClStateChangeMsg;
150 * Method to create ControlLoopUpdateMsg.
152 * @return ControlLoopUpdate message
154 public static ControlLoopUpdate createControlLoopUpdateMsg() {
155 final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
156 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
157 controlLoopId.setName("PMSHInstance0");
158 controlLoopId.setVersion("1.0.0");
160 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
161 participantId.setName("org.onap.PM_Policy");
162 participantId.setVersion("0.0.0");
164 clUpdateMsg.setControlLoopId(controlLoopId);
165 clUpdateMsg.setParticipantId(participantId);
167 ControlLoop controlLoop = new ControlLoop();
168 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
169 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
170 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
171 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
172 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
173 ControlLoopElement clElement = new ControlLoopElement();
174 clElement.setId(UUID.randomUUID());
176 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
177 clElementParticipantId.setName(toscaInputEntry.getKey());
178 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
179 clElement.setParticipantId(clElementParticipantId);
181 clElement.setDefinition(clElementParticipantId);
182 clElement.setState(ControlLoopState.UNINITIALISED);
183 clElement.setDescription(toscaInputEntry.getValue().getDescription());
184 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
185 elements.put(clElement.getId(), clElement);
187 controlLoop.setElements(elements);
188 controlLoop.setName("PMSHInstance0");
189 controlLoop.setVersion("1.0.0");
190 controlLoop.setDefinition(controlLoopId);
191 clUpdateMsg.setControlLoop(controlLoop);
192 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
198 * Method to create participantUpdateMsg.
200 * @return ParticipantUpdate message
202 public static ParticipantUpdate createParticipantUpdateMsg() {
203 final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
204 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
205 ToscaConceptIdentifier participantType = new ToscaConceptIdentifier(
206 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
208 participantUpdateMsg.setParticipantId(participantId);
209 participantUpdateMsg.setTimestamp(Instant.now());
210 participantUpdateMsg.setParticipantType(participantType);
211 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
212 participantUpdateMsg.setMessageId(UUID.randomUUID());
214 ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
215 toscaServiceTemplate.setName("serviceTemplate");
216 toscaServiceTemplate.setDerivedFrom("parentServiceTemplate");
217 toscaServiceTemplate.setDescription("Description of serviceTemplate");
218 toscaServiceTemplate.setVersion("1.2.3");
220 ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
221 clDefinition.setId(UUID.randomUUID());
222 clDefinition.setControlLoopElementToscaServiceTemplate(toscaServiceTemplate);
223 Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
224 clDefinition.setCommonPropertiesMap(commonPropertiesMap);
226 Map<UUID, ControlLoopElementDefinition> controlLoopElementDefinitionMap =
227 Map.of(UUID.randomUUID(), clDefinition);
229 Map<ToscaConceptIdentifier, Map<UUID, ControlLoopElementDefinition>>
230 participantDefinitionUpdateMap = Map.of(participantId, controlLoopElementDefinitionMap);
231 participantUpdateMsg.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap);
233 return participantUpdateMsg;
237 * Method to create ParticipantHealthCheck message.
239 * @return ParticipantHealthCheck message
241 public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
242 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
243 participantId.setName("org.onap.PM_Policy");
244 participantId.setVersion("0.0.0");
246 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
247 controlLoopId.setName("PMSHInstance0");
248 controlLoopId.setVersion("1.0.0");
250 final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
251 participantHealthCheckMsg.setParticipantId(participantId);
252 participantHealthCheckMsg.setControlLoopId(controlLoopId);
253 participantHealthCheckMsg.setTimestamp(Instant.now());
254 participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
256 return participantHealthCheckMsg;
260 * Method to create ControlLoopUpdate using the arguments passed.
262 * @param jsonFilePath the path of the controlloop content
264 * @return ControlLoopUpdate message
265 * @throws CoderException exception while reading the file to object
267 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
268 throws CoderException {
269 ControlLoopUpdate controlLoopUpdateMsg =
270 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
271 return controlLoopUpdateMsg;
274 private static ToscaServiceTemplate testControlLoopRead() {
275 Set<String> controlLoopDirectoryContents =
276 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
278 boolean atLeastOneControlLoopTested = false;
279 ToscaServiceTemplate toscaServiceTemplate = null;
281 for (String controlLoopFilePath : controlLoopDirectoryContents) {
282 if (!controlLoopFilePath.endsWith(".yaml")) {
285 atLeastOneControlLoopTested = true;
286 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
289 // Add policy_types to the toscaServiceTemplate
290 addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
292 assertTrue(atLeastOneControlLoopTested);
293 return toscaServiceTemplate;
296 private static void addPolicyTypesToToscaServiceTemplate(
297 ToscaServiceTemplate toscaServiceTemplate) {
298 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
300 for (String policyTypeFilePath : policyTypeDirectoryContents) {
301 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
303 ToscaServiceTemplate foundPolicyTypeSt =
304 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
306 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
307 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
308 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
309 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
310 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
311 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
313 if (foundPolicyTypeSt.getDataTypes() != null) {
314 if (toscaServiceTemplate.getDataTypes() == null) {
315 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
317 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
321 if (toscaServiceTemplate.getPolicyTypes() == null) {
322 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
324 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
330 * Method to add polcies to the toscaServiceTemplate.
332 * @param toscaServiceTemplate to add policies
334 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
335 Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
337 for (String policiesFilePath : policiesDirectoryContents) {
338 String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
340 ToscaServiceTemplate foundPoliciesSt =
341 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
342 toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
343 foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
347 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
349 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
350 if (controlLoopString == null) {
351 throw new FileNotFoundException(controlLoopFilePath);
354 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
355 controlLoopString, ToscaServiceTemplate.class);
356 return serviceTemplate;
357 } catch (FileNotFoundException e) {
358 LOGGER.error("cannot find YAML file", controlLoopFilePath);
359 throw new IllegalArgumentException(e);