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