febdf6b08517ad01803e6e549e547ff2cf96ebfc
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.dcae.main.rest;
22
23 import java.io.File;
24 import java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
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;
48
49 public class TestListenerUtils {
50
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";
55
56     /**
57      * Method to create a controlLoop from a yaml file.
58      *
59      * @return ControlLoop controlloop
60      */
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());
70
71             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
72             clElementParticipantId.setName(toscaInputEntry.getKey());
73             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
74             clElement.setParticipantId(clElementParticipantId);
75
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);
81         }
82         controlLoop.setElements(elements);
83         controlLoop.setName("PMSHInstance0");
84         controlLoop.setVersion("1.0.0");
85
86         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
87         definition.setName("PMSHInstance0");
88         definition.setVersion("1.0.0");
89         controlLoop.setDefinition(definition);
90
91         return controlLoop;
92     }
93
94     /**
95      * Method to create ControlLoopStateChange message from the arguments passed.
96      *
97      * @param controlLoopOrderedState controlLoopOrderedState
98      *
99      * @return ControlLoopStateChange message
100      */
101     public static ControlLoopStateChange createControlLoopStateChangeMsg(
102             final ControlLoopOrderedState controlLoopOrderedState) {
103         final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
104
105         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
106         controlLoopId.setName("PMSHInstance0");
107         controlLoopId.setVersion("1.0.0");
108
109         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
110         participantId.setName("DCAEParticipant0");
111         participantId.setVersion("1.0.0");
112
113         clStateChangeMsg.setControlLoopId(controlLoopId);
114         clStateChangeMsg.setParticipantId(participantId);
115         clStateChangeMsg.setTimestamp(Instant.now());
116         clStateChangeMsg.setOrderedState(controlLoopOrderedState);
117
118         return clStateChangeMsg;
119     }
120
121     /**
122      * Method to create ControlLoopUpdateMsg.
123      *
124      * @return ControlLoopUpdate message
125      */
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");
132
133         clUpdateMsg.setControlLoopId(controlLoopId);
134         clUpdateMsg.setParticipantId(participantId);
135         clUpdateMsg.setMessageId(UUID.randomUUID());
136         clUpdateMsg.setTimestamp(Instant.now());
137
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;
147                 try {
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);
153                 }
154
155                 clElement.setParticipantId(clParticipantId);
156                 clElement.setParticipantType(clParticipantId);
157
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);
164             }
165         }
166
167         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
168         for (ControlLoopElement element : elements.values()) {
169             prepareParticipantUpdateForControlLoop(element, participantUpdates);
170         }
171         clUpdateMsg.setParticipantUpdatesList(participantUpdates);
172         return clUpdateMsg;
173     }
174
175     private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement,
176         List<ParticipantUpdates> participantUpdates) {
177         if (participantUpdates.isEmpty()) {
178             participantUpdates.add(getControlLoopElementList(clElement));
179         } else {
180             boolean participantExists = false;
181             for (ParticipantUpdates participantUpdate : participantUpdates) {
182                 if (participantUpdate.getParticipantId().equals(clElement.getParticipantId())) {
183                     participantUpdate.getControlLoopElementList().add(clElement);
184                     participantExists = true;
185                 }
186             }
187             if (!participantExists) {
188                 participantUpdates.add(getControlLoopElementList(clElement));
189             }
190         }
191     }
192
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;
200     }
201
202     /**
203      * Method to create participantUpdateMsg.
204      *
205      * @return ParticipantUpdate message
206      */
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");
212
213         participantUpdateMsg.setParticipantId(participantId);
214         participantUpdateMsg.setTimestamp(Instant.now());
215         participantUpdateMsg.setParticipantType(participantType);
216         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
217         participantUpdateMsg.setMessageId(UUID.randomUUID());
218
219         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
220
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;
226                 try {
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);
232                 }
233                 prepareParticipantDefinitionUpdate(clParticipantId, toscaInputEntry.getKey(),
234                     toscaInputEntry.getValue(), participantDefinitionUpdates);
235             }
236         }
237
238         participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
239         participantUpdateMsg.setToscaServiceTemplate(toscaServiceTemplate);
240         return participantUpdateMsg;
241     }
242
243     private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantId, String entryKey,
244         ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
245
246         var clDefinition = new ControlLoopElementDefinition();
247         clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(
248             entryKey, entryValue.getVersion()));
249         clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
250         List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
251
252         if (participantDefinitionUpdates.isEmpty()) {
253             participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
254                 controlLoopElementDefinitionList));
255         } else {
256             boolean participantExists = false;
257             for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
258                 if (participantDefinitionUpdate.getParticipantId().equals(clParticipantId)) {
259                     participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
260                     participantExists = true;
261                 }
262             }
263             if (!participantExists) {
264                 participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantId,
265                     controlLoopElementDefinitionList));
266             }
267         }
268     }
269
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;
278     }
279
280     /**
281      * Method to create a deep copy of ControlLoopUpdateMsg.
282      *
283      * @return ControlLoopUpdate message
284      */
285     public static ControlLoopUpdate createCopyControlLoopUpdateMsg(ControlLoopUpdate cpy) {
286         return new ControlLoopUpdate(cpy);
287     }
288
289     /**
290      * Method to create ControlLoopUpdate using the arguments passed.
291      *
292      * @param jsonFilePath the path of the controlloop content
293      *
294      * @return ControlLoopUpdate message
295      * @throws CoderException exception while reading the file to object
296      */
297     public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
298             throws CoderException {
299         ControlLoopUpdate controlLoopUpdateMsg =
300                 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
301         return controlLoopUpdateMsg;
302     }
303
304     private static ToscaServiceTemplate testControlLoopRead() {
305         return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
306     }
307
308     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
309         String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
310         ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
311         return serviceTemplate;
312     }
313 }