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