Merge "Blueprint Generator Refactored Code Issue-ID: DCAEGEN2-2472"
[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
42  * Application: ONAP - Blueprint Generator
43  * Common ONAP Service used by ONAP and DMAAP Blueprint to add Start Inputs Node under Start
44  */
45
46
47 @Service
48 public class StartInputsService {
49
50     @Autowired
51     private PgaasNodeService pgaasNodeService;
52
53     //Method to create Start Inputs for Start in Interfaces
54     public Map<String, Object> createStartInputs(Map<String, LinkedHashMap<String, Object>> inputs, OnapComponentSpec onapComponentSpec){
55
56         Map<String,Object> response = new HashMap<>();
57         StartInputs startInputs = new StartInputs();
58
59         int count = 0;
60         List<String> portList = new ArrayList();
61         OnapAuxilary aux = onapComponentSpec.getAuxilary();
62
63         if (aux.getPorts() != null) {
64             for(Object p : aux.getPorts()) {
65                 String[] ports = p.toString().split(":");
66                 portList.add(String.format("concat: [\"%s:\", {get_input: external_port_%d}]" , ports[0], count));
67
68                 LinkedHashMap<String, Object> portType = new LinkedHashMap();
69                 portType.put("type", "string");
70                 portType.put("default", ports[1]);
71                 inputs.put("external_port_" + count, portType);
72                 count++;
73             }
74         }
75
76         startInputs.setPorts(portList);
77
78         LinkedHashMap<String, Object> envMap = new LinkedHashMap();
79         if(onapComponentSpec.getAuxilary().getDatabases() != null){
80             Map<String, Object> envVars = pgaasNodeService.getEnvVariables(onapComponentSpec.getAuxilary().getDatabases());
81             startInputs.setEnvs(envVars);
82             envMap.put("default", "&envs {}");
83         }
84         else {
85             GetInput env = new GetInput();
86             env.setBpInputName("envs");
87             startInputs.setEnvs(env);
88             envMap.put("default", "{}");
89         }
90         inputs.put("envs", envMap);
91
92         response.put("startInputs", startInputs);
93         response.put("inputs", inputs);
94         return response;
95     }
96
97 }