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.simulator.main.rest;
23 import static org.junit.Assert.assertTrue;
26 import java.io.FileNotFoundException;
27 import java.time.Instant;
28 import java.util.LinkedHashMap;
31 import java.util.UUID;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
37 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
38 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
39 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.CommonTestData;
40 import org.onap.policy.common.utils.coder.Coder;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
44 import org.onap.policy.common.utils.resources.ResourceUtils;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class TestListenerUtils {
53 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
54 private static final Coder CODER = new StandardCoder();
55 static CommonTestData commonTestData = new CommonTestData();
56 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
58 private TestListenerUtils() {}
61 * Method to create a controlLoop from a yaml file.
63 * @return ControlLoop controlloop
65 public static ControlLoop createControlLoop() {
66 ControlLoop controlLoop = new ControlLoop();
67 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
68 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
69 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
70 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
71 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
72 ControlLoopElement clElement = new ControlLoopElement();
73 clElement.setId(UUID.randomUUID());
75 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
76 clElementParticipantId.setName(toscaInputEntry.getKey());
77 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
78 clElement.setParticipantId(clElementParticipantId);
79 clElement.setParticipantType(clElementParticipantId);
81 clElement.setDefinition(clElementParticipantId);
82 clElement.setState(ControlLoopState.UNINITIALISED);
83 clElement.setDescription(toscaInputEntry.getValue().getDescription());
84 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
85 elements.put(clElement.getId(), clElement);
87 controlLoop.setElements(elements);
88 controlLoop.setName("PMSHInstance0");
89 controlLoop.setVersion("1.0.0");
91 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
92 definition.setName("PMSHInstance0");
93 definition.setVersion("1.0.0");
94 controlLoop.setDefinition(definition);
100 * Method to create ControlLoopStateChange message from the arguments passed.
102 * @param controlLoopOrderedState controlLoopOrderedState
104 * @return ControlLoopStateChange message
106 public static ControlLoopStateChange createControlLoopStateChangeMsg(
107 final ControlLoopOrderedState controlLoopOrderedState) {
108 final ControlLoopStateChange clStateChangeMsg = new ControlLoopStateChange();
110 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
111 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
113 clStateChangeMsg.setControlLoopId(controlLoopId);
114 clStateChangeMsg.setParticipantId(participantId);
115 clStateChangeMsg.setTimestamp(Instant.now());
116 clStateChangeMsg.setOrderedState(controlLoopOrderedState);
118 return clStateChangeMsg;
122 * Method to create ControlLoopUpdateMsg.
124 * @return ControlLoopUpdate message
126 public static ControlLoopUpdate createControlLoopUpdateMsg() {
127 final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
128 ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
129 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
131 clUpdateMsg.setControlLoopId(controlLoopId);
132 clUpdateMsg.setParticipantId(participantId);
133 clUpdateMsg.setParticipantType(participantId);
135 ControlLoop controlLoop = new ControlLoop();
136 Map<UUID, ControlLoopElement> elements = new LinkedHashMap<>();
137 ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead();
138 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
139 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
140 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
141 ControlLoopElement clElement = new ControlLoopElement();
142 clElement.setId(UUID.randomUUID());
144 ToscaConceptIdentifier clElementParticipantId = new ToscaConceptIdentifier();
145 clElementParticipantId.setName(toscaInputEntry.getKey());
146 clElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
147 clElement.setParticipantId(clElementParticipantId);
148 clElement.setParticipantType(clElementParticipantId);
150 clElement.setDefinition(clElementParticipantId);
151 clElement.setState(ControlLoopState.UNINITIALISED);
152 clElement.setDescription(toscaInputEntry.getValue().getDescription());
153 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
154 elements.put(clElement.getId(), clElement);
156 controlLoop.setElements(elements);
157 controlLoop.setName("PMSHInstance0");
158 controlLoop.setVersion("1.0.0");
159 controlLoop.setDefinition(controlLoopId);
160 clUpdateMsg.setControlLoop(controlLoop);
166 * Method to create ControlLoopUpdate using the arguments passed.
168 * @param jsonFilePath the path of the controlloop content
170 * @return ControlLoopUpdate message
171 * @throws CoderException exception while reading the file to object
173 public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
174 throws CoderException {
175 ControlLoopUpdate controlLoopUpdateMsg =
176 CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
177 return controlLoopUpdateMsg;
180 private static ToscaServiceTemplate testControlLoopRead() {
181 Set<String> controlLoopDirectoryContents =
182 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
184 boolean atLeastOneControlLoopTested = false;
185 ToscaServiceTemplate toscaServiceTemplate = null;
187 for (String controlLoopFilePath : controlLoopDirectoryContents) {
188 if (!controlLoopFilePath.endsWith(".yaml")) {
191 atLeastOneControlLoopTested = true;
192 toscaServiceTemplate = testControlLoopYamlSerialization(controlLoopFilePath);
195 assertTrue(atLeastOneControlLoopTested);
196 return toscaServiceTemplate;
199 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
201 String controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
202 if (controlLoopString == null) {
203 throw new FileNotFoundException(controlLoopFilePath);
206 ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
207 controlLoopString, ToscaServiceTemplate.class);
208 return serviceTemplate;
209 } catch (FileNotFoundException e) {
210 LOGGER.error("cannot find YAML file", controlLoopFilePath);
211 throw new IllegalArgumentException(e);