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