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;
28 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
33 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopUpdate;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStateChange;
37 import org.onap.policy.common.utils.coder.Coder;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 public class TestListenerUtils {
48 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
49 private static final Coder CODER = new StandardCoder();
50 private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
53 * Method to create a controlLoop from a yaml file.
55 * @return ControlLoop controlloop
57 public static ControlLoop createControlLoop() {
58 ControlLoop controlLoop = new ControlLoop();
59 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
60 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
61 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
62 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
63 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
64 ControlLoopElement clElement = new ControlLoopElement();
65 clElement.setId(UUID.randomUUID());
67 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
68 clElementParticipantId.setName(toscaInputEntry.getKey());
69 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
70 clElement.setParticipantId(clElementParticipantId);
72 clElement.setDefinition(clElementParticipantId);
73 clElement.setState(ControlLoopState.UNINITIALISED);
74 clElement.setDescription(toscaInputEntry.getValue().getDescription());
75 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
76 elements.put(clElement.getId(), clElement);
78 controlLoop.setElements(elements);
79 controlLoop.setName("PMSHInstance0");
80 controlLoop.setVersion("1.0.0");
82 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
83 definition.setName("PMSHInstance0");
84 definition.setVersion("1.0.0");
85 controlLoop.setDefinition(definition);
91 * Method to create ParticipantStateChange message from the arguments passed.
93 * @param participantState participant State
95 * @return ParticipantStateChange message
97 public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
98 final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
99 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
100 participantId.setName("DCAEParticipant0");
101 participantId.setVersion("1.0.0");
103 participantStateChangeMsg.setParticipantId(participantId);
104 participantStateChangeMsg.setTimestamp(Instant.now());
105 participantStateChangeMsg.setState(participantState);
107 return participantStateChangeMsg;
111 * Method to create ControlLoopStateChange message from the arguments passed.
113 * @param controlLoopOrderedState controlLoopOrderedState
115 * @return ParticipantControlLoopStateChange message
117 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
118 final ControlLoopOrderedState controlLoopOrderedState) {
119 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
121 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
122 controlLoopId.setName("PMSHInstance0");
123 controlLoopId.setVersion("1.0.0");
125 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
126 participantId.setName("DCAEParticipant0");
127 participantId.setVersion("1.0.0");
129 participantClStateChangeMsg.setControlLoopId(controlLoopId);
130 participantClStateChangeMsg.setParticipantId(participantId);
131 participantClStateChangeMsg.setTimestamp(Instant.now());
132 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
134 return participantClStateChangeMsg;
138 * Method to create ControlLoopUpdateMsg.
140 * @return ParticipantControlLoopUpdate message
142 public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
143 final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
144 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
145 controlLoopId.setName("PMSHInstance0");
146 controlLoopId.setVersion("1.0.0");
148 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
149 participantId.setName("DCAEParticipant0");
150 participantId.setVersion("1.0.0");
152 clUpdateMsg.setControlLoopId(controlLoopId);
153 clUpdateMsg.setParticipantId(participantId);
155 ControlLoop controlLoop = new ControlLoop();
156 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
157 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
158 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
159 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
160 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
161 ControlLoopElement clElement = new ControlLoopElement();
162 clElement.setId(UUID.randomUUID());
164 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
165 clElementParticipantId.setName(toscaInputEntry.getKey());
166 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
167 clElement.setParticipantId(clElementParticipantId);
169 clElement.setDefinition(clElementParticipantId);
170 clElement.setState(ControlLoopState.UNINITIALISED);
171 clElement.setDescription(toscaInputEntry.getValue().getDescription());
172 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
173 elements.put(clElement.getId(), clElement);
175 controlLoop.setElements(elements);
176 controlLoop.setName("PMSHInstance0");
177 controlLoop.setVersion("1.0.0");
178 controlLoop.setDefinition(controlLoopId);
179 clUpdateMsg.setControlLoop(controlLoop);
180 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
186 * Method to create ParticipantHealthCheck message.
188 * @return ParticipantHealthCheck message
190 public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
191 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
192 participantId.setName("DCAEParticipant0");
193 participantId.setVersion("1.0.0");
195 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
196 controlLoopId.setName("PMSHInstance0");
197 controlLoopId.setVersion("1.0.0");
199 final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
200 participantHealthCheckMsg.setParticipantId(participantId);
201 participantHealthCheckMsg.setControlLoopId(controlLoopId);
202 participantHealthCheckMsg.setTimestamp(Instant.now());
203 participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
205 return participantHealthCheckMsg;
209 * Method to create ParticipantControlLoopUpdate using the arguments passed.
211 * @param jsonFilePath the path of the controlloop content
213 * @return ParticipantControlLoopUpdate message
214 * @throws CoderException exception while reading the file to object
216 public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
217 throws CoderException {
218 ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
219 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
220 return participantControlLoopUpdateMsg;
223 private static ToscaServiceTemplate testControlLoopRead() {
224 return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
227 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
228 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
229 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
230 return serviceTemplate;