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.ArrayList;
29 import java.util.List;
32 import java.util.UUID;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
43 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
44 import org.onap.policy.clamp.controlloop.participant.policy.main.handler.PolicyProvider;
45 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData;
46 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters;
47 import org.onap.policy.common.utils.coder.Coder;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
51 import org.onap.policy.common.utils.resources.ResourceUtils;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 public class TestListenerUtils {
60 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
61 private static final Coder CODER = new StandardCoder();
62 static CommonTestData commonTestData = new CommonTestData();
63 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
66 private static ParticipantHandler participantHandler;
68 private TestListenerUtils() {}
71 * Initializes participantHandler.
73 public static void initParticipantHandler() {
75 final ParticipantPolicyParameters participantParameters = commonTestData.toObject(
76 commonTestData.getParticipantPolicyParametersMap(CommonTestData.PARTICIPANT_GROUP_NAME),
77 ParticipantPolicyParameters.class);
79 PolicyProvider policyProvider =
80 new PolicyProvider(participantParameters.getIntermediaryParameters());
82 participantHandler = policyProvider.getIntermediaryApi().getParticipantHandler();
86 * Method to create a controlLoop from a yaml file.
88 * @return ControlLoop controlloop
90 public static ControlLoop createControlLoop() {
91 ControlLoop controlLoop = new ControlLoop();
92 List<ControlLoopElement> elements = new ArrayList<>();
93 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead("src/test/resources/utils/servicetemplates");
94 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
95 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
96 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
97 ControlLoopElement clElement = new ControlLoopElement();
98 clElement.setId(UUID.randomUUID());
100 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
101 clElementParticipantId.setName(toscaInputEntry.getKey());
102 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
103 clElement.setParticipantId(clElementParticipantId);
105 clElement.setDefinition(clElementParticipantId);
106 clElement.setState(ControlLoopState.UNINITIALISED);
107 clElement.setDescription(toscaInputEntry.getValue().getDescription());
108 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
109 elements.add(clElement);
111 controlLoop.setElements(elements);
112 controlLoop.setName("PMSHInstance0");
113 controlLoop.setVersion("1.0.0");
115 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
116 definition.setName("PMSHInstance0");
117 definition.setVersion("1.0.0");
118 controlLoop.setDefinition(definition);
124 * Method to create ParticipantStateChange message from the arguments passed.
126 * @param participantState participant State
128 * @return ParticipantStateChange message
130 public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
131 final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
132 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
133 participantId.setName("org.onap.PM_Policy");
134 participantId.setVersion("0.0.0");
136 participantStateChangeMsg.setParticipantId(participantId);
137 participantStateChangeMsg.setTimestamp(Instant.now());
138 participantStateChangeMsg.setState(participantState);
140 return participantStateChangeMsg;
144 * Method to create ControlLoopStateChange message from the arguments passed.
146 * @param controlLoopOrderedState controlLoopOrderedState
148 * @return ParticipantControlLoopStateChange message
150 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
151 final ControlLoopOrderedState controlLoopOrderedState) {
152 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
154 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
155 controlLoopId.setName("PMSHInstance0");
156 controlLoopId.setVersion("1.0.0");
158 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
159 participantId.setName("org.onap.PM_Policy");
160 participantId.setVersion("0.0.0");
162 participantClStateChangeMsg.setControlLoopId(controlLoopId);
163 participantClStateChangeMsg.setParticipantId(participantId);
164 participantClStateChangeMsg.setTimestamp(Instant.now());
165 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
167 return participantClStateChangeMsg;
171 * Method to create ControlLoopUpdateMsg.
173 * @return ParticipantControlLoopUpdate message
175 public static ParticipantControlLoopUpdate createControlLoopUpdateMsg(final String inputDirPath) {
176 final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
177 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
178 controlLoopId.setName("PMSHInstance0");
179 controlLoopId.setVersion("1.0.0");
181 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
182 participantId.setName("org.onap.PM_Policy");
183 participantId.setVersion("0.0.0");
185 clUpdateMsg.setControlLoopId(controlLoopId);
186 clUpdateMsg.setParticipantId(participantId);
188 ControlLoop controlLoop = new ControlLoop();
189 List<ControlLoopElement> elements = new ArrayList<>();
190 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead(inputDirPath);
191 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
192 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
193 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
194 ControlLoopElement clElement = new ControlLoopElement();
195 clElement.setId(UUID.randomUUID());
197 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
198 clElementParticipantId.setName(toscaInputEntry.getKey());
199 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
200 clElement.setParticipantId(clElementParticipantId);
202 clElement.setDefinition(clElementParticipantId);
203 clElement.setState(ControlLoopState.UNINITIALISED);
204 clElement.setDescription(toscaInputEntry.getValue().getDescription());
205 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
206 elements.add(clElement);
208 controlLoop.setElements(elements);
209 controlLoop.setName("PMSHInstance0");
210 controlLoop.setVersion("1.0.0");
211 controlLoop.setDefinition(controlLoopId);
212 clUpdateMsg.setControlLoop(controlLoop);
213 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
219 * Method to create ParticipantHealthCheck message.
221 * @return ParticipantHealthCheck message
223 public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
224 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
225 participantId.setName("org.onap.PM_Policy");
226 participantId.setVersion("0.0.0");
228 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
229 controlLoopId.setName("PMSHInstance0");
230 controlLoopId.setVersion("1.0.0");
232 final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
233 participantHealthCheckMsg.setParticipantId(participantId);
234 participantHealthCheckMsg.setControlLoopId(controlLoopId);
235 participantHealthCheckMsg.setTimestamp(Instant.now());
236 participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
238 return participantHealthCheckMsg;
242 * Method to create ParticipantControlLoopUpdate using the arguments passed.
244 * @param jsonFilePath the path of the controlloop content
246 * @return ParticipantControlLoopUpdate message
247 * @throws CoderException exception while reading the file to object
249 public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
250 throws CoderException {
251 ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
252 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
253 return participantControlLoopUpdateMsg;
256 private static ToscaServiceTemplate testControlLoopRead(final String inputDirPath) {
257 Set<String> controlLoopDirectoryContents =
258 ResourceUtils.getDirectoryContents(inputDirPath);
260 boolean atLeastOneControlLoopTested = false;
261 ToscaServiceTemplate toscaServiceTemplate = null;
263 for (String controlLoopFilePath : controlLoopDirectoryContents) {
264 if (!controlLoopFilePath.endsWith(".yaml")) {
267 atLeastOneControlLoopTested = true;
268 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
271 assertTrue(atLeastOneControlLoopTested);
272 return toscaServiceTemplate;
276 * Method to add polcies to the toscaServiceTemplate.
278 * @param toscaServiceTemplate to add policies
280 public static void addPolicyTypesToToscaServiceTemplate(
281 ToscaServiceTemplate toscaServiceTemplate) {
282 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
284 for (String policyTypeFilePath : policyTypeDirectoryContents) {
285 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
287 ToscaServiceTemplate foundPolicyTypeSt =
288 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
290 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
291 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
292 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
293 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
294 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
295 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
297 if (foundPolicyTypeSt.getDataTypes() != null) {
298 if (toscaServiceTemplate.getDataTypes() == null) {
299 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
301 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
305 if (toscaServiceTemplate.getPolicyTypes() == null) {
306 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
308 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
314 * Method to add polcies to the toscaServiceTemplate.
316 * @param toscaServiceTemplate to add policies
318 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
319 Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
321 for (String policiesFilePath : policiesDirectoryContents) {
322 String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
324 ToscaServiceTemplate foundPoliciesSt =
325 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
326 toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
327 foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
331 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
333 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
334 if (controlLoopString == null) {
335 throw new FileNotFoundException(controlLoopFilePath);
338 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
339 controlLoopString, ToscaServiceTemplate.class);
340 return serviceTemplate;
341 } catch (FileNotFoundException e) {
342 LOGGER.error("cannot find YAML file", controlLoopFilePath);
343 throw new IllegalArgumentException(e);