Fix sonar issues
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / common / PropertiesService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. All rights reserved.
7  *  *  Copyright (c) 2021  Nokia. All rights reserved.
8  *  *  ================================================================================
9  *  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  *  you may not use this file except in compliance with the License.
11  *  *  You may obtain a copy of the License at
12  *  *
13  *  *       http://www.apache.org/licenses/LICENSE-2.0
14  *  *
15  *  *  Unless required by applicable law or agreed to in writing, software
16  *  *  distributed under the License is distributed on an "AS IS" BASIS,
17  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  *  See the License for the specific language governing permissions and
19  *  *  limitations under the License.
20  *  *  ============LICENSE_END=========================================================
21  *
22  *
23  */
24
25 package org.onap.blueprintgenerator.service.common;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import org.onap.blueprintgenerator.constants.Constants;
33 import org.onap.blueprintgenerator.model.common.Appconfig;
34 import org.onap.blueprintgenerator.model.common.GetInput;
35 import org.onap.blueprintgenerator.model.common.Properties;
36 import org.onap.blueprintgenerator.model.common.ResourceConfig;
37 import org.onap.blueprintgenerator.model.componentspec.OnapAuxilary;
38 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
39 import org.onap.blueprintgenerator.model.componentspec.common.Publishes;
40 import org.onap.blueprintgenerator.model.componentspec.common.Subscribes;
41 import org.onap.blueprintgenerator.model.dmaap.Streams;
42 import org.onap.blueprintgenerator.model.dmaap.TlsInfo;
43 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
44 import org.onap.blueprintgenerator.service.dmaap.StreamsService;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Service;
47
48 /**
49  * @author : Ravi Mantena
50  * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service to create Properties Node
51  */
52 @Service("onapPropertiesService")
53 public class PropertiesService {
54
55     @Autowired
56     private AppConfigService appConfigService;
57
58     @Autowired
59     private ResourceConfigService resourceConfigService;
60
61     @Autowired
62     private StreamsService streamsService;
63
64     @Autowired
65     private ExternalTlsInfoFactoryService externalTlsInfoFactoryService;
66
67     @Autowired
68     private BlueprintHelperService blueprintHelperService;
69
70     /**
71      * Creates ONAP properties
72      *
73      * @param inputs            Inputs
74      * @param onapComponentSpec OnapComponentSpec
75      * @param override          Override
76      * @return
77      */
78     public Map<String, Object> createOnapProperties(
79         Map<String, Map<String, Object>> inputs,
80         OnapComponentSpec onapComponentSpec,
81         String override) {
82         Map<String, Object> response = new HashMap<>();
83         org.onap.blueprintgenerator.model.common.Properties properties =
84             new org.onap.blueprintgenerator.model.common.Properties();
85
86         GetInput image = new GetInput();
87         image.setBpInputName("image");
88         properties.setImage(image);
89
90         inputs.put(
91             "image",
92             blueprintHelperService.createStringInput(onapComponentSpec.getArtifacts()[0].getUri()));
93
94         GetInput location = new GetInput();
95         location.setBpInputName("location_id");
96         properties.setLocation_id(location);
97
98         inputs.put("location_id", blueprintHelperService.createStringInput(Constants.EMPTY_VALUE));
99
100         properties.setLog_info(onapComponentSpec.getAuxilary().getLog_info());
101
102         GetInput replica = new GetInput();
103         replica.setBpInputName("replicas");
104         properties.setReplicas(replica);
105
106         Map<String, Object> replicas =
107             blueprintHelperService.createIntegerInput("number of instances", 1);
108         inputs.put("replicas", replicas);
109
110         OnapAuxilary onapAuxilary = onapComponentSpec.getAuxilary();
111
112         properties.setDocker_config(onapAuxilary);
113
114         Map<String, Object> appConfigResponse =
115             appConfigService.createAppconfig(inputs, onapComponentSpec, false);
116         inputs = (Map<String, Map<String, Object>>) appConfigResponse.get("inputs");
117         properties.setApplication_config((Appconfig) appConfigResponse.get("appconfig"));
118
119         GetInput always_pull_image = new GetInput();
120         always_pull_image.setBpInputName("always_pull_image");
121
122         properties.setAlways_pull_image(always_pull_image);
123
124         Map<String, Object> inputAlwaysPullImage =
125             blueprintHelperService.createBooleanInput(
126                 "Set to true if the image should always be pulled", true);
127         inputs.put("always_pull_image", inputAlwaysPullImage);
128
129         String sType = onapComponentSpec.getSelf().getName();
130         sType = sType.replace('.', '-');
131         properties.setService_component_type(sType);
132
133         inputs.put(Constants.SERVICE_COMPONENT_NAME_OVERRIDE,
134             addServiceComponentNameOverride(override, properties));
135
136         Map<String, Object> tls_info = onapComponentSpec.getAuxilary().getTls_info();
137         if (tls_info != null) {
138             addTlsInfo(onapComponentSpec, inputs, properties);
139             if (tls_info.get(Constants.USE_EXTERNAL_TLS_FIELD) != null) {
140                 inputs.putAll(addExternalTlsInfo(onapComponentSpec, properties));
141             }
142         }
143
144         Map<String, Object> resourceConfigResponse =
145             resourceConfigService
146                 .createResourceConfig(inputs, onapComponentSpec.getSelf().getName());
147         inputs = (Map<String, Map<String, Object>>) resourceConfigResponse.get("inputs");
148         properties
149             .setResource_config((ResourceConfig) resourceConfigResponse.get("resourceConfig"));
150
151         response.put("properties", properties);
152         response.put("inputs", inputs);
153         return response;
154     }
155
156     /**
157      * Creates Dmaap properties
158      *
159      * @param inputs            Inputs
160      * @param onapComponentSpec OnapComponentSpec
161      * @param override          Override
162      * @return
163      */
164     public Map<String, Object> createDmaapProperties(
165         Map<String, Map<String, Object>> inputs,
166         OnapComponentSpec onapComponentSpec,
167         String override) {
168         Map<String, Object> response = new HashMap<>();
169         org.onap.blueprintgenerator.model.common.Properties properties =
170             new org.onap.blueprintgenerator.model.common.Properties();
171
172         GetInput image = new GetInput();
173         image.setBpInputName("tag_version");
174         properties.setImage(image);
175
176         Map<String, Object> img = new LinkedHashMap<>();
177         inputs.put(
178             "tag_version",
179             blueprintHelperService.createStringInput(onapComponentSpec.getArtifacts()[0].getUri()));
180
181         GetInput location = new GetInput();
182         location.setBpInputName("location_id");
183         properties.setLocation_id(location);
184
185         Map<String, Object> locMap = new LinkedHashMap();
186         inputs.put("location_id", blueprintHelperService.createStringInput(Constants.EMPTY_VALUE));
187
188         properties.setLog_info(onapComponentSpec.getAuxilary().getLog_info());
189
190         String sType = onapComponentSpec.getSelf().getName();
191         sType = sType.replace('.', '-');
192         properties.setService_component_type(sType);
193
194         inputs.put(Constants.SERVICE_COMPONENT_NAME_OVERRIDE,
195             addServiceComponentNameOverride(override, properties));
196
197         Map<String, Object> tls_info = onapComponentSpec.getAuxilary().getTls_info();
198         if (tls_info != null) {
199             addTlsInfo(onapComponentSpec, inputs, properties);
200             if (tls_info.get(Constants.USE_EXTERNAL_TLS_FIELD) != null) {
201                 inputs.putAll(addExternalTlsInfo(onapComponentSpec, properties));
202             }
203         }
204
205         GetInput replica = new GetInput();
206         replica.setBpInputName("replicas");
207         properties.setReplicas(replica);
208
209         Map<String, Object> rep =
210             blueprintHelperService.createIntegerInput("number of instances", 1);
211         inputs.put("replicas", rep);
212
213         OnapAuxilary onapAuxilary = onapComponentSpec.getAuxilary();
214
215         properties.setDocker_config(onapAuxilary);
216
217         Map<String, Object> appConfigResponse =
218             appConfigService.createAppconfig(inputs, onapComponentSpec, true);
219         inputs = (Map<String, Map<String, Object>>) appConfigResponse.get("inputs");
220         properties.setApplication_config((Appconfig) appConfigResponse.get("appconfig"));
221
222         List<Streams> pubStreams = new ArrayList();
223         if (onapComponentSpec.getStreams() != null) {
224             if (onapComponentSpec.getStreams().getPublishes() != null) {
225                 for (Publishes publishes : onapComponentSpec.getStreams().getPublishes()) {
226                     if (blueprintHelperService.isMessageRouterType(publishes.getType())) {
227                         String topic = publishes.getConfig_key() + Constants.A_TOPIC;
228                         Map<String, Object> streamsMessageRouterResponse =
229                             streamsService.createStreams(
230                                 inputs,
231                                 topic,
232                                 publishes.getType(),
233                                 publishes.getConfig_key(),
234                                 publishes.getRoute(),
235                                 'p');
236                         inputs =
237                             (Map<String, Map<String, Object>>)
238                                 streamsMessageRouterResponse.get("inputs");
239                         pubStreams.add((Streams) streamsMessageRouterResponse.get("streams"));
240                     } else if (blueprintHelperService.isDataRouterType(publishes.getType())) {
241                         String feed = publishes.getConfig_key() + Constants.A_FEED;
242                         Map<String, Object> streamsDataRouterResponse =
243                             streamsService.createStreams(
244                                 inputs,
245                                 feed,
246                                 publishes.getType(),
247                                 publishes.getConfig_key(),
248                                 publishes.getRoute(),
249                                 'p');
250                         inputs =
251                             (Map<String, Map<String, Object>>)
252                                 streamsDataRouterResponse.get("inputs");
253                         pubStreams.add((Streams) streamsDataRouterResponse.get("streams"));
254                     }
255                 }
256             }
257         }
258
259         ArrayList<Streams> subStreams = new ArrayList();
260         if (onapComponentSpec.getStreams() != null) {
261             if (onapComponentSpec.getStreams().getSubscribes() != null) {
262                 for (Subscribes subscribes : onapComponentSpec.getStreams().getSubscribes()) {
263                     if (blueprintHelperService.isMessageRouterType(subscribes.getType())) {
264                         String topic = subscribes.getConfig_key() + Constants.A_TOPIC;
265                         Map<String, Object> streamsMessageRouterResponse =
266                             streamsService.createStreams(
267                                 inputs,
268                                 topic,
269                                 subscribes.getType(),
270                                 subscribes.getConfig_key(),
271                                 subscribes.getRoute(),
272                                 's');
273                         inputs =
274                             (Map<String, Map<String, Object>>)
275                                 streamsMessageRouterResponse.get("inputs");
276                         subStreams.add((Streams) streamsMessageRouterResponse.get("streams"));
277                     } else if (blueprintHelperService.isDataRouterType(subscribes.getType())) {
278                         String feed = subscribes.getConfig_key() + Constants.A_FEED;
279                         Map<String, Object> streamsDataRouterResponse =
280                             streamsService.createStreams(
281                                 inputs,
282                                 feed,
283                                 subscribes.getType(),
284                                 subscribes.getConfig_key(),
285                                 subscribes.getRoute(),
286                                 's');
287                         inputs =
288                             (Map<String, Map<String, Object>>)
289                                 streamsDataRouterResponse.get("inputs");
290                         subStreams.add((Streams) streamsDataRouterResponse.get("streams"));
291                     }
292                 }
293             }
294         }
295
296         if (!pubStreams.isEmpty()) {
297             properties.setStreams_publishes(pubStreams);
298         }
299
300         if (!subStreams.isEmpty()) {
301             properties.setStreams_subscribes(subStreams);
302         }
303
304         Map<String, Object> resourceConfigResponse =
305             resourceConfigService
306                 .createResourceConfig(inputs, onapComponentSpec.getSelf().getName());
307         inputs = (Map<String, Map<String, Object>>) resourceConfigResponse.get("inputs");
308         properties
309             .setResource_config((ResourceConfig) resourceConfigResponse.get("resourceConfig"));
310
311         response.put("properties", properties);
312         response.put("inputs", inputs);
313         return response;
314     }
315
316     private Map<String, Object> addServiceComponentNameOverride(String override, Properties properties) {
317         GetInput overrideGetInput = new GetInput();
318         overrideGetInput.setBpInputName(Constants.SERVICE_COMPONENT_NAME_OVERRIDE);
319         properties.setService_component_name_override(overrideGetInput);
320         return blueprintHelperService.createStringInput(override != null ? override : "");
321     }
322
323     private void addTlsInfo(
324         OnapComponentSpec onapComponentSpec,
325         Map<String, Map<String, Object>> inputs,
326         Properties properties) {
327         TlsInfo tlsInfo = new TlsInfo();
328         tlsInfo.setCertDirectory(
329             (String) onapComponentSpec.getAuxilary().getTls_info().get("cert_directory"));
330         GetInput useTLSFlag = new GetInput();
331         useTLSFlag.setBpInputName("use_tls");
332         tlsInfo.setUseTls(useTLSFlag);
333         properties.setTls_info(tlsInfo);
334         Map<String, Object> useTlsFlagInput =
335             blueprintHelperService.createBooleanInput(
336                 "flag to indicate tls enable/disable",
337                 onapComponentSpec.getAuxilary().getTls_info().get("use_tls"));
338         inputs.put("use_tls", useTlsFlagInput);
339     }
340
341     private Map<String, Map<String, Object>> addExternalTlsInfo(
342         OnapComponentSpec onapComponentSpec, Properties properties) {
343         properties.setExternal_cert(
344             externalTlsInfoFactoryService.createFromComponentSpec(onapComponentSpec));
345         return externalTlsInfoFactoryService.createInputListFromComponentSpec(onapComponentSpec);
346     }
347 }