Merge "Refactor, fix code formatting and add unittests"
[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 java.util.LinkedHashMap;
25 import java.util.TreeMap;
26
27 import org.onap.blueprintgenerator.models.blueprint.dmaap.DmaapObj;
28 import org.onap.blueprintgenerator.models.componentspec.CallsObj;
29 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
30 import org.onap.blueprintgenerator.models.componentspec.Parameters;
31 import org.onap.blueprintgenerator.models.componentspec.Publishes;
32 import org.onap.blueprintgenerator.models.componentspec.Subscribes;
33
34 import com.fasterxml.jackson.annotation.JsonAnyGetter;
35
36
37 import lombok.Getter;
38 import lombok.Setter;
39
40 @Getter
41 @Setter
42 public class Appconfig {
43         private CallsObj[] service_calls;
44         private TreeMap<String, DmaapObj> streams_publishes;
45         private TreeMap<String, DmaapObj> streams_subscribes;
46         private TreeMap<String, Object> params;
47
48         @JsonAnyGetter
49         public TreeMap<String, Object> getParams(){
50                 return params;
51         }
52
53         public TreeMap<String, LinkedHashMap<String, Object>> createAppconfig(TreeMap<String, LinkedHashMap<String, Object>> inps, ComponentSpec cs, String override,
54                                                                                                                                                   boolean isDmaap) {
55                 TreeMap<String, LinkedHashMap<String, Object>> retInputs = inps;
56
57                 //set service calls
58                 CallsObj[] call = new CallsObj[0];
59                 this.setService_calls(call);
60
61                 //set the stream publishes
62                 TreeMap<String, DmaapObj> streamPublishes = new TreeMap<String, DmaapObj>();
63                 if(cs.getStreams().getPublishes().length != 0) {
64                         for(Publishes p: cs.getStreams().getPublishes()) {
65                                 if(p.getType().equals("data_router") || p.getType().equals("data router")) {
66                                         //in this case the data router information gets added to the params so for now leave it alone
67                                         String config = p.getConfig_key();
68                                         DmaapObj pub = new DmaapObj();
69                                         String name = p.getConfig_key() +"_feed";
70                                         retInputs = pub.createOnapDmaapDRObj(retInputs, config, 'p', name, name, isDmaap);
71                                         pub.setType(p.getType());
72                                         streamPublishes.put(config, pub);
73                                 } else if(p.getType().equals("message_router") || p.getType().equals("message router")) {
74                                         String config = p.getConfig_key();
75                                         DmaapObj pub = new DmaapObj();
76                                         String name =  p.getConfig_key() + "_topic";
77                                         retInputs = pub.createOnapDmaapMRObj(retInputs, config, 'p', name, name, isDmaap);
78                                         pub.setType(p.getType());
79                                         streamPublishes.put(config, pub);
80                                 }
81                         }
82                 }
83
84                 //set the stream publishes
85                 TreeMap<String, DmaapObj> streamSubscribes = new TreeMap<>();
86
87                 if(cs.getStreams().getSubscribes().length != 0) {
88                         for(Subscribes s: cs.getStreams().getSubscribes()) {
89                                 if(s.getType().equals("data_router") || s.getType().equals("data router")) {
90                                         //in this case the data router information gets added to the params so for now leave it alone
91                                         String config = s.getConfig_key();
92                                         DmaapObj sub = new DmaapObj();
93                                         String name = s.getConfig_key() + "_feed";
94                                         retInputs = sub.createOnapDmaapDRObj(retInputs, config, 'p', name, name, isDmaap);
95                                         sub.setType(s.getType());
96                                         streamSubscribes.put(config, sub);
97                                 } else if(s.getType().equals("message_router") || s.getType().equals("message router")) {
98                                         String config = s.getConfig_key();
99                                         DmaapObj sub = new DmaapObj();
100                                         String name = s.getConfig_key() + "_topic";
101                                         retInputs = sub.createOnapDmaapMRObj(retInputs, config, 's', name, name, isDmaap);
102                                         sub.setType(s.getType());
103                                         streamSubscribes.put(config, sub);
104                                 }
105                         }
106                 }
107
108                 this.setStreams_publishes(streamPublishes);
109                 this.setStreams_subscribes(streamSubscribes);
110
111                 //set the parameters into the appconfig
112                 TreeMap<String, Object> parameters = new TreeMap<>();
113                 for(Parameters p: cs.getParameters()) {
114                         String pName = p.getName();
115                         if(p.isSourced_at_deployment()) {
116                                 GetInput paramInput = new GetInput();
117                                 paramInput.setBpInputName(pName);
118                                 parameters.put(pName, paramInput);
119
120                                 if(!p.getValue().equals("")) {
121                                         LinkedHashMap<String, Object> inputs = new LinkedHashMap<>();
122                                         inputs.put("type", "string");
123                                         inputs.put("default", p.getValue());
124                                         retInputs.put(pName, inputs);
125                                 } else {
126                                         LinkedHashMap<String, Object> inputs = new LinkedHashMap<>();
127                                         inputs.put("type", "string");
128                                         retInputs.put(pName, inputs);
129                                 }
130                         } else {
131                                 if("string".equals(p.getType())) {
132                                         String val  =(String) p.getValue();
133                                         val = '"' + val + '"';
134                                         parameters.put(pName, val);
135                                 }
136                                 else {
137                                         parameters.put(pName, p.getValue());
138                                 }
139                         }
140                 }
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                         retInputs.put("service_component_name_override", over);
149                 }
150                 this.setParams(parameters);
151                 return retInputs;
152         }
153
154
155 }