b8da0d734e77b829d682e013c925750a26ca5197
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / common / NodeService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
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
11  *  *
12  *  *       http://www.apache.org/licenses/LICENSE-2.0
13  *  *
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=========================================================
20  *
21  *
22  */
23
24 package org.onap.blueprintgenerator.service.common;
25
26 import org.onap.blueprintgenerator.constants.Constants;
27 import org.onap.blueprintgenerator.model.common.GetInput;
28 import org.onap.blueprintgenerator.model.common.Interfaces;
29 import org.onap.blueprintgenerator.model.common.Node;
30 import org.onap.blueprintgenerator.model.common.Properties;
31 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
32 import org.onap.blueprintgenerator.model.componentspec.common.Publishes;
33 import org.onap.blueprintgenerator.model.componentspec.common.Subscribes;
34 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37
38 import java.util.LinkedHashMap;
39 import java.util.Map;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.TreeMap;
43 import java.util.List;
44
45
46 /**
47  * @author : Ravi Mantena
48  * @date 10/16/2020
49  * Application: ONAP - Blueprint Generator
50  * Common ONAP Service used by ONAP and DMAAP Blueprint to add ONAP/DMAAP/Feed/Topic Nodes
51  */
52
53
54 @Service
55 public class NodeService {
56
57     @Autowired
58     private InterfacesService interfacesService;
59
60     @Autowired
61     private PolicyNodeService policyNodeService;
62
63     @Autowired
64     private PgaasNodeService pgaasNodeService;
65
66     @Autowired
67     private PropertiesService propertiesService;
68
69     @Autowired
70     private BlueprintHelperService blueprintHelperService;
71
72     // method to create Onap Node to include interface
73     public Map<String,Object> createOnapNode(Map<String, LinkedHashMap<String, Object>> inputs, OnapComponentSpec onapComponentSpec, String override) {
74
75         Map<String,Object> response = new HashMap<>();
76         Node onapNode = new Node();
77
78         Map<String, Object> onapResponse = interfacesService.createInterface(inputs, onapComponentSpec);
79         inputs = (Map<String, LinkedHashMap<String, Object>>) onapResponse.get("inputs");
80
81         Map<String, Interfaces> interfaces = new TreeMap<>();
82         interfaces.put(Constants.CLOUDIFY_INTERFACES_LEFECYCLE, (Interfaces) onapResponse.get("interfaces"));
83         onapNode.setInterfaces(interfaces);
84
85         onapNode.setType(Constants.DCAE_NODES_CONTAINERIZED_SERVICE_COMPONENT);
86
87         List<Map<String, String>> relationships = new ArrayList();
88
89         if(onapComponentSpec.getPolicyInfo() != null){
90             List<Map<String, String>> policyRelationshipsList = policyNodeService.getPolicyRelationships(onapComponentSpec);
91             relationships.addAll(policyRelationshipsList);
92         }
93
94         if(onapComponentSpec.getAuxilary().getDatabases() != null){
95             List<Map<String, String>> pgaasRelationshipsList = pgaasNodeService.getPgaasNodeRelationships(onapComponentSpec);
96             relationships.addAll(pgaasRelationshipsList);
97         }
98
99         onapNode.setRelationships(relationships);
100
101         Map<String, Object> propertiesResponse = propertiesService.createOnapProperties(inputs, onapComponentSpec, override);
102         inputs = (Map<String, LinkedHashMap<String, Object>>) propertiesResponse.get("inputs");
103         onapNode.setProperties((org.onap.blueprintgenerator.model.common.Properties)propertiesResponse.get("properties"));
104
105         response.put("onapNode", onapNode);
106         response.put("inputs", inputs);
107         return response;
108     }
109
110     // method to create Dmaap Node to include interface
111     public Map<String,Object> createDmaapNode(OnapComponentSpec onapComponentSpec, Map<String, LinkedHashMap<String, Object>> inputs, String override) {
112
113         Map<String,Object> response = new HashMap<>();
114         Node dmaapNode = new Node();
115
116         dmaapNode.setType(Constants.DCAE_NODES_CONTAINERIZED_SERVICE_COMPONENT_USING_DMAAP);
117
118         Map<String, Object> dmaapResponse = interfacesService.createInterface(inputs, onapComponentSpec);
119         inputs = (Map<String, LinkedHashMap<String, Object>>) dmaapResponse.get("inputs");
120
121         Map<String, Interfaces> interfaces = new TreeMap<>();
122         interfaces.put(Constants.CLOUDIFY_INTERFACES_LEFECYCLE, (Interfaces) dmaapResponse.get("interfaces"));
123         dmaapNode.setInterfaces(interfaces);
124
125         List<Map<String, String>> relationships = new ArrayList();
126
127         if(onapComponentSpec.getStreams().getPublishes() != null) {
128             for(Publishes publishes: onapComponentSpec.getStreams().getPublishes()) {
129                 Map<String, String> pubRelations = new LinkedHashMap();
130                 if(blueprintHelperService.isMessageRouterType(publishes.getType())){
131                     pubRelations.put("type", Constants.PUBLISH_EVENTS);
132                     pubRelations.put("target", publishes.getConfig_key() + Constants._TOPIC);
133                 } else if(blueprintHelperService.isDataRouterType(publishes.getType())){
134                     pubRelations.put("type", Constants.PUBLISH_FILES);
135                     pubRelations.put("target", publishes.getConfig_key() + Constants._FEED);
136                 }
137                 relationships.add(pubRelations);
138             }
139         }
140
141         if(onapComponentSpec.getStreams().getSubscribes() != null) {
142             for(Subscribes subscribes: onapComponentSpec.getStreams().getSubscribes()) {
143                Map<String, String> subRelations = new LinkedHashMap();
144                 if(blueprintHelperService.isMessageRouterType(subscribes.getType())){
145                     subRelations.put("type", Constants.SUBSCRIBE_TO_EVENTS);
146                     subRelations.put("target", subscribes.getConfig_key() + Constants._TOPIC);
147                 } else  if(blueprintHelperService.isDataRouterType(subscribes.getType())){
148                     subRelations.put("type", Constants.SUBSCRIBE_TO_FILES);
149                     subRelations.put("target", subscribes.getConfig_key() + Constants._FEED);
150                 }
151                 relationships.add(subRelations);
152             }
153         }
154
155         if(onapComponentSpec.getPolicyInfo() != null){
156             List<Map<String, String>> policyRelationshipsList = policyNodeService.getPolicyRelationships(onapComponentSpec);
157             relationships.addAll(policyRelationshipsList);
158         }
159
160         if(onapComponentSpec.getAuxilary().getDatabases() != null){
161             List<Map<String, String>> pgaasRelationshipsList = pgaasNodeService.getPgaasNodeRelationships(onapComponentSpec);
162             relationships.addAll(pgaasRelationshipsList);
163         }
164
165         dmaapNode.setRelationships(relationships);
166
167         Map<String, Object> propertiesResponse = propertiesService.createDmaapProperties(inputs, onapComponentSpec, override);
168         inputs = (Map<String, LinkedHashMap<String, Object>>) propertiesResponse.get("inputs");
169         dmaapNode.setProperties((org.onap.blueprintgenerator.model.common.Properties)propertiesResponse.get("properties"));
170
171         response.put("dmaapNode", dmaapNode);
172         response.put("inputs", inputs);
173         return response;
174     }
175
176     // method to create Feed Node for Streams
177     public Map<String,Object> createFeedNode(Map<String, LinkedHashMap<String, Object>> inputs, String name){
178         Map<String,Object> response = new HashMap<>();
179         Node feedNode = new Node();
180
181         LinkedHashMap<String, Object> stringType = new LinkedHashMap();
182         stringType.put("type", "string");
183
184         feedNode.setType(Constants.FEED);
185
186         org.onap.blueprintgenerator.model.common.Properties props = new org.onap.blueprintgenerator.model.common.Properties();
187         GetInput topicInput = new GetInput();
188         topicInput.setBpInputName(name + "_name");
189         props.setFeed_name(topicInput);
190         props.setUseExisting(true);
191         inputs.put(name + "_name", stringType);
192         feedNode.setProperties(props);
193
194         response.put("feedNode", feedNode);
195         response.put("inputs", inputs);
196         return response;
197     }
198
199     // method to create Topic Node for Streams
200     public Map<String,Object> createTopicNode(Map<String, LinkedHashMap<String, Object>> inputs, String name){
201         Map<String,Object> response = new HashMap<>();
202         Node topicNode = new Node();
203
204         LinkedHashMap<String, Object> stringType = new LinkedHashMap();
205         stringType.put("type", "string");
206
207         topicNode.setType(Constants.TOPIC);
208
209         org.onap.blueprintgenerator.model.common.Properties props = new Properties();
210         GetInput topicInput = new GetInput();
211         topicInput.setBpInputName(name + "_name");
212         props.setTopic_name(topicInput);
213         inputs.put(name + "_name", stringType);
214         topicNode.setProperties(props);
215
216         response.put("topicNode", topicNode);
217         response.put("inputs", inputs);
218         return response;
219     }
220
221
222 }