Refactor, fix code formatting and add unittests
[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.isDataRouterType;
37 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isMessageRouterType;
38 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.joinUnderscore;
39 import static org.onap.blueprintgenerator.models.blueprint.Imports.createDmaapImports;
40 import static org.onap.blueprintgenerator.models.blueprint.Imports.createImportsFromFile;
41
42 public class DmaapBlueprint extends Blueprint {
43
44     private static final String TOPIC = "topic";
45     private static final String FEED = "feed";
46
47     public Blueprint createDmaapBlueprint(ComponentSpec componentSpec, String importPath, String override) {
48         Blueprint blueprint = new Blueprint();
49
50         //set tosca definition
51         blueprint.setTosca_definitions_version("cloudify_dsl_1_3");
52
53         //set the description
54         blueprint.setDescription(componentSpec.getSelf().getDescription());
55
56         //create the inpus object that will be added to over the creation of the blueprint
57         TreeMap<String, LinkedHashMap<String, Object>> inps = new TreeMap<>();
58
59         //set the imports
60         if (importPath.equals("")) {
61             blueprint.setImports(createDmaapImports());
62         } else {
63             blueprint.setImports(createImportsFromFile(importPath));
64         }
65
66         //set and create the node templates
67         TreeMap<String, Node> nodeTemplate = new TreeMap();
68
69         //create and add the main dmaap node
70         DmaapNode dmaap = new DmaapNode();
71         inps = dmaap.createDmaapNode(componentSpec, inps, override);
72         nodeTemplate.put(componentSpec.getSelf().getName(), dmaap);
73
74         //create and add the topic/feed nodes
75
76         //go through the streams publishes
77         if (componentSpec.getStreams().getPublishes() != null) {
78             for (Publishes publisher : componentSpec.getStreams().getPublishes()) {
79                 if (isMessageRouterType(publisher.getType())) {
80                     String topic = joinUnderscore(publisher.getConfig_key(), TOPIC);
81                     DmaapNode topicNode = new DmaapNode();
82                     inps = topicNode.createTopicNode(componentSpec, inps, topic);
83                     nodeTemplate.put(topic, topicNode);
84                 } else if (isDataRouterType(publisher.getType())) {
85                     String feed = joinUnderscore(publisher.getConfig_key(), FEED);
86                     DmaapNode feedNode = new DmaapNode();
87                     inps = feedNode.createFeedNode(componentSpec, inps, feed);
88                     nodeTemplate.put(feed, feedNode);
89                 }
90             }
91         }
92         //go through the stream subscribes
93         if (componentSpec.getStreams().getSubscribes() != null) {
94             for (Subscribes subscriber : componentSpec.getStreams().getSubscribes()) {
95                 if (isMessageRouterType(subscriber.getType())) {
96                     String topic = joinUnderscore(subscriber.getConfig_key(), TOPIC);
97                     DmaapNode topicNode = new DmaapNode();
98                     inps = topicNode.createTopicNode(componentSpec, inps, topic);
99                     nodeTemplate.put(topic, topicNode);
100                 } else if (isDataRouterType(subscriber.getType())) {
101                     String feed = joinUnderscore(subscriber.getConfig_key(), FEED);
102                     DmaapNode feedNode = new DmaapNode();
103                     inps = feedNode.createFeedNode(componentSpec, inps, feed);
104                     nodeTemplate.put(feed, feedNode);
105                 }
106             }
107         }
108
109         //if present in component spec, populate policyNodes information in the blueprint
110         if (componentSpec.getPolicyInfo() != null) {
111             PolicyNodeBuilder.addPolicyNodesAndInputs(componentSpec, nodeTemplate, inps);
112         }
113
114         //if present in component spec, populate pgaasNodes information in the blueprint
115         if (componentSpec.getAuxilary().getDatabases() != null) {
116             PgaasNodeBuilder.addPgaasNodesAndInputs(componentSpec, nodeTemplate, inps);
117         }
118
119         blueprint.setNode_templates(nodeTemplate);
120
121         blueprint.setInputs(inps);
122         return blueprint;
123     }
124
125 }