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