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.ParticipantUpdate;
41 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData;
42 import org.onap.policy.common.utils.coder.Coder;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
46 import org.onap.policy.common.utils.resources.ResourceUtils;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class TestListenerUtils {
55 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
56 private static final Coder CODER = new StandardCoder();
57 static CommonTestData commonTestData = new CommonTestData();
58 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
60 private TestListenerUtils() {}
63 * Method to create a controlLoop from a yaml file.
65 * @return ControlLoop controlloop
67 public static ControlLoop createControlLoop() {
68 ControlLoop controlLoop = new ControlLoop();
69 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
70 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
71 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
72 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
73 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
74 ControlLoopElement clElement = new ControlLoopElement();
75 clElement.setId(UUID.randomUUID());
77 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
78 clElementParticipantId.setName(toscaInputEntry.getKey());
79 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
80 clElement.setParticipantId(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 ControlLoopStateChange message from the arguments passed.
103 * @param controlLoopOrderedState controlLoopOrderedState
105 * @return ControlLoopStateChange message
107 public static ControlLoopStateChange createControlLoopStateChangeMsg(
108 final ControlLoopOrderedState controlLoopOrderedState) {
109 final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
111 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
112 controlLoopId.setName("PMSHInstance0");
113 controlLoopId.setVersion("1.0.0");
115 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
116 participantId.setName("org.onap.PM_Policy");
117 participantId.setVersion("0.0.0");
119 clStateChangeMsg.setControlLoopId(controlLoopId);
120 clStateChangeMsg.setParticipantId(participantId);
121 clStateChangeMsg.setTimestamp(Instant.now());
122 clStateChangeMsg.setOrderedState(controlLoopOrderedState);
124 return clStateChangeMsg;
128 * Method to create ControlLoopUpdateMsg.
130 * @return ControlLoopUpdate message
132 public static ControlLoopUpdate createControlLoopUpdateMsg() {
133 final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
134 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
135 controlLoopId.setName("PMSHInstance0");
136 controlLoopId.setVersion("1.0.0");
138 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
139 participantId.setName("org.onap.PM_Policy");
140 participantId.setVersion("0.0.0");
142 clUpdateMsg.setControlLoopId(controlLoopId);
143 clUpdateMsg.setParticipantId(participantId);
145 ControlLoop controlLoop = new ControlLoop();
146 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
147 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
148 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
149 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
150 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
151 ControlLoopElement clElement = new ControlLoopElement();
152 clElement.setId(UUID.randomUUID());
154 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
155 clElementParticipantId.setName(toscaInputEntry.getKey());
156 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
157 clElement.setParticipantId(clElementParticipantId);
158 clElement.setParticipantType(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 ControlLoopUpdate using the arguments passed.
215 * @param jsonFilePath the path of the controlloop content
217 * @return ControlLoopUpdate message
218 * @throws CoderException exception while reading the file to object
220 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
221 throws CoderException {
222 ControlLoopUpdate controlLoopUpdateMsg =
223 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
224 return controlLoopUpdateMsg;
227 private static ToscaServiceTemplate testControlLoopRead() {
228 Set<String> controlLoopDirectoryContents =
229 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
231 boolean atLeastOneControlLoopTested = false;
232 ToscaServiceTemplate toscaServiceTemplate = null;
234 for (String controlLoopFilePath : controlLoopDirectoryContents) {
235 if (!controlLoopFilePath.endsWith(".yaml")) {
238 atLeastOneControlLoopTested = true;
239 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
242 // Add policy_types to the toscaServiceTemplate
243 addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
245 assertTrue(atLeastOneControlLoopTested);
246 return toscaServiceTemplate;
249 private static void addPolicyTypesToToscaServiceTemplate(
250 ToscaServiceTemplate toscaServiceTemplate) {
251 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
253 for (String policyTypeFilePath : policyTypeDirectoryContents) {
254 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
256 ToscaServiceTemplate foundPolicyTypeSt =
257 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
259 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
260 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
261 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
262 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
263 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
264 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
266 if (foundPolicyTypeSt.getDataTypes() != null) {
267 if (toscaServiceTemplate.getDataTypes() == null) {
268 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
270 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
274 if (toscaServiceTemplate.getPolicyTypes() == null) {
275 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
277 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
283 * Method to add polcies to the toscaServiceTemplate.
285 * @param toscaServiceTemplate to add policies
287 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
288 Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
290 for (String policiesFilePath : policiesDirectoryContents) {
291 String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
293 ToscaServiceTemplate foundPoliciesSt =
294 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
295 toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
296 foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
300 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
302 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
303 if (controlLoopString == null) {
304 throw new FileNotFoundException(controlLoopFilePath);
307 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
308 controlLoopString, ToscaServiceTemplate.class);
309 return serviceTemplate;
310 } catch (FileNotFoundException e) {
311 LOGGER.error("cannot find YAML file", controlLoopFilePath);
312 throw new IllegalArgumentException(e);