BpGen refactor Code Quality Issue-ID: DCAEGEN2-2502
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / dmaap / DmaapBlueprintService.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.dmaap;
25
26 import org.onap.blueprintgenerator.constants.Constants;
27 import org.onap.blueprintgenerator.exception.BlueprintException;
28 import org.onap.blueprintgenerator.model.common.Input;
29 import org.onap.blueprintgenerator.model.common.Node;
30 import org.onap.blueprintgenerator.model.common.OnapBlueprint;
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.onap.blueprintgenerator.service.base.BlueprintService;
36 import org.onap.blueprintgenerator.service.common.ImportsService;
37 import org.onap.blueprintgenerator.service.common.NodeService;
38 import org.onap.blueprintgenerator.service.common.PgaasNodeService;
39 import org.onap.blueprintgenerator.service.common.PolicyNodeService;
40 import org.onap.blueprintgenerator.service.common.QuotationService;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Service;
43 import org.springframework.util.StringUtils;
44
45 import java.util.LinkedHashMap;
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 DmaapBlueprintService extends BlueprintService {
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, LinkedHashMap<String, Object>> inputs = new TreeMap<>();
88
89             // if (!"".equals(input.getImportPath()))
90             if (!StringUtils.isEmpty(input.getImportPath())) {
91                 blueprint.setImports(importsService.createImportsFromFile(input.getImportPath()));
92             } else {
93                 blueprint.setImports(importsService.createImports(input.getBpType()));
94             }
95
96             Map<String, Node> nodeTemplate = new TreeMap();
97
98             Map<String, Object> dmaapNodeResponse =
99                 nodeService
100                     .createDmaapNode(onapComponentSpec, inputs, input.getServiceNameOverride());
101             inputs = (Map<String, LinkedHashMap<String, Object>>) dmaapNodeResponse.get("inputs");
102             nodeTemplate.put(
103                 onapComponentSpec.getSelf().getName(), (Node) dmaapNodeResponse.get("dmaapNode"));
104
105             if (onapComponentSpec.getStreams() != null) {
106                 if (onapComponentSpec.getStreams().getPublishes() != null) {
107                     for (Publishes publishes : onapComponentSpec.getStreams().getPublishes()) {
108                         if (blueprintHelperService.isMessageRouterType(publishes.getType())) {
109                             String topic = publishes.getConfig_key() + Constants._TOPIC;
110                             Map<String, Object> topicNodeResponse = nodeService
111                                 .createTopicNode(inputs, topic);
112                             inputs = (Map<String, LinkedHashMap<String, Object>>) topicNodeResponse
113                                 .get("inputs");
114                             nodeTemplate.put(topic, (Node) topicNodeResponse.get("topicNode"));
115                         } else if (blueprintHelperService.isDataRouterType(publishes.getType())) {
116                             String feed = publishes.getConfig_key() + Constants._FEED;
117                             Map<String, Object> feedNodeResponse = nodeService
118                                 .createFeedNode(inputs, feed);
119                             inputs = (Map<String, LinkedHashMap<String, Object>>) feedNodeResponse
120                                 .get("inputs");
121                             nodeTemplate.put(feed, (Node) feedNodeResponse.get("feedNode"));
122                         }
123                     }
124                 }
125                 if (onapComponentSpec.getStreams().getSubscribes() != null) {
126                     for (Subscribes s : onapComponentSpec.getStreams().getSubscribes()) {
127                         if (blueprintHelperService.isMessageRouterType(s.getType())) {
128                             String topic = s.getConfig_key() + Constants._TOPIC;
129                             Map<String, Object> topicNodeResponse = nodeService
130                                 .createTopicNode(inputs, topic);
131                             inputs = (Map<String, LinkedHashMap<String, Object>>) topicNodeResponse
132                                 .get("inputs");
133                             nodeTemplate.put(topic, (Node) topicNodeResponse.get("topicNode"));
134                         } else if (blueprintHelperService.isDataRouterType(s.getType())) {
135                             String feed = s.getConfig_key() + Constants._FEED;
136                             Map<String, Object> feedNodeResponse = nodeService
137                                 .createFeedNode(inputs, feed);
138                             inputs = (Map<String, LinkedHashMap<String, Object>>) feedNodeResponse
139                                 .get("inputs");
140                             nodeTemplate.put(feed, (Node) feedNodeResponse.get("feedNode"));
141                         }
142                     }
143                 }
144             }
145
146             if (onapComponentSpec.getPolicyInfo() != null) {
147                 policyNodeService.addPolicyNodesAndInputs(onapComponentSpec, nodeTemplate, inputs);
148             }
149
150             if (onapComponentSpec.getAuxilary() != null
151                 && onapComponentSpec.getAuxilary().getDatabases() != null) {
152                 pgaasNodeService.addPgaasNodesAndInputs(onapComponentSpec, nodeTemplate, inputs);
153             }
154
155             blueprint.setNode_templates(nodeTemplate);
156             blueprint.setInputs(inputs);
157             return quotationService.setQuotations(blueprint);
158         } catch (Exception ex) {
159             throw new BlueprintException(
160                 "Unable to create ONAP DMAAP Blueprint Object from given input parameters", ex);
161         }
162     }
163 }