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.ControlLoopStateChange;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
35 import org.onap.policy.common.utils.coder.Coder;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 public class TestListenerUtils {
46 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
47 private static final Coder CODER = new StandardCoder();
48 private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
51 * Method to create a controlLoop from a yaml file.
53 * @return ControlLoop controlloop
55 public static ControlLoop createControlLoop() {
56 ControlLoop controlLoop = new ControlLoop();
57 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
58 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
59 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
60 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
61 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
62 ControlLoopElement clElement = new ControlLoopElement();
63 clElement.setId(UUID.randomUUID());
65 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
66 clElementParticipantId.setName(toscaInputEntry.getKey());
67 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
68 clElement.setParticipantId(clElementParticipantId);
70 clElement.setDefinition(clElementParticipantId);
71 clElement.setState(ControlLoopState.UNINITIALISED);
72 clElement.setDescription(toscaInputEntry.getValue().getDescription());
73 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
74 elements.put(clElement.getId(), clElement);
76 controlLoop.setElements(elements);
77 controlLoop.setName("PMSHInstance0");
78 controlLoop.setVersion("1.0.0");
80 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
81 definition.setName("PMSHInstance0");
82 definition.setVersion("1.0.0");
83 controlLoop.setDefinition(definition);
89 * Method to create ControlLoopStateChange message from the arguments passed.
91 * @param controlLoopOrderedState controlLoopOrderedState
93 * @return ControlLoopStateChange message
95 public static ControlLoopStateChange createControlLoopStateChangeMsg(
96 final ControlLoopOrderedState controlLoopOrderedState) {
97 final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
99 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
100 controlLoopId.setName("PMSHInstance0");
101 controlLoopId.setVersion("1.0.0");
103 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
104 participantId.setName("DCAEParticipant0");
105 participantId.setVersion("1.0.0");
107 clStateChangeMsg.setControlLoopId(controlLoopId);
108 clStateChangeMsg.setParticipantId(participantId);
109 clStateChangeMsg.setTimestamp(Instant.now());
110 clStateChangeMsg.setOrderedState(controlLoopOrderedState);
112 return clStateChangeMsg;
116 * Method to create ControlLoopUpdateMsg.
118 * @return ControlLoopUpdate message
120 public static ControlLoopUpdate createControlLoopUpdateMsg() {
121 final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
122 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier();
123 controlLoopId.setName("PMSHInstance0");
124 controlLoopId.setVersion("1.0.0");
126 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
127 participantId.setName("DCAEParticipant0");
128 participantId.setVersion("1.0.0");
130 clUpdateMsg.setControlLoopId(controlLoopId);
131 clUpdateMsg.setParticipantId(participantId);
133 ControlLoop controlLoop = new ControlLoop();
134 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
135 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
136 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
137 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
138 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
139 ControlLoopElement clElement = new ControlLoopElement();
140 clElement.setId(UUID.randomUUID());
142 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
143 clElementParticipantId.setName(toscaInputEntry.getKey());
144 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
145 clElement.setParticipantId(clElementParticipantId);
147 clElement.setDefinition(clElementParticipantId);
148 clElement.setState(ControlLoopState.UNINITIALISED);
149 clElement.setDescription(toscaInputEntry.getValue().getDescription());
150 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
151 elements.put(clElement.getId(), clElement);
153 controlLoop.setElements(elements);
154 controlLoop.setName("PMSHInstance0");
155 controlLoop.setVersion("1.0.0");
156 controlLoop.setDefinition(controlLoopId);
157 clUpdateMsg.setControlLoop(controlLoop);
163 * Method to create a deep copy of ControlLoopUpdateMsg.
165 * @return ControlLoopUpdate message
167 public static ControlLoopUpdate createCopyControlLoopUpdateMsg(ControlLoopUpdate cpy) {
168 return new ControlLoopUpdate(cpy);
172 * Method to create ControlLoopUpdate using the arguments passed.
174 * @param jsonFilePath the path of the controlloop content
176 * @return ControlLoopUpdate message
177 * @throws CoderException exception while reading the file to object
179 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
180 throws CoderException {
181 ControlLoopUpdate controlLoopUpdateMsg =
182 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
183 return controlLoopUpdateMsg;
186 private static ToscaServiceTemplate testControlLoopRead() {
187 return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
190 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
191 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
192 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
193 return serviceTemplate;