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