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