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