9c20ffe22abd27d1b0e0f363ec2f2318330e08dd
[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.simulator.main.rest;
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.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.UUID;
34 import lombok.Getter;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
40 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
41 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
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.simulator.main.parameters.CommonTestData;
45 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.ParticipantSimulatorParameters;
46 import org.onap.policy.clamp.controlloop.participant.simulator.simulation.SimulationProvider;
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     private TestListenerUtils() {}
66
67     @Getter
68     private static ParticipantHandler participantHandler;
69
70     /**
71      * Method to initialize participantHandler.
72      */
73     public static void initParticipantHandler() {
74
75         final ParticipantSimulatorParameters participantParameters = commonTestData.toObject(
76                 commonTestData.getParticipantParameterGroupMap(CommonTestData.PARTICIPANT_GROUP_NAME),
77                 ParticipantSimulatorParameters.class);
78
79         SimulationProvider simulationProvider =
80                 new SimulationProvider(participantParameters.getIntermediaryParameters());
81
82         participantHandler = simulationProvider.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         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
93         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
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             clElement.setParticipantType(clElementParticipantId);
105
106             clElement.setDefinition(clElementParticipantId);
107             clElement.setState(ControlLoopState.UNINITIALISED);
108             clElement.setDescription(toscaInputEntry.getValue().getDescription());
109             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
110             elements.put(clElement.getId(), clElement);
111         }
112         controlLoop.setElements(elements);
113         controlLoop.setName("PMSHInstance0");
114         controlLoop.setVersion("1.0.0");
115
116         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
117         definition.setName("PMSHInstance0");
118         definition.setVersion("1.0.0");
119         controlLoop.setDefinition(definition);
120
121         return controlLoop;
122     }
123
124     /**
125      * Method to create ParticipantStateChange message from the arguments passed.
126      *
127      * @param participantState participant State
128      *
129      * @return ParticipantStateChange message
130      */
131     public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
132         final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
133         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.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("PMSHInstance0", "1.0.0");
154         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
155
156         participantClStateChangeMsg.setControlLoopId(controlLoopId);
157         participantClStateChangeMsg.setParticipantId(participantId);
158         participantClStateChangeMsg.setTimestamp(Instant.now());
159         participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
160
161         return participantClStateChangeMsg;
162     }
163
164     /**
165      * Method to create ControlLoopUpdateMsg.
166      *
167      * @return ParticipantControlLoopUpdate message
168      */
169     public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
170         final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
171         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
172         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
173
174         clUpdateMsg.setControlLoopId(controlLoopId);
175         clUpdateMsg.setParticipantId(participantId);
176         clUpdateMsg.setParticipantType(participantId);
177
178         ControlLoop controlLoop = new ControlLoop();
179         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
180         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
181         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
182                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
183         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
184             ControlLoopElement clElement = new ControlLoopElement();
185             clElement.setId(UUID.randomUUID());
186
187             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
188             clElementParticipantId.setName(toscaInputEntry.getKey());
189             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
190             clElement.setParticipantId(clElementParticipantId);
191             clElement.setParticipantType(clElementParticipantId);
192
193             clElement.setDefinition(clElementParticipantId);
194             clElement.setState(ControlLoopState.UNINITIALISED);
195             clElement.setDescription(toscaInputEntry.getValue().getDescription());
196             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
197             elements.put(clElement.getId(), clElement);
198         }
199         controlLoop.setElements(elements);
200         controlLoop.setName("PMSHInstance0");
201         controlLoop.setVersion("1.0.0");
202         controlLoop.setDefinition(controlLoopId);
203         clUpdateMsg.setControlLoop(controlLoop);
204         clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
205
206         return clUpdateMsg;
207     }
208
209     /**
210      * Method to create ParticipantControlLoopUpdate using the arguments passed.
211      *
212      * @param jsonFilePath the path of the controlloop content
213      *
214      * @return ParticipantControlLoopUpdate message
215      * @throws CoderException exception while reading the file to object
216      */
217     public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
218             throws CoderException {
219         ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
220                 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
221         return participantControlLoopUpdateMsg;
222     }
223
224     private static ToscaServiceTemplate testControlLoopRead() {
225         Set<String> controlLoopDirectoryContents =
226                 ResourceUtils.getDirectoryContents("src/test/resources/rest/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         assertTrue(atLeastOneControlLoopTested);
240         return toscaServiceTemplate;
241     }
242
243     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
244         try {
245             String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
246             if (controlLoopString == null) {
247                 throw new FileNotFoundException(controlLoopFilePath);
248             }
249
250             ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
251                             controlLoopString, ToscaServiceTemplate.class);
252             return serviceTemplate;
253         } catch (FileNotFoundException e) {
254             LOGGER.error("cannot find YAML file", controlLoopFilePath);
255             throw new IllegalArgumentException(e);
256         }
257     }
258 }