bd3316abb50a3acfe7d1281b15fbe2282902fe19
[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.simulator.main.rest;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.time.Instant;
28 import java.util.ArrayList;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.UUID;
34 import lombok.AccessLevel;
35 import lombok.NoArgsConstructor;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
42 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
43 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils;
44 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
45 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
46 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
47 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
48 import org.onap.policy.common.utils.coder.Coder;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
52 import org.onap.policy.common.utils.resources.ResourceUtils;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 @NoArgsConstructor(access = AccessLevel.PRIVATE)
60 public final class TestListenerUtils {
61
62     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
63     private static final Coder CODER = new StandardCoder();
64     static CommonTestData commonTestData = new CommonTestData();
65     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
66
67     /**
68      * Method to create a controlLoop from a yaml file.
69      *
70      * @return ControlLoop controlloop
71      */
72     public static ControlLoop createControlLoop() {
73         ControlLoop controlLoop = new ControlLoop();
74         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
75         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
76         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
77                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
78         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
79             ControlLoopElement clElement = new ControlLoopElement();
80             clElement.setId(UUID.randomUUID());
81
82             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
83             clElementParticipantId.setName(toscaInputEntry.getKey());
84             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
85             clElement.setParticipantId(clElementParticipantId);
86             clElement.setParticipantType(clElementParticipantId);
87
88             clElement.setDefinition(clElementParticipantId);
89             clElement.setState(ControlLoopState.UNINITIALISED);
90             clElement.setDescription(toscaInputEntry.getValue().getDescription());
91             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
92             elements.put(clElement.getId(), clElement);
93         }
94         controlLoop.setElements(elements);
95         controlLoop.setName("PMSHInstance0");
96         controlLoop.setVersion("1.0.0");
97
98         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
99         definition.setName("PMSHInstance0");
100         definition.setVersion("1.0.0");
101         controlLoop.setDefinition(definition);
102
103         return controlLoop;
104     }
105
106     /**
107      * Method to create ControlLoopStateChange message from the arguments passed.
108      *
109      * @param controlLoopOrderedState controlLoopOrderedState
110      *
111      * @return ControlLoopStateChange message
112      */
113     public static ControlLoopStateChange createControlLoopStateChangeMsg(
114             final ControlLoopOrderedState controlLoopOrderedState) {
115         final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
116
117         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
118         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
119
120         clStateChangeMsg.setControlLoopId(controlLoopId);
121         clStateChangeMsg.setParticipantId(participantId);
122         clStateChangeMsg.setTimestamp(Instant.now());
123         clStateChangeMsg.setOrderedState(controlLoopOrderedState);
124
125         return clStateChangeMsg;
126     }
127
128     /**
129      * Method to create ControlLoopUpdateMsg.
130      *
131      * @return ControlLoopUpdate message
132      */
133     public static ControlLoopUpdate createControlLoopUpdateMsg() {
134         final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
135         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
136         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
137
138         clUpdateMsg.setControlLoopId(controlLoopId);
139         clUpdateMsg.setParticipantId(participantId);
140         clUpdateMsg.setMessageId(UUID.randomUUID());
141         clUpdateMsg.setTimestamp(Instant.now());
142
143         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
144         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
145         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
146                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
147         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
148             if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
149                     toscaServiceTemplate)) {
150                 ControlLoopElement clElement = new ControlLoopElement();
151                 clElement.setId(UUID.randomUUID());
152                 var clParticipantType =
153                         ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
154
155                 clElement.setParticipantId(clParticipantType);
156                 clElement.setParticipantType(clParticipantType);
157
158                 clElement.setDefinition(
159                         new ToscaConceptIdentifier(toscaInputEntry.getKey(), 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 =
211                 new ToscaConceptIdentifier("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         // Add policies to the toscaServiceTemplate
221
222         List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
223         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate()
224                 .getNodeTemplates().entrySet()) {
225             if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
226                     toscaServiceTemplate)) {
227                 var clParticipantType =
228                         ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
229                 prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
230                         toscaInputEntry.getValue(), participantDefinitionUpdates);
231             }
232         }
233
234         participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
235         return participantUpdateMsg;
236     }
237
238     private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
239             ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
240
241         var clDefinition = new ControlLoopElementDefinition();
242         clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
243         clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
244         List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
245
246         if (participantDefinitionUpdates.isEmpty()) {
247             participantDefinitionUpdates
248                     .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
249         } else {
250             boolean participantExists = false;
251             for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
252                 if (participantDefinitionUpdate.getParticipantType().equals(clParticipantType)) {
253                     participantDefinitionUpdate.getControlLoopElementDefinitionList().add(clDefinition);
254                     participantExists = true;
255                 }
256             }
257             if (!participantExists) {
258                 participantDefinitionUpdates.add(
259                         getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
260             }
261         }
262     }
263
264     private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
265             ToscaConceptIdentifier clParticipantType,
266             List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
267         ParticipantDefinition participantDefinition = new ParticipantDefinition();
268         participantDefinition.setParticipantType(clParticipantType);
269         controlLoopElementDefinitionList.add(clDefinition);
270         participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
271         return participantDefinition;
272     }
273
274     /**
275      * Method to create ControlLoopUpdate using the arguments passed.
276      *
277      * @param jsonFilePath the path of the controlloop content
278      *
279      * @return ControlLoopUpdate message
280      * @throws CoderException exception while reading the file to object
281      */
282     public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) throws CoderException {
283         ControlLoopUpdate controlLoopUpdateMsg = CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
284         return controlLoopUpdateMsg;
285     }
286
287     private static ToscaServiceTemplate testControlLoopRead() {
288         Set<String> controlLoopDirectoryContents =
289                 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
290
291         boolean atLeastOneControlLoopTested = false;
292         ToscaServiceTemplate toscaServiceTemplate = null;
293
294         for (String controlLoopFilePath : controlLoopDirectoryContents) {
295             if (!controlLoopFilePath.endsWith(".yaml")) {
296                 continue;
297             }
298             atLeastOneControlLoopTested = true;
299             toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
300         }
301
302         assertTrue(atLeastOneControlLoopTested);
303         return toscaServiceTemplate;
304     }
305
306     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
307         try {
308             String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
309             if (controlLoopString == null) {
310                 throw new FileNotFoundException(controlLoopFilePath);
311             }
312
313             ToscaServiceTemplate serviceTemplate =
314                     yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
315             return serviceTemplate;
316         } catch (FileNotFoundException e) {
317             LOGGER.error("cannot find YAML file", controlLoopFilePath);
318             throw new IllegalArgumentException(e);
319         }
320     }
321 }