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