3 * * ============LICENSE_START=======================================================
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
12 * * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.blueprintgenerator.service.common;
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;
42 import java.util.LinkedHashMap;
44 import java.util.HashMap;
45 import java.util.ArrayList;
46 import java.util.List;
49 * @author : Ravi Mantena
51 * Application: ONAP - Blueprint Generator
52 * Common ONAP Service used by ONAP and DMAAP Blueprint to create Properties Node
57 @Service("onapPropertiesService")
58 public class PropertiesService {
61 private AppConfigService appConfigService;
64 private ResourceConfigService resourceConfigService;
67 private StreamsService streamsService;
70 private ExternalTlsInfoFactoryService externalTlsInfoFactoryService;
73 private BlueprintHelperService blueprintHelperService;
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();
80 GetInput image = new GetInput();
81 image.setBpInputName("image");
82 properties.setImage(image);
84 LinkedHashMap<String, Object> img = new LinkedHashMap<>();
85 inputs.put("image", blueprintHelperService.createStringInput(onapComponentSpec.getArtifacts()[0].getUri()));
88 GetInput location = new GetInput();
89 location.setBpInputName("location_id");
90 properties.setLocation_id(location);
92 LinkedHashMap<String, Object> locMap = new LinkedHashMap();
93 inputs.put("location_id", blueprintHelperService.createStringInput(Constants.EMPTY_VALUE));
95 properties.setLog_info(onapComponentSpec.getAuxilary().getLog_info());
97 GetInput replica = new GetInput();
98 replica.setBpInputName("replicas");
99 properties.setReplicas(replica);
101 LinkedHashMap<String, Object> replicas = blueprintHelperService.createIntegerInput("number of instances", 1);
102 inputs.put("replicas", replicas);
104 OnapAuxilary onapAuxilary = onapComponentSpec.getAuxilary();
106 properties.setDocker_config(onapAuxilary);
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"));
112 GetInput always_pull_image = new GetInput();
113 always_pull_image.setBpInputName("always_pull_image");
115 properties.setAlways_pull_image(always_pull_image);
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);
120 String sType = onapComponentSpec.getSelf().getName();
121 sType = sType.replace('.', '-');
122 properties.setService_component_type(sType);
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));
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"));
136 response.put("properties", properties);
137 response.put("inputs", inputs);
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();
146 GetInput image = new GetInput();
147 image.setBpInputName("tag_version");
148 properties.setImage(image);
150 LinkedHashMap<String, Object> img = new LinkedHashMap<>();
151 inputs.put("tag_version", blueprintHelperService.createStringInput(onapComponentSpec.getArtifacts()[0].getUri()));
153 GetInput location = new GetInput();
154 location.setBpInputName("location_id");
155 properties.setLocation_id(location);
157 LinkedHashMap<String, Object> locMap = new LinkedHashMap();
158 inputs.put("location_id", blueprintHelperService.createStringInput(Constants.EMPTY_VALUE));
160 properties.setLog_info(onapComponentSpec.getAuxilary().getLog_info());
162 String sType = onapComponentSpec.getSelf().getName();
163 sType = sType.replace('.', '-');
164 properties.setService_component_type(sType);
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));
174 GetInput replica = new GetInput();
175 replica.setBpInputName("replicas");
176 properties.setReplicas(replica);
178 LinkedHashMap<String, Object> rep = blueprintHelperService.createIntegerInput( "number of instances", 1);
179 inputs.put("replicas", rep);
181 OnapAuxilary onapAuxilary = onapComponentSpec.getAuxilary();
183 properties.setDocker_config(onapAuxilary);
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"));
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"));
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"));
228 if(!pubStreams.isEmpty())
229 properties.setStreams_publishes(pubStreams);
231 if(!subStreams.isEmpty())
232 properties.setStreams_subscribes(subStreams);
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"));
238 response.put("properties", properties);
239 response.put("inputs", inputs);
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);
254 private Map<String, LinkedHashMap<String, Object>> addExternalTlsInfo(OnapComponentSpec onapComponentSpec, Properties properties) {
255 properties.setExternal_cert(externalTlsInfoFactoryService.createFromComponentSpec(onapComponentSpec));
256 return externalTlsInfoFactoryService.createInputListFromComponentSpec(onapComponentSpec);