Refactor, fix code formatting and add unittests
[dcaegen2/platform.git] / mod / bpgenerator / src / main / java / org / onap / blueprintgenerator / models / blueprint / StartInputs.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
22 package org.onap.blueprintgenerator.models.blueprint;
23
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.TreeMap;
27
28 import org.onap.blueprintgenerator.core.PgaasNodeBuilder;
29 import org.onap.blueprintgenerator.models.componentspec.Auxilary;
30 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
31
32 import com.fasterxml.jackson.annotation.JsonInclude;
33 import com.fasterxml.jackson.annotation.JsonInclude.Include;
34
35 import lombok.Getter; import lombok.Setter;
36
37 @Getter @Setter
38 @JsonInclude(value=Include.NON_NULL)
39 public class StartInputs {
40         private ArrayList<String> ports;
41         private Object envs;
42
43         public TreeMap<String, LinkedHashMap<String, Object>> createOnapStartInputs(TreeMap<String, LinkedHashMap<String, Object>> inps, ComponentSpec cs){
44                 TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
45
46                 int count = 0;
47                 ArrayList<String> portList = new ArrayList();
48                 Auxilary aux = cs.getAuxilary();
49
50                 if (aux.getPorts() != null) {
51
52                         for(Object p : aux.getPorts()) {
53                                 String[] ports = p.toString().split(":");
54                                 String internal
55                                                 = String.format("concat: [\"%s:\", {get_input: external_port_%d}]"
56                                                 , ports[0], count);
57                                 portList.add(internal);
58
59                                 LinkedHashMap<String, Object> portType = new LinkedHashMap();
60                                 portType.put("type", "string");
61                                 portType.put("default", ports[1]);
62                                 retInputs.put("external_port_" + count, portType);
63
64                                 count++;
65                         }
66
67                 }
68
69                 this.setPorts(portList);
70 //              ArrayList<String> port = new ArrayList<String>();
71 //              String external = "";
72 //              if(cs.getAuxilary().getPorts() != null) {
73 //                      for(String s: cs.getAuxilary().getPorts()) {
74 //                              //create the ports
75 //                              String portNumber = "";
76 //                              StringBuffer buf = new StringBuffer();
77 //                              for(int i = 0; i < s.length(); i++) {
78 //                                      if(!(s.charAt(i) == ':')) {
79 //                                              buf.append(s.charAt(i));
80 //                                      }
81 //                                      else {
82 //                                              external = s.replace(buf.toString() + ":", "");
83 //                                              break;
84 //                                      }
85 //                              }
86 //                              portNumber = buf.toString();
87 //                              String p = "concat: [" + '"' + portNumber + ":" + '"' +", {get_input: external_port }]";
88 //                              port.add(p);
89 //                      }
90 //              }
91 //              this.setPorts(port);
92 //              //add the external port input
93 //              if(cs.getAuxilary().getPorts() != null) {
94 //                      stringType.put("type", "string");
95 //                      stringType.put("description", "Kubernetes node port on which collector is exposed");
96 //                      stringType.put("default", "'" + external + "'") ;
97 //                      retInputs.put("external_port", stringType);
98 //              }
99
100                 //set the envs
101                 LinkedHashMap<String, Object> eMap = new LinkedHashMap();
102                 if(cs.getAuxilary().getDatabases() != null){
103                         //set db env variables
104                         LinkedHashMap<String, Object> envVars = PgaasNodeBuilder.getEnvVariables(cs.getAuxilary().getDatabases());
105                         this.setEnvs(envVars);
106                         eMap.put("default", "&envs {}");
107                 }
108                 else {
109                         GetInput env = new GetInput();
110                         env.setBpInputName("envs");
111                         this.setEnvs(env);
112                         eMap.put("default", "{}");
113                 }
114                 retInputs.put("envs", eMap);
115
116
117                 return retInputs;
118         }
119 }