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.parameters.topic.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;
56 * Class to hold/create all parameters for test cases.
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();
71 * Get ParticipantIntermediaryParameters.
73 * @return ParticipantIntermediaryParameters
75 public static ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
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);
85 * Get ParticipantParameters.
87 * @return ParticipantParameters
89 public static DummyParticipantParameters getParticipantParameters() {
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);
98 * Returns a property map for a Parameters map for test cases.
100 * @param name name of the parameters
101 * @return a property map suitable for constructing an object
103 public static Map<String, Object> getParametersMap(final String name) {
104 final Map<String, Object> map = new TreeMap<>();
105 map.put("intermediaryParameters", getIntermediaryParametersMap(name));
110 * Returns a property map for a intermediaryParameters map for test cases.
112 * @param name name of the parameters
113 * @return a property map suitable for constructing an object
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));
132 * Returns a property map for a TopicParameters map for test cases.
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
137 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
138 final Map<String, Object> map = new TreeMap<>();
140 map.put("topicSources", TOPIC_SOURCE_PARAMS);
141 map.put("topicSinks", TOPIC_PARAMS);
147 * Returns topic parameters for test cases.
149 * @return topic parameters
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"));
160 * Returns topic parameters for sync topic.
161 * @return topicparamaters
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"));
171 private static Topics getTopics() {
172 return new Topics("policy-acruntime-participant", "acm-ppnt-sync");
176 * Returns participantId for test cases.
178 * @return participant Id
180 public static UUID getParticipantId() {
181 return PARTCICIPANT_ID;
184 public static UUID getReplicaId() {
188 public static ToscaConceptIdentifier getDefinition() {
189 return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
193 * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
195 * @return automationCompositionMap
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;
206 * Returns List of AutomationComposition for test cases.
208 * @return AutomationCompositions
210 public static AutomationCompositions getTestAutomationCompositions() {
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");
224 * Return a AutomationCompositionStateChange.
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
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));
246 * Create a ParticipantRestartAc.
248 * @return a ParticipantRestartAc
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;
271 * Create a ParticipantDeploy from an AutomationComposition.
273 * @param participantId the participantId
274 * @param automationComposition the AutomationComposition
275 * @return the ParticipantDeploy
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);
289 return participantDeploy;
293 * create a List of AutomationCompositionElementDefinition from an AutomationComposition.
295 * @param automationComposition the AutomationComposition
296 * @return the List of AutomationCompositionElementDefinition
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()));
308 * create a new example of AutomationCompositionElementDefinition.
310 * @param definition the composition definition element id
311 * @return the AutomationCompositionElementDefinition
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;