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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.dcae.main.rest;
24 import java.time.Instant;
25 import java.util.LinkedHashMap;
27 import java.util.UUID;
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;
51 public class TestListenerUtils {
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();
59 private static ParticipantHandler participantHandler;
62 * Method to initialize participantHandler.
64 public static void initParticipantHandler() {
66 final ParticipantDcaeParameters parameters = commonTestData.toObject(
67 commonTestData.getParticipantParameterGroupMap(CommonTestData.PARTICIPANT_GROUP_NAME),
68 ParticipantDcaeParameters.class);
70 DcaeProvider dcaeProvider = new DcaeProvider(parameters);
72 participantHandler = dcaeProvider.getIntermediaryApi().getParticipantHandler();
76 * Method to create a controlLoop from a yaml file.
78 * @return ControlLoop controlloop
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());
90 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
91 clElementParticipantId.setName(toscaInputEntry.getKey());
92 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
93 clElement.setParticipantId(clElementParticipantId);
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);
101 controlLoop.setElements(elements);
102 controlLoop.setName("PMSHInstance0");
103 controlLoop.setVersion("1.0.0");
105 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
106 definition.setName("PMSHInstance0");
107 definition.setVersion("1.0.0");
108 controlLoop.setDefinition(definition);
114 * Method to create ParticipantStateChange message from the arguments passed.
116 * @param participantState participant State
118 * @return ParticipantStateChange message
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");
126 participantStateChangeMsg.setParticipantId(participantId);
127 participantStateChangeMsg.setTimestamp(Instant.now());
128 participantStateChangeMsg.setState(participantState);
130 return participantStateChangeMsg;
134 * Method to create ControlLoopStateChange message from the arguments passed.
136 * @param controlLoopOrderedState controlLoopOrderedState
138 * @return ParticipantControlLoopStateChange message
140 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
141 final ControlLoopOrderedState controlLoopOrderedState) {
142 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
144 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
145 controlLoopId.setName("PMSHInstance0");
146 controlLoopId.setVersion("1.0.0");
148 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
149 participantId.setName("CDSParticipant0");
150 participantId.setVersion("1.0.0");
152 participantClStateChangeMsg.setControlLoopId(controlLoopId);
153 participantClStateChangeMsg.setParticipantId(participantId);
154 participantClStateChangeMsg.setTimestamp(Instant.now());
155 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
157 return participantClStateChangeMsg;
161 * Method to create ControlLoopUpdateMsg.
163 * @return ParticipantControlLoopUpdate message
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");
171 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
172 participantId.setName("CDSParticipant0");
173 participantId.setVersion("1.0.0");
175 clUpdateMsg.setControlLoopId(controlLoopId);
176 clUpdateMsg.setParticipantId(participantId);
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());
187 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
188 clElementParticipantId.setName(toscaInputEntry.getKey());
189 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
190 clElement.setParticipantId(clElementParticipantId);
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);
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);
209 * Method to create ParticipantHealthCheck message.
211 * @return ParticipantHealthCheck message
213 public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
214 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
215 participantId.setName("CDSParticipant0");
216 participantId.setVersion("1.0.0");
218 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
219 controlLoopId.setName("PMSHInstance0");
220 controlLoopId.setVersion("1.0.0");
222 final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
223 participantHealthCheckMsg.setParticipantId(participantId);
224 participantHealthCheckMsg.setControlLoopId(controlLoopId);
225 participantHealthCheckMsg.setTimestamp(Instant.now());
226 participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
228 return participantHealthCheckMsg;
232 * Method to create ParticipantControlLoopUpdate using the arguments passed.
234 * @param jsonFilePath the path of the controlloop content
236 * @return ParticipantControlLoopUpdate message
237 * @throws CoderException exception while reading the file to object
239 public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
240 throws CoderException {
241 ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
242 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
243 return participantControlLoopUpdateMsg;
246 private static ToscaServiceTemplate testControlLoopRead() {
247 return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
250 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
251 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
252 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
253 return serviceTemplate;