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