Merge "Improve code quality:"
[dcaegen2/platform.git] / mod / bpgenerator / src / main / java / org / onap / blueprintgenerator / models / blueprint / Properties.java
1 /*============LICENSE_START=======================================================
2  org.onap.dcae
3  ================================================================================
4  Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
5  Copyright (c) 2020 Nokia. All rights reserved.
6  ================================================================================
7  Licensed under the Apache License, Version 2.0 (the "License");
8  you may not use this file except in compliance with the License.
9  You may obtain a copy of the License at
10
11  http://www.apache.org/licenses/LICENSE-2.0
12
13  Unless required by applicable law or agreed to in writing, software
14  distributed under the License is distributed on an "AS IS" BASIS,
15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  See the License for the specific language governing permissions and
17  limitations under the License.
18  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.blueprintgenerator.models.blueprint;
22
23 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createBooleanInput;
24 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createIntegerInput;
25 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createStringInput;
26 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isMessageRouterType;
27 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isDataRouterType;
28 import static org.onap.blueprintgenerator.models.blueprint.tls.TlsConstants.USE_EXTERNAL_TLS_FIELD;
29
30 import com.fasterxml.jackson.annotation.JsonIgnore;
31 import com.fasterxml.jackson.annotation.JsonInclude;
32 import com.fasterxml.jackson.annotation.JsonInclude.Include;
33 import java.util.ArrayList;
34 import java.util.LinkedHashMap;
35 import java.util.Map;
36 import java.util.TreeMap;
37 import lombok.Getter;
38 import lombok.Setter;
39 import org.onap.blueprintgenerator.models.blueprint.tls.ExternalCertificateParametersFactory;
40 import org.onap.blueprintgenerator.models.blueprint.tls.ExternalTlsInfoFactory;
41 import org.onap.blueprintgenerator.models.blueprint.tls.TlsInfo;
42 import org.onap.blueprintgenerator.models.blueprint.tls.impl.ExternalTlsInfo;
43 import org.onap.blueprintgenerator.models.componentspec.Auxilary;
44 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
45 import org.onap.blueprintgenerator.models.componentspec.Publishes;
46 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
47 import org.onap.blueprintgenerator.models.dmaapbp.DmaapStreams;
48
49
50 @Getter
51 @Setter
52 @JsonInclude(value = Include.NON_NULL)
53 public class Properties {
54
55     private static final String EMPTY_VALUE = "";
56
57     ArrayList<DmaapStreams> streams_publishes;
58     ArrayList<DmaapStreams> streams_subscribes;
59     private Appconfig application_config;
60     private Auxilary docker_config;
61     private Object image;
62     private GetInput location_id;
63     private String service_component_type;
64     private TreeMap<String, Object> log_info;
65     private String dns_name;
66     private Object replicas;
67     private String name;
68     private GetInput topic_name;
69     private GetInput feed_name;
70     private TlsInfo tls_info;
71     private ExternalTlsInfo external_cert;
72     private ResourceConfig resource_config;
73     private GetInput always_pull_image;
74     //private boolean useExisting;
75     @JsonIgnore
76     private ExternalTlsInfoFactory externalCertFactory;
77
78     public Properties() {
79         ExternalCertificateParametersFactory externalCertificateDataFactory = new ExternalCertificateParametersFactory();
80         externalCertFactory = new ExternalTlsInfoFactory(externalCertificateDataFactory);
81     }
82
83     public TreeMap<String, LinkedHashMap<String, Object>> createOnapProperties(
84         TreeMap<String, LinkedHashMap<String, Object>> inps, ComponentSpec componentSpec, String override) {
85         TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
86
87         //set the image
88         GetInput image = new GetInput();
89         image.setBpInputName("image");
90         this.setImage(image);
91         retInputs.put("image", createStringInput(componentSpec.getImageUri()));
92
93         //set the location id
94         GetInput location = new GetInput();
95         location.setBpInputName("location_id");
96         this.setLocation_id(location);
97         retInputs.put("location_id", createStringInput(EMPTY_VALUE));
98
99         //set the log info
100         this.setLog_info(componentSpec.getAuxilary().getLog_info());
101
102         //set the replicas
103         GetInput replica = new GetInput();
104         replica.setBpInputName("replicas");
105         this.setReplicas(replica);
106         LinkedHashMap<String, Object> rep = createIntegerInput("number of instances", 1);
107         retInputs.put("replicas", rep);
108
109         //set the dns name
110         //this.setDns_name(cs.getSelf().getName());
111
112         //set the name
113         //this.setName(cs.getSelf().getName());
114
115         //set the docker config
116         Auxilary aux = componentSpec.getAuxilary();
117 //              if(aux.getPorts() != null) {
118 //                      retInputs = aux.createPorts(retInputs);
119 //              }
120         this.setDocker_config(aux);
121
122         //set the app config
123         Appconfig app = new Appconfig();
124         retInputs = app.createAppconfig(retInputs, componentSpec, override, false);
125         this.setApplication_config(app);
126
127         // set always_pull_image
128         this.always_pull_image = new GetInput();
129         this.always_pull_image.setBpInputName("always_pull_image");
130         LinkedHashMap<String, Object> inputAlwaysPullImage = createBooleanInput(
131             "Set to true if the image should always be pulled",
132             true);
133         retInputs.put("always_pull_image", inputAlwaysPullImage);
134
135         //set service component type
136         String serviceComponentType = componentSpec.getSelfName().replace('.', '-');
137         this.setService_component_type(serviceComponentType);
138
139         //set the tls info for internal and external communication
140         TreeMap<String, Object> tls_info = componentSpec.getAuxilary().getTls_info();
141         if (tls_info != null) {
142             addTlsInfo(componentSpec, retInputs);
143             if (tls_info.get(USE_EXTERNAL_TLS_FIELD) != null) {
144                 retInputs.putAll(addExternalTlsInfo(componentSpec));
145             }
146         }
147
148         //set the reource config
149         ResourceConfig resource = new ResourceConfig();
150         retInputs = resource.createResourceConfig(retInputs, componentSpec.getSelf().getName());
151         this.setResource_config(resource);
152
153         return retInputs;
154     }
155
156     public TreeMap<String, LinkedHashMap<String, Object>> createDmaapProperties(
157         TreeMap<String, LinkedHashMap<String, Object>> inps, ComponentSpec componentSpec, String override) {
158         TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
159
160         //set the image
161         GetInput image = new GetInput();
162         image.setBpInputName("tag_version");
163         this.setImage(image);
164         retInputs.put("tag_version", createStringInput(componentSpec.getImageUri()));
165
166         //set the location id
167         GetInput location = new GetInput();
168         location.setBpInputName("location_id");
169         this.setLocation_id(location);
170         retInputs.put("location_id", createStringInput(EMPTY_VALUE));
171
172         //set the log info
173         this.setLog_info(componentSpec.getAuxilary().getLog_info());
174
175         //set service component type
176         String serviceComponentType = componentSpec.getSelfName().replace('.', '-');
177         this.setService_component_type(serviceComponentType);
178
179         //set the tls info for internal and external communication
180         TreeMap<String, Object> tls_info = componentSpec.getAuxilary().getTls_info();
181         if (tls_info != null) {
182             addTlsInfo(componentSpec, retInputs);
183             if (tls_info.get(USE_EXTERNAL_TLS_FIELD) != null) {
184                 retInputs.putAll(addExternalTlsInfo(componentSpec));
185             }
186         }
187
188         //set the replicas
189         GetInput replica = new GetInput();
190         replica.setBpInputName("replicas");
191         this.setReplicas(replica);
192         LinkedHashMap<String, Object> rep = createIntegerInput("number of instances", 1);
193         retInputs.put("replicas", rep);
194
195 //              //set the dns name
196 //              this.setDns_name(cs.getSelf().getName());
197
198 //              //set the name
199 //              this.setName(cs.getSelf().getName());
200
201         //set the docker config
202         Auxilary aux = componentSpec.getAuxilary();
203 //              if(aux.getPorts() != null) {
204 //                      retInputs = aux.createPorts(retInputs);
205 //              }
206         this.setDocker_config(aux);
207
208         //set the appconfig
209         Appconfig app = new Appconfig();
210         retInputs = app.createAppconfig(retInputs, componentSpec, override, true);
211         this.setApplication_config(app);
212
213         //set the stream publishes
214         ArrayList<DmaapStreams> pubStreams = new ArrayList<>();
215         if (componentSpec.getStreams().getPublishes() != null) {
216             for (Publishes publishes : componentSpec.getStreams().getPublishes()) {
217                 if (isMessageRouterType(publishes.getType())) {
218                     String topic = publishes.getConfig_key() + "_topic";
219                     DmaapStreams mrStreams = new DmaapStreams();
220                     retInputs = mrStreams
221                         .createStreams(inps, componentSpec, topic, publishes.getType(), publishes.getConfig_key(),
222                             publishes.getRoute(), 'p');
223                     pubStreams.add(mrStreams);
224                 } else if (isDataRouterType(publishes.getType())) {
225                     String feed = publishes.getConfig_key() + "_feed";
226                     DmaapStreams drStreams = new DmaapStreams();
227                     retInputs = drStreams
228                         .createStreams(inps, componentSpec, feed, publishes.getType(), publishes.getConfig_key(),
229                             publishes.getRoute(), 'p');
230                     pubStreams.add(drStreams);
231                 }
232             }
233         }
234
235         //set the stream subscribes
236         ArrayList<DmaapStreams> subStreams = new ArrayList<>();
237         if (componentSpec.getStreams().getSubscribes() != null) {
238             for (Subscribes subscribes : componentSpec.getStreams().getSubscribes()) {
239                 if (isMessageRouterType(subscribes.getType())) {
240                     String topic = subscribes.getConfig_key() + "_topic";
241                     DmaapStreams mrStreams = new DmaapStreams();
242                     retInputs = mrStreams
243                         .createStreams(inps, componentSpec, topic, subscribes.getType(), subscribes.getConfig_key(),
244                             subscribes.getRoute(), 's');
245                     subStreams.add(mrStreams);
246                 } else if (isDataRouterType(subscribes.getType())) {
247                     String feed = subscribes.getConfig_key() + "_feed";
248                     DmaapStreams drStreams = new DmaapStreams();
249                     retInputs = drStreams
250                         .createStreams(inps, componentSpec, feed, subscribes.getType(), subscribes.getConfig_key(),
251                             subscribes.getRoute(), 's');
252                     subStreams.add(drStreams);
253                 }
254             }
255         }
256
257         if (!pubStreams.isEmpty()) {
258             this.setStreams_publishes(pubStreams);
259         }
260         if (!subStreams.isEmpty()) {
261             this.setStreams_subscribes(subStreams);
262         }
263
264         //set the reource config
265         ResourceConfig resource = new ResourceConfig();
266         retInputs = resource.createResourceConfig(retInputs, componentSpec.getSelf().getName());
267         this.setResource_config(resource);
268
269         return retInputs;
270     }
271
272     private void addTlsInfo(ComponentSpec cs, TreeMap<String, LinkedHashMap<String, Object>> retInputs) {
273         TlsInfo tlsInfo = new TlsInfo();
274         tlsInfo.setCertDirectory((String) cs.getAuxilary().getTls_info().get("cert_directory"));
275         GetInput useTLSFlag = new GetInput();
276         useTLSFlag.setBpInputName("use_tls");
277         tlsInfo.setUseTls(useTLSFlag);
278         this.setTls_info(tlsInfo);
279         LinkedHashMap<String, Object> useTlsFlagInput = createBooleanInput(
280             "flag to indicate tls enable/disable",
281             cs.getAuxilary().getTls_info().get("use_tls"));
282         retInputs.put("use_tls", useTlsFlagInput);
283     }
284
285     private Map<String, LinkedHashMap<String, Object>> addExternalTlsInfo(ComponentSpec cs) {
286         this.setExternal_cert(externalCertFactory.createFromComponentSpec(cs));
287         return externalCertFactory.createInputListFromComponentSpec(cs);
288     }
289
290 }