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