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     private Appconfig application_config;
55     private Auxilary docker_config;
56     private Object image;
57     private GetInput location_id;
58     private String service_component_type;
59     private TreeMap<String, Object> log_info;
60     private String dns_name;
61     private Object replicas;
62     private String name;
63     private GetInput topic_name;
64     private GetInput feed_name;
65     ArrayList<DmaapStreams> streams_publishes;
66     ArrayList<DmaapStreams> streams_subscribes;
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 = new TreeMap<String, LinkedHashMap<String, Object>>();
163         retInputs = inps;
164
165         //set the image
166         GetInput image = new GetInput();
167         image.setBpInputName("tag_version");
168         this.setImage(image);
169         LinkedHashMap<String, Object> img = new LinkedHashMap<String, Object>();
170         img.put("type", "string");
171         img.put("default", cs.getArtifacts()[0].getUri());
172         retInputs.put("tag_version", img);
173
174         //set the location id
175         GetInput location = new GetInput();
176         location.setBpInputName("location_id");
177         this.setLocation_id(location);
178         LinkedHashMap<String, Object> locMap = new LinkedHashMap();
179         locMap.put("type", "string");
180         locMap.put("default", "");
181         retInputs.put("location_id", locMap);
182
183         //set the log info
184         this.setLog_info(cs.getAuxilary().getLog_info());
185
186         //set service component type
187         String sType = cs.getSelf().getName();
188         sType = sType.replace('.', '-');
189         this.setService_component_type(sType);
190
191         //set the tls info for internal and external communication
192         TreeMap<String, Object> tls_info = cs.getAuxilary().getTls_info();
193         if (tls_info != null) {
194             addTlsInfo(cs, retInputs);
195             if (tls_info.get(USE_EXTERNAL_TLS_FIELD) != null) {
196                 retInputs.putAll(addExternalTlsInfo(cs));
197             }
198         }
199
200         //set the replicas
201         GetInput replica = new GetInput();
202         replica.setBpInputName("replicas");
203         this.setReplicas(replica);
204         LinkedHashMap<String, Object> rep = createIntegerInput( "number of instances", 1);
205         retInputs.put("replicas", rep);
206
207 //              //set the dns name
208 //              this.setDns_name(cs.getSelf().getName());
209
210 //              //set the name
211 //              this.setName(cs.getSelf().getName());
212
213         //set the docker config
214         Auxilary aux = cs.getAuxilary();
215 //              if(aux.getPorts() != null) {
216 //                      retInputs = aux.createPorts(retInputs);
217 //              }
218         this.setDocker_config(aux);
219
220         //set the appconfig
221         Appconfig app = new Appconfig();
222         retInputs = app.createAppconfig(retInputs, cs, override, true);
223         this.setApplication_config(app);
224
225         //set the stream publishes
226         ArrayList<DmaapStreams> pubStreams = new ArrayList();
227         if (cs.getStreams().getPublishes() != null) {
228             for (Publishes publishes : cs.getStreams().getPublishes()) {
229                 if (isMessageRouterType(publishes.getType())) {
230                     String topic = publishes.getConfig_key() + "_topic";
231                     DmaapStreams mrStreams = new DmaapStreams();
232                     retInputs = mrStreams
233                         .createStreams(inps, cs, topic, publishes.getType(), publishes.getConfig_key(),
234                             publishes.getRoute(), 'p');
235                     pubStreams.add(mrStreams);
236                 } else if (isDataRouterType(publishes.getType())) {
237                     String feed = publishes.getConfig_key() + "_feed";
238                     DmaapStreams drStreams = new DmaapStreams();
239                     retInputs = drStreams
240                         .createStreams(inps, cs, feed, publishes.getType(), publishes.getConfig_key(),
241                             publishes.getRoute(), 'p');
242                     pubStreams.add(drStreams);
243                 }
244             }
245         }
246
247         //set the stream subscribes
248         ArrayList<DmaapStreams> subStreams = new ArrayList();
249         if (cs.getStreams().getSubscribes() != null) {
250             for (Subscribes subscribes : cs.getStreams().getSubscribes()) {
251                 if (isMessageRouterType(subscribes.getType())) {
252                     String topic = subscribes.getConfig_key() + "_topic";
253                     DmaapStreams mrStreams = new DmaapStreams();
254                     retInputs = mrStreams
255                         .createStreams(inps, cs, topic, subscribes.getType(), subscribes.getConfig_key(),
256                             subscribes.getRoute(), 's');
257                     subStreams.add(mrStreams);
258                 } else if (isDataRouterType(subscribes.getType())) {
259                     String feed = subscribes.getConfig_key() + "_feed";
260                     DmaapStreams drStreams = new DmaapStreams();
261                     retInputs = drStreams
262                         .createStreams(inps, cs, feed, subscribes.getType(), subscribes.getConfig_key(),
263                             subscribes.getRoute(), 's');
264                     subStreams.add(drStreams);
265                 }
266             }
267         }
268
269         if (pubStreams.size() != 0) {
270             this.setStreams_publishes(pubStreams);
271         }
272         if (subStreams.size() != 0) {
273             this.setStreams_subscribes(subStreams);
274         }
275
276         //set the reource config
277         ResourceConfig resource = new ResourceConfig();
278         retInputs = resource.createResourceConfig(retInputs, cs.getSelf().getName());
279         this.setResource_config(resource);
280
281         return retInputs;
282     }
283
284     private void addTlsInfo(ComponentSpec cs, TreeMap<String, LinkedHashMap<String, Object>> retInputs) {
285         TlsInfo tlsInfo = new TlsInfo();
286         tlsInfo.setCertDirectory((String) cs.getAuxilary().getTls_info().get("cert_directory"));
287         GetInput useTLSFlag = new GetInput();
288         useTLSFlag.setBpInputName("use_tls");
289         tlsInfo.setUseTls(useTLSFlag);
290         this.setTls_info(tlsInfo);
291         LinkedHashMap<String, Object> useTlsFlagInput = createBooleanInput(
292             "flag to indicate tls enable/disable",
293             cs.getAuxilary().getTls_info().get("use_tls"));
294         retInputs.put("use_tls", useTlsFlagInput);
295     }
296
297     private Map<String, LinkedHashMap<String, Object>> addExternalTlsInfo(ComponentSpec cs) {
298         this.setExternal_cert(externalCertFactory.createFromComponentSpec(cs));
299         return externalCertFactory.createInputListFromComponentSpec(cs);
300     }
301
302 }