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();
67 * Get ParticipantIntermediaryParameters.
69 * @return ParticipantIntermediaryParameters
71 public static ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
73 return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
74 ParticipantIntermediaryParameters.class);
75 } catch (final CoderException e) {
76 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
81 * Get ParticipantParameters.
83 * @return ParticipantParameters
85 public static DummyParticipantParameters getParticipantParameters() {
87 return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), DummyParticipantParameters.class);
88 } catch (final CoderException e) {
89 throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
94 * Returns a property map for a Parameters map for test cases.
96 * @param name name of the parameters
97 * @return a property map suitable for constructing an object
99 public static Map<String, Object> getParametersMap(final String name) {
100 final Map<String, Object> map = new TreeMap<>();
101 map.put("intermediaryParameters", getIntermediaryParametersMap(name));
106 * Returns a property map for a intermediaryParameters map for test cases.
108 * @param name name of the parameters
109 * @return a property map suitable for constructing an object
111 public static Map<String, Object> getIntermediaryParametersMap(final String name) {
112 final Map<String, Object> map = new TreeMap<>();
113 map.put("name", name);
114 map.put("participantId", getParticipantId());
115 map.put("description", DESCRIPTION);
116 map.put("reportingTimeIntervalMs", TIME_INTERVAL);
117 map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
118 var supportedElementType = new ParticipantSupportedElementType();
119 supportedElementType.setTypeName("org.onap.policy.clamp.acm.HttpAutomationCompositionElement");
120 supportedElementType.setTypeVersion("1.0.0");
121 map.put("participantSupportedElementTypes", List.of(supportedElementType));
127 * Returns a property map for a TopicParameters map for test cases.
129 * @param isEmpty boolean value to represent that object created should be empty or not
130 * @return a property map suitable for constructing an object
132 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
133 final Map<String, Object> map = new TreeMap<>();
135 map.put("topicSources", TOPIC_PARAMS);
136 map.put("topicSinks", TOPIC_PARAMS);
142 * Returns topic parameters for test cases.
144 * @return topic parameters
146 public static TopicParameters getTopicParams() {
147 final var topicParams = new TopicParameters();
148 topicParams.setTopic("policy-acruntime-participant");
149 topicParams.setTopicCommInfrastructure("NOOP");
150 topicParams.setServers(List.of("localhost"));
155 * Returns participantId for test cases.
157 * @return participant Id
159 public static UUID getParticipantId() {
160 return PARTCICIPANT_ID;
163 public static UUID getRndParticipantId() {
164 return UUID.randomUUID();
167 public static ToscaConceptIdentifier getDefinition() {
168 return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
172 * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
174 * @return automationCompositionMap
176 * @throws CoderException if there is an error with .json file.
178 public static Map<UUID, AutomationComposition> getTestAutomationCompositionMap() {
179 var automationCompositions = getTestAutomationCompositions();
180 var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
181 Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
182 automationCompositionMap.put(automationComposition.getInstanceId(), automationComposition);
183 return automationCompositionMap;
187 * Returns List of AutomationComposition for test cases.
189 * @return AutomationCompositions
191 * @throws CoderException if there is an error with .json file.
193 public static AutomationCompositions getTestAutomationCompositions() {
195 var automationCompositions =
196 new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
197 AutomationCompositions.class);
198 automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_0);
199 automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_1);
200 return automationCompositions;
201 } catch (Exception e) {
202 throw new RuntimeException("cannot read TestAutomationCompositions.json");
207 * Return a AutomationCompositionStateChange.
209 * @param participantId the participantId
210 * @param instanceId the AutomationComposition Id
211 * @param deployOrder a DeployOrder
212 * @param lockOrder a LockOrder
213 * @return a AutomationCompositionStateChange
215 public static AutomationCompositionStateChange getStateChange(UUID participantId, UUID instanceId,
216 DeployOrder deployOrder, LockOrder lockOrder) {
217 var stateChange = new AutomationCompositionStateChange();
218 stateChange.setStartPhase(0);
219 stateChange.setAutomationCompositionId(instanceId);
220 stateChange.setParticipantId(participantId);
221 stateChange.setMessageId(UUID.randomUUID());
222 stateChange.setDeployOrderedState(deployOrder);
223 stateChange.setLockOrderedState(lockOrder);
224 stateChange.setTimestamp(Instant.ofEpochMilli(3000));
229 * Create a ParticipantRestartAc.
231 * @return a ParticipantRestartAc
233 public static ParticipantRestartAc createParticipantRestartAc() {
234 var participantRestartAc = new ParticipantRestartAc();
235 participantRestartAc.setAutomationCompositionId(AC_ID_0);
236 var acElementRestart = new AcElementRestart();
237 acElementRestart.setDefinition(getDefinition());
238 acElementRestart.setDeployState(DeployState.DEPLOYED);
239 acElementRestart.setLockState(LockState.LOCKED);
240 acElementRestart.setOperationalState("OperationalState");
241 acElementRestart.setUseState("UseState");
242 acElementRestart.setProperties(Map.of("key", "value"));
243 acElementRestart.setOutProperties(Map.of("keyOut", "valueOut"));
244 acElementRestart.setId(UUID.randomUUID());
245 participantRestartAc.getAcElementList().add(acElementRestart);
246 return participantRestartAc;
250 * Create a ParticipantDeploy from an AutomationComposition.
252 * @param participantId the participantId
253 * @param automationComposition the AutomationComposition
254 * @return the ParticipantDeploy
256 public static ParticipantDeploy createparticipantDeploy(UUID participantId,
257 AutomationComposition automationComposition) {
258 var participantDeploy = new ParticipantDeploy();
259 participantDeploy.setParticipantId(participantId);
260 for (var element : automationComposition.getElements().values()) {
261 var acElement = new AcElementDeploy();
262 acElement.setId(element.getId());
263 acElement.setDefinition(element.getDefinition());
264 acElement.setProperties(element.getProperties());
265 participantDeploy.getAcElementList().add(acElement);
267 return participantDeploy;
271 * create a List of AutomationCompositionElementDefinition from an AutomationComposition.
273 * @param automationComposition the AutomationComposition
274 * @return the List of AutomationCompositionElementDefinition
276 public static List<AutomationCompositionElementDefinition>
277 createAutomationCompositionElementDefinitionList(AutomationComposition automationComposition) {
278 List<AutomationCompositionElementDefinition> definitions = new ArrayList<>();
279 for (var element : automationComposition.getElements().values()) {
280 var acElementDefinition = new AutomationCompositionElementDefinition();
281 acElementDefinition.setAcElementDefinitionId(element.getDefinition());
282 var nodeTemplate = new ToscaNodeTemplate();
283 nodeTemplate.setProperties(Map.of("key", "value"));
284 acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(nodeTemplate);
285 definitions.add(acElementDefinition);