1536a0be0d4df72a5e9050e26f46810cf17783d0
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2024 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.intermediary.main.parameters;
22
23 import java.io.File;
24 import java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.TreeMap;
30 import java.util.UUID;
31 import org.onap.policy.clamp.acm.participant.intermediary.handler.DummyParticipantParameters;
32 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantIntermediaryParameters;
33 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
34 import org.onap.policy.clamp.models.acm.concepts.AcElementRestart;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
38 import org.onap.policy.clamp.models.acm.concepts.DeployState;
39 import org.onap.policy.clamp.models.acm.concepts.LockState;
40 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
42 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
43 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionStateChange;
44 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
45 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
46 import org.onap.policy.common.endpoints.parameters.TopicParameters;
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.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
52
53 /**
54  * Class to hold/create all parameters for test cases.
55  */
56 public class CommonTestData {
57     public static final String PARTICIPANT_GROUP_NAME = "AutomationCompositionParticipantGroup";
58     public static final String DESCRIPTION = "Participant description";
59     public static final long TIME_INTERVAL = 2000;
60     public static final List<TopicParameters> TOPIC_PARAMS = List.of(getTopicParams());
61     public static final Coder CODER = new StandardCoder();
62     public static final UUID AC_ID_0 = UUID.randomUUID();
63     public static final UUID AC_ID_1 = UUID.randomUUID();
64     public static final UUID PARTCICIPANT_ID = UUID.randomUUID();
65     public static final UUID REPLICA_ID = UUID.randomUUID();
66
67     /**
68      * Get ParticipantIntermediaryParameters.
69      *
70      * @return ParticipantIntermediaryParameters
71      */
72     public static ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
73         try {
74             return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
75                     ParticipantIntermediaryParameters.class);
76         } catch (final CoderException e) {
77             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
78         }
79     }
80
81     /**
82      * Get ParticipantParameters.
83      *
84      * @return ParticipantParameters
85      */
86     public static DummyParticipantParameters getParticipantParameters() {
87         try {
88             return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), DummyParticipantParameters.class);
89         } catch (final CoderException e) {
90             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
91         }
92     }
93
94     /**
95      * Returns a property map for a Parameters map for test cases.
96      *
97      * @param name name of the parameters
98      * @return a property map suitable for constructing an object
99      */
100     public static Map<String, Object> getParametersMap(final String name) {
101         final Map<String, Object> map = new TreeMap<>();
102         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
103         return map;
104     }
105
106     /**
107      * Returns a property map for a intermediaryParameters map for test cases.
108      *
109      * @param name name of the parameters
110      * @return a property map suitable for constructing an object
111      */
112     public static Map<String, Object> getIntermediaryParametersMap(final String name) {
113         final Map<String, Object> map = new TreeMap<>();
114         map.put("name", name);
115         map.put("participantId", getParticipantId());
116         map.put("description", DESCRIPTION);
117         map.put("reportingTimeIntervalMs", TIME_INTERVAL);
118         map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
119         var supportedElementType = new ParticipantSupportedElementType();
120         supportedElementType.setTypeName("org.onap.policy.clamp.acm.HttpAutomationCompositionElement");
121         supportedElementType.setTypeVersion("1.0.0");
122         map.put("participantSupportedElementTypes", List.of(supportedElementType));
123
124         return map;
125     }
126
127     /**
128      * Returns a property map for a TopicParameters map for test cases.
129      *
130      * @param isEmpty boolean value to represent that object created should be empty or not
131      * @return a property map suitable for constructing an object
132      */
133     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
134         final Map<String, Object> map = new TreeMap<>();
135         if (!isEmpty) {
136             map.put("topicSources", TOPIC_PARAMS);
137             map.put("topicSinks", TOPIC_PARAMS);
138         }
139         return map;
140     }
141
142     /**
143      * Returns topic parameters for test cases.
144      *
145      * @return topic parameters
146      */
147     public static TopicParameters getTopicParams() {
148         final var topicParams = new TopicParameters();
149         topicParams.setTopic("policy-acruntime-participant");
150         topicParams.setTopicCommInfrastructure("NOOP");
151         topicParams.setServers(List.of("localhost"));
152         return topicParams;
153     }
154
155     /**
156      * Returns participantId for test cases.
157      *
158      * @return participant Id
159      */
160     public static UUID getParticipantId() {
161         return PARTCICIPANT_ID;
162     }
163
164     public static UUID getReplicaId() {
165         return REPLICA_ID;
166     }
167
168     public static UUID getRndParticipantId() {
169         return UUID.randomUUID();
170     }
171
172     public static ToscaConceptIdentifier getDefinition() {
173         return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
174     }
175
176     /**
177      * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
178      *
179      * @return automationCompositionMap
180      *
181      * @throws CoderException if there is an error with .json file.
182      */
183     public static Map<UUID, AutomationComposition> getTestAutomationCompositionMap() {
184         var automationCompositions = getTestAutomationCompositions();
185         var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
186         Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
187         automationCompositionMap.put(automationComposition.getInstanceId(), automationComposition);
188         return automationCompositionMap;
189     }
190
191     /**
192      * Returns List of AutomationComposition for test cases.
193      *
194      * @return AutomationCompositions
195      *
196      * @throws CoderException if there is an error with .json file.
197      */
198     public static AutomationCompositions getTestAutomationCompositions() {
199         try {
200             var automationCompositions =
201                     new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
202                             AutomationCompositions.class);
203             automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_0);
204             automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_1);
205             return automationCompositions;
206         } catch (Exception e) {
207             throw new RuntimeException("cannot read TestAutomationCompositions.json");
208         }
209     }
210
211     /**
212      * Return a AutomationCompositionStateChange.
213      *
214      * @param participantId the participantId
215      * @param instanceId the AutomationComposition Id
216      * @param deployOrder a DeployOrder
217      * @param lockOrder a LockOrder
218      * @return a AutomationCompositionStateChange
219      */
220     public static AutomationCompositionStateChange getStateChange(UUID participantId, UUID instanceId,
221             DeployOrder deployOrder, LockOrder lockOrder) {
222         var stateChange = new AutomationCompositionStateChange();
223         stateChange.setStartPhase(0);
224         stateChange.setAutomationCompositionId(instanceId);
225         stateChange.setParticipantId(participantId);
226         stateChange.setMessageId(UUID.randomUUID());
227         stateChange.setDeployOrderedState(deployOrder);
228         stateChange.setLockOrderedState(lockOrder);
229         stateChange.setTimestamp(Instant.ofEpochMilli(3000));
230         return stateChange;
231     }
232
233     /**
234      * Create a ParticipantRestartAc.
235      *
236      * @return a ParticipantRestartAc
237      */
238     public static ParticipantRestartAc createParticipantRestartAc() {
239         var participantRestartAc = new ParticipantRestartAc();
240         participantRestartAc.setAutomationCompositionId(AC_ID_0);
241         var acElementRestart = new AcElementRestart();
242         acElementRestart.setDefinition(getDefinition());
243         acElementRestart.setDeployState(DeployState.DEPLOYED);
244         acElementRestart.setLockState(LockState.LOCKED);
245         acElementRestart.setOperationalState("OperationalState");
246         acElementRestart.setUseState("UseState");
247         acElementRestart.setProperties(Map.of("key", "value"));
248         acElementRestart.setOutProperties(Map.of("keyOut", "valueOut"));
249         acElementRestart.setId(UUID.randomUUID());
250         participantRestartAc.getAcElementList().add(acElementRestart);
251         return participantRestartAc;
252     }
253
254     /**
255      * Create a ParticipantDeploy from an AutomationComposition.
256      *
257      * @param participantId the participantId
258      * @param automationComposition the AutomationComposition
259      * @return the ParticipantDeploy
260      */
261     public static ParticipantDeploy createparticipantDeploy(UUID participantId,
262             AutomationComposition automationComposition) {
263         var participantDeploy = new ParticipantDeploy();
264         participantDeploy.setParticipantId(participantId);
265         for (var element : automationComposition.getElements().values()) {
266             var acElement = new AcElementDeploy();
267             acElement.setId(element.getId());
268             acElement.setDefinition(element.getDefinition());
269             acElement.setProperties(element.getProperties());
270             participantDeploy.getAcElementList().add(acElement);
271         }
272         return participantDeploy;
273     }
274
275     /**
276      * create a List of AutomationCompositionElementDefinition from an AutomationComposition.
277      *
278      * @param automationComposition the AutomationComposition
279      * @return the List of AutomationCompositionElementDefinition
280      */
281     public static List<AutomationCompositionElementDefinition>
282             createAutomationCompositionElementDefinitionList(AutomationComposition automationComposition) {
283         List<AutomationCompositionElementDefinition> definitions = new ArrayList<>();
284         for (var element : automationComposition.getElements().values()) {
285             definitions.add(createAutomationCompositionElementDefinition(element.getDefinition()));
286         }
287         return definitions;
288     }
289
290     /**
291      * create a new example of AutomationCompositionElementDefinition.
292      *
293      * @param definition the composition definition element id
294      * @return the AutomationCompositionElementDefinition
295      */
296     public static AutomationCompositionElementDefinition createAutomationCompositionElementDefinition(
297             ToscaConceptIdentifier definition) {
298         var acElementDefinition = new AutomationCompositionElementDefinition();
299         acElementDefinition.setAcElementDefinitionId(definition);
300         var nodeTemplate = new ToscaNodeTemplate();
301         nodeTemplate.setProperties(Map.of("key", "value"));
302         acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(nodeTemplate);
303         return acElementDefinition;
304     }
305 }