Refactor, fix code formatting and add unittests
[dcaegen2/platform.git] / mod / bpgenerator / src / main / java / org / onap / blueprintgenerator / models / dmaapbp / DmaapNode.java
1 /*============LICENSE_START=======================================================
2  org.onap.dcae
3  ================================================================================
4  Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
5  Copyright (c) 2020 Nokia. All rights reserved.
6  ================================================================================
7  Licensed under the Apache License, Version 2.0 (the "License");
8  you may not use this file except in compliance with the License.
9  You may obtain a copy of the License at
10
11       http://www.apache.org/licenses/LICENSE-2.0
12
13  Unless required by applicable law or agreed to in writing, software
14  distributed under the License is distributed on an "AS IS" BASIS,
15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  See the License for the specific language governing permissions and
17  limitations under the License.
18  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.blueprintgenerator.models.dmaapbp;
22
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.TreeMap;
26
27 import org.onap.blueprintgenerator.core.PgaasNodeBuilder;
28 import org.onap.blueprintgenerator.core.PolicyNodeBuilder;
29 import org.onap.blueprintgenerator.models.blueprint.GetInput;
30 import org.onap.blueprintgenerator.models.blueprint.Interfaces;
31 import org.onap.blueprintgenerator.models.blueprint.Node;
32 import org.onap.blueprintgenerator.models.blueprint.Properties;
33 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
34 import org.onap.blueprintgenerator.models.componentspec.Publishes;
35 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
36
37 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
38 import com.fasterxml.jackson.annotation.JsonInclude;
39 import com.fasterxml.jackson.annotation.JsonInclude.Include;
40
41 import lombok.EqualsAndHashCode;
42 import lombok.Getter;
43 import lombok.NoArgsConstructor;
44 import lombok.Setter;
45
46 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isDataRouterType;
47 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isMessageRouterType;
48
49 @JsonIgnoreProperties(ignoreUnknown = true)
50 @Getter
51 @Setter
52 @EqualsAndHashCode(callSuper = false)
53 @NoArgsConstructor
54 @JsonInclude(value = Include.NON_NULL)
55
56 public class DmaapNode extends Node {
57
58     public TreeMap<String, LinkedHashMap<String, Object>> createDmaapNode(ComponentSpec componentSpec,
59         TreeMap<String, LinkedHashMap<String, Object>> inps, String override) {
60         TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
61
62         //set the type
63         this.setType("dcae.nodes.ContainerizedServiceComponentUsingDmaap");
64
65         //create the interface
66         Interfaces inter = new Interfaces();
67         retInputs = inter.createInterface(retInputs, componentSpec);
68         TreeMap<String, Interfaces> interfaces = new TreeMap<>();
69         interfaces.put("cloudify.interfaces.lifecycle", inter);
70         this.setInterfaces(interfaces);
71
72         //create and set the relationships
73         ArrayList<LinkedHashMap<String, String>> relationships = new ArrayList<>();
74
75         //go through the streams publishes
76         if (componentSpec.getStreams().getPublishes() != null) {
77             for (Publishes publishes : componentSpec.getStreams().getPublishes()) {
78                 relationships.add(createTypeAndTargetPubRelations(publishes));
79             }
80         }
81         //go through the stream subscribes
82         if (componentSpec.getStreams().getSubscribes() != null) {
83             for (Subscribes subscribes : componentSpec.getStreams().getSubscribes()) {
84                 relationships.add(createTypeAndTargetSubRelations(subscribes));
85             }
86         }
87
88         //add relationship for policy if exist
89         if (componentSpec.getPolicyInfo() != null) {
90             ArrayList<LinkedHashMap<String, String>> policyRelationshipsList = PolicyNodeBuilder
91                 .getPolicyRelationships(componentSpec);
92             relationships.addAll(policyRelationshipsList);
93         }
94
95         //add relationships and env_variables for pgaas dbs if exist
96         if (componentSpec.getAuxilary().getDatabases() != null) {
97             ArrayList<LinkedHashMap<String, String>> pgaasRelationshipsList = PgaasNodeBuilder
98                 .getPgaasNodeRelationships(componentSpec);
99             relationships.addAll(pgaasRelationshipsList);
100         }
101
102         this.setRelationships(relationships);
103
104         //create and set the properties
105         Properties props = new Properties();
106         retInputs = props.createDmaapProperties(retInputs, componentSpec, override);
107         this.setProperties(props);
108
109         return retInputs;
110     }
111
112     public TreeMap<String, LinkedHashMap<String, Object>> createFeedNode(ComponentSpec cs,
113         TreeMap<String, LinkedHashMap<String, Object>> inps, String name) {
114         TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
115         LinkedHashMap<String, Object> stringType = new LinkedHashMap<>();
116         stringType.put("type", "string");
117
118         //set the type
119         this.setType("ccsdk.nodes.Feed");
120
121         //create and set the properties
122         Properties props = new Properties();
123         GetInput topicInput = new GetInput();
124         topicInput.setBpInputName(name + "_name");
125         props.setFeed_name(topicInput);
126         //props.setUseExisting(true);
127         retInputs.put(name + "_name", stringType);
128         this.setProperties(props);
129
130         return retInputs;
131     }
132
133     public TreeMap<String, LinkedHashMap<String, Object>> createTopicNode(ComponentSpec cs,
134         TreeMap<String, LinkedHashMap<String, Object>> inps, String name) {
135         TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
136         LinkedHashMap<String, Object> stringType = new LinkedHashMap<>();
137         stringType.put("type", "string");
138
139         //set the type
140         this.setType("ccsdk.nodes.Topic");
141
142         //create and set the properties
143         Properties props = new Properties();
144         GetInput topicInput = new GetInput();
145         topicInput.setBpInputName(name + "_name");
146         props.setTopic_name(topicInput);
147         //props.setUseExisting(true);
148         retInputs.put(name + "_name", stringType);
149         this.setProperties(props);
150
151         return retInputs;
152     }
153
154     private LinkedHashMap<String, String> createTypeAndTargetPubRelations(Publishes publishes) {
155         LinkedHashMap<String, String> pubRelations = new LinkedHashMap<>();
156         if (isMessageRouterType(publishes.getType())) {
157             pubRelations.put("type", "ccsdk.relationships.publish_events");
158             pubRelations.put("target", publishes.getConfig_key() + "_topic");
159         } else if (isDataRouterType(publishes.getType())) {
160             pubRelations.put("type", "ccsdk.relationships.publish_files");
161             pubRelations.put("target", publishes.getConfig_key() + "_feed");
162         }
163         return pubRelations;
164     }
165
166     private LinkedHashMap<String, String> createTypeAndTargetSubRelations(Subscribes subscribes) {
167         LinkedHashMap<String, String> subRelations = new LinkedHashMap<>();
168         if (isMessageRouterType(subscribes.getType())) {
169             subRelations.put("type", "ccsdk.relationships.subscribe_to_events");
170             subRelations.put("target", subscribes.getConfig_key() + "_topic");
171         } else if (isDataRouterType(subscribes.getType())) {
172             subRelations.put("type", "ccsdk.relationships.subscribe_to_files");
173             subRelations.put("target", subscribes.getConfig_key() + "_feed");
174         }
175         return subRelations;
176     }
177 }