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