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.ControlLoopStateChange;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
42 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
47 import org.onap.policy.common.utils.resources.ResourceUtils;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class TestListenerUtils {
56 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
57 private static final Coder CODER = new StandardCoder();
58 static CommonTestData commonTestData = new CommonTestData();
59 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
61 private TestListenerUtils() {}
64 * Method to create a controlLoop from a yaml file.
66 * @return ControlLoop controlloop
68 public static ControlLoop createControlLoop() {
69 ControlLoop controlLoop = new ControlLoop();
70 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
71 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
72 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
73 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
74 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
75 ControlLoopElement clElement = new ControlLoopElement();
76 clElement.setId(UUID.randomUUID());
78 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
79 clElementParticipantId.setName(toscaInputEntry.getKey());
80 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
81 clElement.setParticipantId(clElementParticipantId);
83 clElement.setDefinition(clElementParticipantId);
84 clElement.setState(ControlLoopState.UNINITIALISED);
85 clElement.setDescription(toscaInputEntry.getValue().getDescription());
86 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
87 elements.put(clElement.getId(), clElement);
89 controlLoop.setElements(elements);
90 controlLoop.setName("PMSHInstance0");
91 controlLoop.setVersion("1.0.0");
93 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
94 definition.setName("PMSHInstance0");
95 definition.setVersion("1.0.0");
96 controlLoop.setDefinition(definition);
102 * Method to create ControlLoopStateChange message from the arguments passed.
104 * @param controlLoopOrderedState controlLoopOrderedState
106 * @return ControlLoopStateChange message
108 public static ControlLoopStateChange createControlLoopStateChangeMsg(
109 final ControlLoopOrderedState controlLoopOrderedState) {
110 final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
112 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
113 controlLoopId.setName("PMSHInstance0");
114 controlLoopId.setVersion("1.0.0");
116 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
117 participantId.setName("org.onap.PM_Policy");
118 participantId.setVersion("0.0.0");
120 clStateChangeMsg.setControlLoopId(controlLoopId);
121 clStateChangeMsg.setParticipantId(participantId);
122 clStateChangeMsg.setTimestamp(Instant.now());
123 clStateChangeMsg.setOrderedState(controlLoopOrderedState);
125 return clStateChangeMsg;
129 * Method to create ControlLoopUpdateMsg.
131 * @return ControlLoopUpdate message
133 public static ControlLoopUpdate createControlLoopUpdateMsg() {
134 final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
135 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
136 controlLoopId.setName("PMSHInstance0");
137 controlLoopId.setVersion("1.0.0");
139 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
140 participantId.setName("org.onap.PM_Policy");
141 participantId.setVersion("0.0.0");
143 clUpdateMsg.setControlLoopId(controlLoopId);
144 clUpdateMsg.setParticipantId(participantId);
146 ControlLoop controlLoop = new ControlLoop();
147 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
148 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
149 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
150 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
151 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
152 ControlLoopElement clElement = new ControlLoopElement();
153 clElement.setId(UUID.randomUUID());
155 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
156 clElementParticipantId.setName(toscaInputEntry.getKey());
157 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
158 clElement.setParticipantId(clElementParticipantId);
160 clElement.setDefinition(clElementParticipantId);
161 clElement.setState(ControlLoopState.UNINITIALISED);
162 clElement.setDescription(toscaInputEntry.getValue().getDescription());
163 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
164 elements.put(clElement.getId(), clElement);
166 controlLoop.setElements(elements);
167 controlLoop.setName("PMSHInstance0");
168 controlLoop.setVersion("1.0.0");
169 controlLoop.setDefinition(controlLoopId);
170 clUpdateMsg.setControlLoop(controlLoop);
171 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
177 * Method to create participantUpdateMsg.
179 * @return ParticipantUpdate message
181 public static ParticipantUpdate createParticipantUpdateMsg() {
182 final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
183 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
184 ToscaConceptIdentifier participantType = new ToscaConceptIdentifier(
185 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
187 participantUpdateMsg.setParticipantId(participantId);
188 participantUpdateMsg.setTimestamp(Instant.now());
189 participantUpdateMsg.setParticipantType(participantType);
190 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
191 participantUpdateMsg.setMessageId(UUID.randomUUID());
193 ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
194 toscaServiceTemplate.setName("serviceTemplate");
195 toscaServiceTemplate.setDerivedFrom("parentServiceTemplate");
196 toscaServiceTemplate.setDescription("Description of serviceTemplate");
197 toscaServiceTemplate.setVersion("1.2.3");
199 ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
200 clDefinition.setId(UUID.randomUUID());
201 clDefinition.setControlLoopElementToscaServiceTemplate(toscaServiceTemplate);
202 Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
203 clDefinition.setCommonPropertiesMap(commonPropertiesMap);
205 Map<UUID, ControlLoopElementDefinition> controlLoopElementDefinitionMap =
206 Map.of(UUID.randomUUID(), clDefinition);
208 Map<ToscaConceptIdentifier, Map<UUID, ControlLoopElementDefinition>>
209 participantDefinitionUpdateMap = Map.of(participantId, controlLoopElementDefinitionMap);
210 participantUpdateMsg.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap);
212 return participantUpdateMsg;
216 * Method to create ParticipantHealthCheck message.
218 * @return ParticipantHealthCheck message
220 public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
221 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
222 participantId.setName("org.onap.PM_Policy");
223 participantId.setVersion("0.0.0");
225 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
226 controlLoopId.setName("PMSHInstance0");
227 controlLoopId.setVersion("1.0.0");
229 final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
230 participantHealthCheckMsg.setParticipantId(participantId);
231 participantHealthCheckMsg.setControlLoopId(controlLoopId);
232 participantHealthCheckMsg.setTimestamp(Instant.now());
233 participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
235 return participantHealthCheckMsg;
239 * Method to create ControlLoopUpdate using the arguments passed.
241 * @param jsonFilePath the path of the controlloop content
243 * @return ControlLoopUpdate message
244 * @throws CoderException exception while reading the file to object
246 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
247 throws CoderException {
248 ControlLoopUpdate controlLoopUpdateMsg =
249 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
250 return controlLoopUpdateMsg;
253 private static ToscaServiceTemplate testControlLoopRead() {
254 Set<String> controlLoopDirectoryContents =
255 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
257 boolean atLeastOneControlLoopTested = false;
258 ToscaServiceTemplate toscaServiceTemplate = null;
260 for (String controlLoopFilePath : controlLoopDirectoryContents) {
261 if (!controlLoopFilePath.endsWith(".yaml")) {
264 atLeastOneControlLoopTested = true;
265 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
268 // Add policy_types to the toscaServiceTemplate
269 addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
271 assertTrue(atLeastOneControlLoopTested);
272 return toscaServiceTemplate;
275 private static void addPolicyTypesToToscaServiceTemplate(
276 ToscaServiceTemplate toscaServiceTemplate) {
277 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
279 for (String policyTypeFilePath : policyTypeDirectoryContents) {
280 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
282 ToscaServiceTemplate foundPolicyTypeSt =
283 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
285 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
286 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
287 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
288 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
289 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
290 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
292 if (foundPolicyTypeSt.getDataTypes() != null) {
293 if (toscaServiceTemplate.getDataTypes() == null) {
294 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
296 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
300 if (toscaServiceTemplate.getPolicyTypes() == null) {
301 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
303 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
309 * Method to add polcies to the toscaServiceTemplate.
311 * @param toscaServiceTemplate to add policies
313 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
314 Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
316 for (String policiesFilePath : policiesDirectoryContents) {
317 String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
319 ToscaServiceTemplate foundPoliciesSt =
320 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
321 toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
322 foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
326 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
328 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
329 if (controlLoopString == null) {
330 throw new FileNotFoundException(controlLoopFilePath);
333 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
334 controlLoopString, ToscaServiceTemplate.class);
335 return serviceTemplate;
336 } catch (FileNotFoundException e) {
337 LOGGER.error("cannot find YAML file", controlLoopFilePath);
338 throw new IllegalArgumentException(e);