60812c27a0e1bd5515e6c08760130c971380fb4e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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.Collections;
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.mockito.Mockito;
32 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
33 import org.onap.policy.clamp.acm.participant.intermediary.handler.AutomationCompositionHandler;
34 import org.onap.policy.clamp.acm.participant.intermediary.handler.DummyParticipantParameters;
35 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
36 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantIntermediaryParameters;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
40 import org.onap.policy.clamp.models.acm.concepts.DeployState;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
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.event.comm.TopicSink;
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
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     private static final Object lockit = new Object();
63     public static final UUID AC_ID_0 = UUID.randomUUID();
64     public static final UUID AC_ID_1 = UUID.randomUUID();
65     public static final UUID PARTCICIPANT_ID = UUID.randomUUID();
66
67     /**
68      * Get ParticipantIntermediaryParameters.
69      *
70      * @return ParticipantIntermediaryParameters
71      */
72     public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
73         try {
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);
78         }
79     }
80
81     /**
82      * Get ParticipantParameters.
83      *
84      * @return ParticipantParameters
85      */
86     public static DummyParticipantParameters getParticipantParameters() {
87         try {
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);
91         }
92     }
93
94     /**
95      * Returns a property map for a Parameters map for test cases.
96      *
97      * @param name name of the parameters
98      * @return a property map suitable for constructing an object
99      */
100     public static Map<String, Object> getParametersMap(final String name) {
101         final Map<String, Object> map = new TreeMap<>();
102         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
103         return map;
104     }
105
106     /**
107      * Returns a property map for a intermediaryParameters map for test cases.
108      *
109      * @param name name of the parameters
110      * @return a property map suitable for constructing an object
111      */
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));
123
124         return map;
125     }
126
127     /**
128      * Returns a property map for a TopicParameters map for test cases.
129      *
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
132      */
133     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
134         final Map<String, Object> map = new TreeMap<>();
135         if (!isEmpty) {
136             map.put("topicSources", TOPIC_PARAMS);
137             map.put("topicSinks", TOPIC_PARAMS);
138         }
139         return map;
140     }
141
142     /**
143      * Returns topic parameters for test cases.
144      *
145      * @return topic parameters
146      */
147     public static TopicParameters getTopicParams() {
148         final var topicParams = new TopicParameters();
149         topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT");
150         topicParams.setTopicCommInfrastructure("dmaap");
151         topicParams.setServers(List.of("localhost"));
152         return topicParams;
153     }
154
155     /**
156      * Returns participantId for test cases.
157      *
158      * @return participant Id
159      */
160     public static UUID getParticipantId() {
161         return PARTCICIPANT_ID;
162     }
163
164     public static UUID getRndParticipantId() {
165         return UUID.randomUUID();
166     }
167
168     public static ToscaConceptIdentifier getDefinition() {
169         return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
170     }
171
172     /**
173      * Returns a participantMessagePublisher for MessageSender.
174      *
175      * @return participant Message Publisher
176      */
177     private ParticipantMessagePublisher getParticipantMessagePublisher() {
178         synchronized (lockit) {
179             var participantMessagePublisher = new ParticipantMessagePublisher();
180             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
181             return participantMessagePublisher;
182         }
183     }
184
185     /**
186      * Returns a mocked AutomationCompositionHandler for test cases.
187      *
188      * @return AutomationCompositionHandler
189      */
190     public AutomationCompositionHandler getMockAutomationCompositionHandler() {
191         return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
192     }
193
194     /**
195      * Returns a mocked ParticipantHandler for test cases.
196      *
197      * @return participant Handler
198      */
199     public ParticipantHandler getMockParticipantHandler() {
200         var parameters = getParticipantParameters();
201         var automationCompositionHandler = getMockAutomationCompositionHandler();
202         var publisher = new ParticipantMessagePublisher();
203         publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
204         return new ParticipantHandler(parameters, publisher, automationCompositionHandler);
205     }
206
207     public ParticipantHandler getParticipantHandlerAutomationCompositions() {
208         var automationCompositionHandler = Mockito.mock(AutomationCompositionHandler.class);
209         return getParticipantHandlerAutomationCompositions(automationCompositionHandler);
210     }
211
212     /**
213      * Returns a mocked ParticipantHandler for test cases.
214      *
215      * @return participant Handler
216      *
217      * @throws CoderException if there is an error with .json file.
218      */
219     public ParticipantHandler getParticipantHandlerAutomationCompositions(
220             AutomationCompositionHandler automationCompositionHandler) {
221         Mockito.doReturn(getTestAutomationCompositionMap()).when(automationCompositionHandler)
222                 .getAutomationCompositionMap();
223         var publisher = new ParticipantMessagePublisher();
224         publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
225         var parameters = getParticipantParameters();
226         var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
227         participantHandler.sendParticipantRegister();
228         participantHandler.handleParticipantStatusReq(null);
229         participantHandler.sendParticipantDeregister();
230         var participantDeregisterAckMsg = new ParticipantDeregisterAck();
231         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
232         participantHandler.handleParticipantDeregisterAck(participantDeregisterAckMsg);
233         return participantHandler;
234     }
235
236     /**
237      * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
238      *
239      * @return automationCompositionMap
240      *
241      * @throws CoderException if there is an error with .json file.
242      */
243     public static Map<UUID, AutomationComposition> getTestAutomationCompositionMap() {
244         var automationCompositions = getTestAutomationCompositions();
245         var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
246         Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
247         automationCompositionMap.put(automationComposition.getInstanceId(), automationComposition);
248         return automationCompositionMap;
249     }
250
251     /**
252      * Returns List of AutomationComposition for test cases.
253      *
254      * @return AutomationCompositions
255      *
256      * @throws CoderException if there is an error with .json file.
257      */
258     public static AutomationCompositions getTestAutomationCompositions() {
259         try {
260             var automationCompositions =
261                     new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
262                             AutomationCompositions.class);
263             automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_0);
264             automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_1);
265             return automationCompositions;
266         } catch (Exception e) {
267             throw new RuntimeException("cannot read TestAutomationCompositions.json");
268         }
269     }
270
271     /**
272      * Returns a map for a elementsOnThisParticipant for test cases.
273      *
274      * @param uuid UUID
275      * @param definition ToscaConceptIdentifier
276      * @return a map suitable for elementsOnThisParticipant
277      */
278     public Map<UUID, AutomationCompositionElement> setAutomationCompositionElementTest(UUID uuid,
279             ToscaConceptIdentifier definition, UUID participantId) {
280         var acElement = new AutomationCompositionElement();
281         acElement.setId(uuid);
282         acElement.setParticipantId(participantId);
283         acElement.setDefinition(definition);
284         acElement.setDeployState(DeployState.UNDEPLOYED);
285
286         Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
287         elementsOnThisParticipant.put(uuid, acElement);
288         return elementsOnThisParticipant;
289     }
290
291     /**
292      * Returns a AutomationCompositionHandler with elements on the definition,uuid.
293      *
294      * @param definition ToscaConceptIdentifier
295      * @param  uuid UUID
296      * @return a AutomationCompositionHander with elements
297      */
298     public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier definition,
299             UUID uuid, UUID participantId) {
300         var ach = getMockAutomationCompositionHandler();
301         ach.getAutomationCompositionMap().putAll(getTestAutomationCompositionMap());
302         var acKey = ach.getAutomationCompositionMap().keySet().iterator().next();
303         ach.getAutomationCompositionMap().get(acKey)
304                 .setElements(setAutomationCompositionElementTest(uuid, definition, participantId));
305
306         return ach;
307     }
308
309     /**
310      * Return a AutomationCompositionStateChange.
311      *
312      * @param participantId the participantId
313      * @param  uuid UUID
314      * @param deployOrder a DeployOrder
315      * @param lockOrder a LockOrder
316      * @return a AutomationCompositionStateChange
317      */
318     public AutomationCompositionStateChange getStateChange(UUID participantId, UUID uuid,
319             DeployOrder deployOrder, LockOrder lockOrder) {
320         var stateChange = new AutomationCompositionStateChange();
321         stateChange.setStartPhase(0);
322         stateChange.setAutomationCompositionId(UUID.randomUUID());
323         stateChange.setParticipantId(participantId);
324         stateChange.setMessageId(uuid);
325         stateChange.setDeployOrderedState(deployOrder);
326         stateChange.setLockOrderedState(lockOrder);
327         stateChange.setTimestamp(Instant.ofEpochMilli(3000));
328         return stateChange;
329     }
330 }