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