Adjust BP-gen to correctly support DFC component spec - types
[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 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.CONTENERIZED_SERVICE_COMPONENT_USING_DMAAP;
50 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.FEED;
51 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.TOPIC;
52 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.PUBLISH_EVENTS;
53 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.PUBLISH_FILES;
54 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.SUBSCRIBE_TO_EVENTS;
55 import static org.onap.blueprintgenerator.models.blueprint.BpConstants.SUBSCRIBE_TO_FILES;
56
57 @JsonIgnoreProperties(ignoreUnknown = true)
58 @Getter
59 @Setter
60 @EqualsAndHashCode(callSuper = false)
61 @NoArgsConstructor
62 @JsonInclude(value = Include.NON_NULL)
63
64 public class DmaapNode extends Node {
65
66     public TreeMap<String, LinkedHashMap<String, Object>> createDmaapNode(ComponentSpec componentSpec,
67         TreeMap<String, LinkedHashMap<String, Object>> inps, String override) {
68         TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
69
70         //set the type
71         this.setType(CONTENERIZED_SERVICE_COMPONENT_USING_DMAAP);
72
73         //create the interface
74         Interfaces inter = new Interfaces();
75         retInputs = inter.createInterface(retInputs, componentSpec);
76         TreeMap<String, Interfaces> interfaces = new TreeMap<>();
77         interfaces.put("cloudify.interfaces.lifecycle", inter);
78         this.setInterfaces(interfaces);
79
80         //create and set the relationships
81         ArrayList<LinkedHashMap<String, String>> relationships = new ArrayList<>();
82
83         //go through the streams publishes
84         if (componentSpec.getStreams().getPublishes() != null) {
85             for (Publishes publishes : componentSpec.getStreams().getPublishes()) {
86                 relationships.add(createTypeAndTargetPubRelations(publishes));
87             }
88         }
89         //go through the stream subscribes
90         if (componentSpec.getStreams().getSubscribes() != null) {
91             for (Subscribes subscribes : componentSpec.getStreams().getSubscribes()) {
92                 relationships.add(createTypeAndTargetSubRelations(subscribes));
93             }
94         }
95
96         //add relationship for policy if exist
97         if (componentSpec.getPolicyInfo() != null) {
98             ArrayList<LinkedHashMap<String, String>> policyRelationshipsList = PolicyNodeBuilder
99                 .getPolicyRelationships(componentSpec);
100             relationships.addAll(policyRelationshipsList);
101         }
102
103         //add relationships and env_variables for pgaas dbs if exist
104         if (componentSpec.getAuxilary().getDatabases() != null) {
105             ArrayList<LinkedHashMap<String, String>> pgaasRelationshipsList = PgaasNodeBuilder
106                 .getPgaasNodeRelationships(componentSpec);
107             relationships.addAll(pgaasRelationshipsList);
108         }
109
110         this.setRelationships(relationships);
111
112         //create and set the properties
113         Properties props = new Properties();
114         retInputs = props.createDmaapProperties(retInputs, componentSpec, override);
115         this.setProperties(props);
116
117         return retInputs;
118     }
119
120     public TreeMap<String, LinkedHashMap<String, Object>> createFeedNode(ComponentSpec cs,
121         TreeMap<String, LinkedHashMap<String, Object>> inps, String name) {
122         TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
123         LinkedHashMap<String, Object> stringType = new LinkedHashMap<>();
124         stringType.put("type", "string");
125
126         //set the type
127         this.setType(FEED);
128
129         //create and set the properties
130         Properties props = new Properties();
131         GetInput topicInput = new GetInput();
132         topicInput.setBpInputName(name + "_name");
133         props.setFeed_name(topicInput);
134         //props.setUseExisting(true);
135         retInputs.put(name + "_name", stringType);
136         this.setProperties(props);
137
138         return retInputs;
139     }
140
141     public TreeMap<String, LinkedHashMap<String, Object>> createTopicNode(ComponentSpec cs,
142         TreeMap<String, LinkedHashMap<String, Object>> inps, String name) {
143         TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
144         LinkedHashMap<String, Object> stringType = new LinkedHashMap<>();
145         stringType.put("type", "string");
146
147         //set the type
148         this.setType(TOPIC);
149
150         //create and set the properties
151         Properties props = new Properties();
152         GetInput topicInput = new GetInput();
153         topicInput.setBpInputName(name + "_name");
154         props.setTopic_name(topicInput);
155         //props.setUseExisting(true);
156         retInputs.put(name + "_name", stringType);
157         this.setProperties(props);
158
159         return retInputs;
160     }
161
162     private LinkedHashMap<String, String> createTypeAndTargetPubRelations(Publishes publishes) {
163         LinkedHashMap<String, String> pubRelations = new LinkedHashMap<>();
164         if (isMessageRouterType(publishes.getType())) {
165             pubRelations.put("type", PUBLISH_EVENTS);
166             pubRelations.put("target", publishes.getConfig_key() + "_topic");
167         } else if (isDataRouterType(publishes.getType())) {
168             pubRelations.put("type", PUBLISH_FILES);
169             pubRelations.put("target", publishes.getConfig_key() + "_feed");
170         }
171         return pubRelations;
172     }
173
174     private LinkedHashMap<String, String> createTypeAndTargetSubRelations(Subscribes subscribes) {
175         LinkedHashMap<String, String> subRelations = new LinkedHashMap<>();
176         if (isMessageRouterType(subscribes.getType())) {
177             subRelations.put("type", SUBSCRIBE_TO_EVENTS);
178             subRelations.put("target", subscribes.getConfig_key() + "_topic");
179         } else if (isDataRouterType(subscribes.getType())) {
180             subRelations.put("type", SUBSCRIBE_TO_FILES);
181             subRelations.put("target", subscribes.getConfig_key() + "_feed");
182         }
183         return subRelations;
184     }
185 }