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.dcae.main.rest;
24 import java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import java.util.UUID;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
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.common.utils.coder.Coder;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
44 import org.onap.policy.common.utils.resources.ResourceUtils;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
49 public class TestListenerUtils {
51 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
52 private static final Coder CODER = new StandardCoder();
53 private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
54 private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement";
57 * Method to create a controlLoop from a yaml file.
59 * @return ControlLoop controlloop
61 public static ControlLoop createControlLoop() {
62 ControlLoop controlLoop = new ControlLoop();
63 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
64 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
65 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
66 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
67 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
68 ControlLoopElement clElement = new ControlLoopElement();
69 clElement.setId(UUID.randomUUID());
71 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
72 clElementParticipantId.setName(toscaInputEntry.getKey());
73 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
74 clElement.setParticipantId(clElementParticipantId);
76 clElement.setDefinition(clElementParticipantId);
77 clElement.setState(ControlLoopState.UNINITIALISED);
78 clElement.setDescription(toscaInputEntry.getValue().getDescription());
79 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
80 elements.put(clElement.getId(), clElement);
82 controlLoop.setElements(elements);
83 controlLoop.setName("PMSHInstance0");
84 controlLoop.setVersion("1.0.0");
86 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
87 definition.setName("PMSHInstance0");
88 definition.setVersion("1.0.0");
89 controlLoop.setDefinition(definition);
95 * Method to create ControlLoopStateChange message from the arguments passed.
97 * @param controlLoopOrderedState controlLoopOrderedState
99 * @return ControlLoopStateChange message
101 public static ControlLoopStateChange createControlLoopStateChangeMsg(
102 final ControlLoopOrderedState controlLoopOrderedState) {
103 final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
105 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
106 controlLoopId.setName("PMSHInstance0");
107 controlLoopId.setVersion("1.0.0");
109 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
110 participantId.setName("DCAEParticipant0");
111 participantId.setVersion("1.0.0");
113 clStateChangeMsg.setControlLoopId(controlLoopId);
114 clStateChangeMsg.setParticipantId(participantId);
115 clStateChangeMsg.setTimestamp(Instant.now());
116 clStateChangeMsg.setOrderedState(controlLoopOrderedState);
118 return clStateChangeMsg;
122 * Method to create ControlLoopUpdateMsg.
124 * @return ControlLoopUpdate message
126 public static ControlLoopUpdate createControlLoopUpdateMsg() {
127 final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
128 ToscaConceptIdentifier controlLoopId =
129 new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
130 ToscaConceptIdentifier participantId =
131 new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
133 clUpdateMsg.setControlLoopId(controlLoopId);
134 clUpdateMsg.setParticipantId(participantId);
135 clUpdateMsg.setMessageId(UUID.randomUUID());
136 clUpdateMsg.setTimestamp(Instant.now());
138 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
139 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
140 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
141 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
142 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
143 if (toscaInputEntry.getValue().getType().contains(CONTROL_LOOP_ELEMENT)) {
144 ControlLoopElement clElement = new ControlLoopElement();
145 clElement.setId(UUID.randomUUID());
146 ToscaConceptIdentifier clParticipantId;
148 clParticipantId = CODER.decode(
149 toscaInputEntry.getValue().getProperties().get("participant_id").toString(),
150 ToscaConceptIdentifier.class);
151 } catch (CoderException e) {
152 throw new RuntimeException("cannot get ParticipantId from toscaNodeTemplate", e);
155 clElement.setParticipantId(clParticipantId);
156 clElement.setParticipantType(clParticipantId);
158 clElement.setDefinition(new ToscaConceptIdentifier(toscaInputEntry.getKey(),
159 toscaInputEntry.getValue().getVersion()));
160 clElement.setState(ControlLoopState.UNINITIALISED);
161 clElement.setDescription(toscaInputEntry.getValue().getDescription());
162 clElement.setOrderedState(ControlLoopOrderedState.PASSIVE);
163 elements.put(clElement.getId(), clElement);
167 List<ParticipantUpdates> participantUpdates = new ArrayList<>();
168 for (ControlLoopElement element : elements.values()) {
169 prepareParticipantUpdateForControlLoop(element, participantUpdates);
171 clUpdateMsg.setParticipantUpdatesList(participantUpdates);
175 private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement,
176 List<ParticipantUpdates> participantUpdates) {
177 if (participantUpdates.isEmpty()) {
178 participantUpdates.add(getControlLoopElementList(clElement));
180 boolean participantExists = false;
181 for (ParticipantUpdates participantUpdate : participantUpdates) {
182 if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
183 participantUpdate.getControlLoopElementList().add(clElement);
184 participantExists = true;
187 if (!participantExists) {
188 participantUpdates.add(getControlLoopElementList(clElement));
193 private static ParticipantUpdates getControlLoopElementList(ControlLoopElement clElement) {
194 ParticipantUpdates participantUpdate = new ParticipantUpdates();
195 List<ControlLoopElement> controlLoopElementList = new ArrayList<>();
196 participantUpdate.setParticipantId(clElement.getParticipantId());
197 controlLoopElementList.add(clElement);
198 participantUpdate.setControlLoopElementList(controlLoopElementList);
199 return participantUpdate;
203 * Method to create participantUpdateMsg.
205 * @return ParticipantUpdate message
207 public static ParticipantUpdate createParticipantUpdateMsg() {
208 final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
209 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
210 ToscaConceptIdentifier participantType = new ToscaConceptIdentifier(
211 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
213 participantUpdateMsg.setParticipantId(participantId);
214 participantUpdateMsg.setTimestamp(Instant.now());
215 participantUpdateMsg.setParticipantType(participantType);
216 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
217 participantUpdateMsg.setMessageId(UUID.randomUUID());
219 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
221 List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
222 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry :
223 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
224 if (toscaInputEntry.getValue().getType().contains(CONTROL_LOOP_ELEMENT)) {
225 ToscaConceptIdentifier clParticipantId;
227 clParticipantId = CODER.decode(
228 toscaInputEntry.getValue().getProperties().get("participant_id").toString(),
229 ToscaConceptIdentifier.class);
230 } catch (CoderException e) {
231 throw new RuntimeException("cannot get ParticipantId from toscaNodeTemplate", e);
233 prepareParticipantDefinitionUpdate(clParticipantId, toscaInputEntry.getKey(),
234 toscaInputEntry.getValue(), participantDefinitionUpdates);
238 participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
239 participantUpdateMsg.setToscaServiceTemplate(toscaServiceTemplate);
240 return participantUpdateMsg;
243 private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantId, String entryKey,
244 ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
246 var clDefinition = new ControlLoopElementDefinition();
247 clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(
248 entryKey, entryValue.getVersion()));
249 clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
250 List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
252 if (participantDefinitionUpdates.isEmpty()) {
253 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
254 controlLoopElementDefinitionList));
256 boolean participantExists = false;
257 for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
258 if (participantDefinitionUpdate.getParticipantId().equals(clParticipantId)) {
259 participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
260 participantExists = true;
263 if (!participantExists) {
264 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
265 controlLoopElementDefinitionList));
270 private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
271 ToscaConceptIdentifier clParticipantId,
272 List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
273 ParticipantDefinition participantDefinition = new ParticipantDefinition();
274 participantDefinition.setParticipantId(clParticipantId);
275 controlLoopElementDefinitionList.add(clDefinition);
276 participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
277 return participantDefinition;
281 * Method to create a deep copy of ControlLoopUpdateMsg.
283 * @return ControlLoopUpdate message
285 public static ControlLoopUpdate createCopyControlLoopUpdateMsg(ControlLoopUpdate cpy) {
286 return new ControlLoopUpdate(cpy);
290 * Method to create ControlLoopUpdate using the arguments passed.
292 * @param jsonFilePath the path of the controlloop content
294 * @return ControlLoopUpdate message
295 * @throws CoderException exception while reading the file to object
297 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
298 throws CoderException {
299 ControlLoopUpdate controlLoopUpdateMsg =
300 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
301 return controlLoopUpdateMsg;
304 private static ToscaServiceTemplate testControlLoopRead() {
305 return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
308 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
309 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
310 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
311 return serviceTemplate;