2774abbdaf91c83f18aee30f501bdb15545edb3a
[dcaegen2/platform.git] / mod / bpgenerator / src / main / java / org / onap / blueprintgenerator / models / dmaapbp / DmaapBlueprint.java
1 /*============LICENSE_START=======================================================
2  org.onap.dcae
3  ================================================================================
4  Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
5  ================================================================================
6  Modifications Copyright (c) 2020 Nokia. 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 package org.onap.blueprintgenerator.models.dmaapbp;
24
25 import java.util.LinkedHashMap;
26 import java.util.TreeMap;
27
28 import org.onap.blueprintgenerator.core.PgaasNodeBuilder;
29 import org.onap.blueprintgenerator.core.PolicyNodeBuilder;
30 import org.onap.blueprintgenerator.models.blueprint.Blueprint;
31 import org.onap.blueprintgenerator.models.blueprint.Node;
32 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
33 import org.onap.blueprintgenerator.models.componentspec.Publishes;
34 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
35
36 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.joinUnderscore;
37 import static org.onap.blueprintgenerator.models.blueprint.Imports.createDmaapImports;
38 import static org.onap.blueprintgenerator.models.blueprint.Imports.createImportsFromFile;
39
40 public class DmaapBlueprint extends Blueprint {
41
42     private static final String TOPIC = "topic";
43     private static final String FEED = "feed";
44
45     public Blueprint createDmaapBlueprint(ComponentSpec componentSpec, String importPath, String override) {
46         Blueprint blueprint = new Blueprint();
47
48         //set tosca definition
49         blueprint.setTosca_definitions_version("cloudify_dsl_1_3");
50
51         //set the description
52         blueprint.setDescription(componentSpec.getSelf().getDescription());
53
54         //create the inpus object that will be added to over the creation of the blueprint
55         TreeMap<String, LinkedHashMap<String, Object>> inps = new TreeMap<>();
56
57         //set the imports
58         if (importPath.equals("")) {
59             blueprint.setImports(createDmaapImports());
60         } else {
61             blueprint.setImports(createImportsFromFile(importPath));
62         }
63
64         //set and create the node templates
65         TreeMap<String, Node> nodeTemplate = new TreeMap();
66
67         //create and add the main dmaap node
68         DmaapNode dmaap = new DmaapNode();
69         inps = dmaap.createDmaapNode(componentSpec, inps, override);
70         nodeTemplate.put(componentSpec.getSelf().getName(), dmaap);
71
72         //create and add the topic/feed nodes
73
74         //go through the streams publishes
75         if (componentSpec.getStreams().getPublishes() != null) {
76             for (Publishes publisher : componentSpec.getStreams().getPublishes()) {
77                 if (isMessageRouter(publisher.getType())) {
78                     String topic = joinUnderscore(publisher.getConfig_key(), TOPIC);
79                     DmaapNode topicNode = new DmaapNode();
80                     inps = topicNode.createTopicNode(componentSpec, inps, topic);
81                     nodeTemplate.put(topic, topicNode);
82                 } else if (isDataRouter(publisher.getType())) {
83                     String feed = joinUnderscore(publisher.getConfig_key(), FEED);
84                     DmaapNode feedNode = new DmaapNode();
85                     inps = feedNode.createFeedNode(componentSpec, inps, feed);
86                     nodeTemplate.put(feed, feedNode);
87                 }
88             }
89         }
90         //go through the stream subscribes
91         if (componentSpec.getStreams().getSubscribes() != null) {
92             for (Subscribes subscriber : componentSpec.getStreams().getSubscribes()) {
93                 if (isMessageRouter(subscriber.getType())) {
94                     String topic = joinUnderscore(subscriber.getConfig_key(), TOPIC);
95                     DmaapNode topicNode = new DmaapNode();
96                     inps = topicNode.createTopicNode(componentSpec, inps, topic);
97                     nodeTemplate.put(topic, topicNode);
98                 } else if (isDataRouter(subscriber.getType())) {
99                     String feed = joinUnderscore(subscriber.getConfig_key(), FEED);
100                     DmaapNode feedNode = new DmaapNode();
101                     inps = feedNode.createFeedNode(componentSpec, inps, feed);
102                     nodeTemplate.put(feed, feedNode);
103                 }
104             }
105         }
106
107         //if present in component spec, populate policyNodes information in the blueprint
108         if (componentSpec.getPolicyInfo() != null) {
109             PolicyNodeBuilder.addPolicyNodesAndInputs(componentSpec, nodeTemplate, inps);
110         }
111
112         //if present in component spec, populate pgaasNodes information in the blueprint
113         if (componentSpec.getAuxilary().getDatabases() != null) {
114             PgaasNodeBuilder.addPgaasNodesAndInputs(componentSpec, nodeTemplate, inps);
115         }
116
117         blueprint.setNode_templates(nodeTemplate);
118
119         blueprint.setInputs(inps);
120         return blueprint;
121     }
122
123     private boolean isDataRouter(String type) {
124         return type.equals("data_router") || type.equals("data router");
125     }
126
127     private boolean isMessageRouter(String type) {
128         return type.equals("message_router") || type.equals("message router");
129     }
130 }