8ec0024ea2ae68ed957859d48036eb013314023c
[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  *  *  ================================================================================
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  */
23
24 package org.onap.blueprintgenerator.service.common;
25
26 import org.onap.blueprintgenerator.model.common.GetInput;
27 import org.onap.blueprintgenerator.model.common.StartInputs;
28 import org.onap.blueprintgenerator.model.componentspec.OnapAuxilary;
29 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Service;
32
33 import java.util.LinkedHashMap;
34 import java.util.Map;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38
39 /**
40  * @author : Ravi Mantena
41  * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service to add Start Inputs
42  * Node under Start
43  */
44 @Service
45 public class StartInputsService {
46
47     @Autowired
48     private PgaasNodeService pgaasNodeService;
49
50     /**
51      * Creates Start Inputs for Start in Interfaces
52      *
53      * @param inputs Inputs
54      * @param onapComponentSpec OnapComponentSpec
55      * @return
56      */
57     public Map<String, Object> createStartInputs(
58         Map<String, LinkedHashMap<String, Object>> inputs, OnapComponentSpec onapComponentSpec) {
59
60         Map<String, Object> response = new HashMap<>();
61         StartInputs startInputs = new StartInputs();
62
63         int count = 0;
64         List<String> portList = new ArrayList();
65         OnapAuxilary aux = onapComponentSpec.getAuxilary();
66
67         if (aux.getPorts() != null) {
68             for (Object p : aux.getPorts()) {
69                 String[] ports = p.toString().split(":");
70                 portList.add(
71                     String.format("concat: [\"%s:\", {get_input: external_port_%d}]", ports[0],
72                         count));
73
74                 LinkedHashMap<String, Object> portType = new LinkedHashMap();
75                 portType.put("type", "string");
76                 portType.put("default", ports[1]);
77                 inputs.put("external_port_" + count, portType);
78                 count++;
79             }
80         }
81
82         startInputs.setPorts(portList);
83
84         LinkedHashMap<String, Object> envMap = new LinkedHashMap();
85         if (onapComponentSpec.getAuxilary().getDatabases() != null) {
86             Map<String, Object> envVars =
87                 pgaasNodeService.getEnvVariables(onapComponentSpec.getAuxilary().getDatabases());
88             startInputs.setEnvs(envVars);
89             envMap.put("default", "&envs {}");
90         } else {
91             GetInput env = new GetInput();
92             env.setBpInputName("envs");
93             startInputs.setEnvs(env);
94             envMap.put("default", "{}");
95         }
96         inputs.put("envs", envMap);
97
98         response.put("startInputs", startInputs);
99         response.put("inputs", inputs);
100         return response;
101     }
102 }