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