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.acm.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.ArrayList;
29 import java.util.LinkedHashMap;
30 import java.util.List;
33 import java.util.UUID;
34 import lombok.AccessLevel;
35 import lombok.NoArgsConstructor;
36 import org.onap.policy.clamp.acm.participant.simulator.main.parameters.CommonTestData;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
42 import org.onap.policy.clamp.models.acm.concepts.ParticipantUpdates;
43 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionUpdate;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
47 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
48 import org.onap.policy.common.utils.coder.Coder;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
52 import org.onap.policy.common.utils.resources.ResourceUtils;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 @NoArgsConstructor(access = AccessLevel.PRIVATE)
60 public final class TestListenerUtils {
62 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
63 private static final Coder CODER = new StandardCoder();
64 static CommonTestData commonTestData = new CommonTestData();
65 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
68 * Method to create a automationComposition from a yaml file.
70 * @return AutomationComposition automation composition
72 public static AutomationComposition createAutomationComposition() {
73 AutomationComposition automationComposition = new AutomationComposition();
74 Map<UUID, AutomationCompositionElement> elements = new LinkedHashMap<>();
75 ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead();
76 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
77 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
78 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
79 AutomationCompositionElement acElement = new AutomationCompositionElement();
80 acElement.setId(UUID.randomUUID());
82 ToscaConceptIdentifier acElementParticipantId = new ToscaConceptIdentifier();
83 acElementParticipantId.setName(toscaInputEntry.getKey());
84 acElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
85 acElement.setParticipantId(acElementParticipantId);
86 acElement.setParticipantType(acElementParticipantId);
88 acElement.setDefinition(acElementParticipantId);
89 acElement.setState(AutomationCompositionState.UNINITIALISED);
90 acElement.setDescription(toscaInputEntry.getValue().getDescription());
91 acElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
92 elements.put(acElement.getId(), acElement);
94 automationComposition.setElements(elements);
95 automationComposition.setName("PMSHInstance0");
96 automationComposition.setVersion("1.0.0");
98 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
99 definition.setName("PMSHInstance0");
100 definition.setVersion("1.0.0");
101 automationComposition.setDefinition(definition);
103 return automationComposition;
107 * Method to create AutomationCompositionStateChange message from the arguments passed.
109 * @param automationCompositionOrderedState automationCompositionOrderedState
111 * @return AutomationCompositionStateChange message
113 public static AutomationCompositionStateChange createAutomationCompositionStateChangeMsg(
114 final AutomationCompositionOrderedState automationCompositionOrderedState) {
115 final AutomationCompositionStateChange acStateChangeMsg = new AutomationCompositionStateChange();
117 ToscaConceptIdentifier automationCompositionId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
118 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
120 acStateChangeMsg.setAutomationCompositionId(automationCompositionId);
121 acStateChangeMsg.setParticipantId(participantId);
122 acStateChangeMsg.setTimestamp(Instant.now());
123 acStateChangeMsg.setOrderedState(automationCompositionOrderedState);
125 return acStateChangeMsg;
129 * Method to create AutomationCompositionUpdateMsg.
131 * @return AutomationCompositionUpdate message
133 public static AutomationCompositionUpdate createAutomationCompositionUpdateMsg() {
134 final AutomationCompositionUpdate acUpdateMsg = new AutomationCompositionUpdate();
135 ToscaConceptIdentifier automationCompositionId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
136 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
138 acUpdateMsg.setAutomationCompositionId(automationCompositionId);
139 acUpdateMsg.setParticipantId(participantId);
140 acUpdateMsg.setMessageId(UUID.randomUUID());
141 acUpdateMsg.setTimestamp(Instant.now());
143 Map<UUID, AutomationCompositionElement> elements = new LinkedHashMap<>();
144 ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead();
145 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
146 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
147 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
148 if (ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(toscaInputEntry.getValue(),
149 toscaServiceTemplate)) {
150 AutomationCompositionElement acElement = new AutomationCompositionElement();
151 acElement.setId(UUID.randomUUID());
152 var acParticipantType =
153 ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
155 acElement.setParticipantId(acParticipantType);
156 acElement.setParticipantType(acParticipantType);
158 acElement.setDefinition(
159 new ToscaConceptIdentifier(toscaInputEntry.getKey(), toscaInputEntry.getValue().getVersion()));
160 acElement.setState(AutomationCompositionState.UNINITIALISED);
161 acElement.setDescription(toscaInputEntry.getValue().getDescription());
162 acElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
163 elements.put(acElement.getId(), acElement);
167 List<ParticipantUpdates> participantUpdates = new ArrayList<>();
168 for (AutomationCompositionElement element : elements.values()) {
169 AcmUtils.prepareParticipantUpdate(element, participantUpdates);
171 acUpdateMsg.setParticipantUpdatesList(participantUpdates);
176 * Method to create participantUpdateMsg.
178 * @return ParticipantUpdate message
180 public static ParticipantUpdate createParticipantUpdateMsg() {
181 final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
182 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
183 ToscaConceptIdentifier participantType =
184 new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1");
186 participantUpdateMsg.setParticipantId(participantId);
187 participantUpdateMsg.setTimestamp(Instant.now());
188 participantUpdateMsg.setParticipantType(participantType);
189 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
190 participantUpdateMsg.setMessageId(UUID.randomUUID());
192 ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead();
193 // Add policies to the toscaServiceTemplate
194 List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
195 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate()
196 .getNodeTemplates().entrySet()) {
197 if (ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(toscaInputEntry.getValue(),
198 toscaServiceTemplate)) {
199 var acParticipantType =
200 ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
201 AcmUtils.prepareParticipantDefinitionUpdate(acParticipantType, toscaInputEntry.getKey(),
202 toscaInputEntry.getValue(), participantDefinitionUpdates, null);
206 participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
207 return participantUpdateMsg;
211 * Method to create AutomationCompositionUpdate using the arguments passed.
213 * @param jsonFilePath the path of the automation composition content
215 * @return AutomationCompositionUpdate message
216 * @throws CoderException exception while reading the file to object
218 public static AutomationCompositionUpdate createParticipantAcUpdateMsgFromJson(String jsonFilePath)
219 throws CoderException {
220 AutomationCompositionUpdate automationCompositionUpdateMsg =
221 CODER.decode(new File(jsonFilePath), AutomationCompositionUpdate.class);
222 return automationCompositionUpdateMsg;
225 private static ToscaServiceTemplate testAutomationCompositionRead() {
226 Set<String> automationCompositionDirectoryContents =
227 ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
229 boolean atLeastOneAutomationCompositionTested = false;
230 ToscaServiceTemplate toscaServiceTemplate = null;
232 for (String automationCompositionFilePath : automationCompositionDirectoryContents) {
233 if (!automationCompositionFilePath.endsWith(".yaml")) {
236 atLeastOneAutomationCompositionTested = true;
237 toscaServiceTemplate = testAutomationCompositionYamlSerialization(automationCompositionFilePath);
240 assertTrue(atLeastOneAutomationCompositionTested);
241 return toscaServiceTemplate;
244 private static ToscaServiceTemplate testAutomationCompositionYamlSerialization(
245 String automationCompositionFilePath) {
247 String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath);
248 if (automationCompositionString == null) {
249 throw new FileNotFoundException(automationCompositionFilePath);
252 ToscaServiceTemplate serviceTemplate =
253 yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class);
254 return serviceTemplate;
255 } catch (FileNotFoundException e) {
256 LOGGER.error("cannot find YAML file", automationCompositionFilePath);
257 throw new IllegalArgumentException(e);