c3cc8b7559772df221885bc73c4f62d8f59e7a51
[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.dcae.main.rest;
22
23 import java.io.File;
24 import java.time.Instant;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.UUID;
28 import lombok.Getter;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
38 import org.onap.policy.clamp.controlloop.participant.dcae.main.handler.DcaeProvider;
39 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
40 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ParticipantDcaeParameters;
41 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
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
51 public class TestListenerUtils {
52
53     private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
54     private static final Coder CODER = new StandardCoder();
55     private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
56     static CommonTestData commonTestData = new CommonTestData();
57
58     @Getter
59     private static ParticipantHandler participantHandler;
60
61     /**
62      * Method to initialize participantHandler.
63      */
64     public static void initParticipantHandler() {
65
66         final ParticipantDcaeParameters parameters = commonTestData.toObject(
67                 commonTestData.getParticipantParameterGroupMap(CommonTestData.PARTICIPANT_GROUP_NAME),
68                 ParticipantDcaeParameters.class);
69
70         DcaeProvider dcaeProvider = new DcaeProvider(parameters);
71
72         participantHandler = dcaeProvider.getIntermediaryApi().getParticipantHandler();
73     }
74
75     /**
76      * Method to create a controlLoop from a yaml file.
77      *
78      * @return ControlLoop controlloop
79      */
80     public static ControlLoop createControlLoop() {
81         ControlLoop controlLoop = new ControlLoop();
82         Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
83         ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
84         Map<String, ToscaNodeTemplate> nodeTemplatesMap =
85                 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
86         for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
87             ControlLoopElement clElement = new ControlLoopElement();
88             clElement.setId(UUID.randomUUID());
89
90             ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
91             clElementParticipantId.setName(toscaInputEntry.getKey());
92             clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
93             clElement.setParticipantId(clElementParticipantId);
94
95             clElement.setDefinition(clElementParticipantId);
96             clElement.setState(ControlLoopState.UNINITIALISED);
97             clElement.setDescription(toscaInputEntry.getValue().getDescription());
98             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
99             elements.put(clElement.getId(), clElement);
100         }
101         controlLoop.setElements(elements);
102         controlLoop.setName("PMSHInstance0");
103         controlLoop.setVersion("1.0.0");
104
105         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
106         definition.setName("PMSHInstance0");
107         definition.setVersion("1.0.0");
108         controlLoop.setDefinition(definition);
109
110         return controlLoop;
111     }
112
113     /**
114      * Method to create ParticipantStateChange message from the arguments passed.
115      *
116      * @param participantState participant State
117      *
118      * @return ParticipantStateChange message
119      */
120     public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
121         final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
122         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
123         participantId.setName("CDSParticipant0");
124         participantId.setVersion("1.0.0");
125
126         participantStateChangeMsg.setParticipantId(participantId);
127         participantStateChangeMsg.setTimestamp(Instant.now());
128         participantStateChangeMsg.setState(participantState);
129
130         return participantStateChangeMsg;
131     }
132
133     /**
134      * Method to create ControlLoopStateChange message from the arguments passed.
135      *
136      * @param controlLoopOrderedState controlLoopOrderedState
137      *
138      * @return ParticipantControlLoopStateChange message
139      */
140     public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
141             final ControlLoopOrderedState controlLoopOrderedState) {
142         final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
143
144         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
145         controlLoopId.setName("PMSHInstance0");
146         controlLoopId.setVersion("1.0.0");
147
148         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
149         participantId.setName("CDSParticipant0");
150         participantId.setVersion("1.0.0");
151
152         participantClStateChangeMsg.setControlLoopId(controlLoopId);
153         participantClStateChangeMsg.setParticipantId(participantId);
154         participantClStateChangeMsg.setTimestamp(Instant.now());
155         participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
156
157         return participantClStateChangeMsg;
158     }
159
160     /**
161      * Method to create ControlLoopUpdateMsg.
162      *
163      * @return ParticipantControlLoopUpdate message
164      */
165     public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
166         final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
167         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
168         controlLoopId.setName("PMSHInstance0");
169         controlLoopId.setVersion("1.0.0");
170
171         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
172         participantId.setName("CDSParticipant0");
173         participantId.setVersion("1.0.0");
174
175         clUpdateMsg.setControlLoopId(controlLoopId);
176         clUpdateMsg.setParticipantId(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
192             clElement.setDefinition(clElementParticipantId);
193             clElement.setState(ControlLoopState.UNINITIALISED);
194             clElement.setDescription(toscaInputEntry.getValue().getDescription());
195             clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
196             elements.put(clElement.getId(), clElement);
197         }
198         controlLoop.setElements(elements);
199         controlLoop.setName("PMSHInstance0");
200         controlLoop.setVersion("1.0.0");
201         controlLoop.setDefinition(controlLoopId);
202         clUpdateMsg.setControlLoop(controlLoop);
203         clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
204
205         return clUpdateMsg;
206     }
207
208     /**
209      * Method to create ParticipantHealthCheck message.
210      *
211      * @return ParticipantHealthCheck message
212      */
213     public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
214         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
215         participantId.setName("CDSParticipant0");
216         participantId.setVersion("1.0.0");
217
218         ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
219         controlLoopId.setName("PMSHInstance0");
220         controlLoopId.setVersion("1.0.0");
221
222         final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
223         participantHealthCheckMsg.setParticipantId(participantId);
224         participantHealthCheckMsg.setControlLoopId(controlLoopId);
225         participantHealthCheckMsg.setTimestamp(Instant.now());
226         participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
227
228         return participantHealthCheckMsg;
229     }
230
231     /**
232      * Method to create ParticipantControlLoopUpdate using the arguments passed.
233      *
234      * @param jsonFilePath the path of the controlloop content
235      *
236      * @return ParticipantControlLoopUpdate message
237      * @throws CoderException exception while reading the file to object
238      */
239     public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
240             throws CoderException {
241         ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
242                 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
243         return participantControlLoopUpdateMsg;
244     }
245
246     private static ToscaServiceTemplate testControlLoopRead() {
247         return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
248     }
249
250     private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
251         String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
252         ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
253         return serviceTemplate;
254     }
255 }