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