b6b95ba0f39bd8cbbc40482aceb021782f30713c
[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 ToscaConceptIdentifier getDefinition() {
188         return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
189     }
190
191     /**
192      * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
193      *
194      * @return automationCompositionMap
195      */
196     public static Map<UUID, AutomationComposition> getTestAutomationCompositionMap() {
197         var automationCompositions = getTestAutomationCompositions();
198         var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
199         Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
200         automationCompositionMap.put(automationComposition.getInstanceId(), automationComposition);
201         return automationCompositionMap;
202     }
203
204     /**
205      * Returns List of AutomationComposition for test cases.
206      *
207      * @return AutomationCompositions
208      */
209     public static AutomationCompositions getTestAutomationCompositions() {
210         try {
211             var automationCompositions =
212                     new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
213                             AutomationCompositions.class);
214             automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_0);
215             automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_1);
216             return automationCompositions;
217         } catch (Exception e) {
218             throw new RuntimeException("cannot read TestAutomationCompositions.json");
219         }
220     }
221
222     /**
223      * Return a AutomationCompositionStateChange.
224      *
225      * @param participantId the participantId
226      * @param instanceId the AutomationComposition Id
227      * @param deployOrder a DeployOrder
228      * @param lockOrder a LockOrder
229      * @return a AutomationCompositionStateChange
230      */
231     public static AutomationCompositionStateChange getStateChange(UUID participantId, UUID instanceId,
232             DeployOrder deployOrder, LockOrder lockOrder) {
233         var stateChange = new AutomationCompositionStateChange();
234         stateChange.setStartPhase(0);
235         stateChange.setAutomationCompositionId(instanceId);
236         stateChange.setParticipantId(participantId);
237         stateChange.setMessageId(UUID.randomUUID());
238         stateChange.setDeployOrderedState(deployOrder);
239         stateChange.setLockOrderedState(lockOrder);
240         stateChange.setTimestamp(Instant.ofEpochMilli(3000));
241         return stateChange;
242     }
243
244     /**
245      * Create a ParticipantRestartAc.
246      *
247      * @return a ParticipantRestartAc
248      */
249     public static ParticipantRestartAc createParticipantRestartAc() {
250         var participantRestartAc = new ParticipantRestartAc();
251         participantRestartAc.setAutomationCompositionId(AC_ID_0);
252         participantRestartAc.setDeployState(DeployState.DEPLOYED);
253         participantRestartAc.setLockState(LockState.LOCKED);
254         var acElementRestart = new AcElementRestart();
255         acElementRestart.setDefinition(getDefinition());
256         acElementRestart.setParticipantId(PARTCICIPANT_ID);
257         acElementRestart.setDeployState(DeployState.DEPLOYED);
258         acElementRestart.setLockState(LockState.LOCKED);
259         acElementRestart.setOperationalState("OperationalState");
260         acElementRestart.setUseState("UseState");
261         acElementRestart.setProperties(Map.of("key", "value"));
262         acElementRestart.setOutProperties(Map.of("keyOut", "valueOut"));
263         acElementRestart.setId(UUID.randomUUID());
264         participantRestartAc.getAcElementList().add(acElementRestart);
265         return participantRestartAc;
266     }
267
268     /**
269      * Create a ParticipantDeploy from an AutomationComposition.
270      *
271      * @param participantId the participantId
272      * @param automationComposition the AutomationComposition
273      * @return the ParticipantDeploy
274      */
275     public static ParticipantDeploy createparticipantDeploy(UUID participantId,
276             AutomationComposition automationComposition) {
277         var participantDeploy = new ParticipantDeploy();
278         participantDeploy.setParticipantId(participantId);
279         for (var element : automationComposition.getElements().values()) {
280             var acElement = new AcElementDeploy();
281             acElement.setId(element.getId());
282             acElement.setDefinition(element.getDefinition());
283             acElement.setProperties(element.getProperties());
284             participantDeploy.getAcElementList().add(acElement);
285         }
286         return participantDeploy;
287     }
288
289     /**
290      * create a List of AutomationCompositionElementDefinition from an AutomationComposition.
291      *
292      * @param automationComposition the AutomationComposition
293      * @return the List of AutomationCompositionElementDefinition
294      */
295     public static List<AutomationCompositionElementDefinition>
296             createAutomationCompositionElementDefinitionList(AutomationComposition automationComposition) {
297         List<AutomationCompositionElementDefinition> definitions = new ArrayList<>();
298         for (var element : automationComposition.getElements().values()) {
299             definitions.add(createAutomationCompositionElementDefinition(element.getDefinition()));
300         }
301         return definitions;
302     }
303
304     /**
305      * create a new example of AutomationCompositionElementDefinition.
306      *
307      * @param definition the composition definition element id
308      * @return the AutomationCompositionElementDefinition
309      */
310     public static AutomationCompositionElementDefinition createAutomationCompositionElementDefinition(
311             ToscaConceptIdentifier definition) {
312         var acElementDefinition = new AutomationCompositionElementDefinition();
313         acElementDefinition.setAcElementDefinitionId(definition);
314         var nodeTemplate = new ToscaNodeTemplate();
315         nodeTemplate.setProperties(Map.of("key", "value"));
316         acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(nodeTemplate);
317         return acElementDefinition;
318     }
319 }