Fix sonar issues
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / dmaap / DmaapBlueprintCreatorService.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) 2020-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.dmaap;
26
27 import org.onap.blueprintgenerator.constants.Constants;
28 import org.onap.blueprintgenerator.exception.BlueprintException;
29 import org.onap.blueprintgenerator.model.common.Input;
30 import org.onap.blueprintgenerator.model.common.Node;
31 import org.onap.blueprintgenerator.model.common.OnapBlueprint;
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.onap.blueprintgenerator.service.base.BlueprintService;
37 import org.onap.blueprintgenerator.service.common.ImportsService;
38 import org.onap.blueprintgenerator.service.common.NodeService;
39 import org.onap.blueprintgenerator.service.common.PgaasNodeService;
40 import org.onap.blueprintgenerator.service.common.PolicyNodeService;
41 import org.onap.blueprintgenerator.service.common.QuotationService;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Service;
44 import org.springframework.util.StringUtils;
45
46 import java.util.Map;
47 import java.util.TreeMap;
48
49 /**
50  * @author : Ravi Mantena
51  * @date 10/16/2020 Application: ONAP - Blueprint Generator Service to create DMAAP Blueprint
52  */
53 @Service
54 public class DmaapBlueprintCreatorService {
55
56     @Autowired
57     protected ImportsService importsService;
58
59     @Autowired
60     private NodeService nodeService;
61
62     @Autowired
63     private PolicyNodeService policyNodeService;
64
65     @Autowired
66     private PgaasNodeService pgaasNodeService;
67
68     @Autowired
69     private QuotationService quotationService;
70
71     @Autowired
72     private BlueprintHelperService blueprintHelperService;
73
74     /**
75      * Creates Dmaap Blueprint
76      *
77      * @param onapComponentSpec OnapComponentSpec
78      * @param input Inputs
79      * @return
80      */
81     public OnapBlueprint createBlueprint(OnapComponentSpec onapComponentSpec, Input input) {
82         try {
83             OnapBlueprint blueprint = new OnapBlueprint();
84             blueprint.setTosca_definitions_version(Constants.TOSCA_DEF_VERSION);
85             blueprint.setDescription(onapComponentSpec.getSelf().getDescription());
86
87             Map<String, Map<String, Object>> inputs = new TreeMap<>();
88
89             if (!StringUtils.isEmpty(input.getImportPath())) {
90                 blueprint.setImports(importsService.createImportsFromFile(input.getImportPath()));
91             } else {
92                 blueprint.setImports(importsService.createImports(input.getBpType()));
93             }
94
95             Map<String, Node> nodeTemplate = new TreeMap();
96
97             Map<String, Object> dmaapNodeResponse =
98                 nodeService
99                     .createDmaapNode(onapComponentSpec, inputs, input.getServiceNameOverride());
100             inputs = (Map<String, Map<String, Object>>) dmaapNodeResponse.get("inputs");
101             nodeTemplate.put(
102                 onapComponentSpec.getSelf().getName(), (Node) dmaapNodeResponse.get("dmaapNode"));
103
104             if (onapComponentSpec.getStreams() != null) {
105                 if (onapComponentSpec.getStreams().getPublishes() != null) {
106                     for (Publishes publishes : onapComponentSpec.getStreams().getPublishes()) {
107                         if (blueprintHelperService.isMessageRouterType(publishes.getType())) {
108                             String topic = publishes.getConfig_key() + Constants.A_TOPIC;
109                             Map<String, Object> topicNodeResponse = nodeService
110                                 .createTopicNode(inputs, topic);
111                             inputs = (Map<String, Map<String, Object>>) topicNodeResponse
112                                 .get("inputs");
113                             nodeTemplate.put(topic, (Node) topicNodeResponse.get("topicNode"));
114                         } else if (blueprintHelperService.isDataRouterType(publishes.getType())) {
115                             String feed = publishes.getConfig_key() + Constants.A_FEED;
116                             Map<String, Object> feedNodeResponse = nodeService
117                                 .createFeedNode(inputs, feed);
118                             inputs = (Map<String, Map<String, Object>>) feedNodeResponse
119                                 .get("inputs");
120                             nodeTemplate.put(feed, (Node) feedNodeResponse.get("feedNode"));
121                         }
122                     }
123                 }
124                 if (onapComponentSpec.getStreams().getSubscribes() != null) {
125                     for (Subscribes s : onapComponentSpec.getStreams().getSubscribes()) {
126                         if (blueprintHelperService.isMessageRouterType(s.getType())) {
127                             String topic = s.getConfig_key() + Constants.A_TOPIC;
128                             Map<String, Object> topicNodeResponse = nodeService
129                                 .createTopicNode(inputs, topic);
130                             inputs = (Map<String, Map<String, Object>>) topicNodeResponse
131                                 .get("inputs");
132                             nodeTemplate.put(topic, (Node) topicNodeResponse.get("topicNode"));
133                         } else if (blueprintHelperService.isDataRouterType(s.getType())) {
134                             String feed = s.getConfig_key() + Constants.A_FEED;
135                             Map<String, Object> feedNodeResponse = nodeService
136                                 .createFeedNode(inputs, feed);
137                             inputs = (Map<String, Map<String, Object>>) feedNodeResponse
138                                 .get("inputs");
139                             nodeTemplate.put(feed, (Node) feedNodeResponse.get("feedNode"));
140                         }
141                     }
142                 }
143             }
144
145             if (onapComponentSpec.getPolicyInfo() != null) {
146                 policyNodeService.addPolicyNodesAndInputs(onapComponentSpec, nodeTemplate, inputs);
147             }
148
149             if (onapComponentSpec.getAuxilary() != null
150                 && onapComponentSpec.getAuxilary().getDatabases() != null) {
151                 pgaasNodeService.addPgaasNodesAndInputs(onapComponentSpec, nodeTemplate, inputs);
152             }
153
154             blueprint.setNode_templates(nodeTemplate);
155             blueprint.setInputs(inputs);
156             return quotationService.setQuotations(blueprint);
157         } catch (Exception ex) {
158             throw new BlueprintException(
159                 "Unable to create ONAP DMAAP Blueprint Object from given input parameters", ex);
160         }
161     }
162 }