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.ControlLoopOrderedState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
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 ParticipantStateChange message from the arguments passed.
103 * @param participantState participant State
105 * @return ParticipantStateChange message
107 public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
108 final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
109 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
110 participantId.setName("org.onap.PM_Policy");
111 participantId.setVersion("0.0.0");
113 participantStateChangeMsg.setParticipantId(participantId);
114 participantStateChangeMsg.setTimestamp(Instant.now());
115 participantStateChangeMsg.setState(participantState);
117 return participantStateChangeMsg;
121 * Method to create ControlLoopStateChange message from the arguments passed.
123 * @param controlLoopOrderedState controlLoopOrderedState
125 * @return ParticipantControlLoopStateChange message
127 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
128 final ControlLoopOrderedState controlLoopOrderedState) {
129 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
131 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
132 controlLoopId.setName("PMSHInstance0");
133 controlLoopId.setVersion("1.0.0");
135 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
136 participantId.setName("org.onap.PM_Policy");
137 participantId.setVersion("0.0.0");
139 participantClStateChangeMsg.setControlLoopId(controlLoopId);
140 participantClStateChangeMsg.setParticipantId(participantId);
141 participantClStateChangeMsg.setTimestamp(Instant.now());
142 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
144 return participantClStateChangeMsg;
148 * Method to create ControlLoopUpdateMsg.
150 * @return ParticipantControlLoopUpdate message
152 public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
153 final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
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 clUpdateMsg.setControlLoopId(controlLoopId);
163 clUpdateMsg.setParticipantId(participantId);
165 ControlLoop controlLoop = new ControlLoop();
166 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
167 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
168 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
169 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
170 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
171 ControlLoopElement clElement = new ControlLoopElement();
172 clElement.setId(UUID.randomUUID());
174 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
175 clElementParticipantId.setName(toscaInputEntry.getKey());
176 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
177 clElement.setParticipantId(clElementParticipantId);
179 clElement.setDefinition(clElementParticipantId);
180 clElement.setState(ControlLoopState.UNINITIALISED);
181 clElement.setDescription(toscaInputEntry.getValue().getDescription());
182 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
183 elements.put(clElement.getId(), clElement);
185 controlLoop.setElements(elements);
186 controlLoop.setName("PMSHInstance0");
187 controlLoop.setVersion("1.0.0");
188 controlLoop.setDefinition(controlLoopId);
189 clUpdateMsg.setControlLoop(controlLoop);
190 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
196 * Method to create ParticipantHealthCheck message.
198 * @return ParticipantHealthCheck message
200 public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
201 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
202 participantId.setName("org.onap.PM_Policy");
203 participantId.setVersion("0.0.0");
205 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
206 controlLoopId.setName("PMSHInstance0");
207 controlLoopId.setVersion("1.0.0");
209 final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
210 participantHealthCheckMsg.setParticipantId(participantId);
211 participantHealthCheckMsg.setControlLoopId(controlLoopId);
212 participantHealthCheckMsg.setTimestamp(Instant.now());
213 participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
215 return participantHealthCheckMsg;
219 * Method to create ParticipantControlLoopUpdate using the arguments passed.
221 * @param jsonFilePath the path of the controlloop content
223 * @return ParticipantControlLoopUpdate message
224 * @throws CoderException exception while reading the file to object
226 public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
227 throws CoderException {
228 ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
229 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
230 return participantControlLoopUpdateMsg;
233 private static ToscaServiceTemplate testControlLoopRead() {
234 Set<String> controlLoopDirectoryContents =
235 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
237 boolean atLeastOneControlLoopTested = false;
238 ToscaServiceTemplate toscaServiceTemplate = null;
240 for (String controlLoopFilePath : controlLoopDirectoryContents) {
241 if (!controlLoopFilePath.endsWith(".yaml")) {
244 atLeastOneControlLoopTested = true;
245 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
248 // Add policy_types to the toscaServiceTemplate
249 addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
251 assertTrue(atLeastOneControlLoopTested);
252 return toscaServiceTemplate;
255 private static void addPolicyTypesToToscaServiceTemplate(
256 ToscaServiceTemplate toscaServiceTemplate) {
257 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
259 for (String policyTypeFilePath : policyTypeDirectoryContents) {
260 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
262 ToscaServiceTemplate foundPolicyTypeSt =
263 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
265 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
266 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
267 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
268 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
269 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
270 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
272 if (foundPolicyTypeSt.getDataTypes() != null) {
273 if (toscaServiceTemplate.getDataTypes() == null) {
274 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
276 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
280 if (toscaServiceTemplate.getPolicyTypes() == null) {
281 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
283 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
289 * Method to add polcies to the toscaServiceTemplate.
291 * @param toscaServiceTemplate to add policies
293 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
294 Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
296 for (String policiesFilePath : policiesDirectoryContents) {
297 String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
299 ToscaServiceTemplate foundPoliciesSt =
300 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
301 toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
302 foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
306 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
308 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
309 if (controlLoopString == null) {
310 throw new FileNotFoundException(controlLoopFilePath);
313 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
314 controlLoopString, ToscaServiceTemplate.class);
315 return serviceTemplate;
316 } catch (FileNotFoundException e) {
317 LOGGER.error("cannot find YAML file", controlLoopFilePath);
318 throw new IllegalArgumentException(e);