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