b91cff22caa52bc5ba058e18044eb4e813289ee5
[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.ParticipantUpdate;
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 ControlLoopStateChange message from the arguments passed.
102      *
103      * @param controlLoopOrderedState controlLoopOrderedState
104      *
105      * @return ControlLoopStateChange message
106      */
107     public static ControlLoopStateChange createControlLoopStateChangeMsg(
108             final ControlLoopOrderedState controlLoopOrderedState) {
109         final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
110
111         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
112         controlLoopId.setName("PMSHInstance0");
113         controlLoopId.setVersion("1.0.0");
114
115         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
116         participantId.setName("org.onap.PM_Policy");
117         participantId.setVersion("0.0.0");
118
119         clStateChangeMsg.setControlLoopId(controlLoopId);
120         clStateChangeMsg.setParticipantId(participantId);
121         clStateChangeMsg.setTimestamp(Instant.now());
122         clStateChangeMsg.setOrderedState(controlLoopOrderedState);
123
124         return clStateChangeMsg;
125     }
126
127     /**
128      * Method to create ControlLoopUpdateMsg.
129      *
130      * @return ControlLoopUpdate message
131      */
132     public static ControlLoopUpdate createControlLoopUpdateMsg() {
133         final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
134         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
135         controlLoopId.setName("PMSHInstance0");
136         controlLoopId.setVersion("1.0.0");
137
138         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
139         participantId.setName("org.onap.PM_Policy");
140         participantId.setVersion("0.0.0");
141
142         clUpdateMsg.setControlLoopId(controlLoopId);
143         clUpdateMsg.setParticipantId(participantId);
144
145         ControlLoop controlLoop = new ControlLoop();
146         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
147         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
148         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
149                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
150         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
151             ControlLoopElement clElement = new ControlLoopElement();
152             clElement.setId(UUID.randomUUID());
153
154             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
155             clElementParticipantId.setName(toscaInputEntry.getKey());
156             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
157             clElement.setParticipantId(clElementParticipantId);
158             clElement.setParticipantType(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 ControlLoopUpdate using the arguments passed.
214      *
215      * @param jsonFilePath the path of the controlloop content
216      *
217      * @return ControlLoopUpdate message
218      * @throws CoderException exception while reading the file to object
219      */
220     public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
221             throws CoderException {
222         ControlLoopUpdate controlLoopUpdateMsg =
223                 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
224         return controlLoopUpdateMsg;
225     }
226
227     private static ToscaServiceTemplate testControlLoopRead() {
228         Set<String> controlLoopDirectoryContents =
229                 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
230
231         boolean atLeastOneControlLoopTested = false;
232         ToscaServiceTemplate toscaServiceTemplate = null;
233
234         for (String controlLoopFilePath : controlLoopDirectoryContents) {
235             if (!controlLoopFilePath.endsWith(".yaml")) {
236                 continue;
237             }
238             atLeastOneControlLoopTested = true;
239             toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
240         }
241
242         // Add policy_types to the toscaServiceTemplate
243         addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
244
245         assertTrue(atLeastOneControlLoopTested);
246         return toscaServiceTemplate;
247     }
248
249     private static void addPolicyTypesToToscaServiceTemplate(
250             ToscaServiceTemplate toscaServiceTemplate) {
251         Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
252
253         for (String policyTypeFilePath : policyTypeDirectoryContents) {
254             String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
255
256             ToscaServiceTemplate foundPolicyTypeSt =
257                 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
258
259             toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
260             toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
261             toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
262             toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
263             toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
264             toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
265
266             if (foundPolicyTypeSt.getDataTypes() != null) {
267                 if (toscaServiceTemplate.getDataTypes() == null) {
268                     toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
269                 } else {
270                     toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
271                 }
272             }
273
274             if (toscaServiceTemplate.getPolicyTypes() == null) {
275                 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
276             } else {
277                 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
278             }
279         }
280     }
281
282     /**
283      * Method to add polcies to the toscaServiceTemplate.
284      *
285      * @param toscaServiceTemplate to add policies
286      */
287     public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
288         Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
289
290         for (String policiesFilePath : policiesDirectoryContents) {
291             String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
292
293             ToscaServiceTemplate foundPoliciesSt =
294                 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
295             toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
296                     foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
297         }
298     }
299
300     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
301         try {
302             String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
303             if (controlLoopString == null) {
304                 throw new FileNotFoundException(controlLoopFilePath);
305             }
306
307             ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
308                         controlLoopString, ToscaServiceTemplate.class);
309             return serviceTemplate;
310         } catch (FileNotFoundException e) {
311             LOGGER.error("cannot find YAML file", controlLoopFilePath);
312             throw new IllegalArgumentException(e);
313         }
314     }
315 }