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.policy.main.utils;
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.models.acm.concepts.AutomationComposition;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
40 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantUpdates;
42 import org.onap.policy.clamp.models.acm.concepts.ParticipantUtils;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionUpdate;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantUpdate;
46 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
47 import org.onap.policy.common.utils.coder.Coder;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
51 import org.onap.policy.common.utils.resources.ResourceUtils;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 @NoArgsConstructor(access = AccessLevel.PRIVATE)
59 public final class TestListenerUtils {
61 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
62 private static final Coder CODER = new StandardCoder();
63 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
66 * Method to create a automationComposition from a yaml file.
68 * @return AutomationComposition automation composition
70 public static AutomationComposition createAutomationComposition() {
71 AutomationComposition automationComposition = new AutomationComposition();
72 Map<UUID, AutomationCompositionElement> elements = new LinkedHashMap<>();
73 ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead();
74 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
75 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
76 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
77 AutomationCompositionElement acElement = new AutomationCompositionElement();
78 acElement.setId(UUID.randomUUID());
80 ToscaConceptIdentifier acElementParticipantId = new ToscaConceptIdentifier();
81 acElementParticipantId.setName(toscaInputEntry.getKey());
82 acElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
83 acElement.setParticipantId(acElementParticipantId);
85 acElement.setDefinition(acElementParticipantId);
86 acElement.setState(AutomationCompositionState.UNINITIALISED);
87 acElement.setDescription(toscaInputEntry.getValue().getDescription());
88 acElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
89 elements.put(acElement.getId(), acElement);
91 automationComposition.setElements(elements);
92 automationComposition.setName("PMSHInstance0");
93 automationComposition.setVersion("1.0.0");
95 ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
96 definition.setName("PMSHInstance0");
97 definition.setVersion("1.0.0");
98 automationComposition.setDefinition(definition);
100 return automationComposition;
104 * Method to create AutomationCompositionStateChange message from the arguments passed.
106 * @param automationCompositionOrderedState automationCompositionOrderedState
107 * @return AutomationCompositionStateChange message
109 public static AutomationCompositionStateChange createAutomationCompositionStateChangeMsg(
110 final AutomationCompositionOrderedState automationCompositionOrderedState) {
111 final AutomationCompositionStateChange acStateChangeMsg = new AutomationCompositionStateChange();
113 ToscaConceptIdentifier automationCompositionId = new ToscaConceptIdentifier();
114 automationCompositionId.setName("PMSHInstance0");
115 automationCompositionId.setVersion("1.0.0");
117 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
118 participantId.setName("org.onap.PM_Policy");
119 participantId.setVersion("0.0.0");
121 acStateChangeMsg.setAutomationCompositionId(automationCompositionId);
122 acStateChangeMsg.setParticipantId(participantId);
123 acStateChangeMsg.setTimestamp(Instant.now());
124 acStateChangeMsg.setOrderedState(automationCompositionOrderedState);
126 return acStateChangeMsg;
130 * Method to create AutomationCompositionUpdateMsg.
132 * @return AutomationCompositionUpdate message
134 public static AutomationCompositionUpdate createAutomationCompositionUpdateMsg() {
135 final AutomationCompositionUpdate acUpdateMsg = new AutomationCompositionUpdate();
136 ToscaConceptIdentifier automationCompositionId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
137 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
139 acUpdateMsg.setAutomationCompositionId(automationCompositionId);
140 acUpdateMsg.setParticipantId(participantId);
141 acUpdateMsg.setMessageId(UUID.randomUUID());
142 acUpdateMsg.setTimestamp(Instant.now());
144 Map<UUID, AutomationCompositionElement> elements = new LinkedHashMap<>();
145 ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead();
146 TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
147 Map<String, ToscaNodeTemplate> nodeTemplatesMap =
148 toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
149 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
150 if (ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(toscaInputEntry.getValue(),
151 toscaServiceTemplate)) {
152 AutomationCompositionElement acElement = new AutomationCompositionElement();
153 acElement.setId(UUID.randomUUID());
154 var acParticipantType =
155 ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
157 acElement.setParticipantId(acParticipantType);
158 acElement.setParticipantType(acParticipantType);
160 acElement.setDefinition(
161 new ToscaConceptIdentifier(toscaInputEntry.getKey(), toscaInputEntry.getValue().getVersion()));
162 acElement.setState(AutomationCompositionState.UNINITIALISED);
163 acElement.setDescription(toscaInputEntry.getValue().getDescription());
164 acElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
165 elements.put(acElement.getId(), acElement);
169 List<ParticipantUpdates> participantUpdates = new ArrayList<>();
170 for (AutomationCompositionElement element : elements.values()) {
171 AcmUtils.setServiceTemplatePolicyInfo(element, toscaServiceTemplate);
172 AcmUtils.prepareParticipantUpdate(element, participantUpdates);
174 acUpdateMsg.setParticipantUpdatesList(participantUpdates);
179 * Method to create participantUpdateMsg.
181 * @return ParticipantUpdate message
183 public static ParticipantUpdate createParticipantUpdateMsg() {
184 final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
185 ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
186 ToscaConceptIdentifier participantType =
187 new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "2.3.1");
189 participantUpdateMsg.setParticipantId(participantId);
190 participantUpdateMsg.setTimestamp(Instant.now());
191 participantUpdateMsg.setParticipantType(participantType);
192 participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
193 participantUpdateMsg.setMessageId(UUID.randomUUID());
195 ToscaServiceTemplate toscaServiceTemplate = testAutomationCompositionRead();
196 // Add policies to the toscaServiceTemplate
197 TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
199 List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
200 for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate()
201 .getNodeTemplates().entrySet()) {
202 if (ParticipantUtils.checkIfNodeTemplateIsAutomationCompositionElement(toscaInputEntry.getValue(),
203 toscaServiceTemplate)) {
204 AcmUtils.prepareParticipantDefinitionUpdate(
205 ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties()),
206 toscaInputEntry.getKey(), toscaInputEntry.getValue(),
207 participantDefinitionUpdates, null);
211 participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
212 return participantUpdateMsg;
216 * Method to create AutomationCompositionUpdate using the arguments passed.
218 * @param jsonFilePath the path of the automation composition content
219 * @return AutomationCompositionUpdate message
220 * @throws CoderException exception while reading the file to object
222 public static AutomationCompositionUpdate createParticipantAcUpdateMsgFromJson(String jsonFilePath)
223 throws CoderException {
224 AutomationCompositionUpdate automationCompositionUpdateMsg =
225 CODER.decode(new File(jsonFilePath), AutomationCompositionUpdate.class);
226 return automationCompositionUpdateMsg;
229 private static ToscaServiceTemplate testAutomationCompositionRead() {
230 Set<String> automationCompositionDirectoryContents =
231 ResourceUtils.getDirectoryContents("src/test/resources/utils/servicetemplates");
233 boolean atLeastOneAutomationCompositionTested = false;
234 ToscaServiceTemplate toscaServiceTemplate = null;
236 for (String automationCompositionFilePath : automationCompositionDirectoryContents) {
237 if (!automationCompositionFilePath.endsWith(".yaml")) {
240 atLeastOneAutomationCompositionTested = true;
241 toscaServiceTemplate = testAutomationCompositionYamlSerialization(automationCompositionFilePath);
244 // Add policy_types to the toscaServiceTemplate
245 addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
247 assertTrue(atLeastOneAutomationCompositionTested);
248 return toscaServiceTemplate;
251 private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
252 Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
254 for (String policyTypeFilePath : policyTypeDirectoryContents) {
255 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
257 ToscaServiceTemplate foundPolicyTypeSt =
258 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
260 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
261 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
262 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
263 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
264 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
265 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
267 if (foundPolicyTypeSt.getDataTypes() != null) {
268 if (toscaServiceTemplate.getDataTypes() == null) {
269 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
271 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
275 if (toscaServiceTemplate.getPolicyTypes() == null) {
276 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
278 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
284 * Method to add polcies to the toscaServiceTemplate.
286 * @param toscaServiceTemplate to add policies
288 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
289 Set<String> policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
291 for (String policiesFilePath : policiesDirectoryContents) {
292 if (!policiesFilePath.endsWith("yaml")) {
296 String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
298 ToscaServiceTemplate foundPoliciesSt =
299 yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
300 toscaServiceTemplate.getToscaTopologyTemplate()
301 .setPolicies(foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
305 private static ToscaServiceTemplate testAutomationCompositionYamlSerialization(
306 String automationCompositionFilePath) {
308 String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath);
309 if (automationCompositionString == null) {
310 throw new FileNotFoundException(automationCompositionFilePath);
313 ToscaServiceTemplate serviceTemplate =
314 yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class);
315 return serviceTemplate;
316 } catch (FileNotFoundException e) {
317 LOGGER.error("cannot find YAML file", automationCompositionFilePath);
318 throw new IllegalArgumentException(e);