3 * * ============LICENSE_START=======================================================
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
13 * * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
25 package org.onap.blueprintgenerator.service.common;
27 import org.onap.blueprintgenerator.constants.Constants;
28 import org.onap.blueprintgenerator.exception.DatabasesNotFoundException;
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;
40 import java.util.Arrays;
41 import java.util.LinkedHashMap;
43 import java.util.ArrayList;
44 import java.util.List;
47 * @author : Ravi Mantena
48 * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service to add Pgaas Node
51 public class PgaasNodeService {
54 private BlueprintHelperService blueprintHelperService;
57 * Creates Pgaas Nodes and Inputs for Databases
59 * @param onapComponentSpec OnapComponentSpec
60 * @param nodeTemplate Node template
61 * @param inputs Inputs
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);
77 private void addPgaasInputs(
78 Map.Entry<String, String> database, Map<String, Map<String, Object>> inputs) {
80 database.getKey() + Constants.NAME_POSTFIX,
81 blueprintHelperService.createStringInput("db name", ""));
83 database.getKey() + Constants.WRITER_FQDN_POSTFIX,
84 blueprintHelperService.createStringInput("db writerfqdn", ""));
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);
95 private PgaasNodeProperties buildPgaasNodeProperties(String dbName) {
96 PgaasNodeProperties pgaasNodeProperties = new PgaasNodeProperties();
98 GetInput nameValue = new GetInput();
99 nameValue.setBpInputName(dbName + Constants.NAME_POSTFIX);
100 pgaasNodeProperties.setName(nameValue);
102 GetInput writerfqdnValue = new GetInput();
103 writerfqdnValue.setBpInputName(dbName + Constants.WRITER_FQDN_POSTFIX);
104 pgaasNodeProperties.setWriterfqdn(writerfqdnValue);
106 pgaasNodeProperties.setUseExisting(Constants.USE_EXISTING);
108 return pgaasNodeProperties;
112 * Creates Pgaas Nodes Relationships for Databases
114 * @param onapComponentSpec OnapComponentSpec
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);
127 return relationships;
131 * Creates Env Variables for Databases
133 * @param databases Database
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");
142 GetInput nameValue = new GetInput();
143 nameValue.setBpInputName(name.toLowerCase() + Constants.NAME_POSTFIX);
144 envVariables.put(name + "_DB_NAME", nameValue);
146 GetAttribute adminHostValue = buildGetAttributeValue(name.toLowerCase(), "admin",
148 envVariables.put(name.toUpperCase() + "_DB_ADMIN_HOST", adminHostValue);
150 GetAttribute adminUserValue = buildGetAttributeValue(name.toLowerCase(), "admin",
152 envVariables.put(name.toUpperCase() + "_DB_ADMIN_USER", adminUserValue);
154 GetAttribute adminPasswordValue =
155 buildGetAttributeValue(name.toLowerCase(), "admin", "password");
156 envVariables.put(name.toUpperCase() + "_DB_ADMIN_PASS", adminPasswordValue);
161 private GetAttribute buildGetAttributeValue(String dbName, String owner, String type) {
162 GetAttribute attribute = new GetAttribute();
164 .setAttribute(Arrays.asList(dbName + Constants.PGAAS_NODE_NAME_POSTFIX, owner, type));