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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.intermediary.main.parameters;
24 import java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
27 import java.util.List;
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;
55 * Class to hold/create all parameters for test cases.
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();
70 * Get ParticipantIntermediaryParameters.
72 * @return ParticipantIntermediaryParameters
74 public static ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
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);
84 * Get ParticipantParameters.
86 * @return ParticipantParameters
88 public static DummyParticipantParameters getParticipantParameters() {
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);
97 * Returns a property map for a Parameters map for test cases.
99 * @param name name of the parameters
100 * @return a property map suitable for constructing an object
102 public static Map<String, Object> getParametersMap(final String name) {
103 final Map<String, Object> map = new TreeMap<>();
104 map.put("intermediaryParameters", getIntermediaryParametersMap(name));
109 * Returns a property map for a intermediaryParameters map for test cases.
111 * @param name name of the parameters
112 * @return a property map suitable for constructing an object
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));
131 * Returns a property map for a TopicParameters map for test cases.
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
136 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
137 final Map<String, Object> map = new TreeMap<>();
139 map.put("topicSources", TOPIC_SOURCE_PARAMS);
140 map.put("topicSinks", TOPIC_PARAMS);
146 * Returns topic parameters for test cases.
148 * @return topic parameters
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"));
159 * Returns topic parameters for sync topic.
160 * @return topicparamaters
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"));
170 private static Topics getTopics() {
171 return new Topics("policy-acruntime-participant", "acm-ppnt-sync");
175 * Returns participantId for test cases.
177 * @return participant Id
179 public static UUID getParticipantId() {
180 return PARTCICIPANT_ID;
183 public static UUID getReplicaId() {
187 public static UUID getRndParticipantId() {
188 return UUID.randomUUID();
191 public static ToscaConceptIdentifier getDefinition() {
192 return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
196 * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
198 * @return automationCompositionMap
200 * @throws CoderException if there is an error with .json file.
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;
211 * Returns List of AutomationComposition for test cases.
213 * @return AutomationCompositions
215 * @throws CoderException if there is an error with .json file.
217 public static AutomationCompositions getTestAutomationCompositions() {
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");
231 * Return a AutomationCompositionStateChange.
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
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));
253 * Create a ParticipantRestartAc.
255 * @return a ParticipantRestartAc
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;
274 * Create a ParticipantDeploy from an AutomationComposition.
276 * @param participantId the participantId
277 * @param automationComposition the AutomationComposition
278 * @return the ParticipantDeploy
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);
291 return participantDeploy;
295 * create a List of AutomationCompositionElementDefinition from an AutomationComposition.
297 * @param automationComposition the AutomationComposition
298 * @return the List of AutomationCompositionElementDefinition
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()));
310 * create a new example of AutomationCompositionElementDefinition.
312 * @param definition the composition definition element id
313 * @return the AutomationCompositionElementDefinition
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;