Blueprint Generator Refactored Code Issue-ID: DCAEGEN2-2472
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / common / AppConfigService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. 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
24 package org.onap.blueprintgenerator.service.common;
25
26 import org.onap.blueprintgenerator.constants.Constants;
27 import org.onap.blueprintgenerator.model.common.Appconfig;
28 import org.onap.blueprintgenerator.model.common.Dmaap;
29 import org.onap.blueprintgenerator.model.common.GetInput;
30 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
31 import org.onap.blueprintgenerator.model.componentspec.common.Calls;
32 import org.onap.blueprintgenerator.model.componentspec.common.Parameters;
33 import org.onap.blueprintgenerator.model.componentspec.common.Publishes;
34 import org.onap.blueprintgenerator.model.componentspec.common.Subscribes;
35 import org.onap.blueprintgenerator.service.base.BlueprintHelperService;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Service;
38
39 import java.util.HashMap;
40 import java.util.LinkedHashMap;
41 import java.util.Map;
42 import java.util.TreeMap;
43
44 /**
45  * @author : Ravi Mantena
46  * @date 10/16/2020
47  * Application: ONAP - Blueprint Generator
48  * Common ONAP Service used by ONAP and DMAAP Blueprint to create App Config
49  */
50
51
52 @Service("onapAppConfigService")
53 public class AppConfigService {
54
55     @Autowired
56     private DmaapService dmaapService;
57
58     @Autowired
59     private BlueprintHelperService blueprintHelperService;
60
61     public Map<String,Object> createAppconfig(Map<String, LinkedHashMap<String, Object>> inputs, OnapComponentSpec onapComponentSpec, String override, boolean isDmaap) {
62
63         Map<String,Object> response = new HashMap<>();
64         Appconfig appconfig = new Appconfig();
65
66         Calls[] call = new Calls[0];
67         appconfig.setService_calls(call);
68
69         Map<String, Dmaap> streamPublishes = new TreeMap<>();
70         if(onapComponentSpec.getStreams() != null) {
71             if (onapComponentSpec.getStreams().getPublishes() != null) {
72                 for (Publishes publishes : onapComponentSpec.getStreams().getPublishes()) {
73                     if (blueprintHelperService.isDataRouterType(publishes.getType())) {
74                         String config = publishes.getConfig_key();
75                         String name = config + Constants._FEED;
76                         Map<String, Object> dmaapDataRouterResponse = dmaapService.createDmaapDataRouter(inputs, config, name, isDmaap);
77                         inputs = (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse.get("inputs");
78                         Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
79                         dmaap.setType(publishes.getType());
80                         streamPublishes.put(config, dmaap);
81                     } else if (blueprintHelperService.isMessageRouterType(publishes.getType())) {
82                         String config = publishes.getConfig_key();
83                         String name = config + Constants._TOPIC;
84                         Map<String, Object> dmaapDataRouterResponse = dmaapService.createDmaapMessageRouter(inputs, config, 'p', name, name, isDmaap);
85                         inputs = (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse.get("inputs");
86                         Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
87                         dmaap.setType(publishes.getType());
88                         streamPublishes.put(config, dmaap);
89                     }
90                 }
91             }
92         }
93
94         Map<String, Dmaap> streamSubscribes = new TreeMap<>();
95
96         if(onapComponentSpec.getStreams() != null) {
97             if (onapComponentSpec.getStreams().getSubscribes() != null) {
98                 for (Subscribes subscribes : onapComponentSpec.getStreams().getSubscribes()) {
99                     if (blueprintHelperService.isDataRouterType(subscribes.getType())) {
100                         String config = subscribes.getConfig_key();
101                         String name = config + Constants._FEED;
102                         Map<String, Object> dmaapDataRouterResponse = dmaapService.createDmaapDataRouter(inputs, config, name, isDmaap);
103                         inputs = (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse.get("inputs");
104                         Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
105                         dmaap.setType(subscribes.getType());
106                         streamSubscribes.put(config, dmaap);
107                     } else if (blueprintHelperService.isMessageRouterType(subscribes.getType())) {
108                         String config = subscribes.getConfig_key();
109                         String name = config + Constants._TOPIC;
110                         Map<String, Object> dmaapDataRouterResponse = dmaapService.createDmaapMessageRouter(inputs, config, 's', name, name, isDmaap);
111                         inputs = (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse.get("inputs");
112                         Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
113                         dmaap.setType(subscribes.getType());
114                         streamSubscribes.put(config, dmaap);
115                     }
116                 }
117             }
118         }
119
120         appconfig.setStreams_publishes(streamPublishes);
121         appconfig.setStreams_subscribes(streamSubscribes);
122
123         Map<String, Object> parameters = new TreeMap<>();
124         for(Parameters p: onapComponentSpec.getParameters()) {
125             String pName = p.getName();
126             if(p.isSourced_at_deployment()) {
127                 GetInput paramInput = new GetInput();
128                 paramInput.setBpInputName(pName);
129                 parameters.put(pName, paramInput);
130                 if(!"".equals(p.getValue())) {
131                     LinkedHashMap<String, Object> pInputs =  blueprintHelperService.createStringInput( p.getValue());
132                     inputs.put(pName, pInputs);
133                 } else {
134                     LinkedHashMap<String, Object> pInputs = new LinkedHashMap<>();
135                     pInputs.put("type", "string");
136                     inputs.put(pName, pInputs);
137                 }
138             } else {
139                 if("string".equals(p.getType())) {
140                     String val  =(String) p.getValue();
141                     val = '"' + val + '"';
142                     parameters.put(pName, val);
143                 }
144                 else {
145                     parameters.put(pName, p.getValue());
146                     // Updated code to resolve the issue of missing \ for collector.schema.file
147                     //parameters.put(pName, pName.equals("collector.schema.file") ? ((String)p.getValue()).replace("\"", "\\\"") : p.getValue());
148                 }
149             }
150         }
151         if(override != null) {
152             GetInput ov = new GetInput();
153             ov.setBpInputName(Constants.SERVICE_COMPONENT_NAME_OVERRIDE);
154             parameters.put(Constants.SERVICE_COMPONENT_NAME_OVERRIDE, ov);
155             LinkedHashMap<String, Object> over =  blueprintHelperService.createStringInput( override);
156             inputs.put(Constants.SERVICE_COMPONENT_NAME_OVERRIDE, over);
157         }
158         appconfig.setParams(parameters);
159
160         response.put("appconfig", appconfig);
161         response.put("inputs", inputs);
162         return response;
163
164     }
165
166 }