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