acff103a2bbcdd41e4322ced98b95bcc14503af9
[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.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.TreeMap;
29 import java.util.UUID;
30 import org.onap.policy.clamp.acm.participant.intermediary.handler.DummyParticipantParameters;
31 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantIntermediaryParameters;
32 import org.onap.policy.clamp.models.acm.concepts.AcElementRestart;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
35 import org.onap.policy.clamp.models.acm.concepts.DeployState;
36 import org.onap.policy.clamp.models.acm.concepts.LockState;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
40 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
41 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
42 import org.onap.policy.common.endpoints.parameters.TopicParameters;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47
48 /**
49  * Class to hold/create all parameters for test cases.
50  */
51 public class CommonTestData {
52     public static final String PARTICIPANT_GROUP_NAME = "AutomationCompositionParticipantGroup";
53     public static final String DESCRIPTION = "Participant description";
54     public static final long TIME_INTERVAL = 2000;
55     public static final List<TopicParameters> TOPIC_PARAMS = List.of(getTopicParams());
56     public static final Coder CODER = new StandardCoder();
57     public static final UUID AC_ID_0 = UUID.randomUUID();
58     public static final UUID AC_ID_1 = UUID.randomUUID();
59     public static final UUID PARTCICIPANT_ID = UUID.randomUUID();
60
61     /**
62      * Get ParticipantIntermediaryParameters.
63      *
64      * @return ParticipantIntermediaryParameters
65      */
66     public static ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
67         try {
68             return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
69                     ParticipantIntermediaryParameters.class);
70         } catch (final CoderException e) {
71             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
72         }
73     }
74
75     /**
76      * Get ParticipantParameters.
77      *
78      * @return ParticipantParameters
79      */
80     public static DummyParticipantParameters getParticipantParameters() {
81         try {
82             return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), DummyParticipantParameters.class);
83         } catch (final CoderException e) {
84             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
85         }
86     }
87
88     /**
89      * Returns a property map for a Parameters map for test cases.
90      *
91      * @param name name of the parameters
92      * @return a property map suitable for constructing an object
93      */
94     public static Map<String, Object> getParametersMap(final String name) {
95         final Map<String, Object> map = new TreeMap<>();
96         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
97         return map;
98     }
99
100     /**
101      * Returns a property map for a intermediaryParameters map for test cases.
102      *
103      * @param name name of the parameters
104      * @return a property map suitable for constructing an object
105      */
106     public static Map<String, Object> getIntermediaryParametersMap(final String name) {
107         final Map<String, Object> map = new TreeMap<>();
108         map.put("name", name);
109         map.put("participantId", getParticipantId());
110         map.put("description", DESCRIPTION);
111         map.put("reportingTimeIntervalMs", TIME_INTERVAL);
112         map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
113         var supportedElementType = new ParticipantSupportedElementType();
114         supportedElementType.setTypeName("org.onap.policy.clamp.acm.HttpAutomationCompositionElement");
115         supportedElementType.setTypeVersion("1.0.0");
116         map.put("participantSupportedElementTypes", List.of(supportedElementType));
117
118         return map;
119     }
120
121     /**
122      * Returns a property map for a TopicParameters map for test cases.
123      *
124      * @param isEmpty boolean value to represent that object created should be empty or not
125      * @return a property map suitable for constructing an object
126      */
127     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
128         final Map<String, Object> map = new TreeMap<>();
129         if (!isEmpty) {
130             map.put("topicSources", TOPIC_PARAMS);
131             map.put("topicSinks", TOPIC_PARAMS);
132         }
133         return map;
134     }
135
136     /**
137      * Returns topic parameters for test cases.
138      *
139      * @return topic parameters
140      */
141     public static TopicParameters getTopicParams() {
142         final var topicParams = new TopicParameters();
143         topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT");
144         topicParams.setTopicCommInfrastructure("dmaap");
145         topicParams.setServers(List.of("localhost"));
146         return topicParams;
147     }
148
149     /**
150      * Returns participantId for test cases.
151      *
152      * @return participant Id
153      */
154     public static UUID getParticipantId() {
155         return PARTCICIPANT_ID;
156     }
157
158     public static UUID getRndParticipantId() {
159         return UUID.randomUUID();
160     }
161
162     public static ToscaConceptIdentifier getDefinition() {
163         return new ToscaConceptIdentifier("org.onap.domain.pmsh.PMSH_DCAEMicroservice", "1.2.3");
164     }
165
166     /**
167      * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
168      *
169      * @return automationCompositionMap
170      *
171      * @throws CoderException if there is an error with .json file.
172      */
173     public static Map<UUID, AutomationComposition> getTestAutomationCompositionMap() {
174         var automationCompositions = getTestAutomationCompositions();
175         var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
176         Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
177         automationCompositionMap.put(automationComposition.getInstanceId(), automationComposition);
178         return automationCompositionMap;
179     }
180
181     /**
182      * Returns List of AutomationComposition for test cases.
183      *
184      * @return AutomationCompositions
185      *
186      * @throws CoderException if there is an error with .json file.
187      */
188     public static AutomationCompositions getTestAutomationCompositions() {
189         try {
190             var automationCompositions =
191                     new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
192                             AutomationCompositions.class);
193             automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_0);
194             automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_1);
195             return automationCompositions;
196         } catch (Exception e) {
197             throw new RuntimeException("cannot read TestAutomationCompositions.json");
198         }
199     }
200
201     /**
202      * Return a AutomationCompositionStateChange.
203      *
204      * @param participantId the participantId
205      * @param instanceId th AutomationComposition Id
206      * @param deployOrder a DeployOrder
207      * @param lockOrder a LockOrder
208      * @return a AutomationCompositionStateChange
209      */
210     public static AutomationCompositionStateChange getStateChange(UUID participantId, UUID instanceId,
211             DeployOrder deployOrder, LockOrder lockOrder) {
212         var stateChange = new AutomationCompositionStateChange();
213         stateChange.setStartPhase(0);
214         stateChange.setAutomationCompositionId(instanceId);
215         stateChange.setParticipantId(participantId);
216         stateChange.setMessageId(UUID.randomUUID());
217         stateChange.setDeployOrderedState(deployOrder);
218         stateChange.setLockOrderedState(lockOrder);
219         stateChange.setTimestamp(Instant.ofEpochMilli(3000));
220         return stateChange;
221     }
222
223     /**
224      * Create a ParticipantRestartAc.
225      *
226      * @return a ParticipantRestartAc
227      */
228     public static ParticipantRestartAc createParticipantRestartAc() {
229         var participantRestartAc = new ParticipantRestartAc();
230         participantRestartAc.setAutomationCompositionId(AC_ID_0);
231         var acElementRestart = new AcElementRestart();
232         acElementRestart.setDefinition(getDefinition());
233         acElementRestart.setDeployState(DeployState.DEPLOYED);
234         acElementRestart.setLockState(LockState.LOCKED);
235         acElementRestart.setId(UUID.randomUUID());
236         participantRestartAc.getAcElementList().add(acElementRestart);
237         return participantRestartAc;
238     }
239 }