Merge "Refactor, fix code formatting and add unittests"
[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.isMessageRouterType;
26 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isDataRouterType;
27 import static org.onap.blueprintgenerator.models.blueprint.tls.TlsConstants.USE_EXTERNAL_TLS_FIELD;
28
29 import com.fasterxml.jackson.annotation.JsonIgnore;
30 import com.fasterxml.jackson.annotation.JsonInclude;
31 import com.fasterxml.jackson.annotation.JsonInclude.Include;
32 import java.util.ArrayList;
33 import java.util.LinkedHashMap;
34 import java.util.Map;
35 import java.util.TreeMap;
36 import lombok.Getter;
37 import lombok.Setter;
38 import org.onap.blueprintgenerator.models.blueprint.tls.ExternalCertificateParametersFactory;
39 import org.onap.blueprintgenerator.models.blueprint.tls.ExternalTlsInfoFactory;
40 import org.onap.blueprintgenerator.models.blueprint.tls.TlsInfo;
41 import org.onap.blueprintgenerator.models.blueprint.tls.impl.ExternalTlsInfo;
42 import org.onap.blueprintgenerator.models.componentspec.Auxilary;
43 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
44 import org.onap.blueprintgenerator.models.componentspec.Publishes;
45 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
46 import org.onap.blueprintgenerator.models.dmaapbp.DmaapStreams;
47
48
49 @Getter
50 @Setter
51 @JsonInclude(value = Include.NON_NULL)
52 public class Properties {
53
54     ArrayList<DmaapStreams> streams_publishes;
55     ArrayList<DmaapStreams> streams_subscribes;
56     private Appconfig application_config;
57     private Auxilary docker_config;
58     private Object image;
59     private GetInput location_id;
60     private String service_component_type;
61     private TreeMap<String, Object> log_info;
62     private String dns_name;
63     private Object replicas;
64     private String name;
65     private GetInput topic_name;
66     private GetInput feed_name;
67     private TlsInfo tls_info;
68     private ExternalTlsInfo external_cert;
69     private ResourceConfig resource_config;
70     private GetInput always_pull_image;
71     //private boolean useExisting;
72     @JsonIgnore
73     private ExternalTlsInfoFactory externalCertFactory;
74
75     public Properties() {
76         ExternalCertificateParametersFactory externalCertificateDataFactory = new ExternalCertificateParametersFactory();
77         externalCertFactory = new ExternalTlsInfoFactory(externalCertificateDataFactory);
78     }
79
80     public TreeMap<String, LinkedHashMap<String, Object>> createOnapProperties(
81         TreeMap<String, LinkedHashMap<String, Object>> inps, ComponentSpec cs, String override) {
82         TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
83
84         //set the image
85         GetInput image = new GetInput();
86         image.setBpInputName("image");
87         this.setImage(image);
88         LinkedHashMap<String, Object> img = new LinkedHashMap<String, Object>();
89         img.put("type", "string");
90         img.put("default", cs.getArtifacts()[0].getUri());
91         retInputs.put("image", img);
92
93         //set the location id
94         GetInput location = new GetInput();
95         location.setBpInputName("location_id");
96         this.setLocation_id(location);
97         LinkedHashMap<String, Object> locMap = new LinkedHashMap();
98         locMap.put("type", "string");
99         locMap.put("default", "");
100         retInputs.put("location_id", locMap);
101
102         //set the log info
103         this.setLog_info(cs.getAuxilary().getLog_info());
104
105         //set the replicas
106         GetInput replica = new GetInput();
107         replica.setBpInputName("replicas");
108         this.setReplicas(replica);
109         LinkedHashMap<String, Object> rep = createIntegerInput("number of instances", 1);
110         retInputs.put("replicas", rep);
111
112         //set the dns name
113         //this.setDns_name(cs.getSelf().getName());
114
115         //set the name
116         //this.setName(cs.getSelf().getName());
117
118         //set the docker config
119         Auxilary aux = cs.getAuxilary();
120 //              if(aux.getPorts() != null) {
121 //                      retInputs = aux.createPorts(retInputs);
122 //              }
123         this.setDocker_config(aux);
124
125         //set the app config
126         Appconfig app = new Appconfig();
127         retInputs = app.createAppconfig(retInputs, cs, override, false);
128         this.setApplication_config(app);
129
130         // set always_pull_image
131         this.always_pull_image = new GetInput();
132         this.always_pull_image.setBpInputName("always_pull_image");
133         LinkedHashMap<String, Object> inputAlwaysPullImage = createBooleanInput(
134             "Set to true if the image should always be pulled",
135             true);
136         retInputs.put("always_pull_image", inputAlwaysPullImage);
137
138         //set service component type
139         String sType = cs.getSelf().getName();
140         sType = sType.replace('.', '-');
141         this.setService_component_type(sType);
142
143         //set the tls info for internal and external communication
144         TreeMap<String, Object> tls_info = cs.getAuxilary().getTls_info();
145         if (tls_info != null) {
146             addTlsInfo(cs, retInputs);
147             if (tls_info.get(USE_EXTERNAL_TLS_FIELD) != null) {
148                 retInputs.putAll(addExternalTlsInfo(cs));
149             }
150         }
151
152         //set the reource config
153         ResourceConfig resource = new ResourceConfig();
154         retInputs = resource.createResourceConfig(retInputs, cs.getSelf().getName());
155         this.setResource_config(resource);
156
157         return retInputs;
158     }
159
160     public TreeMap<String, LinkedHashMap<String, Object>> createDmaapProperties(
161         TreeMap<String, LinkedHashMap<String, Object>> inps, ComponentSpec cs, String override) {
162         TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
163
164         //set the image
165         GetInput image = new GetInput();
166         image.setBpInputName("tag_version");
167         this.setImage(image);
168         LinkedHashMap<String, Object> img = new LinkedHashMap<String, Object>();
169         img.put("type", "string");
170         img.put("default", cs.getArtifacts()[0].getUri());
171         retInputs.put("tag_version", img);
172
173         //set the location id
174         GetInput location = new GetInput();
175         location.setBpInputName("location_id");
176         this.setLocation_id(location);
177         LinkedHashMap<String, Object> locMap = new LinkedHashMap<>();
178         locMap.put("type", "string");
179         locMap.put("default", "");
180         retInputs.put("location_id", locMap);
181
182         //set the log info
183         this.setLog_info(cs.getAuxilary().getLog_info());
184
185         //set service component type
186         String sType = cs.getSelf().getName();
187         sType = sType.replace('.', '-');
188         this.setService_component_type(sType);
189
190         //set the tls info for internal and external communication
191         TreeMap<String, Object> tls_info = cs.getAuxilary().getTls_info();
192         if (tls_info != null) {
193             addTlsInfo(cs, retInputs);
194             if (tls_info.get(USE_EXTERNAL_TLS_FIELD) != null) {
195                 retInputs.putAll(addExternalTlsInfo(cs));
196             }
197         }
198
199         //set the replicas
200         GetInput replica = new GetInput();
201         replica.setBpInputName("replicas");
202         this.setReplicas(replica);
203         LinkedHashMap<String, Object> rep = createIntegerInput( "number of instances", 1);
204         retInputs.put("replicas", rep);
205
206 //              //set the dns name
207 //              this.setDns_name(cs.getSelf().getName());
208
209 //              //set the name
210 //              this.setName(cs.getSelf().getName());
211
212         //set the docker config
213         Auxilary aux = cs.getAuxilary();
214 //              if(aux.getPorts() != null) {
215 //                      retInputs = aux.createPorts(retInputs);
216 //              }
217         this.setDocker_config(aux);
218
219         //set the appconfig
220         Appconfig app = new Appconfig();
221         retInputs = app.createAppconfig(retInputs, cs, override, true);
222         this.setApplication_config(app);
223
224         //set the stream publishes
225         ArrayList<DmaapStreams> pubStreams = new ArrayList<>();
226         if (cs.getStreams().getPublishes() != null) {
227             for (Publishes publishes : cs.getStreams().getPublishes()) {
228                 if (isMessageRouterType(publishes.getType())) {
229                     String topic = publishes.getConfig_key() + "_topic";
230                     DmaapStreams mrStreams = new DmaapStreams();
231                     retInputs = mrStreams
232                         .createStreams(inps, cs, topic, publishes.getType(), publishes.getConfig_key(),
233                             publishes.getRoute(), 'p');
234                     pubStreams.add(mrStreams);
235                 } else if (isDataRouterType(publishes.getType())) {
236                     String feed = publishes.getConfig_key() + "_feed";
237                     DmaapStreams drStreams = new DmaapStreams();
238                     retInputs = drStreams
239                         .createStreams(inps, cs, feed, publishes.getType(), publishes.getConfig_key(),
240                             publishes.getRoute(), 'p');
241                     pubStreams.add(drStreams);
242                 }
243             }
244         }
245
246         //set the stream subscribes
247         ArrayList<DmaapStreams> subStreams = new ArrayList<>();
248         if (cs.getStreams().getSubscribes() != null) {
249             for (Subscribes subscribes : cs.getStreams().getSubscribes()) {
250                 if (isMessageRouterType(subscribes.getType())) {
251                     String topic = subscribes.getConfig_key() + "_topic";
252                     DmaapStreams mrStreams = new DmaapStreams();
253                     retInputs = mrStreams
254                         .createStreams(inps, cs, topic, subscribes.getType(), subscribes.getConfig_key(),
255                             subscribes.getRoute(), 's');
256                     subStreams.add(mrStreams);
257                 } else if (isDataRouterType(subscribes.getType())) {
258                     String feed = subscribes.getConfig_key() + "_feed";
259                     DmaapStreams drStreams = new DmaapStreams();
260                     retInputs = drStreams
261                         .createStreams(inps, cs, feed, subscribes.getType(), subscribes.getConfig_key(),
262                             subscribes.getRoute(), 's');
263                     subStreams.add(drStreams);
264                 }
265             }
266         }
267
268         if (!pubStreams.isEmpty()) {
269             this.setStreams_publishes(pubStreams);
270         }
271         if (!subStreams.isEmpty()) {
272             this.setStreams_subscribes(subStreams);
273         }
274
275         //set the reource config
276         ResourceConfig resource = new ResourceConfig();
277         retInputs = resource.createResourceConfig(retInputs, cs.getSelf().getName());
278         this.setResource_config(resource);
279
280         return retInputs;
281     }
282
283     private void addTlsInfo(ComponentSpec cs, TreeMap<String, LinkedHashMap<String, Object>> retInputs) {
284         TlsInfo tlsInfo = new TlsInfo();
285         tlsInfo.setCertDirectory((String) cs.getAuxilary().getTls_info().get("cert_directory"));
286         GetInput useTLSFlag = new GetInput();
287         useTLSFlag.setBpInputName("use_tls");
288         tlsInfo.setUseTls(useTLSFlag);
289         this.setTls_info(tlsInfo);
290         LinkedHashMap<String, Object> useTlsFlagInput = createBooleanInput(
291             "flag to indicate tls enable/disable",
292             cs.getAuxilary().getTls_info().get("use_tls"));
293         retInputs.put("use_tls", useTlsFlagInput);
294     }
295
296     private Map<String, LinkedHashMap<String, Object>> addExternalTlsInfo(ComponentSpec cs) {
297         this.setExternal_cert(externalCertFactory.createFromComponentSpec(cs));
298         return externalCertFactory.createInputListFromComponentSpec(cs);
299     }
300
301 }