6f1e001437f6790d8dd8b061ca7bbd64f0dd9478
[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         clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
172
173         return clUpdateMsg;
174     }
175
176     /**
177      * Method to create participantUpdateMsg.
178      *
179      * @return ParticipantUpdate message
180      */
181     public static ParticipantUpdate createParticipantUpdateMsg() {
182         final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
183         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
184         ToscaConceptIdentifier participantType = new ToscaConceptIdentifier(
185                         "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
186
187         participantUpdateMsg.setParticipantId(participantId);
188         participantUpdateMsg.setTimestamp(Instant.now());
189         participantUpdateMsg.setParticipantType(participantType);
190         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
191         participantUpdateMsg.setMessageId(UUID.randomUUID());
192
193         ToscaServiceTemplate toscaServiceTemplate = new ToscaServiceTemplate();
194         toscaServiceTemplate.setName("serviceTemplate");
195         toscaServiceTemplate.setDerivedFrom("parentServiceTemplate");
196         toscaServiceTemplate.setDescription("Description of serviceTemplate");
197         toscaServiceTemplate.setVersion("1.2.3");
198
199         ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
200         clDefinition.setId(UUID.randomUUID());
201         clDefinition.setControlLoopElementToscaServiceTemplate(toscaServiceTemplate);
202         Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
203         clDefinition.setCommonPropertiesMap(commonPropertiesMap);
204
205         Map<UUID, ControlLoopElementDefinition> controlLoopElementDefinitionMap =
206             Map.of(UUID.randomUUID(), clDefinition);
207
208         Map<ToscaConceptIdentifier, Map<UUID, ControlLoopElementDefinition>>
209             participantDefinitionUpdateMap = Map.of(participantId, controlLoopElementDefinitionMap);
210         participantUpdateMsg.setParticipantDefinitionUpdateMap(participantDefinitionUpdateMap);
211
212         return participantUpdateMsg;
213     }
214
215     /**
216      * Method to create ParticipantHealthCheck message.
217      *
218      * @return ParticipantHealthCheck message
219      */
220     public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
221         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
222         participantId.setName("org.onap.PM_Policy");
223         participantId.setVersion("0.0.0");
224
225         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
226         controlLoopId.setName("PMSHInstance0");
227         controlLoopId.setVersion("1.0.0");
228
229         final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
230         participantHealthCheckMsg.setParticipantId(participantId);
231         participantHealthCheckMsg.setControlLoopId(controlLoopId);
232         participantHealthCheckMsg.setTimestamp(Instant.now());
233         participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
234
235         return participantHealthCheckMsg;
236     }
237
238     /**
239      * Method to create ControlLoopUpdate using the arguments passed.
240      *
241      * @param jsonFilePath the path of the controlloop content
242      *
243      * @return ControlLoopUpdate message
244      * @throws CoderException exception while reading the file to object
245      */
246     public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
247             throws CoderException {
248         ControlLoopUpdate controlLoopUpdateMsg =
249                 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
250         return controlLoopUpdateMsg;
251     }
252
253     private static ToscaServiceTemplate testControlLoopRead() {
254         Set<String> controlLoopDirectoryContents =
255                 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
256
257         boolean atLeastOneControlLoopTested = false;
258         ToscaServiceTemplate toscaServiceTemplate = null;
259
260         for (String controlLoopFilePath : controlLoopDirectoryContents) {
261             if (!controlLoopFilePath.endsWith(".yaml")) {
262                 continue;
263             }
264             atLeastOneControlLoopTested = true;
265             toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
266         }
267
268         // Add policy_types to the toscaServiceTemplate
269         addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
270
271         assertTrue(atLeastOneControlLoopTested);
272         return toscaServiceTemplate;
273     }
274
275     private static void addPolicyTypesToToscaServiceTemplate(
276             ToscaServiceTemplate toscaServiceTemplate) {
277         Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
278
279         for (String policyTypeFilePath : policyTypeDirectoryContents) {
280             String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
281
282             ToscaServiceTemplate foundPolicyTypeSt =
283                 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
284
285             toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
286             toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
287             toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
288             toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
289             toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
290             toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
291
292             if (foundPolicyTypeSt.getDataTypes() != null) {
293                 if (toscaServiceTemplate.getDataTypes() == null) {
294                     toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
295                 } else {
296                     toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
297                 }
298             }
299
300             if (toscaServiceTemplate.getPolicyTypes() == null) {
301                 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
302             } else {
303                 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
304             }
305         }
306     }
307
308     /**
309      * Method to add polcies to the toscaServiceTemplate.
310      *
311      * @param toscaServiceTemplate to add policies
312      */
313     public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
314         Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
315
316         for (String policiesFilePath : policiesDirectoryContents) {
317             String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
318
319             ToscaServiceTemplate foundPoliciesSt =
320                 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
321             toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
322                     foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
323         }
324     }
325
326     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
327         try {
328             String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
329             if (controlLoopString == null) {
330                 throw new FileNotFoundException(controlLoopFilePath);
331             }
332
333             ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
334                         controlLoopString, ToscaServiceTemplate.class);
335             return serviceTemplate;
336         } catch (FileNotFoundException e) {
337             LOGGER.error("cannot find YAML file", controlLoopFilePath);
338             throw new IllegalArgumentException(e);
339         }
340     }
341 }