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.ControlLoopUpdate;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantControlLoopStateChange;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantHealthCheck;
36 import org.onap.policy.common.utils.coder.Coder;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 public class TestListenerUtils {
47 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
48 private static final Coder CODER = new StandardCoder();
49 private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
52 * Method to create a controlLoop from a yaml file.
54 * @return ControlLoop controlloop
56 public static ControlLoop createControlLoop() {
57 ControlLoop controlLoop = new ControlLoop();
58 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
59 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
60 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
61 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
62 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
63 ControlLoopElement clElement = new ControlLoopElement();
64 clElement.setId(UUID.randomUUID());
66 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
67 clElementParticipantId.setName(toscaInputEntry.getKey());
68 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
69 clElement.setParticipantId(clElementParticipantId);
71 clElement.setDefinition(clElementParticipantId);
72 clElement.setState(ControlLoopState.UNINITIALISED);
73 clElement.setDescription(toscaInputEntry.getValue().getDescription());
74 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
75 elements.put(clElement.getId(), clElement);
77 controlLoop.setElements(elements);
78 controlLoop.setName("PMSHInstance0");
79 controlLoop.setVersion("1.0.0");
81 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
82 definition.setName("PMSHInstance0");
83 definition.setVersion("1.0.0");
84 controlLoop.setDefinition(definition);
90 * Method to create ControlLoopStateChange message from the arguments passed.
92 * @param controlLoopOrderedState controlLoopOrderedState
94 * @return ParticipantControlLoopStateChange message
96 public static ParticipantControlLoopStateChange createControlLoopStateChangeMsg(
97 final ControlLoopOrderedState controlLoopOrderedState) {
98 final ParticipantControlLoopStateChange participantClStateChangeMsg = new ParticipantControlLoopStateChange();
100 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
101 controlLoopId.setName("PMSHInstance0");
102 controlLoopId.setVersion("1.0.0");
104 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
105 participantId.setName("DCAEParticipant0");
106 participantId.setVersion("1.0.0");
108 participantClStateChangeMsg.setControlLoopId(controlLoopId);
109 participantClStateChangeMsg.setParticipantId(participantId);
110 participantClStateChangeMsg.setTimestamp(Instant.now());
111 participantClStateChangeMsg.setOrderedState(controlLoopOrderedState);
113 return participantClStateChangeMsg;
117 * Method to create ControlLoopUpdateMsg.
119 * @return ControlLoopUpdate message
121 public static ControlLoopUpdate createControlLoopUpdateMsg() {
122 final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
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 clUpdateMsg.setControlLoopId(controlLoopId);
132 clUpdateMsg.setParticipantId(participantId);
134 ControlLoop controlLoop = new ControlLoop();
135 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
136 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
137 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
138 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
139 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
140 ControlLoopElement clElement = new ControlLoopElement();
141 clElement.setId(UUID.randomUUID());
143 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
144 clElementParticipantId.setName(toscaInputEntry.getKey());
145 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
146 clElement.setParticipantId(clElementParticipantId);
148 clElement.setDefinition(clElementParticipantId);
149 clElement.setState(ControlLoopState.UNINITIALISED);
150 clElement.setDescription(toscaInputEntry.getValue().getDescription());
151 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
152 elements.put(clElement.getId(), clElement);
154 controlLoop.setElements(elements);
155 controlLoop.setName("PMSHInstance0");
156 controlLoop.setVersion("1.0.0");
157 controlLoop.setDefinition(controlLoopId);
158 clUpdateMsg.setControlLoop(controlLoop);
159 clUpdateMsg.setControlLoopDefinition(toscaServiceTemplate);
165 * Method to create a deep copy of ControlLoopUpdateMsg.
167 * @return ControlLoopUpdate message
169 public static ControlLoopUpdate createCopyControlLoopUpdateMsg(ControlLoopUpdate cpy) {
170 return new ControlLoopUpdate(cpy);
174 * Method to create ParticipantHealthCheck message.
176 * @return ParticipantHealthCheck message
178 public static ParticipantHealthCheck createParticipantHealthCheckMsg() {
179 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
180 participantId.setName("DCAEParticipant0");
181 participantId.setVersion("1.0.0");
183 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
184 controlLoopId.setName("PMSHInstance0");
185 controlLoopId.setVersion("1.0.0");
187 final ParticipantHealthCheck participantHealthCheckMsg = new ParticipantHealthCheck();
188 participantHealthCheckMsg.setParticipantId(participantId);
189 participantHealthCheckMsg.setControlLoopId(controlLoopId);
190 participantHealthCheckMsg.setTimestamp(Instant.now());
191 participantHealthCheckMsg.setState(ParticipantState.PASSIVE);
193 return participantHealthCheckMsg;
197 * Method to create ControlLoopUpdate using the arguments passed.
199 * @param jsonFilePath the path of the controlloop content
201 * @return ControlLoopUpdate message
202 * @throws CoderException exception while reading the file to object
204 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
205 throws CoderException {
206 ControlLoopUpdate controlLoopUpdateMsg =
207 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
208 return controlLoopUpdateMsg;
211 private static ToscaServiceTemplate testControlLoopRead() {
212 return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
215 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
216 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
217 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
218 return serviceTemplate;