Fix sonar issues
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / common / StartInputsService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. All rights reserved.
7  *  *  Copyright (c) 2021 Nokia. All rights reserved.
8  *  *  ================================================================================
9  *  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  *  you may not use this file except in compliance with the License.
11  *  *  You may obtain a copy of the License at
12  *  *
13  *  *       http://www.apache.org/licenses/LICENSE-2.0
14  *  *
15  *  *  Unless required by applicable law or agreed to in writing, software
16  *  *  distributed under the License is distributed on an "AS IS" BASIS,
17  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  *  See the License for the specific language governing permissions and
19  *  *  limitations under the License.
20  *  *  ============LICENSE_END=========================================================
21  *
22  *
23  */
24
25 package org.onap.blueprintgenerator.service.common;
26
27 import org.onap.blueprintgenerator.model.common.GetInput;
28 import org.onap.blueprintgenerator.model.common.StartInputs;
29 import org.onap.blueprintgenerator.model.componentspec.OnapAuxilary;
30 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
33
34 import java.util.LinkedHashMap;
35 import java.util.Map;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39
40 /**
41  * @author : Ravi Mantena
42  * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service to add Start Inputs
43  * Node under Start
44  */
45 @Service
46 public class StartInputsService {
47
48     @Autowired
49     private PgaasNodeService pgaasNodeService;
50
51     /**
52      * Creates Start Inputs for Start in Interfaces
53      *
54      * @param inputs Inputs
55      * @param onapComponentSpec OnapComponentSpec
56      * @return
57      */
58     public Map<String, Object> createStartInputs(
59         Map<String, Map<String, Object>> inputs, OnapComponentSpec onapComponentSpec) {
60
61         Map<String, Object> response = new HashMap<>();
62         StartInputs startInputs = new StartInputs();
63
64         int count = 0;
65         List<String> portList = new ArrayList();
66         OnapAuxilary aux = onapComponentSpec.getAuxilary();
67
68         if (aux.getPorts() != null) {
69             for (Object p : aux.getPorts()) {
70                 String[] ports = p.toString().split(":");
71                 portList.add(
72                     String.format("concat: [\"%s:\", {get_input: external_port_%d}]", ports[0],
73                         count));
74
75                 Map<String, Object> portType = new LinkedHashMap();
76                 portType.put("type", "string");
77                 portType.put("default", ports[1]);
78                 inputs.put("external_port_" + count, portType);
79                 count++;
80             }
81         }
82
83         startInputs.setPorts(portList);
84
85         Map<String, Object> envMap = new LinkedHashMap();
86         if (onapComponentSpec.getAuxilary().getDatabases() != null) {
87             Map<String, Object> envVars =
88                 pgaasNodeService.getEnvVariables(onapComponentSpec.getAuxilary().getDatabases());
89             startInputs.setEnvs(envVars);
90             envMap.put("default", "&envs {}");
91         } else {
92             GetInput env = new GetInput();
93             env.setBpInputName("envs");
94             startInputs.setEnvs(env);
95             envMap.put("default", "{}");
96         }
97         inputs.put("envs", envMap);
98
99         response.put("startInputs", startInputs);
100         response.put("inputs", inputs);
101         return response;
102     }
103 }