Merge "Changes to Regex for Name and version Issue-ID: DCAEGEN2-1867"
[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
51  * Application: ONAP - Blueprint Generator
52  * Common ONAP Service used by ONAP and DMAAP Blueprint to create Properties Node
53  */
54
55
56
57 @Service("onapPropertiesService")
58 public class PropertiesService {
59
60     @Autowired
61     private AppConfigService appConfigService;
62
63     @Autowired
64     private ResourceConfigService resourceConfigService;
65
66     @Autowired
67     private StreamsService streamsService;
68
69     @Autowired
70     private ExternalTlsInfoFactoryService externalTlsInfoFactoryService;
71
72     @Autowired
73     private BlueprintHelperService blueprintHelperService;
74
75     // Method to create ONAP properties
76     public Map<String,Object> createOnapProperties(Map<String, LinkedHashMap<String, Object>> inputs, OnapComponentSpec onapComponentSpec, String override) {
77         Map<String,Object> response = new HashMap<>();
78         org.onap.blueprintgenerator.model.common.Properties properties = new org.onap.blueprintgenerator.model.common.Properties();
79
80         GetInput image = new GetInput();
81         image.setBpInputName("image");
82         properties.setImage(image);
83
84         LinkedHashMap<String, Object> img = new LinkedHashMap<>();
85         inputs.put("image", blueprintHelperService.createStringInput(onapComponentSpec.getArtifacts()[0].getUri()));
86
87
88         GetInput location = new GetInput();
89         location.setBpInputName("location_id");
90         properties.setLocation_id(location);
91
92         LinkedHashMap<String, Object> locMap = new LinkedHashMap();
93         inputs.put("location_id", blueprintHelperService.createStringInput(Constants.EMPTY_VALUE));
94
95         properties.setLog_info(onapComponentSpec.getAuxilary().getLog_info());
96
97         GetInput replica = new GetInput();
98         replica.setBpInputName("replicas");
99         properties.setReplicas(replica);
100
101         LinkedHashMap<String, Object> replicas = blueprintHelperService.createIntegerInput("number of instances", 1);
102         inputs.put("replicas", replicas);
103
104         OnapAuxilary onapAuxilary = onapComponentSpec.getAuxilary();
105
106         properties.setDocker_config(onapAuxilary);
107
108         Map<String, Object> appConfigResponse = appConfigService.createAppconfig(inputs, onapComponentSpec, override, false);
109         inputs = (Map<String, LinkedHashMap<String, Object>>) appConfigResponse.get("inputs");
110         properties.setApplication_config((Appconfig) appConfigResponse.get("appconfig"));
111
112         GetInput always_pull_image = new GetInput();
113         always_pull_image.setBpInputName("always_pull_image");
114
115         properties.setAlways_pull_image(always_pull_image);
116
117         LinkedHashMap<String, Object> inputAlwaysPullImage = blueprintHelperService.createBooleanInput("Set to true if the image should always be pulled",true);
118         inputs.put("always_pull_image", inputAlwaysPullImage);
119
120         String sType = onapComponentSpec.getSelf().getName();
121         sType = sType.replace('.', '-');
122         properties.setService_component_type(sType);
123
124         Map<String, Object> tls_info = onapComponentSpec.getAuxilary().getTls_info();
125         if(tls_info != null) {
126             addTlsInfo(onapComponentSpec, inputs, properties);
127             if (tls_info.get(Constants.USE_EXTERNAL_TLS_FIELD) != null) {
128                 inputs.putAll(addExternalTlsInfo(onapComponentSpec,properties));
129             }
130         }
131
132         Map<String, Object> resourceConfigResponse = resourceConfigService.createResourceConfig(inputs, onapComponentSpec.getSelf().getName());
133         inputs = (Map<String, LinkedHashMap<String, Object>>) resourceConfigResponse.get("inputs");
134         properties.setResource_config((ResourceConfig) resourceConfigResponse.get("resourceConfig"));
135
136         response.put("properties", properties);
137         response.put("inputs", inputs);
138         return response;
139     }
140
141     // Method to create Dmaap properties
142     public  Map<String,Object> createDmaapProperties(Map<String, LinkedHashMap<String, Object>> inputs, OnapComponentSpec onapComponentSpec, String override) {
143         Map<String,Object> response = new HashMap<>();
144         org.onap.blueprintgenerator.model.common.Properties properties = new org.onap.blueprintgenerator.model.common.Properties();
145
146         GetInput image = new GetInput();
147         image.setBpInputName("tag_version");
148         properties.setImage(image);
149
150         LinkedHashMap<String, Object> img = new LinkedHashMap<>();
151         inputs.put("tag_version", blueprintHelperService.createStringInput(onapComponentSpec.getArtifacts()[0].getUri()));
152
153         GetInput location = new GetInput();
154         location.setBpInputName("location_id");
155         properties.setLocation_id(location);
156
157         LinkedHashMap<String, Object> locMap = new LinkedHashMap();
158         inputs.put("location_id", blueprintHelperService.createStringInput(Constants.EMPTY_VALUE));
159
160         properties.setLog_info(onapComponentSpec.getAuxilary().getLog_info());
161
162         String sType = onapComponentSpec.getSelf().getName();
163         sType = sType.replace('.', '-');
164         properties.setService_component_type(sType);
165
166         Map<String, Object> tls_info = onapComponentSpec.getAuxilary().getTls_info();
167         if(tls_info != null) {
168             addTlsInfo(onapComponentSpec, inputs, properties);
169             if (tls_info.get(Constants.USE_EXTERNAL_TLS_FIELD) != null) {
170                 inputs.putAll(addExternalTlsInfo(onapComponentSpec,properties));
171             }
172         }
173
174         GetInput replica = new GetInput();
175         replica.setBpInputName("replicas");
176         properties.setReplicas(replica);
177
178         LinkedHashMap<String, Object> rep =  blueprintHelperService.createIntegerInput( "number of instances", 1);
179         inputs.put("replicas", rep);
180
181         OnapAuxilary onapAuxilary = onapComponentSpec.getAuxilary();
182
183         properties.setDocker_config(onapAuxilary);
184
185         Map<String, Object> appConfigResponse = appConfigService.createAppconfig(inputs, onapComponentSpec, override, true);
186         inputs = (Map<String, LinkedHashMap<String, Object>>) appConfigResponse.get("inputs");
187         properties.setApplication_config((Appconfig) appConfigResponse.get("appconfig"));
188
189
190         List<Streams> pubStreams = new ArrayList();
191         if(onapComponentSpec.getStreams() != null) {
192             if (onapComponentSpec.getStreams().getPublishes() != null) {
193                 for (Publishes publishes : onapComponentSpec.getStreams().getPublishes()) {
194                     if (blueprintHelperService.isMessageRouterType(publishes.getType())) {
195                         String topic = publishes.getConfig_key() + Constants._TOPIC;
196                         Map<String, Object> streamsMessageRouterResponse = streamsService.createStreams(inputs, topic, publishes.getType(), publishes.getConfig_key(), publishes.getRoute(), 'p');
197                         inputs = (Map<String, LinkedHashMap<String, Object>>) streamsMessageRouterResponse.get("inputs");
198                         pubStreams.add((Streams) streamsMessageRouterResponse.get("streams"));
199                     } else if (blueprintHelperService.isDataRouterType(publishes.getType())) {
200                         String feed = publishes.getConfig_key() + Constants._FEED;
201                         Map<String, Object> streamsDataRouterResponse = streamsService.createStreams(inputs, feed, publishes.getType(), publishes.getConfig_key(), publishes.getRoute(), 'p');
202                         inputs = (Map<String, LinkedHashMap<String, Object>>) streamsDataRouterResponse.get("inputs");
203                         pubStreams.add((Streams) streamsDataRouterResponse.get("streams"));
204                     }
205                 }
206             }
207         }
208
209         ArrayList<Streams> subStreams = new ArrayList();
210         if(onapComponentSpec.getStreams() != null) {
211             if (onapComponentSpec.getStreams().getSubscribes() != null) {
212                 for (Subscribes subscribes : onapComponentSpec.getStreams().getSubscribes()) {
213                     if (blueprintHelperService.isMessageRouterType(subscribes.getType())) {
214                         String topic = subscribes.getConfig_key() + Constants._TOPIC;
215                         Map<String, Object> streamsMessageRouterResponse = streamsService.createStreams(inputs, topic, subscribes.getType(), subscribes.getConfig_key(), subscribes.getRoute(), 's');
216                         inputs = (Map<String, LinkedHashMap<String, Object>>) streamsMessageRouterResponse.get("inputs");
217                         subStreams.add((Streams) streamsMessageRouterResponse.get("streams"));
218                     } else if (blueprintHelperService.isDataRouterType(subscribes.getType())) {
219                         String feed = subscribes.getConfig_key() + Constants._FEED;
220                         Map<String, Object> streamsDataRouterResponse = streamsService.createStreams(inputs, feed, subscribes.getType(), subscribes.getConfig_key(), subscribes.getRoute(), 's');
221                         inputs = (Map<String, LinkedHashMap<String, Object>>) streamsDataRouterResponse.get("inputs");
222                         subStreams.add((Streams) streamsDataRouterResponse.get("streams"));
223                     }
224                 }
225             }
226         }
227
228         if(!pubStreams.isEmpty())
229             properties.setStreams_publishes(pubStreams);
230
231         if(!subStreams.isEmpty())
232             properties.setStreams_subscribes(subStreams);
233
234         Map<String, Object> resourceConfigResponse = resourceConfigService.createResourceConfig(inputs, onapComponentSpec.getSelf().getName());
235         inputs = (Map<String, LinkedHashMap<String, Object>>) resourceConfigResponse.get("inputs");
236         properties.setResource_config((ResourceConfig) resourceConfigResponse.get("resourceConfig"));
237
238         response.put("properties", properties);
239         response.put("inputs", inputs);
240         return response;
241     }
242
243     private void addTlsInfo(OnapComponentSpec onapComponentSpec, Map<String, LinkedHashMap<String, Object>> inputs, Properties properties) {
244         TlsInfo tlsInfo = new TlsInfo();
245         tlsInfo.setCertDirectory((String) onapComponentSpec.getAuxilary().getTls_info().get("cert_directory"));
246         GetInput useTLSFlag = new GetInput();
247         useTLSFlag.setBpInputName("use_tls");
248         tlsInfo.setUseTls(useTLSFlag);
249         properties.setTls_info(tlsInfo);
250         LinkedHashMap<String, Object> useTlsFlagInput = blueprintHelperService.createBooleanInput("flag to indicate tls enable/disable",onapComponentSpec.getAuxilary().getTls_info().get("use_tls"));
251         inputs.put("use_tls", useTlsFlagInput);
252     }
253
254  private Map<String, LinkedHashMap<String, Object>> addExternalTlsInfo(OnapComponentSpec onapComponentSpec, Properties properties) {
255   properties.setExternal_cert(externalTlsInfoFactoryService.createFromComponentSpec(onapComponentSpec));
256   return externalTlsInfoFactoryService.createInputListFromComponentSpec(onapComponentSpec);
257  }
258
259 }