Add external tls info to bpgenerator and component spec schema
[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  ================================================================================
6  Modifications Copyright (c) 2020 Nokia. All rights reserved.
7  ================================================================================
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.blueprintgenerator.models.blueprint;
23
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 import org.onap.blueprintgenerator.models.componentspec.Auxilary;
30 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
31 import org.onap.blueprintgenerator.models.componentspec.Publishes;
32 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
33 import org.onap.blueprintgenerator.models.dmaapbp.DmaapStreams;
34
35 import com.fasterxml.jackson.annotation.JsonInclude;
36 import com.fasterxml.jackson.annotation.JsonInclude.Include;
37
38 import lombok.Getter;
39 import lombok.Setter;
40
41 @Getter
42 @Setter
43 @JsonInclude(value = Include.NON_NULL)
44 public class Properties {
45         private Appconfig application_config;
46         private Auxilary docker_config;
47         private Object image;
48         private GetInput location_id;
49         private String service_component_type;
50         private TreeMap<String, Object> log_info;
51         private String dns_name;
52         private Object replicas;
53         private String name;
54         private GetInput topic_name;
55         private GetInput feed_name;
56         ArrayList<DmaapStreams> streams_publishes;
57         ArrayList<DmaapStreams> streams_subscribes;
58         private TlsInfo tls_info;
59         private ExternalTlsInfo external_cert;
60         private ResourceConfig resource_config;
61         private GetInput always_pull_image;
62         //private boolean useExisting;
63
64         public TreeMap<String, LinkedHashMap<String, Object>> createOnapProperties(TreeMap<String, LinkedHashMap<String, Object>> inps, ComponentSpec cs, String override) {
65                 TreeMap<String, LinkedHashMap<String, Object>> retInputs = new TreeMap<String, LinkedHashMap<String, Object>>();
66                 retInputs = inps;
67
68                 //set the image
69                 GetInput image = new GetInput();
70                 image.setGet_input("image");
71                 this.setImage(image);
72                 LinkedHashMap<String, Object> img = new LinkedHashMap<String, Object>();
73                 img.put("type", "string");
74                 img.put("default", cs.getArtifacts()[0].getUri());
75                 retInputs.put("image", img);
76
77                 //set the location id
78                 GetInput location = new GetInput();
79                 location.setGet_input("location_id");
80                 this.setLocation_id(location);
81                 LinkedHashMap<String, Object> locMap = new LinkedHashMap();
82                 locMap.put("type", "string");
83                 locMap.put("default", "");
84                 retInputs.put("location_id", locMap);
85
86                 //set the log info
87                 this.setLog_info(cs.getAuxilary().getLog_info());
88
89                 //set the replicas
90                 GetInput replica = new GetInput();
91                 replica.setGet_input("replicas");
92                 this.setReplicas(replica);
93                 LinkedHashMap<String, Object> rep = makeInput("integer", "number of instances", 1);
94                 retInputs.put("replicas", rep);
95
96                 //set the dns name
97                 //this.setDns_name(cs.getSelf().getName());
98
99                 //set the name
100                 //this.setName(cs.getSelf().getName());
101
102                 //set the docker config
103                 Auxilary aux = cs.getAuxilary();
104 //              if(aux.getPorts() != null) {
105 //                      retInputs = aux.createPorts(retInputs);
106 //              }
107                 this.setDocker_config(aux);
108
109                 //set the app config
110                 Appconfig app = new Appconfig();
111                 retInputs = app.createAppconfig(retInputs, cs, override, false);
112                 this.setApplication_config(app);
113
114                 // set always_pull_image
115                 this.always_pull_image = new GetInput();
116                 this.always_pull_image.setGet_input("always_pull_image");
117                 LinkedHashMap<String, Object> inputAlwaysPullImage = makeInput("boolean",
118                                 "Set to true if the image should always be pulled",
119                                 true);
120                 retInputs.put("always_pull_image", inputAlwaysPullImage);
121
122
123                 //set service component type
124                 String sType = cs.getSelf().getName();
125                 sType = sType.replace('.', '-');
126                 this.setService_component_type(sType);
127                 
128                 //set the tls info for internal and external communication
129                 if (cs.getAuxilary().getTls_info() != null) {
130                         addTlsInfo(cs, retInputs);
131                         addExternalTlsInfo(cs, retInputs);
132                 }
133
134                 //set the reource config
135                 ResourceConfig resource = new ResourceConfig();
136                 retInputs = resource.createResourceConfig(retInputs, cs.getSelf().getName());
137                 this.setResource_config(resource);
138
139                 return retInputs;
140         }
141
142         public TreeMap<String, LinkedHashMap<String, Object>> createDmaapProperties(TreeMap<String, LinkedHashMap<String, Object>> inps, ComponentSpec cs, String override) {
143                 TreeMap<String, LinkedHashMap<String, Object>> retInputs = new TreeMap<String, LinkedHashMap<String, Object>>();
144                 retInputs = inps;
145
146                 //set the image
147                 GetInput image = new GetInput();
148                 image.setGet_input("tag_version");
149                 this.setImage(image);
150                 LinkedHashMap<String, Object> img = new LinkedHashMap<String, Object>();
151                 img.put("type", "string");
152                 img.put("default", cs.getArtifacts()[0].getUri());
153                 retInputs.put("tag_version", img);
154
155                 
156                 //set the location id
157                 GetInput location = new GetInput();
158                 location.setGet_input("location_id");
159                 this.setLocation_id(location);
160                 LinkedHashMap<String, Object> locMap = new LinkedHashMap();
161                 locMap.put("type", "string");
162                 locMap.put("default", "");
163                 retInputs.put("location_id", locMap);
164                 
165                 //set the log info
166                 this.setLog_info(cs.getAuxilary().getLog_info());
167
168                 //set service component type
169                 String sType = cs.getSelf().getName();
170                 sType = sType.replace('.', '-');
171                 this.setService_component_type(sType);
172
173                 //set the tls info for internal and external communication
174                 if (cs.getAuxilary().getTls_info() != null) {
175                         addTlsInfo(cs, retInputs);
176                         addExternalTlsInfo(cs, retInputs);
177                 }
178
179                 //set the replicas
180                 GetInput replica = new GetInput();
181                 replica.setGet_input("replicas");
182                 this.setReplicas(replica);
183                 LinkedHashMap<String, Object> rep = makeInput("integer", "number of instances", 1);
184                 retInputs.put("replicas", rep);
185
186 //              //set the dns name
187 //              this.setDns_name(cs.getSelf().getName());
188
189 //              //set the name
190 //              this.setName(cs.getSelf().getName());
191
192                 //set the docker config
193                 Auxilary aux = cs.getAuxilary();
194 //              if(aux.getPorts() != null) {
195 //                      retInputs = aux.createPorts(retInputs);
196 //              }
197                 this.setDocker_config(aux);
198
199                 //set the appconfig
200                 Appconfig app = new Appconfig();
201                 retInputs = app.createAppconfig(retInputs, cs, override, true);
202                 this.setApplication_config(app);
203
204                 //set the stream publishes
205                 ArrayList<DmaapStreams> pubStreams = new ArrayList();
206                 if (cs.getStreams().getPublishes() != null) {
207                         for (Publishes p : cs.getStreams().getPublishes()) {
208                                 if (p.getType().equals("message_router") || p.getType().equals("message router")) {
209                                         String topic = p.getConfig_key() + "_topic";
210                                         DmaapStreams mrStreams = new DmaapStreams();
211                                         retInputs = mrStreams.createStreams(inps, cs, topic, p.getType(), p.getConfig_key(), p.getRoute(), 'p');
212                                         pubStreams.add(mrStreams);
213                                 } else if (p.getType().equals("data_router") || p.getType().equals("data router")) {
214                                         String feed = p.getConfig_key() + "_feed";
215                                         DmaapStreams drStreams = new DmaapStreams();
216                                         retInputs = drStreams.createStreams(inps, cs, feed, p.getType(), p.getConfig_key(), p.getRoute(), 'p');
217                                         pubStreams.add(drStreams);
218                                 }
219                         }
220                 }
221
222                 //set the stream subscribes
223                 ArrayList<DmaapStreams> subStreams = new ArrayList();
224                 if (cs.getStreams().getSubscribes() != null) {
225                         for (Subscribes s : cs.getStreams().getSubscribes()) {
226                                 if (s.getType().equals("message_router") || s.getType().equals("message router")) {
227                                         String topic = s.getConfig_key() + "_topic";
228                                         DmaapStreams mrStreams = new DmaapStreams();
229                                         retInputs = mrStreams.createStreams(inps, cs, topic, s.getType(), s.getConfig_key(), s.getRoute(), 's');
230                                         subStreams.add(mrStreams);
231                                 } else if (s.getType().equals("data_router") || s.getType().equals("data router")) {
232                                         String feed = s.getConfig_key() + "_feed";
233                                         DmaapStreams drStreams = new DmaapStreams();
234                                         retInputs = drStreams.createStreams(inps, cs, feed, s.getType(), s.getConfig_key(), s.getRoute(), 's');
235                                         subStreams.add(drStreams);
236                                 }
237                         }
238                 }
239
240                 if (pubStreams.size() != 0) {
241                         this.setStreams_publishes(pubStreams);
242                 }
243                 if (subStreams.size() != 0) {
244                         this.setStreams_subscribes(subStreams);
245                 }
246
247                 //set the reource config
248                 ResourceConfig resource = new ResourceConfig();
249                 retInputs = resource.createResourceConfig(retInputs, cs.getSelf().getName());
250                 this.setResource_config(resource);
251
252
253                 return retInputs;
254         }
255
256         private void addTlsInfo(ComponentSpec cs, TreeMap<String, LinkedHashMap<String, Object>> retInputs) {
257                 TlsInfo tlsInfo = new TlsInfo();
258                 tlsInfo.setCertDirectory((String) cs.getAuxilary().getTls_info().get("cert_directory"));
259                 GetInput useTLSFlag = new GetInput();
260                 useTLSFlag.setGet_input("use_tls");
261                 tlsInfo.setUseTls(useTLSFlag);
262                 this.setTls_info(tlsInfo);
263                 LinkedHashMap<String, Object> useTlsFlagInput = makeInput("boolean",
264                                 "flag to indicate tls enable/disable",
265                                 cs.getAuxilary().getTls_info().get("use_tls"));
266                 retInputs.put("use_tls", useTlsFlagInput);
267         }
268
269         private void addExternalTlsInfo(ComponentSpec cs, Map<String, LinkedHashMap<String, Object>> retInputs) {
270                 if(cs.getAuxilary().getTls_info().get(ExternalTlsInfo.USE_EXTERNAL_TLS_FIELD) == null)
271                         return;
272                 this.setExternal_cert(ExternalTlsInfo.createFromComponentSpec(cs));
273                 retInputs.putAll(ExternalTlsInfo.createInputMapFromComponentSpec(cs));
274         }
275
276         static LinkedHashMap<String, Object> makeInput(String type, String description, Object defaultValue) {
277                 LinkedHashMap<String, Object> inputMap = new LinkedHashMap<>();
278                 inputMap.put("type", type);
279                 inputMap.put("description", description);
280                 inputMap.put("default", defaultValue);
281                 return inputMap;
282         }
283 }