f434002599206f030f6318faf86f25e1ed8d9b6b
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.simulator.main.rest;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
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;
31 import java.util.Map;
32 import java.util.Set;
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;
58
59 @NoArgsConstructor(access = AccessLevel.PRIVATE)
60 public final class TestListenerUtils {
61
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);
66
67     /**
68      * Method to create a automationComposition from a yaml file.
69      *
70      * @return AutomationComposition automation composition
71      */
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());
81
82             ToscaConceptIdentifier acElementParticipantId = new ToscaConceptIdentifier();
83             acElementParticipantId.setName(toscaInputEntry.getKey());
84             acElementParticipantId.setVersion(toscaInputEntry.getValue().getVersion());
85             acElement.setParticipantId(acElementParticipantId);
86             acElement.setParticipantType(acElementParticipantId);
87
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);
93         }
94         automationComposition.setElements(elements);
95         automationComposition.setName("PMSHInstance0");
96         automationComposition.setVersion("1.0.0");
97
98         ToscaConceptIdentifier definition = new ToscaConceptIdentifier();
99         definition.setName("PMSHInstance0");
100         definition.setVersion("1.0.0");
101         automationComposition.setDefinition(definition);
102
103         return automationComposition;
104     }
105
106     /**
107      * Method to create AutomationCompositionStateChange message from the arguments passed.
108      *
109      * @param automationCompositionOrderedState automationCompositionOrderedState
110      *
111      * @return AutomationCompositionStateChange message
112      */
113     public static AutomationCompositionStateChange createAutomationCompositionStateChangeMsg(
114         final AutomationCompositionOrderedState automationCompositionOrderedState) {
115         final AutomationCompositionStateChange acStateChangeMsg = new AutomationCompositionStateChange();
116
117         ToscaConceptIdentifier automationCompositionId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
118         ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.0");
119
120         acStateChangeMsg.setAutomationCompositionId(automationCompositionId);
121         acStateChangeMsg.setParticipantId(participantId);
122         acStateChangeMsg.setTimestamp(Instant.now());
123         acStateChangeMsg.setOrderedState(automationCompositionOrderedState);
124
125         return acStateChangeMsg;
126     }
127
128     /**
129      * Method to create AutomationCompositionUpdateMsg.
130      *
131      * @return AutomationCompositionUpdate message
132      */
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");
137
138         acUpdateMsg.setAutomationCompositionId(automationCompositionId);
139         acUpdateMsg.setParticipantId(participantId);
140         acUpdateMsg.setMessageId(UUID.randomUUID());
141         acUpdateMsg.setTimestamp(Instant.now());
142
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());
154
155                 acElement.setParticipantId(acParticipantType);
156                 acElement.setParticipantType(acParticipantType);
157
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);
164             }
165         }
166
167         List<ParticipantUpdates> participantUpdates = new ArrayList<>();
168         for (AutomationCompositionElement element : elements.values()) {
169             AcmUtils.prepareParticipantUpdate(element, participantUpdates);
170         }
171         acUpdateMsg.setParticipantUpdatesList(participantUpdates);
172         return acUpdateMsg;
173     }
174
175     /**
176      * Method to create participantUpdateMsg.
177      *
178      * @return ParticipantUpdate message
179      */
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");
185
186         participantUpdateMsg.setParticipantId(participantId);
187         participantUpdateMsg.setTimestamp(Instant.now());
188         participantUpdateMsg.setParticipantType(participantType);
189         participantUpdateMsg.setTimestamp(Instant.ofEpochMilli(3000));
190         participantUpdateMsg.setMessageId(UUID.randomUUID());
191
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);
203             }
204         }
205
206         participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
207         return participantUpdateMsg;
208     }
209
210     /**
211      * Method to create AutomationCompositionUpdate using the arguments passed.
212      *
213      * @param jsonFilePath the path of the automation composition content
214      *
215      * @return AutomationCompositionUpdate message
216      * @throws CoderException exception while reading the file to object
217      */
218     public static AutomationCompositionUpdate createParticipantAcUpdateMsgFromJson(String jsonFilePath)
219         throws CoderException {
220         AutomationCompositionUpdate automationCompositionUpdateMsg =
221             CODER.decode(new File(jsonFilePath), AutomationCompositionUpdate.class);
222         return automationCompositionUpdateMsg;
223     }
224
225     private static ToscaServiceTemplate testAutomationCompositionRead() {
226         Set<String> automationCompositionDirectoryContents =
227             ResourceUtils.getDirectoryContents("src/test/resources/rest/servicetemplates");
228
229         boolean atLeastOneAutomationCompositionTested = false;
230         ToscaServiceTemplate toscaServiceTemplate = null;
231
232         for (String automationCompositionFilePath : automationCompositionDirectoryContents) {
233             if (!automationCompositionFilePath.endsWith(".yaml")) {
234                 continue;
235             }
236             atLeastOneAutomationCompositionTested = true;
237             toscaServiceTemplate = testAutomationCompositionYamlSerialization(automationCompositionFilePath);
238         }
239
240         assertTrue(atLeastOneAutomationCompositionTested);
241         return toscaServiceTemplate;
242     }
243
244     private static ToscaServiceTemplate testAutomationCompositionYamlSerialization(
245         String automationCompositionFilePath) {
246         try {
247             String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath);
248             if (automationCompositionString == null) {
249                 throw new FileNotFoundException(automationCompositionFilePath);
250             }
251
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);
258         }
259     }
260 }