4b6dcd6196be0a3aa0c4afdbcd54c51211f5e5e0
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.controlloop.participant.dcae.main.parameters;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.TreeMap;
31 import javax.ws.rs.core.Response;
32 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
33 import org.onap.policy.common.endpoints.parameters.TopicParameters;
34 import org.onap.policy.common.utils.coder.Coder;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.network.NetworkUtil;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39
40 /**
41  * Class to hold/create all parameters for test cases.
42  */
43 public class CommonTestData {
44     public static final String PARTICIPANT_GROUP_NAME = "ControlLoopParticipantGroup";
45     public static final String DESCRIPTION = "Participant description";
46     public static final long TIME_INTERVAL = 2000;
47     public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams());
48     private static final String REST_CLIENT_PASSWORD = "password";
49     private static final String REST_CLIENT_USER = "admin";
50     private static final String REST_CLAMP_HOST = "0.0.0.0";
51     private static final String REST_CONSUL_HOST = "0.0.0.0";
52     private static final boolean REST_CLAMP_HTTPS = false;
53     private static final boolean REST_CONSUL_HTTPS = false;
54     private static final boolean REST_CLIENT_AAF = false;
55
56     public static final Coder CODER = new StandardCoder();
57
58     /**
59      * Get ParticipantDcaeParameters.
60      *
61      * @return ParticipantDcaeParameters
62      */
63     public ParticipantDcaeParameters getParticipantDcaeParameters() {
64         try {
65             return CODER.convert(getParticipantParameterGroupMap(PARTICIPANT_GROUP_NAME),
66                     ParticipantDcaeParameters.class);
67         } catch (final CoderException e) {
68             throw new RuntimeException("cannot create ParticipantDcaeParameters from map", e);
69         }
70     }
71
72     /**
73      * Returns a property map for a ApexStarterParameterGroup map for test cases.
74      *
75      * @param name name of the parameters
76      *
77      * @return a property map suitable for constructing an object
78      */
79     public Map<String, Object> getParticipantParameterGroupMap(final String name) {
80         final Map<String, Object> map = new TreeMap<>();
81
82         map.put("name", name);
83         map.put("checkCount", 10);
84         map.put("secCount", 10);
85         map.put("jsonBodyConsulPath", "src/main/resources/parameters/consul.json");
86         map.put("clampClientParameters", getClampClientParametersMap(false));
87         map.put("consulClientParameters", getConsulClientParametersMap(false));
88         map.put("intermediaryParameters", getIntermediaryParametersMap(false));
89         map.put("clampClientEndPoints", getClampClientEndPoints());
90         map.put("consulClientEndPoints", getConsulClientEndPoints());
91         return map;
92     }
93
94     private Map<String, Object> getConsulClientEndPoints() {
95         final Map<String, Object> map = new TreeMap<>();
96         map.put("deploy", "/v1/kv/dcae-pmsh:");
97         return map;
98     }
99
100     private Map<String, Object> getClampClientEndPoints() {
101         final Map<String, Object> map = new TreeMap<>();
102         map.put("status", "/restservices/clds/v2/loop/getstatus/");
103         map.put("create", "/restservices/clds/v2/loop/create/%s?templateName=%s");
104         map.put("deploy", "/restservices/clds/v2/loop/deploy/");
105         map.put("stop", "/restservices/clds/v2/loop/stop/");
106         map.put("delete", "/restservices/clds/v2/loop/delete/");
107         map.put("undeploy", "/restservices/clds/v2/loop/undeploy/");
108         return map;
109     }
110
111     /**
112      * Returns a property map for a RestServerParameters map for test cases.
113      *
114      * @param isEmpty boolean value to represent that object created should be empty or not
115      * @return a property map suitable for constructing an object
116      * @throws ControlLoopRuntimeException on errors
117      */
118     public Map<String, Object> getClampClientParametersMap(final boolean isEmpty) {
119         final Map<String, Object> map = new TreeMap<>();
120         map.put("clientName", "Clamp");
121         map.put("https", REST_CLAMP_HTTPS);
122         map.put("aaf", REST_CLIENT_AAF);
123
124         if (!isEmpty) {
125             map.put("hostname", REST_CLAMP_HOST);
126             try {
127                 map.put("port", NetworkUtil.allocPort());
128             } catch (IOException e) {
129                 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "not valid port", e);
130             }
131             map.put("userName", REST_CLIENT_USER);
132             map.put("password", REST_CLIENT_PASSWORD);
133         }
134
135         return map;
136     }
137
138     /**
139      * Returns a property map for a RestServerParameters map for test cases.
140      *
141      * @param isEmpty boolean value to represent that object created should be empty or not
142      * @return a property map suitable for constructing an object
143      * @throws ControlLoopRuntimeException on errors
144      */
145     public Map<String, Object> getConsulClientParametersMap(final boolean isEmpty) {
146         final Map<String, Object> map = new TreeMap<>();
147         map.put("clientName", "Consul");
148         map.put("https", REST_CONSUL_HTTPS);
149         map.put("aaf", REST_CLIENT_AAF);
150
151         if (!isEmpty) {
152             map.put("hostname", REST_CONSUL_HOST);
153             try {
154                 map.put("port", NetworkUtil.allocPort());
155             } catch (IOException e) {
156                 throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "not valid port", e);
157             }
158             map.put("userName", REST_CLIENT_USER);
159             map.put("password", REST_CLIENT_PASSWORD);
160         }
161
162         return map;
163     }
164
165     /**
166      * Returns a property map for a intermediaryParameters map for test cases.
167      *
168      * @param isEmpty boolean value to represent that object created should be empty or not
169      * @return a property map suitable for constructing an object
170      */
171     public Map<String, Object> getIntermediaryParametersMap(final boolean isEmpty) {
172         final Map<String, Object> map = new TreeMap<>();
173         if (!isEmpty) {
174             map.put("name", "Participant parameters");
175             map.put("reportingTimeInterval", TIME_INTERVAL);
176             map.put("description", DESCRIPTION);
177             map.put("participantId", getParticipantId());
178             map.put("participantType", getParticipantId());
179             map.put("clampControlLoopTopics", getTopicParametersMap(false));
180         }
181
182         return map;
183     }
184
185     /**
186      * Returns a property map for a TopicParameters map for test cases.
187      *
188      * @param isEmpty boolean value to represent that object created should be empty or not
189      * @return a property map suitable for constructing an object
190      */
191     public Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
192         final Map<String, Object> map = new TreeMap<>();
193         if (!isEmpty) {
194             map.put("topicSources", TOPIC_PARAMS);
195             map.put("topicSinks", TOPIC_PARAMS);
196         }
197         return map;
198     }
199
200     /**
201      * Returns topic parameters for test cases.
202      *
203      * @return topic parameters
204      */
205     public static TopicParameters getTopicParams() {
206         final TopicParameters topicParams = new TopicParameters();
207         topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
208         topicParams.setTopicCommInfrastructure("dmaap");
209         topicParams.setServers(Arrays.asList("localhost"));
210         return topicParams;
211     }
212
213     /**
214      * Returns participantId for test cases.
215      *
216      * @return participant Id
217      */
218     public static ToscaConceptIdentifier getParticipantId() {
219         final ToscaConceptIdentifier participantId = new ToscaConceptIdentifier();
220         participantId.setName("DCAEParticipant0");
221         participantId.setVersion("1.0.0");
222         return participantId;
223     }
224
225     /**
226      * Nulls out a field within a JSON string.
227      *
228      * @param json JSON string
229      * @param field field to be nulled out
230      * @return a new JSON string with the field nulled out
231      */
232     public String nullifyField(String json, String field) {
233         return json.replace(field + "\"", field + "\":null, \"" + field + "Xxx\"");
234     }
235
236     /**
237      * Create Json response from getstatus call.
238      *
239      * @param status the status of Partecipant
240      * @return the JSON
241      * @throws ControlLoopRuntimeException on errors
242      */
243     public static String createJsonStatus(String status) {
244         try {
245             File file = new File("src/test/resources/rest/status.json");
246             String json = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
247             return json.replace("${status}", status);
248
249         } catch (IOException e) {
250             throw new ControlLoopRuntimeException(Response.Status.NOT_ACCEPTABLE, "cannot read json file", e);
251         }
252     }
253 }