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