3 * * ============LICENSE_START=======================================================
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
12 * * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.blueprintgenerator.service.common;
26 import org.onap.blueprintgenerator.constants.Constants;
27 import org.onap.blueprintgenerator.exception.DatabasesNotFoundException;
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;
39 import java.util.Arrays;
40 import java.util.LinkedHashMap;
42 import java.util.ArrayList;
43 import java.util.List;
46 * @author : Ravi Mantena
47 * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service to add Pgaas Node
50 public class PgaasNodeService {
53 private BlueprintHelperService blueprintHelperService;
56 * Creates Pgaas Nodes and Inputs for Databases
58 * @param onapComponentSpec OnapComponentSpec
59 * @param nodeTemplate Node template
60 * @param inputs Inputs
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);
76 private void addPgaasInputs(
77 Map.Entry<String, String> database, Map<String, LinkedHashMap<String, Object>> inputs) {
79 database.getKey() + Constants.NAME_POSTFIX,
80 blueprintHelperService.createStringInput("db name", ""));
82 database.getKey() + Constants.WRITER_FQDN_POSTFIX,
83 blueprintHelperService.createStringInput("db writerfqdn", ""));
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);
94 private PgaasNodeProperties buildPgaasNodeProperties(String dbName) {
95 PgaasNodeProperties pgaasNodeProperties = new PgaasNodeProperties();
97 GetInput nameValue = new GetInput();
98 nameValue.setBpInputName(dbName + Constants.NAME_POSTFIX);
99 pgaasNodeProperties.setName(nameValue);
101 GetInput writerfqdnValue = new GetInput();
102 writerfqdnValue.setBpInputName(dbName + Constants.WRITER_FQDN_POSTFIX);
103 pgaasNodeProperties.setWriterfqdn(writerfqdnValue);
105 pgaasNodeProperties.setUseExisting(Constants.USE_EXISTING);
107 return pgaasNodeProperties;
111 * Creates Pgaas Nodes Relationships for Databases
113 * @param onapComponentSpec OnapComponentSpec
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);
126 return relationships;
130 * Creates Env Variables for Databases
132 * @param databases Database
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");
141 GetInput nameValue = new GetInput();
142 nameValue.setBpInputName(name.toLowerCase() + Constants.NAME_POSTFIX);
143 envVariables.put(name + "_DB_NAME", nameValue);
145 GetAttribute adminHostValue = buildGetAttributeValue(name.toLowerCase(), "admin",
147 envVariables.put(name.toUpperCase() + "_DB_ADMIN_HOST", adminHostValue);
149 GetAttribute adminUserValue = buildGetAttributeValue(name.toLowerCase(), "admin",
151 envVariables.put(name.toUpperCase() + "_DB_ADMIN_USER", adminUserValue);
153 GetAttribute adminPasswordValue =
154 buildGetAttributeValue(name.toLowerCase(), "admin", "password");
155 envVariables.put(name.toUpperCase() + "_DB_ADMIN_PASS", adminPasswordValue);
160 private GetAttribute buildGetAttributeValue(String dbName, String owner, String type) {
161 GetAttribute attribute = new GetAttribute();
163 .setAttribute(Arrays.asList(dbName + Constants.PGAAS_NODE_NAME_POSTFIX, owner, type));