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;
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
49 * Application: ONAP - Blueprint Generator
50 * Common ONAP Service used by ONAP and DMAAP Blueprint to add Pgaas Node
55 public class PgaasNodeService {
58 private BlueprintHelperService blueprintHelperService;
60 // method to create Pgaas Nodes and Inputs for Databases
61 public void addPgaasNodesAndInputs(OnapComponentSpec onapComponentSpec, Map<String, Node> nodeTemplate, Map<String, LinkedHashMap<String, Object>> inputs) {
62 Map<String, String> databases = onapComponentSpec.getAuxilary().getDatabases();
63 for(Map.Entry<String, String> database : databases.entrySet()){
64 addPgaasNode(database, nodeTemplate);
65 addPgaasInputs(database, inputs);
69 private void addPgaasInputs(Map.Entry<String, String> database, Map<String, LinkedHashMap<String, Object>> inputs) {
70 inputs.put(database.getKey() + Constants.NAME_POSTFIX, blueprintHelperService.createStringInput( "db name", ""));
71 inputs.put(database.getKey() + Constants.WRITER_FQDN_POSTFIX, blueprintHelperService.createStringInput( "db writerfqdn", ""));
74 private void addPgaasNode(Map.Entry<String, String> database, Map<String, Node> nodeTemplate) {
75 PgaasNode pgaasNode = new PgaasNode();
76 String dbName = database.getKey();
77 pgaasNode.setType(Constants.PGAAS_NODE_TYPE);
78 pgaasNode.setPgaasNodeProperties(buildPgaasNodeProperties(dbName));
79 nodeTemplate.put(dbName + Constants.PGAAS_NODE_NAME_POSTFIX , pgaasNode);
82 private PgaasNodeProperties buildPgaasNodeProperties(String dbName) {
83 PgaasNodeProperties pgaasNodeProperties = new PgaasNodeProperties();
85 GetInput nameValue = new GetInput();
86 nameValue.setBpInputName(dbName + Constants.NAME_POSTFIX);
87 pgaasNodeProperties.setName(nameValue);
89 GetInput writerfqdnValue = new GetInput();
90 writerfqdnValue.setBpInputName(dbName + Constants.WRITER_FQDN_POSTFIX);
91 pgaasNodeProperties.setWriterfqdn(writerfqdnValue);
93 pgaasNodeProperties.setUseExisting(Constants.USE_EXISTING);
95 return pgaasNodeProperties;
98 // method to create Pgaas Node Relationships for Databases
99 public List<Map<String, String>> getPgaasNodeRelationships(OnapComponentSpec onapComponentSpec) {
100 List<Map<String, String>> relationships = new ArrayList<>();
101 for(Map.Entry<String, String> database : onapComponentSpec.getAuxilary().getDatabases().entrySet()){
102 Map<String, String> relationship = new LinkedHashMap<>();
103 relationship.put("type", Constants.DB_RELATIONSHIP_TYPE);
104 relationship.put("target", database.getKey() + Constants.PGAAS_NODE_NAME_POSTFIX);
105 relationships.add(relationship);
107 return relationships;
110 // method to create Env Variables for Databases
111 public Map<String, Object> getEnvVariables(Map<String, String> databases) {
112 Map<String, Object> envVariables = new LinkedHashMap<>();
113 for(Map.Entry<String, String> database : databases.entrySet()){
114 String name = database.getKey().toUpperCase();
115 envVariables.put("<<", "*envs");
117 GetInput nameValue = new GetInput();
118 nameValue.setBpInputName(name.toLowerCase() + Constants.NAME_POSTFIX);
119 envVariables.put(name + "_DB_NAME", nameValue);
121 GetAttribute adminHostValue = buildGetAttributeValue(name.toLowerCase(), "admin", "host");
122 envVariables.put( name.toUpperCase() + "_DB_ADMIN_HOST", adminHostValue);
124 GetAttribute adminUserValue = buildGetAttributeValue(name.toLowerCase(), "admin", "user");
125 envVariables.put( name.toUpperCase() + "_DB_ADMIN_USER", adminUserValue);
127 GetAttribute adminPasswordValue = buildGetAttributeValue(name.toLowerCase(), "admin", "password");
128 envVariables.put( name.toUpperCase() + "_DB_ADMIN_PASS", adminPasswordValue);
133 private GetAttribute buildGetAttributeValue(String dbName, String owner, String type) {
134 GetAttribute attribute = new GetAttribute();
135 attribute.setAttribute(Arrays.asList(dbName + Constants.PGAAS_NODE_NAME_POSTFIX, owner, type));