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;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
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.ParticipantControlLoopStateChange;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
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.participant.intermediary.handler.ParticipantHandler;
43 import org.onap.policy.clamp.controlloop.participant.policy.main.handler.PolicyProvider;
44 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData;
45 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
50 import org.onap.policy.common.utils.resources.ResourceUtils;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public class TestListenerUtils {
59 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
60 private static final Coder CODER = new StandardCoder();
61 static CommonTestData commonTestData = new CommonTestData();
62 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
65 private static ParticipantHandler participantHandler;
67 private TestListenerUtils() {}
70 * Method to initialize participantHandler.
72 public static void initParticipantHandler() {
74 final ParticipantPolicyParameters participantParameters = commonTestData.toObject(
75 commonTestData.getParticipantPolicyParametersMap(CommonTestData.PARTICIPANT_GROUP_NAME),
76 ParticipantPolicyParameters.class);
78 PolicyProvider policyProvider =
79 new PolicyProvider(participantParameters.getIntermediaryParameters());
81 participantHandler = policyProvider.getIntermediaryApi().getParticipantHandler();
85 * Method to create a controlLoop from a yaml file.
87 * @return ControlLoop controlloop
89 public static ControlLoop createControlLoop() {
90 ControlLoop controlLoop = new ControlLoop();
91 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
92 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
93 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
94 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
95 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
96 ControlLoopElement clElement = new ControlLoopElement();
97 clElement.setId(UUID.randomUUID());
99 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
100 clElementParticipantId.setName(toscaInputEntry.getKey());
101 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
102 clElement.setParticipantId(clElementParticipantId);
104 clElement.setDefinition(clElementParticipantId);
105 clElement.setState(ControlLoopState.UNINITIALISED);
106 clElement.setDescription(toscaInputEntry.getValue().getDescription());
107 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
108 elements.put(clElement.getId(), clElement);
110 controlLoop.setElements(elements);
111 controlLoop.setName("PMSHInstance0");
112 controlLoop.setVersion("1.0.0");
114 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
115 definition.setName("PMSHInstance0");
116 definition.setVersion("1.0.0");
117 controlLoop.setDefinition(definition);
123 * Method to create ParticipantStateChange message from the arguments passed.
125 * @param participantState participant State
127 * @return ParticipantStateChange message
129 public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
130 final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
131 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
132 participantId.setName("org.onap.PM_Policy");
133 participantId.setVersion("0.0.0");
135 participantStateChangeMsg.setParticipantId(participantId);
136 participantStateChangeMsg.setTimestamp(Instant.now());
137 participantStateChangeMsg.setState(participantState);
139 return participantStateChangeMsg;
143 * Method to create ControlLoopStateChange message from the arguments passed.
145 * @param controlLoopOrderedState controlLoopOrderedState
147 * @return ParticipantControlLoopStateChange message
149 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
150 final ControlLoopOrderedState controlLoopOrderedState) {
151 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
153 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
154 controlLoopId.setName("PMSHInstance0");
155 controlLoopId.setVersion("1.0.0");
157 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
158 participantId.setName("org.onap.PM_Policy");
159 participantId.setVersion("0.0.0");
161 participantClStateChangeMsg.setControlLoopId(controlLoopId);
162 participantClStateChangeMsg.setParticipantId(participantId);
163 participantClStateChangeMsg.setTimestamp(Instant.now());
164 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
166 return participantClStateChangeMsg;
170 * Method to create ControlLoopUpdateMsg.
172 * @return ParticipantControlLoopUpdate message
174 public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
175 final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
176 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
177 controlLoopId.setName("PMSHInstance0");
178 controlLoopId.setVersion("1.0.0");
180 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
181 participantId.setName("org.onap.PM_Policy");
182 participantId.setVersion("0.0.0");
184 clUpdateMsg.setControlLoopId(controlLoopId);
185 clUpdateMsg.setParticipantId(participantId);
187 ControlLoop controlLoop = new ControlLoop();
188 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
189 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
190 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
191 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
192 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
193 ControlLoopElement clElement = new ControlLoopElement();
194 clElement.setId(UUID.randomUUID());
196 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
197 clElementParticipantId.setName(toscaInputEntry.getKey());
198 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
199 clElement.setParticipantId(clElementParticipantId);
201 clElement.setDefinition(clElementParticipantId);
202 clElement.setState(ControlLoopState.UNINITIALISED);
203 clElement.setDescription(toscaInputEntry.getValue().getDescription());
204 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
205 elements.put(clElement.getId(), clElement);
207 controlLoop.setElements(elements);
208 controlLoop.setName("PMSHInstance0");
209 controlLoop.setVersion("1.0.0");
210 controlLoop.setDefinition(controlLoopId);
211 clUpdateMsg.setControlLoop(controlLoop);
212 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
218 * Method to create ParticipantHealthCheck message.
220 * @return ParticipantHealthCheck message
222 public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
223 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
224 participantId.setName("org.onap.PM_Policy");
225 participantId.setVersion("0.0.0");
227 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
228 controlLoopId.setName("PMSHInstance0");
229 controlLoopId.setVersion("1.0.0");
231 final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
232 participantHealthCheckMsg.setParticipantId(participantId);
233 participantHealthCheckMsg.setControlLoopId(controlLoopId);
234 participantHealthCheckMsg.setTimestamp(Instant.now());
235 participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
237 return participantHealthCheckMsg;
241 * Method to create ParticipantControlLoopUpdate using the arguments passed.
243 * @param jsonFilePath the path of the controlloop content
245 * @return ParticipantControlLoopUpdate message
246 * @throws CoderException exception while reading the file to object
248 public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
249 throws CoderException {
250 ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
251 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
252 return participantControlLoopUpdateMsg;
255 private static ToscaServiceTemplate testControlLoopRead() {
256 Set<String> controlLoopDirectoryContents =
257 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
259 boolean atLeastOneControlLoopTested = false;
260 ToscaServiceTemplate toscaServiceTemplate = null;
262 for (String controlLoopFilePath : controlLoopDirectoryContents) {
263 if (!controlLoopFilePath.endsWith(".yaml")) {
266 atLeastOneControlLoopTested = true;
267 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
270 // Add policy_types to the toscaServiceTemplate
271 addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
273 assertTrue(atLeastOneControlLoopTested);
274 return toscaServiceTemplate;
277 private static void addPolicyTypesToToscaServiceTemplate(
278 ToscaServiceTemplate toscaServiceTemplate) {
279 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
281 for (String policyTypeFilePath : policyTypeDirectoryContents) {
282 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
284 ToscaServiceTemplate foundPolicyTypeSt =
285 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
287 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
288 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
289 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
290 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
291 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
292 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
294 if (foundPolicyTypeSt.getDataTypes() != null) {
295 if (toscaServiceTemplate.getDataTypes() == null) {
296 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
298 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
302 if (toscaServiceTemplate.getPolicyTypes() == null) {
303 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
305 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
311 * Method to add polcies to the toscaServiceTemplate.
313 * @param toscaServiceTemplate to add policies
315 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
316 Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
318 for (String policiesFilePath : policiesDirectoryContents) {
319 String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
321 ToscaServiceTemplate foundPoliciesSt =
322 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
323 toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
324 foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
328 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
330 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
331 if (controlLoopString == null) {
332 throw new FileNotFoundException(controlLoopFilePath);
335 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
336 controlLoopString, ToscaServiceTemplate.class);
337 return serviceTemplate;
338 } catch (FileNotFoundException e) {
339 LOGGER.error("cannot find YAML file", controlLoopFilePath);
340 throw new IllegalArgumentException(e);