3ee39d57e14dc7874c976f3cd58613fd2491a6fd
[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.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.ParticipantStateChange;
42 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
43 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
44 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.ParticipantSimulatorParameters;
45 import org.onap.policy.clamp.controlloop.participant.simulator.simulation.SimulationProvider;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
50 import org.onap.policy.common.utils.resources.ResourceUtils;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 public class TestListenerUtils {
58
59     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
60     private static final Coder CODER = new StandardCoder();
61     static CommonTestData commonTestData = new CommonTestData();
62     private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
63
64     private TestListenerUtils() {}
65
66     @Getter
67     private static ParticipantHandler participantHandler;
68
69     /**
70      * Method to initialize participantHandler.
71      */
72     public static void initParticipantHandler() {
73
74         final ParticipantSimulatorParameters participantParameters = commonTestData.toObject(
75                 commonTestData.getParticipantParameterGroupMap(CommonTestData.PARTICIPANT_GROUP_NAME),
76                 ParticipantSimulatorParameters.class);
77
78         SimulationProvider simulationProvider =
79                 new SimulationProvider(participantParameters.getIntermediaryParameters());
80
81         participantHandler = simulationProvider.getIntermediaryApi().getParticipantHandler();
82     }
83
84     /**
85      * Method to create a controlLoop from a yaml file.
86      *
87      * @return ControlLoop controlloop
88      */
89     public static ControlLoop createControlLoop() {
90         ControlLoop controlLoop = new ControlLoop();
91         List<ControlLoopElement> elements = new ArrayList<>();
92         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
93         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
94                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
95         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
96             ControlLoopElement clElement = new ControlLoopElement();
97             clElement.setId(UUID.randomUUID());
98
99             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
100             clElementParticipantId.setName(toscaInputEntry.getKey());
101             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
102             clElement.setParticipantId(clElementParticipantId);
103
104             clElement.setDefinition(clElementParticipantId);
105             clElement.setState(ControlLoopState.UNINITIALISED);
106             clElement.setDescription(toscaInputEntry.getValue().getDescription());
107             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
108             elements.add(clElement);
109         }
110         controlLoop.setElements(elements);
111         controlLoop.setName("PMSHInstance0");
112         controlLoop.setVersion("1.0.0");
113
114         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
115         definition.setName("PMSHInstance0");
116         definition.setVersion("1.0.0");
117         controlLoop.setDefinition(definition);
118
119         return controlLoop;
120     }
121
122     /**
123      * Method to create ParticipantStateChange message from the arguments passed.
124      *
125      * @param participantState participant State
126      *
127      * @return ParticipantStateChange message
128      */
129     public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
130         final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
131         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
132
133         participantStateChangeMsg.setParticipantId(participantId);
134         participantStateChangeMsg.setTimestamp(Instant.now());
135         participantStateChangeMsg.setState(participantState);
136
137         return participantStateChangeMsg;
138     }
139
140     /**
141      * Method to create ControlLoopStateChange message from the arguments passed.
142      *
143      * @param controlLoopOrderedState controlLoopOrderedState
144      *
145      * @return ParticipantControlLoopStateChange message
146      */
147     public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
148             final ControlLoopOrderedState controlLoopOrderedState) {
149         final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
150
151         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
152         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
153
154         participantClStateChangeMsg.setControlLoopId(controlLoopId);
155         participantClStateChangeMsg.setParticipantId(participantId);
156         participantClStateChangeMsg.setTimestamp(Instant.now());
157         participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
158
159         return participantClStateChangeMsg;
160     }
161
162     /**
163      * Method to create ControlLoopUpdateMsg.
164      *
165      * @return ParticipantControlLoopUpdate message
166      */
167     public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
168         final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
169         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0.");
170         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
171
172         clUpdateMsg.setControlLoopId(controlLoopId);
173         clUpdateMsg.setParticipantId(participantId);
174
175         ControlLoop controlLoop = new ControlLoop();
176         List<ControlLoopElement> elements = new ArrayList<>();
177         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
178         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
179                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
180         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
181             ControlLoopElement clElement = new ControlLoopElement();
182             clElement.setId(UUID.randomUUID());
183
184             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
185             clElementParticipantId.setName(toscaInputEntry.getKey());
186             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
187             clElement.setParticipantId(clElementParticipantId);
188
189             clElement.setDefinition(clElementParticipantId);
190             clElement.setState(ControlLoopState.UNINITIALISED);
191             clElement.setDescription(toscaInputEntry.getValue().getDescription());
192             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
193             elements.add(clElement);
194         }
195         controlLoop.setElements(elements);
196         controlLoop.setName("PMSHInstance0");
197         controlLoop.setVersion("1.0.0");
198         controlLoop.setDefinition(controlLoopId);
199         clUpdateMsg.setControlLoop(controlLoop);
200         clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
201
202         return clUpdateMsg;
203     }
204
205     /**
206      * Method to create ParticipantControlLoopUpdate using the arguments passed.
207      *
208      * @param jsonFilePath the path of the controlloop content
209      *
210      * @return ParticipantControlLoopUpdate message
211      * @throws CoderException exception while reading the file to object
212      */
213     public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
214             throws CoderException {
215         ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
216                 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
217         return participantControlLoopUpdateMsg;
218     }
219
220     private static ToscaServiceTemplate testControlLoopRead() {
221         Set<String> controlLoopDirectoryContents =
222                 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
223
224         boolean atLeastOneControlLoopTested = false;
225         ToscaServiceTemplate toscaServiceTemplate = null;
226
227         for (String controlLoopFilePath : controlLoopDirectoryContents) {
228             if (!controlLoopFilePath.endsWith(".yaml")) {
229                 continue;
230             }
231             atLeastOneControlLoopTested = true;
232             toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
233         }
234
235         assertTrue(atLeastOneControlLoopTested);
236         return toscaServiceTemplate;
237     }
238
239     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
240         try {
241             String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
242             if (controlLoopString == null) {
243                 throw new FileNotFoundException(controlLoopFilePath);
244             }
245
246             ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
247                             controlLoopString, ToscaServiceTemplate.class);
248             return serviceTemplate;
249         } catch (FileNotFoundException e) {
250             LOGGER.error("cannot find YAML file", controlLoopFilePath);
251             throw new IllegalArgumentException(e);
252         }
253     }
254 }