d439c9daf61b6875f731abc1e62540ac7043d21a
[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.policy.main.utils;
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.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Set;
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.ControlLoopElementDefinition;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
39 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
43 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.CommonTestData;
44 import org.onap.policy.common.utils.coder.Coder;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.common.utils.coder.StandardCoder;
47 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
48 import org.onap.policy.common.utils.resources.ResourceUtils;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class TestListenerUtils {
56
57     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
58     private static final Coder CODER = new StandardCoder();
59     static CommonTestData commonTestData = new CommonTestData();
60     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
61
62     private TestListenerUtils() {}
63
64     /**
65      * Method to create a controlLoop from a yaml file.
66      *
67      * @return ControlLoop controlloop
68      */
69     public static ControlLoop createControlLoop() {
70         ControlLoop controlLoop = new ControlLoop();
71         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
72         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
73         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
74                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
75         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
76             ControlLoopElement clElement = new ControlLoopElement();
77             clElement.setId(UUID.randomUUID());
78
79             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
80             clElementParticipantId.setName(toscaInputEntry.getKey());
81             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
82             clElement.setParticipantId(clElementParticipantId);
83
84             clElement.setDefinition(clElementParticipantId);
85             clElement.setState(ControlLoopState.UNINITIALISED);
86             clElement.setDescription(toscaInputEntry.getValue().getDescription());
87             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
88             elements.put(clElement.getId(), clElement);
89         }
90         controlLoop.setElements(elements);
91         controlLoop.setName("PMSHInstance0");
92         controlLoop.setVersion("1.0.0");
93
94         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
95         definition.setName("PMSHInstance0");
96         definition.setVersion("1.0.0");
97         controlLoop.setDefinition(definition);
98
99         return controlLoop;
100     }
101
102     /**
103      * Method to create ParticipantStateChange message from the arguments passed.
104      *
105      * @param participantState participant State
106      *
107      * @return ParticipantStateChange message
108      */
109     public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
110         final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
111         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
112         participantId.setName("org.onap.PM_Policy");
113         participantId.setVersion("0.0.0");
114
115         participantStateChangeMsg.setParticipantId(participantId);
116         participantStateChangeMsg.setTimestamp(Instant.now());
117         participantStateChangeMsg.setState(participantState);
118
119         return participantStateChangeMsg;
120     }
121
122     /**
123      * Method to create ControlLoopStateChange message from the arguments passed.
124      *
125      * @param controlLoopOrderedState controlLoopOrderedState
126      *
127      * @return ParticipantControlLoopStateChange message
128      */
129     public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
130             final ControlLoopOrderedState controlLoopOrderedState) {
131         final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
132
133         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
134         controlLoopId.setName("PMSHInstance0");
135         controlLoopId.setVersion("1.0.0");
136
137         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
138         participantId.setName("org.onap.PM_Policy");
139         participantId.setVersion("0.0.0");
140
141         participantClStateChangeMsg.setControlLoopId(controlLoopId);
142         participantClStateChangeMsg.setParticipantId(participantId);
143         participantClStateChangeMsg.setTimestamp(Instant.now());
144         participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
145
146         return participantClStateChangeMsg;
147     }
148
149     /**
150      * Method to create ControlLoopUpdateMsg.
151      *
152      * @return ParticipantControlLoopUpdate message
153      */
154     public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
155         final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
156         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
157         controlLoopId.setName("PMSHInstance0");
158         controlLoopId.setVersion("1.0.0");
159
160         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
161         participantId.setName("org.onap.PM_Policy");
162         participantId.setVersion("0.0.0");
163
164         clUpdateMsg.setControlLoopId(controlLoopId);
165         clUpdateMsg.setParticipantId(participantId);
166
167         ControlLoop controlLoop = new ControlLoop();
168         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
169         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
170         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
171                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
172         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
173             ControlLoopElement clElement = new ControlLoopElement();
174             clElement.setId(UUID.randomUUID());
175
176             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
177             clElementParticipantId.setName(toscaInputEntry.getKey());
178             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
179             clElement.setParticipantId(clElementParticipantId);
180
181             clElement.setDefinition(clElementParticipantId);
182             clElement.setState(ControlLoopState.UNINITIALISED);
183             clElement.setDescription(toscaInputEntry.getValue().getDescription());
184             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
185             elements.put(clElement.getId(), clElement);
186         }
187         controlLoop.setElements(elements);
188         controlLoop.setName("PMSHInstance0");
189         controlLoop.setVersion("1.0.0");
190         controlLoop.setDefinition(controlLoopId);
191         clUpdateMsg.setControlLoop(controlLoop);
192         clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
193
194         return clUpdateMsg;
195     }
196
197     /**
198      * Method to create participantUpdateMsg.
199      *
200      * @return ParticipantUpdate message
201      */
202     public static ParticipantUpdate createParticipantUpdateMsg() {
203         final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
204         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
205         ToscaConceptIdentifier participantType = new ToscaConceptIdentifier(
206                         "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
207
208         participantUpdateMsg.setParticipantId(participantId);
209         participantUpdateMsg.setTimestamp(Instant.now());
210         participantUpdateMsg.setParticipantType(participantType);
211         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
212         participantUpdateMsg.setMessageId(UUID.randomUUID());
213
214         ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
215         toscaServiceTemplate.setName("serviceTemplate");
216         toscaServiceTemplate.setDerivedFrom("parentServiceTemplate");
217         toscaServiceTemplate.setDescription("Description of serviceTemplate");
218         toscaServiceTemplate.setVersion("1.2.3");
219
220         ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
221         clDefinition.setId(UUID.randomUUID());
222         clDefinition.setControlLoopElementToscaServiceTemplate(toscaServiceTemplate);
223         Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
224         clDefinition.setCommonPropertiesMap(commonPropertiesMap);
225
226         Map<UUID, ControlLoopElementDefinition> controlLoopElementDefinitionMap =
227             Map.of(UUID.randomUUID(), clDefinition);
228
229         Map<ToscaConceptIdentifier, Map<UUID, ControlLoopElementDefinition>>
230             participantDefinitionUpdateMap = Map.of(participantId, controlLoopElementDefinitionMap);
231         participantUpdateMsg.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap);
232
233         return participantUpdateMsg;
234     }
235
236     /**
237      * Method to create ParticipantHealthCheck message.
238      *
239      * @return ParticipantHealthCheck message
240      */
241     public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
242         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
243         participantId.setName("org.onap.PM_Policy");
244         participantId.setVersion("0.0.0");
245
246         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
247         controlLoopId.setName("PMSHInstance0");
248         controlLoopId.setVersion("1.0.0");
249
250         final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
251         participantHealthCheckMsg.setParticipantId(participantId);
252         participantHealthCheckMsg.setControlLoopId(controlLoopId);
253         participantHealthCheckMsg.setTimestamp(Instant.now());
254         participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
255
256         return participantHealthCheckMsg;
257     }
258
259     /**
260      * Method to create ParticipantControlLoopUpdate using the arguments passed.
261      *
262      * @param jsonFilePath the path of the controlloop content
263      *
264      * @return ParticipantControlLoopUpdate message
265      * @throws CoderException exception while reading the file to object
266      */
267     public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
268             throws CoderException {
269         ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
270                 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
271         return participantControlLoopUpdateMsg;
272     }
273
274     private static ToscaServiceTemplate testControlLoopRead() {
275         Set<String> controlLoopDirectoryContents =
276                 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
277
278         boolean atLeastOneControlLoopTested = false;
279         ToscaServiceTemplate toscaServiceTemplate = null;
280
281         for (String controlLoopFilePath : controlLoopDirectoryContents) {
282             if (!controlLoopFilePath.endsWith(".yaml")) {
283                 continue;
284             }
285             atLeastOneControlLoopTested = true;
286             toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
287         }
288
289         // Add policy_types to the toscaServiceTemplate
290         addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
291
292         assertTrue(atLeastOneControlLoopTested);
293         return toscaServiceTemplate;
294     }
295
296     private static void addPolicyTypesToToscaServiceTemplate(
297             ToscaServiceTemplate toscaServiceTemplate) {
298         Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
299
300         for (String policyTypeFilePath : policyTypeDirectoryContents) {
301             String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
302
303             ToscaServiceTemplate foundPolicyTypeSt =
304                 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
305
306             toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
307             toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
308             toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
309             toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
310             toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
311             toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
312
313             if (foundPolicyTypeSt.getDataTypes() != null) {
314                 if (toscaServiceTemplate.getDataTypes() == null) {
315                     toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
316                 } else {
317                     toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
318                 }
319             }
320
321             if (toscaServiceTemplate.getPolicyTypes() == null) {
322                 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
323             } else {
324                 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
325             }
326         }
327     }
328
329     /**
330      * Method to add polcies to the toscaServiceTemplate.
331      *
332      * @param toscaServiceTemplate to add policies
333      */
334     public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
335         Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
336
337         for (String policiesFilePath : policiesDirectoryContents) {
338             String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
339
340             ToscaServiceTemplate foundPoliciesSt =
341                 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
342             toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
343                     foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
344         }
345     }
346
347     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
348         try {
349             String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
350             if (controlLoopString == null) {
351                 throw new FileNotFoundException(controlLoopFilePath);
352             }
353
354             ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
355                         controlLoopString, ToscaServiceTemplate.class);
356             return serviceTemplate;
357         } catch (FileNotFoundException e) {
358             LOGGER.error("cannot find YAML file", controlLoopFilePath);
359             throw new IllegalArgumentException(e);
360         }
361     }
362 }