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.clamp.controlloop.participant.dcae.main.parameters.CommonTestData;
38 import org.onap.policy.common.utils.coder.Coder;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47 public class TestListenerUtils {
49 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
50 private static final Coder CODER = new StandardCoder();
51 private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
52 static CommonTestData commonTestData = new CommonTestData();
55 * Method to create a controlLoop from a yaml file.
57 * @return ControlLoop controlloop
59 public static ControlLoop createControlLoop() {
60 ControlLoop controlLoop = new ControlLoop();
61 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
62 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
63 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
64 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
65 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
66 ControlLoopElement clElement = new ControlLoopElement();
67 clElement.setId(UUID.randomUUID());
69 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
70 clElementParticipantId.setName(toscaInputEntry.getKey());
71 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
72 clElement.setParticipantId(clElementParticipantId);
74 clElement.setDefinition(clElementParticipantId);
75 clElement.setState(ControlLoopState.UNINITIALISED);
76 clElement.setDescription(toscaInputEntry.getValue().getDescription());
77 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
78 elements.put(clElement.getId(), clElement);
80 controlLoop.setElements(elements);
81 controlLoop.setName("PMSHInstance0");
82 controlLoop.setVersion("1.0.0");
84 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
85 definition.setName("PMSHInstance0");
86 definition.setVersion("1.0.0");
87 controlLoop.setDefinition(definition);
93 * Method to create ParticipantStateChange message from the arguments passed.
95 * @param participantState participant State
97 * @return ParticipantStateChange message
99 public static ParticipantStateChange createParticipantStateChangeMsg(final ParticipantState participantState) {
100 final ParticipantStateChange participantStateChangeMsg = new ParticipantStateChange();
101 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
102 participantId.setName("DCAEParticipant0");
103 participantId.setVersion("1.0.0");
105 participantStateChangeMsg.setParticipantId(participantId);
106 participantStateChangeMsg.setTimestamp(Instant.now());
107 participantStateChangeMsg.setState(participantState);
109 return participantStateChangeMsg;
113 * Method to create ControlLoopStateChange message from the arguments passed.
115 * @param controlLoopOrderedState controlLoopOrderedState
117 * @return ParticipantControlLoopStateChange message
119 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
120 final ControlLoopOrderedState controlLoopOrderedState) {
121 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
123 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
124 controlLoopId.setName("PMSHInstance0");
125 controlLoopId.setVersion("1.0.0");
127 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
128 participantId.setName("DCAEParticipant0");
129 participantId.setVersion("1.0.0");
131 participantClStateChangeMsg.setControlLoopId(controlLoopId);
132 participantClStateChangeMsg.setParticipantId(participantId);
133 participantClStateChangeMsg.setTimestamp(Instant.now());
134 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
136 return participantClStateChangeMsg;
140 * Method to create ControlLoopUpdateMsg.
142 * @return ParticipantControlLoopUpdate message
144 public static ParticipantControlLoopUpdate createControlLoopUpdateMsg() {
145 final ParticipantControlLoopUpdate clUpdateMsg = new ParticipantControlLoopUpdate();
146 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
147 controlLoopId.setName("PMSHInstance0");
148 controlLoopId.setVersion("1.0.0");
150 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
151 participantId.setName("DCAEParticipant0");
152 participantId.setVersion("1.0.0");
154 clUpdateMsg.setControlLoopId(controlLoopId);
155 clUpdateMsg.setParticipantId(participantId);
157 ControlLoop controlLoop = new ControlLoop();
158 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
159 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
160 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
161 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
162 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
163 ControlLoopElement clElement = new ControlLoopElement();
164 clElement.setId(UUID.randomUUID());
166 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
167 clElementParticipantId.setName(toscaInputEntry.getKey());
168 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
169 clElement.setParticipantId(clElementParticipantId);
171 clElement.setDefinition(clElementParticipantId);
172 clElement.setState(ControlLoopState.UNINITIALISED);
173 clElement.setDescription(toscaInputEntry.getValue().getDescription());
174 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
175 elements.put(clElement.getId(), clElement);
177 controlLoop.setElements(elements);
178 controlLoop.setName("PMSHInstance0");
179 controlLoop.setVersion("1.0.0");
180 controlLoop.setDefinition(controlLoopId);
181 clUpdateMsg.setControlLoop(controlLoop);
182 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
188 * Method to create ParticipantHealthCheck message.
190 * @return ParticipantHealthCheck message
192 public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
193 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
194 participantId.setName("DCAEParticipant0");
195 participantId.setVersion("1.0.0");
197 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
198 controlLoopId.setName("PMSHInstance0");
199 controlLoopId.setVersion("1.0.0");
201 final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
202 participantHealthCheckMsg.setParticipantId(participantId);
203 participantHealthCheckMsg.setControlLoopId(controlLoopId);
204 participantHealthCheckMsg.setTimestamp(Instant.now());
205 participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
207 return participantHealthCheckMsg;
211 * Method to create ParticipantControlLoopUpdate using the arguments passed.
213 * @param jsonFilePath the path of the controlloop content
215 * @return ParticipantControlLoopUpdate message
216 * @throws CoderException exception while reading the file to object
218 public static ParticipantControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
219 throws CoderException {
220 ParticipantControlLoopUpdate participantControlLoopUpdateMsg =
221 CODER.decode(new File(jsonFilePath), ParticipantControlLoopUpdate.class);
222 return participantControlLoopUpdateMsg;
225 private static ToscaServiceTemplate testControlLoopRead() {
226 return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
229 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
230 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
231 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
232 return serviceTemplate;