Merge "Add factories for Ext tls parameters"
[dcaegen2/platform.git] / mod / bpgenerator / src / main / java / org / onap / blueprintgenerator / core / PgaasNodeBuilder.java
1 /*============LICENSE_START=======================================================
2  org.onap.dcae
3  ================================================================================
4  Copyright (c) 2019-2020 AT&T Intellectual Property. All rights reserved.
5  Copyright (c) 2020 Nokia. All rights reserved.
6  ================================================================================
7  Licensed under the Apache License, Version 2.0 (the "License");
8  you may not use this file except in compliance with the License.
9  You may obtain a copy of the License at
10
11  http://www.apache.org/licenses/LICENSE-2.0
12
13  Unless required by applicable law or agreed to in writing, software
14  distributed under the License is distributed on an "AS IS" BASIS,
15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  See the License for the specific language governing permissions and
17  limitations under the License.
18  ============LICENSE_END=========================================================
19
20  */
21 package org.onap.blueprintgenerator.core;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.TreeMap;
28 import org.onap.blueprintgenerator.exception.DatabasesNotFoundException;
29 import org.onap.blueprintgenerator.models.GetAttribute;
30 import org.onap.blueprintgenerator.models.blueprint.GetInput;
31 import org.onap.blueprintgenerator.models.blueprint.Node;
32 import org.onap.blueprintgenerator.models.blueprint.pgaas.PgaasNode;
33 import org.onap.blueprintgenerator.models.blueprint.pgaas.PgaasNodeProperties;
34 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
35
36 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createInputValue;
37
38 public class PgaasNodeBuilder {
39
40     private static final String PGAAS_NODE_TYPE = "dcae.nodes.pgaas.database";
41     private static final String PGAAS_NODE_NAME_POSTFIX = "_pgaasdb";
42     private static final String WRITER_FQDN_POSTFIX = "_database_writerfqdn";
43     private static final String NAME_POSTFIX = "_database_name";
44     private static final boolean USE_EXISTING = true;
45     private static final String DB_RELATIONSHIP_TYPE = "cloudify.relationships.depends_on";
46
47
48     public static void addPgaasNodesAndInputs(ComponentSpec cs, TreeMap<String, Node> nodeTemplate,
49         TreeMap<String, LinkedHashMap<String, Object>> inps) {
50         TreeMap<String, String> databases = cs.getAuxilary().getDatabases();
51         if (databases == null) {
52             throw new DatabasesNotFoundException("databases section not found in componentspec");
53         }
54         for (Map.Entry<String, String> database : databases.entrySet()) {
55             addPgaasNode(database, nodeTemplate);
56             addPgaasInputs(database, inps);
57         }
58     }
59
60     private static void addPgaasInputs(Map.Entry<String, String> database,
61         TreeMap<String, LinkedHashMap<String, Object>> inps) {
62         inps.put(database.getKey() + NAME_POSTFIX, createInputValue("string", "db name", ""));
63         inps.put(database.getKey() + WRITER_FQDN_POSTFIX, createInputValue("string", "db writerfqdn", ""));
64     }
65
66     private static void addPgaasNode(Map.Entry<String, String> database, TreeMap<String, Node> nodeTemplate) {
67         PgaasNode pgaasNode = new PgaasNode();
68         String dbName = database.getKey();
69         pgaasNode.setType(PGAAS_NODE_TYPE);
70         pgaasNode.setPgaasNodeProperties(buildPgaasNodeProperties(dbName));
71         nodeTemplate.put(dbName + PGAAS_NODE_NAME_POSTFIX, pgaasNode);
72     }
73
74     private static PgaasNodeProperties buildPgaasNodeProperties(String dbName) {
75         PgaasNodeProperties pgaasNodeProperties = new PgaasNodeProperties();
76
77         GetInput nameValue = new GetInput();
78         nameValue.setBpInputName(dbName + NAME_POSTFIX);
79         pgaasNodeProperties.setName(nameValue);
80
81         GetInput writerfqdnValue = new GetInput();
82         writerfqdnValue.setBpInputName(dbName + WRITER_FQDN_POSTFIX);
83         pgaasNodeProperties.setWriterfqdn(writerfqdnValue);
84
85         pgaasNodeProperties.setUseExisting(USE_EXISTING);
86
87         return pgaasNodeProperties;
88     }
89
90     public static ArrayList<LinkedHashMap<String, String>> getPgaasNodeRelationships(ComponentSpec cs) {
91         ArrayList<LinkedHashMap<String, String>> relationships = new ArrayList<>();
92         for (Map.Entry<String, String> database : cs.getAuxilary().getDatabases().entrySet()) {
93             LinkedHashMap<String, String> relationship = new LinkedHashMap<>();
94             relationship.put("type", DB_RELATIONSHIP_TYPE);
95             relationship.put("target", database.getKey() + PGAAS_NODE_NAME_POSTFIX);
96             relationships.add(relationship);
97         }
98         return relationships;
99     }
100
101     public static LinkedHashMap<String, Object> getEnvVariables(TreeMap<String, String> databases) {
102         LinkedHashMap<String, Object> envVariables = new LinkedHashMap<String, Object>();
103         for (Map.Entry<String, String> database : databases.entrySet()) {
104             String name = database.getKey().toUpperCase();
105
106             envVariables.put("<<", "*envs");
107
108             GetInput nameValue = new GetInput();
109             nameValue.setBpInputName(name.toLowerCase() + NAME_POSTFIX);
110             envVariables.put(name + "_DB_NAME", nameValue);
111
112             GetAttribute adminHostValue = buildGetAttributeValue(name.toLowerCase(), "admin", "host");
113             envVariables.put(name.toUpperCase() + "_DB_ADMIN_HOST", adminHostValue);
114
115             GetAttribute adminUserValue = buildGetAttributeValue(name.toLowerCase(), "admin", "user");
116             envVariables.put(name.toUpperCase() + "_DB_ADMIN_USER", adminUserValue);
117
118             GetAttribute adminPasswordValue = buildGetAttributeValue(name.toLowerCase(), "admin", "password");
119             envVariables.put(name.toUpperCase() + "_DB_ADMIN_PASS", adminPasswordValue);
120         }
121         return envVariables;
122     }
123
124     private static GetAttribute buildGetAttributeValue(String dbName, String owner, String type) {
125         GetAttribute attribute = new GetAttribute();
126         attribute.setAttribute(Arrays.asList(dbName + PGAAS_NODE_NAME_POSTFIX, owner, type));
127         return attribute;
128     }
129 }