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.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;
54 * Class to hold/create all parameters for test cases.
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();
68 * Get ParticipantIntermediaryParameters.
70 * @return ParticipantIntermediaryParameters
72 public static ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
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);
82 * Get ParticipantParameters.
84 * @return ParticipantParameters
86 public static DummyParticipantParameters getParticipantParameters() {
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);
95 * Returns a property map for a Parameters map for test cases.
97 * @param name name of the parameters
98 * @return a property map suitable for constructing an object
100 public static Map<String, Object> getParametersMap(final String name) {
101 final Map<String, Object> map = new TreeMap<>();
102 map.put("intermediaryParameters", getIntermediaryParametersMap(name));
107 * Returns a property map for a intermediaryParameters map for test cases.
109 * @param name name of the parameters
110 * @return a property map suitable for constructing an object
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));
128 * Returns a property map for a TopicParameters map for test cases.
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
133 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
134 final Map<String, Object> map = new TreeMap<>();
136 map.put("topicSources", TOPIC_PARAMS);
137 map.put("topicSinks", TOPIC_PARAMS);
143 * Returns topic parameters for test cases.
145 * @return topic parameters
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"));
156 * Returns participantId for test cases.
158 * @return participant Id
160 public static UUID getParticipantId() {
161 return PARTCICIPANT_ID;
164 public static UUID getReplicaId() {
168 public static UUID getRndParticipantId() {
169 return UUID.randomUUID();
172 public static ToscaConceptIdentifier getDefinition() {
173 return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
177 * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
179 * @return automationCompositionMap
181 * @throws CoderException if there is an error with .json file.
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;
192 * Returns List of AutomationComposition for test cases.
194 * @return AutomationCompositions
196 * @throws CoderException if there is an error with .json file.
198 public static AutomationCompositions getTestAutomationCompositions() {
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");
212 * Return a AutomationCompositionStateChange.
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
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));
234 * Create a ParticipantRestartAc.
236 * @return a ParticipantRestartAc
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;
255 * Create a ParticipantDeploy from an AutomationComposition.
257 * @param participantId the participantId
258 * @param automationComposition the AutomationComposition
259 * @return the ParticipantDeploy
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);
272 return participantDeploy;
276 * create a List of AutomationCompositionElementDefinition from an AutomationComposition.
278 * @param automationComposition the AutomationComposition
279 * @return the List of AutomationCompositionElementDefinition
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()));
291 * create a new example of AutomationCompositionElementDefinition.
293 * @param definition the composition definition element id
294 * @return the AutomationCompositionElementDefinition
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;