Merge "Blueprint Generator Refactored Code Issue-ID: DCAEGEN2-2472"
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / common / PgaasNodeService.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.constants.Constants;
27 import org.onap.blueprintgenerator.exception.DatabasesNotFoundException;
28
29
30 import org.onap.blueprintgenerator.model.common.Node;
31 import org.onap.blueprintgenerator.model.common.PgaasNode;
32 import org.onap.blueprintgenerator.model.common.GetInput;
33 import org.onap.blueprintgenerator.model.common.PgaasNodeProperties;
34 import org.onap.blueprintgenerator.model.common.GetAttribute;
35 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
36 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Service;
39
40 import java.util.Arrays;
41 import java.util.LinkedHashMap;
42 import java.util.Map;
43 import java.util.ArrayList;
44 import java.util.List;
45
46 /**
47  * @author : Ravi Mantena
48  * @date 10/16/2020
49  * Application: ONAP - Blueprint Generator
50  * Common ONAP Service used by ONAP and DMAAP Blueprint to add Pgaas Node
51  */
52
53
54 @Service
55 public class PgaasNodeService {
56
57     @Autowired
58     private BlueprintHelperService blueprintHelperService;
59
60     // method to create Pgaas Nodes and Inputs for Databases
61     public void addPgaasNodesAndInputs(OnapComponentSpec onapComponentSpec, Map<String, Node> nodeTemplate, Map<String, LinkedHashMap<String, Object>> inputs)  {
62         Map<String, String> databases = onapComponentSpec.getAuxilary().getDatabases();
63         for(Map.Entry<String, String> database : databases.entrySet()){
64             addPgaasNode(database, nodeTemplate);
65             addPgaasInputs(database, inputs);
66         }
67     }
68
69     private void addPgaasInputs(Map.Entry<String, String> database, Map<String, LinkedHashMap<String, Object>> inputs) {
70         inputs.put(database.getKey() + Constants.NAME_POSTFIX, blueprintHelperService.createStringInput( "db name", ""));
71         inputs.put(database.getKey() + Constants.WRITER_FQDN_POSTFIX, blueprintHelperService.createStringInput( "db writerfqdn", ""));
72     }
73
74     private void addPgaasNode(Map.Entry<String, String> database, Map<String, Node> nodeTemplate) {
75         PgaasNode pgaasNode = new PgaasNode();
76         String dbName = database.getKey();
77         pgaasNode.setType(Constants.PGAAS_NODE_TYPE);
78         pgaasNode.setPgaasNodeProperties(buildPgaasNodeProperties(dbName));
79         nodeTemplate.put(dbName + Constants.PGAAS_NODE_NAME_POSTFIX , pgaasNode);
80     }
81
82     private PgaasNodeProperties buildPgaasNodeProperties(String dbName) {
83         PgaasNodeProperties pgaasNodeProperties = new PgaasNodeProperties();
84
85         GetInput nameValue = new GetInput();
86         nameValue.setBpInputName(dbName + Constants.NAME_POSTFIX);
87         pgaasNodeProperties.setName(nameValue);
88
89         GetInput writerfqdnValue = new GetInput();
90         writerfqdnValue.setBpInputName(dbName + Constants.WRITER_FQDN_POSTFIX);
91         pgaasNodeProperties.setWriterfqdn(writerfqdnValue);
92
93         pgaasNodeProperties.setUseExisting(Constants.USE_EXISTING);
94
95         return pgaasNodeProperties;
96     }
97
98     // method to create Pgaas Node Relationships for Databases
99     public List<Map<String, String>> getPgaasNodeRelationships(OnapComponentSpec onapComponentSpec) {
100         List<Map<String, String>> relationships = new ArrayList<>();
101         for(Map.Entry<String, String> database : onapComponentSpec.getAuxilary().getDatabases().entrySet()){
102             Map<String, String> relationship = new LinkedHashMap<>();
103             relationship.put("type", Constants.DB_RELATIONSHIP_TYPE);
104             relationship.put("target", database.getKey() + Constants.PGAAS_NODE_NAME_POSTFIX);
105             relationships.add(relationship);
106         }
107         return relationships;
108     }
109
110     // method to create Env Variables for Databases
111     public Map<String, Object> getEnvVariables(Map<String, String> databases) {
112         Map<String, Object> envVariables = new LinkedHashMap<>();
113         for(Map.Entry<String, String> database : databases.entrySet()){
114             String name = database.getKey().toUpperCase();
115             envVariables.put("<<", "*envs");
116
117             GetInput nameValue = new GetInput();
118             nameValue.setBpInputName(name.toLowerCase() + Constants.NAME_POSTFIX);
119             envVariables.put(name + "_DB_NAME", nameValue);
120
121             GetAttribute adminHostValue = buildGetAttributeValue(name.toLowerCase(), "admin", "host");
122             envVariables.put( name.toUpperCase() + "_DB_ADMIN_HOST", adminHostValue);
123
124             GetAttribute adminUserValue = buildGetAttributeValue(name.toLowerCase(), "admin", "user");
125             envVariables.put( name.toUpperCase() + "_DB_ADMIN_USER", adminUserValue);
126
127             GetAttribute adminPasswordValue = buildGetAttributeValue(name.toLowerCase(), "admin", "password");
128             envVariables.put( name.toUpperCase() + "_DB_ADMIN_PASS", adminPasswordValue);
129         }
130         return envVariables;
131     }
132
133     private GetAttribute buildGetAttributeValue(String dbName, String owner, String type) {
134         GetAttribute attribute = new GetAttribute();
135         attribute.setAttribute(Arrays.asList(dbName + Constants.PGAAS_NODE_NAME_POSTFIX, owner, type));
136         return attribute;
137     }
138 }