ab1b7f20dc069289b6592dcc7fb1424db947a639
[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  * @author : Ravi Mantena
47  * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service to add
48  * ONAP/DMAAP/Feed/Topic Nodes
49  */
50 @Service
51 public class NodeService {
52
53     @Autowired
54     private InterfacesService interfacesService;
55
56     @Autowired
57     private PolicyNodeService policyNodeService;
58
59     @Autowired
60     private PgaasNodeService pgaasNodeService;
61
62     @Autowired
63     private PropertiesService propertiesService;
64
65     @Autowired
66     private BlueprintHelperService blueprintHelperService;
67
68     /**
69      * Creates Onap Node to include interface
70      *
71      * @param inputs Inputs
72      * @param onapComponentSpec OnapComponentSpec
73      * @param override Service Name Override
74      * @return
75      */
76     public Map<String, Object> createOnapNode(
77         Map<String, LinkedHashMap<String, Object>> inputs,
78         OnapComponentSpec onapComponentSpec,
79         String override) {
80
81         Map<String, Object> response = new HashMap<>();
82         Node onapNode = new Node();
83
84         Map<String, Object> onapResponse = interfacesService
85             .createInterface(inputs, onapComponentSpec);
86         inputs = (Map<String, LinkedHashMap<String, Object>>) onapResponse.get("inputs");
87
88         Map<String, Interfaces> interfaces = new TreeMap<>();
89         interfaces.put(
90             Constants.CLOUDIFY_INTERFACES_LEFECYCLE, (Interfaces) onapResponse.get("interfaces"));
91         onapNode.setInterfaces(interfaces);
92
93         onapNode.setType(Constants.DCAE_NODES_CONTAINERIZED_SERVICE_COMPONENT);
94
95         List<Map<String, String>> relationships = new ArrayList();
96
97         if (onapComponentSpec.getPolicyInfo() != null) {
98             List<Map<String, String>> policyRelationshipsList =
99                 policyNodeService.getPolicyRelationships(onapComponentSpec);
100             relationships.addAll(policyRelationshipsList);
101         }
102
103         if (onapComponentSpec.getAuxilary().getDatabases() != null) {
104             List<Map<String, String>> pgaasRelationshipsList =
105                 pgaasNodeService.getPgaasNodeRelationships(onapComponentSpec);
106             relationships.addAll(pgaasRelationshipsList);
107         }
108
109         onapNode.setRelationships(relationships);
110
111         Map<String, Object> propertiesResponse =
112             propertiesService.createOnapProperties(inputs, onapComponentSpec, override);
113         inputs = (Map<String, LinkedHashMap<String, Object>>) propertiesResponse.get("inputs");
114         onapNode.setProperties(
115             (org.onap.blueprintgenerator.model.common.Properties) propertiesResponse
116                 .get("properties"));
117
118         response.put("onapNode", onapNode);
119         response.put("inputs", inputs);
120         return response;
121     }
122
123     /**
124      * Creates Dmaap Node to include interface
125      *
126      * @param inputs Inputs
127      * @param onapComponentSpec OnapComponentSpec
128      * @param override Service Name Override
129      * @return
130      */
131     public Map<String, Object> createDmaapNode(
132         OnapComponentSpec onapComponentSpec,
133         Map<String, LinkedHashMap<String, Object>> inputs,
134         String override) {
135
136         Map<String, Object> response = new HashMap<>();
137         Node dmaapNode = new Node();
138
139         dmaapNode.setType(Constants.DCAE_NODES_CONTAINERIZED_SERVICE_COMPONENT_USING_DMAAP);
140
141         Map<String, Object> dmaapResponse =
142             interfacesService.createInterface(inputs, onapComponentSpec);
143         inputs = (Map<String, LinkedHashMap<String, Object>>) dmaapResponse.get("inputs");
144
145         Map<String, Interfaces> interfaces = new TreeMap<>();
146         interfaces.put(
147             Constants.CLOUDIFY_INTERFACES_LEFECYCLE, (Interfaces) dmaapResponse.get("interfaces"));
148         dmaapNode.setInterfaces(interfaces);
149
150         List<Map<String, String>> relationships = new ArrayList();
151
152         if (onapComponentSpec.getStreams().getPublishes() != null) {
153             for (Publishes publishes : onapComponentSpec.getStreams().getPublishes()) {
154                 Map<String, String> pubRelations = new LinkedHashMap();
155                 if (blueprintHelperService.isMessageRouterType(publishes.getType())) {
156                     pubRelations.put("type", Constants.PUBLISH_EVENTS);
157                     pubRelations.put("target", publishes.getConfig_key() + Constants._TOPIC);
158                 } else if (blueprintHelperService.isDataRouterType(publishes.getType())) {
159                     pubRelations.put("type", Constants.PUBLISH_FILES);
160                     pubRelations.put("target", publishes.getConfig_key() + Constants._FEED);
161                 }
162                 relationships.add(pubRelations);
163             }
164         }
165
166         if (onapComponentSpec.getStreams().getSubscribes() != null) {
167             for (Subscribes subscribes : onapComponentSpec.getStreams().getSubscribes()) {
168                 Map<String, String> subRelations = new LinkedHashMap();
169                 if (blueprintHelperService.isMessageRouterType(subscribes.getType())) {
170                     subRelations.put("type", Constants.SUBSCRIBE_TO_EVENTS);
171                     subRelations.put("target", subscribes.getConfig_key() + Constants._TOPIC);
172                 } else if (blueprintHelperService.isDataRouterType(subscribes.getType())) {
173                     subRelations.put("type", Constants.SUBSCRIBE_TO_FILES);
174                     subRelations.put("target", subscribes.getConfig_key() + Constants._FEED);
175                 }
176                 relationships.add(subRelations);
177             }
178         }
179
180         if (onapComponentSpec.getPolicyInfo() != null) {
181             List<Map<String, String>> policyRelationshipsList =
182                 policyNodeService.getPolicyRelationships(onapComponentSpec);
183             relationships.addAll(policyRelationshipsList);
184         }
185
186         if (onapComponentSpec.getAuxilary().getDatabases() != null) {
187             List<Map<String, String>> pgaasRelationshipsList =
188                 pgaasNodeService.getPgaasNodeRelationships(onapComponentSpec);
189             relationships.addAll(pgaasRelationshipsList);
190         }
191
192         dmaapNode.setRelationships(relationships);
193
194         Map<String, Object> propertiesResponse =
195             propertiesService.createDmaapProperties(inputs, onapComponentSpec, override);
196         inputs = (Map<String, LinkedHashMap<String, Object>>) propertiesResponse.get("inputs");
197         dmaapNode.setProperties(
198             (org.onap.blueprintgenerator.model.common.Properties) propertiesResponse
199                 .get("properties"));
200
201         response.put("dmaapNode", dmaapNode);
202         response.put("inputs", inputs);
203         return response;
204     }
205
206     /**
207      * Creates Feed Node for Streams
208      *
209      * @param inputs Inputs
210      * @param name Name
211      * @return
212      */
213     public Map<String, Object> createFeedNode(
214         Map<String, LinkedHashMap<String, Object>> inputs, String name) {
215         Map<String, Object> response = new HashMap<>();
216         Node feedNode = new Node();
217
218         LinkedHashMap<String, Object> stringType = new LinkedHashMap();
219         stringType.put("type", "string");
220
221         feedNode.setType(Constants.FEED);
222
223         org.onap.blueprintgenerator.model.common.Properties props =
224             new org.onap.blueprintgenerator.model.common.Properties();
225         GetInput topicInput = new GetInput();
226         topicInput.setBpInputName(name + "_name");
227         props.setFeed_name(topicInput);
228         props.setUseExisting(true);
229         inputs.put(name + "_name", stringType);
230         feedNode.setProperties(props);
231
232         response.put("feedNode", feedNode);
233         response.put("inputs", inputs);
234         return response;
235     }
236
237     /**
238      * Creates Topic Node for Streams
239      *
240      * @param inputs Inpts
241      * @param name Name
242      * @return
243      */
244     public Map<String, Object> createTopicNode(
245         Map<String, LinkedHashMap<String, Object>> inputs, String name) {
246         Map<String, Object> response = new HashMap<>();
247         Node topicNode = new Node();
248
249         LinkedHashMap<String, Object> stringType = new LinkedHashMap();
250         stringType.put("type", "string");
251
252         topicNode.setType(Constants.TOPIC);
253
254         org.onap.blueprintgenerator.model.common.Properties props = new Properties();
255         GetInput topicInput = new GetInput();
256         topicInput.setBpInputName(name + "_name");
257         props.setTopic_name(topicInput);
258         inputs.put(name + "_name", stringType);
259         topicNode.setProperties(props);
260
261         response.put("topicNode", topicNode);
262         response.put("inputs", inputs);
263         return response;
264     }
265 }