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