be79b252a6e780f6e549b8bb04a8c3d4b6e9abb0
[dcaegen2/platform.git] /
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         inputs.put(
92             "image",
93             blueprintHelperService.createStringInput(onapComponentSpec.getArtifacts()[0].getUri()));
94
95         GetInput location = new GetInput();
96         location.setBpInputName("location_id");
97         properties.setLocation_id(location);
98
99         inputs.put("location_id", blueprintHelperService.createStringInput(Constants.EMPTY_VALUE));
100
101         properties.setLog_info(onapComponentSpec.getAuxilary().getLog_info());
102
103         GetInput replica = new GetInput();
104         replica.setBpInputName("replicas");
105         properties.setReplicas(replica);
106
107         LinkedHashMap<String, Object> replicas =
108             blueprintHelperService.createIntegerInput("number of instances", 1);
109         inputs.put("replicas", replicas);
110
111         OnapAuxilary onapAuxilary = onapComponentSpec.getAuxilary();
112
113         properties.setDocker_config(onapAuxilary);
114
115         Map<String, Object> appConfigResponse =
116             appConfigService.createAppconfig(inputs, onapComponentSpec, override, false);
117         inputs = (Map<String, LinkedHashMap<String, Object>>) appConfigResponse.get("inputs");
118         properties.setApplication_config((Appconfig) appConfigResponse.get("appconfig"));
119
120         GetInput always_pull_image = new GetInput();
121         always_pull_image.setBpInputName("always_pull_image");
122
123         properties.setAlways_pull_image(always_pull_image);
124
125         LinkedHashMap<String, Object> inputAlwaysPullImage =
126             blueprintHelperService.createBooleanInput(
127                 "Set to true if the image should always be pulled", true);
128         inputs.put("always_pull_image", inputAlwaysPullImage);
129
130         String sType = onapComponentSpec.getSelf().getName();
131         sType = sType.replace('.', '-');
132         properties.setService_component_type(sType);
133
134         Map<String, Object> tls_info = onapComponentSpec.getAuxilary().getTls_info();
135         if (tls_info != null) {
136             addTlsInfo(onapComponentSpec, inputs, properties);
137             if (tls_info.get(Constants.USE_EXTERNAL_TLS_FIELD) != null) {
138                 inputs.putAll(addExternalTlsInfo(onapComponentSpec, properties));
139             }
140         }
141
142         Map<String, Object> resourceConfigResponse =
143             resourceConfigService
144                 .createResourceConfig(inputs, onapComponentSpec.getSelf().getName());
145         inputs = (Map<String, LinkedHashMap<String, Object>>) resourceConfigResponse.get("inputs");
146         properties
147             .setResource_config((ResourceConfig) resourceConfigResponse.get("resourceConfig"));
148
149         response.put("properties", properties);
150         response.put("inputs", inputs);
151         return response;
152     }
153
154     /**
155      * Creates Dmaap properties
156      *
157      * @param inputs Inputs
158      * @param onapComponentSpec OnapComponentSpec
159      * @param override Override
160      * @return
161      */
162     public Map<String, Object> createDmaapProperties(
163         Map<String, LinkedHashMap<String, Object>> inputs,
164         OnapComponentSpec onapComponentSpec,
165         String override) {
166         Map<String, Object> response = new HashMap<>();
167         org.onap.blueprintgenerator.model.common.Properties properties =
168             new org.onap.blueprintgenerator.model.common.Properties();
169
170         GetInput image = new GetInput();
171         image.setBpInputName("tag_version");
172         properties.setImage(image);
173
174         LinkedHashMap<String, Object> img = new LinkedHashMap<>();
175         inputs.put(
176             "tag_version",
177             blueprintHelperService.createStringInput(onapComponentSpec.getArtifacts()[0].getUri()));
178
179         GetInput location = new GetInput();
180         location.setBpInputName("location_id");
181         properties.setLocation_id(location);
182
183         LinkedHashMap<String, Object> locMap = new LinkedHashMap();
184         inputs.put("location_id", blueprintHelperService.createStringInput(Constants.EMPTY_VALUE));
185
186         properties.setLog_info(onapComponentSpec.getAuxilary().getLog_info());
187
188         String sType = onapComponentSpec.getSelf().getName();
189         sType = sType.replace('.', '-');
190         properties.setService_component_type(sType);
191
192         Map<String, Object> tls_info = onapComponentSpec.getAuxilary().getTls_info();
193         if (tls_info != null) {
194             addTlsInfo(onapComponentSpec, inputs, properties);
195             if (tls_info.get(Constants.USE_EXTERNAL_TLS_FIELD) != null) {
196                 inputs.putAll(addExternalTlsInfo(onapComponentSpec, properties));
197             }
198         }
199
200         GetInput replica = new GetInput();
201         replica.setBpInputName("replicas");
202         properties.setReplicas(replica);
203
204         LinkedHashMap<String, Object> rep =
205             blueprintHelperService.createIntegerInput("number of instances", 1);
206         inputs.put("replicas", rep);
207
208         OnapAuxilary onapAuxilary = onapComponentSpec.getAuxilary();
209
210         properties.setDocker_config(onapAuxilary);
211
212         Map<String, Object> appConfigResponse =
213             appConfigService.createAppconfig(inputs, onapComponentSpec, override, true);
214         inputs = (Map<String, LinkedHashMap<String, Object>>) appConfigResponse.get("inputs");
215         properties.setApplication_config((Appconfig) appConfigResponse.get("appconfig"));
216
217         List<Streams> pubStreams = new ArrayList();
218         if (onapComponentSpec.getStreams() != null) {
219             if (onapComponentSpec.getStreams().getPublishes() != null) {
220                 for (Publishes publishes : onapComponentSpec.getStreams().getPublishes()) {
221                     if (blueprintHelperService.isMessageRouterType(publishes.getType())) {
222                         String topic = publishes.getConfig_key() + Constants._TOPIC;
223                         Map<String, Object> streamsMessageRouterResponse =
224                             streamsService.createStreams(
225                                 inputs,
226                                 topic,
227                                 publishes.getType(),
228                                 publishes.getConfig_key(),
229                                 publishes.getRoute(),
230                                 'p');
231                         inputs =
232                             (Map<String, LinkedHashMap<String, Object>>)
233                                 streamsMessageRouterResponse.get("inputs");
234                         pubStreams.add((Streams) streamsMessageRouterResponse.get("streams"));
235                     } else if (blueprintHelperService.isDataRouterType(publishes.getType())) {
236                         String feed = publishes.getConfig_key() + Constants._FEED;
237                         Map<String, Object> streamsDataRouterResponse =
238                             streamsService.createStreams(
239                                 inputs,
240                                 feed,
241                                 publishes.getType(),
242                                 publishes.getConfig_key(),
243                                 publishes.getRoute(),
244                                 'p');
245                         inputs =
246                             (Map<String, LinkedHashMap<String, Object>>)
247                                 streamsDataRouterResponse.get("inputs");
248                         pubStreams.add((Streams) streamsDataRouterResponse.get("streams"));
249                     }
250                 }
251             }
252         }
253
254         ArrayList<Streams> subStreams = new ArrayList();
255         if (onapComponentSpec.getStreams() != null) {
256             if (onapComponentSpec.getStreams().getSubscribes() != null) {
257                 for (Subscribes subscribes : onapComponentSpec.getStreams().getSubscribes()) {
258                     if (blueprintHelperService.isMessageRouterType(subscribes.getType())) {
259                         String topic = subscribes.getConfig_key() + Constants._TOPIC;
260                         Map<String, Object> streamsMessageRouterResponse =
261                             streamsService.createStreams(
262                                 inputs,
263                                 topic,
264                                 subscribes.getType(),
265                                 subscribes.getConfig_key(),
266                                 subscribes.getRoute(),
267                                 's');
268                         inputs =
269                             (Map<String, LinkedHashMap<String, Object>>)
270                                 streamsMessageRouterResponse.get("inputs");
271                         subStreams.add((Streams) streamsMessageRouterResponse.get("streams"));
272                     } else if (blueprintHelperService.isDataRouterType(subscribes.getType())) {
273                         String feed = subscribes.getConfig_key() + Constants._FEED;
274                         Map<String, Object> streamsDataRouterResponse =
275                             streamsService.createStreams(
276                                 inputs,
277                                 feed,
278                                 subscribes.getType(),
279                                 subscribes.getConfig_key(),
280                                 subscribes.getRoute(),
281                                 's');
282                         inputs =
283                             (Map<String, LinkedHashMap<String, Object>>)
284                                 streamsDataRouterResponse.get("inputs");
285                         subStreams.add((Streams) streamsDataRouterResponse.get("streams"));
286                     }
287                 }
288             }
289         }
290
291         if (!pubStreams.isEmpty()) {
292             properties.setStreams_publishes(pubStreams);
293         }
294
295         if (!subStreams.isEmpty()) {
296             properties.setStreams_subscribes(subStreams);
297         }
298
299         Map<String, Object> resourceConfigResponse =
300             resourceConfigService
301                 .createResourceConfig(inputs, onapComponentSpec.getSelf().getName());
302         inputs = (Map<String, LinkedHashMap<String, Object>>) resourceConfigResponse.get("inputs");
303         properties
304             .setResource_config((ResourceConfig) resourceConfigResponse.get("resourceConfig"));
305
306         response.put("properties", properties);
307         response.put("inputs", inputs);
308         return response;
309     }
310
311     private void addTlsInfo(
312         OnapComponentSpec onapComponentSpec,
313         Map<String, LinkedHashMap<String, Object>> inputs,
314         Properties properties) {
315         TlsInfo tlsInfo = new TlsInfo();
316         tlsInfo.setCertDirectory(
317             (String) onapComponentSpec.getAuxilary().getTls_info().get("cert_directory"));
318         GetInput useTLSFlag = new GetInput();
319         useTLSFlag.setBpInputName("use_tls");
320         tlsInfo.setUseTls(useTLSFlag);
321         properties.setTls_info(tlsInfo);
322         LinkedHashMap<String, Object> useTlsFlagInput =
323             blueprintHelperService.createBooleanInput(
324                 "flag to indicate tls enable/disable",
325                 onapComponentSpec.getAuxilary().getTls_info().get("use_tls"));
326         inputs.put("use_tls", useTlsFlagInput);
327     }
328
329     private Map<String, LinkedHashMap<String, Object>> addExternalTlsInfo(
330         OnapComponentSpec onapComponentSpec, Properties properties) {
331         properties.setExternal_cert(
332             externalTlsInfoFactoryService.createFromComponentSpec(onapComponentSpec));
333         return externalTlsInfoFactoryService.createInputListFromComponentSpec(onapComponentSpec);
334     }
335 }