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