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);
176 * Method to create participantUpdateMsg.
178 * @return ParticipantUpdate message
180 public static ParticipantUpdate createParticipantUpdateMsg() {
181 final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
182 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
183 ToscaConceptIdentifier participantType = new ToscaConceptIdentifier(
184 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
186 participantUpdateMsg.setParticipantId(participantId);
187 participantUpdateMsg.setTimestamp(Instant.now());
188 participantUpdateMsg.setParticipantType(participantType);
189 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
190 participantUpdateMsg.setMessageId(UUID.randomUUID());
192 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
193 // Add policies to the toscaServiceTemplate
194 TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
196 ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
197 clDefinition.setId(UUID.randomUUID());
198 clDefinition.setControlLoopElementToscaServiceTemplate(toscaServiceTemplate);
199 Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
200 clDefinition.setCommonPropertiesMap(commonPropertiesMap);
202 Map<UUID, ControlLoopElementDefinition> controlLoopElementDefinitionMap =
203 Map.of(UUID.randomUUID(), clDefinition);
205 Map<ToscaConceptIdentifier, Map<UUID, ControlLoopElementDefinition>>
206 participantDefinitionUpdateMap = Map.of(participantId, controlLoopElementDefinitionMap);
207 participantUpdateMsg.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap);
209 return participantUpdateMsg;
213 * Method to create ParticipantHealthCheck message.
215 * @return ParticipantHealthCheck message
217 public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
218 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
219 participantId.setName("org.onap.PM_Policy");
220 participantId.setVersion("0.0.0");
222 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
223 controlLoopId.setName("PMSHInstance0");
224 controlLoopId.setVersion("1.0.0");
226 final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
227 participantHealthCheckMsg.setParticipantId(participantId);
228 participantHealthCheckMsg.setControlLoopId(controlLoopId);
229 participantHealthCheckMsg.setTimestamp(Instant.now());
230 participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
232 return participantHealthCheckMsg;
236 * Method to create ControlLoopUpdate using the arguments passed.
238 * @param jsonFilePath the path of the controlloop content
240 * @return ControlLoopUpdate message
241 * @throws CoderException exception while reading the file to object
243 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
244 throws CoderException {
245 ControlLoopUpdate controlLoopUpdateMsg =
246 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
247 return controlLoopUpdateMsg;
250 private static ToscaServiceTemplate testControlLoopRead() {
251 Set<String> controlLoopDirectoryContents =
252 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
254 boolean atLeastOneControlLoopTested = false;
255 ToscaServiceTemplate toscaServiceTemplate = null;
257 for (String controlLoopFilePath : controlLoopDirectoryContents) {
258 if (!controlLoopFilePath.endsWith(".yaml")) {
261 atLeastOneControlLoopTested = true;
262 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
265 // Add policy_types to the toscaServiceTemplate
266 addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
268 assertTrue(atLeastOneControlLoopTested);
269 return toscaServiceTemplate;
272 private static void addPolicyTypesToToscaServiceTemplate(
273 ToscaServiceTemplate toscaServiceTemplate) {
274 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
276 for (String policyTypeFilePath : policyTypeDirectoryContents) {
277 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
279 ToscaServiceTemplate foundPolicyTypeSt =
280 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
282 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
283 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
284 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
285 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
286 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
287 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
289 if (foundPolicyTypeSt.getDataTypes() != null) {
290 if (toscaServiceTemplate.getDataTypes() == null) {
291 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
293 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
297 if (toscaServiceTemplate.getPolicyTypes() == null) {
298 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
300 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
306 * Method to add polcies to the toscaServiceTemplate.
308 * @param toscaServiceTemplate to add policies
310 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
311 Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
313 for (String policiesFilePath : policiesDirectoryContents) {
314 String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
316 ToscaServiceTemplate foundPoliciesSt =
317 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
318 toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
319 foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
323 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
325 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
326 if (controlLoopString == null) {
327 throw new FileNotFoundException(controlLoopFilePath);
330 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
331 controlLoopString, ToscaServiceTemplate.class);
332 return serviceTemplate;
333 } catch (FileNotFoundException e) {
334 LOGGER.error("cannot find YAML file", controlLoopFilePath);
335 throw new IllegalArgumentException(e);