3011c91f5c90d6804b25cda1de40774855cb94c0
[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.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;
52
53 /**
54  * Class to hold/create all parameters for test cases.
55  */
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
66     /**
67      * Get ParticipantIntermediaryParameters.
68      *
69      * @return ParticipantIntermediaryParameters
70      */
71     public static ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
72         try {
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);
77         }
78     }
79
80     /**
81      * Get ParticipantParameters.
82      *
83      * @return ParticipantParameters
84      */
85     public static DummyParticipantParameters getParticipantParameters() {
86         try {
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);
90         }
91     }
92
93     /**
94      * Returns a property map for a Parameters map for test cases.
95      *
96      * @param name name of the parameters
97      * @return a property map suitable for constructing an object
98      */
99     public static Map<String, Object> getParametersMap(final String name) {
100         final Map<String, Object> map = new TreeMap<>();
101         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
102         return map;
103     }
104
105     /**
106      * Returns a property map for a intermediaryParameters map for test cases.
107      *
108      * @param name name of the parameters
109      * @return a property map suitable for constructing an object
110      */
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));
122
123         return map;
124     }
125
126     /**
127      * Returns a property map for a TopicParameters map for test cases.
128      *
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
131      */
132     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
133         final Map<String, Object> map = new TreeMap<>();
134         if (!isEmpty) {
135             map.put("topicSources", TOPIC_PARAMS);
136             map.put("topicSinks", TOPIC_PARAMS);
137         }
138         return map;
139     }
140
141     /**
142      * Returns topic parameters for test cases.
143      *
144      * @return topic parameters
145      */
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"));
151         return topicParams;
152     }
153
154     /**
155      * Returns participantId for test cases.
156      *
157      * @return participant Id
158      */
159     public static UUID getParticipantId() {
160         return PARTCICIPANT_ID;
161     }
162
163     public static UUID getRndParticipantId() {
164         return UUID.randomUUID();
165     }
166
167     public static ToscaConceptIdentifier getDefinition() {
168         return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
169     }
170
171     /**
172      * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
173      *
174      * @return automationCompositionMap
175      *
176      * @throws CoderException if there is an error with .json file.
177      */
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;
184     }
185
186     /**
187      * Returns List of AutomationComposition for test cases.
188      *
189      * @return AutomationCompositions
190      *
191      * @throws CoderException if there is an error with .json file.
192      */
193     public static AutomationCompositions getTestAutomationCompositions() {
194         try {
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");
203         }
204     }
205
206     /**
207      * Return a AutomationCompositionStateChange.
208      *
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
214      */
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));
225         return stateChange;
226     }
227
228     /**
229      * Create a ParticipantRestartAc.
230      *
231      * @return a ParticipantRestartAc
232      */
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;
247     }
248
249     /**
250      * Create a ParticipantDeploy from an AutomationComposition.
251      *
252      * @param participantId the participantId
253      * @param automationComposition the AutomationComposition
254      * @return the ParticipantDeploy
255      */
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);
266         }
267         return participantDeploy;
268     }
269
270     /**
271      * create a List of AutomationCompositionElementDefinition from an AutomationComposition.
272      *
273      * @param automationComposition the AutomationComposition
274      * @return the List of AutomationCompositionElementDefinition
275      */
276     public static List<AutomationCompositionElementDefinition>
277             createAutomationCompositionElementDefinitionList(AutomationComposition automationComposition) {
278         List<AutomationCompositionElementDefinition> definitions = new ArrayList<>();
279         for (var element : automationComposition.getElements().values()) {
280             definitions.add(createAutomationCompositionElementDefinition(element.getDefinition()));
281         }
282         return definitions;
283     }
284
285     /**
286      * create a new example of AutomationCompositionElementDefinition.
287      *
288      * @param definition the composition definition element id
289      * @return the AutomationCompositionElementDefinition
290      */
291     public static AutomationCompositionElementDefinition createAutomationCompositionElementDefinition(
292             ToscaConceptIdentifier definition) {
293         var acElementDefinition = new AutomationCompositionElementDefinition();
294         acElementDefinition.setAcElementDefinitionId(definition);
295         var nodeTemplate = new ToscaNodeTemplate();
296         nodeTemplate.setProperties(Map.of("key", "value"));
297         acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(nodeTemplate);
298         return acElementDefinition;
299     }
300 }