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.messages.dmaap.participant.ControlLoopStateChange;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
40 import org.onap.policy.clamp.controlloop.participant.policy.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);
81 clElement.setDefinition(clElementParticipantId);
82 clElement.setState(ControlLoopState.UNINITIALISED);
83 clElement.setDescription(toscaInputEntry.getValue().getDescription());
84 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
85 elements.put(clElement.getId(), clElement);
87 controlLoop.setElements(elements);
88 controlLoop.setName("PMSHInstance0");
89 controlLoop.setVersion("1.0.0");
91 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
92 definition.setName("PMSHInstance0");
93 definition.setVersion("1.0.0");
94 controlLoop.setDefinition(definition);
100 * Method to create ControlLoopStateChange message from the arguments passed.
102 * @param controlLoopOrderedState controlLoopOrderedState
104 * @return ControlLoopStateChange message
106 public static ControlLoopStateChange createControlLoopStateChangeMsg(
107 final ControlLoopOrderedState controlLoopOrderedState) {
108 final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
110 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
111 controlLoopId.setName("PMSHInstance0");
112 controlLoopId.setVersion("1.0.0");
114 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
115 participantId.setName("org.onap.PM_Policy");
116 participantId.setVersion("0.0.0");
118 clStateChangeMsg.setControlLoopId(controlLoopId);
119 clStateChangeMsg.setParticipantId(participantId);
120 clStateChangeMsg.setTimestamp(Instant.now());
121 clStateChangeMsg.setOrderedState(controlLoopOrderedState);
123 return clStateChangeMsg;
127 * Method to create ControlLoopUpdateMsg.
129 * @return ControlLoopUpdate message
131 public static ControlLoopUpdate createControlLoopUpdateMsg() {
132 final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
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 clUpdateMsg.setControlLoopId(controlLoopId);
142 clUpdateMsg.setParticipantId(participantId);
144 ControlLoop controlLoop = new ControlLoop();
145 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
146 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
147 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
148 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
149 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
150 ControlLoopElement clElement = new ControlLoopElement();
151 clElement.setId(UUID.randomUUID());
153 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
154 clElementParticipantId.setName(toscaInputEntry.getKey());
155 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
156 clElement.setParticipantId(clElementParticipantId);
157 clElement.setParticipantType(clElementParticipantId);
159 clElement.setDefinition(clElementParticipantId);
160 clElement.setState(ControlLoopState.UNINITIALISED);
161 clElement.setDescription(toscaInputEntry.getValue().getDescription());
162 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
163 elements.put(clElement.getId(), clElement);
165 controlLoop.setElements(elements);
166 controlLoop.setName("PMSHInstance0");
167 controlLoop.setVersion("1.0.0");
168 controlLoop.setDefinition(controlLoopId);
169 clUpdateMsg.setControlLoop(controlLoop);
175 * Method to create participantUpdateMsg.
177 * @return ParticipantUpdate message
179 public static ParticipantUpdate createParticipantUpdateMsg() {
180 final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
181 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
182 ToscaConceptIdentifier participantType = new ToscaConceptIdentifier(
183 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
185 participantUpdateMsg.setParticipantId(participantId);
186 participantUpdateMsg.setTimestamp(Instant.now());
187 participantUpdateMsg.setParticipantType(participantType);
188 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
189 participantUpdateMsg.setMessageId(UUID.randomUUID());
191 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
192 // Add policies to the toscaServiceTemplate
193 TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
195 ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
196 clDefinition.setId(UUID.randomUUID());
197 clDefinition.setControlLoopElementToscaServiceTemplate(toscaServiceTemplate);
198 Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
199 clDefinition.setCommonPropertiesMap(commonPropertiesMap);
201 Map<UUID, ControlLoopElementDefinition> controlLoopElementDefinitionMap =
202 Map.of(UUID.randomUUID(), clDefinition);
204 Map<String, Map<UUID, ControlLoopElementDefinition>> participantDefinitionUpdateMap =
205 Map.of(participantId.toString(), controlLoopElementDefinitionMap);
206 participantUpdateMsg.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap);
208 return participantUpdateMsg;
212 * Method to create ControlLoopUpdate using the arguments passed.
214 * @param jsonFilePath the path of the controlloop content
216 * @return ControlLoopUpdate message
217 * @throws CoderException exception while reading the file to object
219 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) throws CoderException {
220 ControlLoopUpdate controlLoopUpdateMsg = CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
221 return controlLoopUpdateMsg;
224 private static ToscaServiceTemplate testControlLoopRead() {
225 Set<String> controlLoopDirectoryContents =
226 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
228 boolean atLeastOneControlLoopTested = false;
229 ToscaServiceTemplate toscaServiceTemplate = null;
231 for (String controlLoopFilePath : controlLoopDirectoryContents) {
232 if (!controlLoopFilePath.endsWith(".yaml")) {
235 atLeastOneControlLoopTested = true;
236 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
239 // Add policy_types to the toscaServiceTemplate
240 addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
242 assertTrue(atLeastOneControlLoopTested);
243 return toscaServiceTemplate;
246 private static void addPolicyTypesToToscaServiceTemplate(
247 ToscaServiceTemplate toscaServiceTemplate) {
248 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
250 for (String policyTypeFilePath : policyTypeDirectoryContents) {
251 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
253 ToscaServiceTemplate foundPolicyTypeSt =
254 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
256 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
257 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
258 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
259 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
260 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
261 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
263 if (foundPolicyTypeSt.getDataTypes() != null) {
264 if (toscaServiceTemplate.getDataTypes() == null) {
265 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
267 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
271 if (toscaServiceTemplate.getPolicyTypes() == null) {
272 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
274 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
280 * Method to add polcies to the toscaServiceTemplate.
282 * @param toscaServiceTemplate to add policies
284 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
285 Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
287 for (String policiesFilePath : policiesDirectoryContents) {
288 String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
290 ToscaServiceTemplate foundPoliciesSt =
291 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
292 toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
293 foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
297 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
299 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
300 if (controlLoopString == null) {
301 throw new FileNotFoundException(controlLoopFilePath);
304 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
305 controlLoopString, ToscaServiceTemplate.class);
306 return serviceTemplate;
307 } catch (FileNotFoundException e) {
308 LOGGER.error("cannot find YAML file", controlLoopFilePath);
309 throw new IllegalArgumentException(e);