e9157add6e58a830445c6f014d064ec95d667e6f
[dcaegen2/platform.git] / mod / bpgenerator / src / main / java / org / onap / blueprintgenerator / models / blueprint / Appconfig.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
22 package org.onap.blueprintgenerator.models.blueprint;
23
24 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isDataRouterType;
25 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.isMessageRouterType;
26 import static org.onap.blueprintgenerator.common.blueprint.BlueprintHelper.createStringInput;
27
28 import com.fasterxml.jackson.annotation.JsonAnyGetter;
29 import java.util.LinkedHashMap;
30 import java.util.TreeMap;
31 import lombok.Getter;
32 import lombok.Setter;
33 import org.onap.blueprintgenerator.models.blueprint.dmaap.DmaapObj;
34 import org.onap.blueprintgenerator.models.componentspec.CallsObj;
35 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
36 import org.onap.blueprintgenerator.models.componentspec.Parameters;
37 import org.onap.blueprintgenerator.models.componentspec.Publishes;
38 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
39
40 @Getter
41 @Setter
42 public class Appconfig {
43
44     private CallsObj[] service_calls;
45     private TreeMap<String, DmaapObj> streams_publishes;
46     private TreeMap<String, DmaapObj> streams_subscribes;
47     private TreeMap<String, Object> params;
48
49     @JsonAnyGetter
50     public TreeMap<String, Object> getParams() {
51         return params;
52     }
53
54     public TreeMap<String, LinkedHashMap<String, Object>> createAppconfig(
55         TreeMap<String, LinkedHashMap<String, Object>> inps, ComponentSpec componentSpec, String override,
56         boolean isDmaap) {
57
58         service_calls = new CallsObj[0];
59         streams_publishes = createStreamPublishes(componentSpec, inps, isDmaap);
60         streams_subscribes = createStreamSubscribes(componentSpec, inps, isDmaap);
61         params = createParameters(componentSpec, inps, override);
62
63         return inps;
64     }
65
66     private TreeMap<String, DmaapObj> createStreamPublishes(ComponentSpec componentSpec,
67         TreeMap<String, LinkedHashMap<String, Object>> inps, boolean isDmaap) {
68         TreeMap<String, DmaapObj> streamPublishes = new TreeMap<>();
69         for (Publishes publishes : componentSpec.getStreams().getPublishes()) {
70             String config = publishes.getConfig_key();
71             DmaapObj pub = new DmaapObj();
72             if (isDataRouterType(publishes.getType())) {
73                 //in this case the data router information gets added to the params so for now leave it alone
74                 String name = publishes.getConfig_key() + "_feed";
75                 pub.createOnapDmaapDRObj(inps, config, 'p', name, name, isDmaap);
76             } else if (isMessageRouterType(publishes.getType())) {
77                 String name = publishes.getConfig_key() + "_topic";
78                 pub.createOnapDmaapMRObj(inps, config, 'p', name, name, isDmaap);
79             }
80             pub.setType(publishes.getType());
81             streamPublishes.put(config, pub);
82         }
83         return streamPublishes;
84     }
85
86     private TreeMap<String, DmaapObj> createStreamSubscribes(ComponentSpec componentSpec,
87         TreeMap<String, LinkedHashMap<String, Object>> inputs, boolean isDmaap) {
88         TreeMap<String, DmaapObj> streamSubscribes = new TreeMap<>();
89         for (Subscribes subscribes : componentSpec.getStreams().getSubscribes()) {
90             String config = subscribes.getConfig_key();
91             DmaapObj sub = new DmaapObj();
92             if (isDataRouterType(subscribes.getType())) {
93                 //in this case the data router information gets added to the params so for now leave it alone
94                 String name = subscribes.getConfig_key() + "_feed";
95                 sub.createOnapDmaapDRObj(inputs, config, 'p', name, name, isDmaap);
96             } else if (isMessageRouterType(subscribes.getType())) {
97                 String name = subscribes.getConfig_key() + "_topic";
98                 sub.createOnapDmaapMRObj(inputs, config, 's', name, name, isDmaap);
99             }
100             sub.setType(subscribes.getType());
101             streamSubscribes.put(config, sub);
102         }
103         return streamSubscribes;
104     }
105
106     private TreeMap<String, Object> createParameters(ComponentSpec componentSpec,
107         TreeMap<String, LinkedHashMap<String, Object>> inputs,
108         String override) {
109         TreeMap<String, Object> parameters = new TreeMap<>();
110         for (Parameters params : componentSpec.getParameters()) {
111             String pName = params.getName();
112             if (params.isSourced_at_deployment()) {
113                 GetInput paramInput = new GetInput();
114                 paramInput.setBpInputName(pName);
115                 parameters.put(pName, paramInput);
116
117                 if (!params.getValue().equals("")) {
118                     LinkedHashMap<String, Object> input = createStringInput(params.getValue());
119                     inputs.put(pName, input);
120                 } else {
121                     LinkedHashMap<String, Object> input = new LinkedHashMap<>();
122                     input.put("type", "string");
123                     inputs.put(pName, input);
124                 }
125             } else {
126                 if ("string".equals(params.getType())) {
127                     String val = (String) params.getValue();
128                     val = '"' + val + '"';
129                     parameters.put(pName, val);
130                 } else {
131                     parameters.put(pName, params.getValue());
132                 }
133             }
134         }
135         handleOverride(override, parameters, inputs);
136         return parameters;
137     }
138
139     private void handleOverride(String override, TreeMap<String, Object> parameters,
140         TreeMap<String, LinkedHashMap<String, Object>> inps) {
141         if (override != null) {
142             GetInput ov = new GetInput();
143             ov.setBpInputName("service_component_name_override");
144             parameters.put("service_component_name_override", ov);
145             LinkedHashMap<String, Object> over = new LinkedHashMap<>();
146             over.put("type", "string");
147             over.put("default", override);
148             inps.put("service_component_name_override", over);
149         }
150     }
151
152 }