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