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