BpGen refactor Code Quality Issue-ID: DCAEGEN2-2502
[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 Application: ONAP - Blueprint Generator Common ONAP Service used to create App
47  * Config
48  */
49 @Service("onapAppConfigService")
50 public class AppConfigService {
51
52     @Autowired
53     private DmaapService dmaapService;
54
55     @Autowired
56     private BlueprintHelperService blueprintHelperService;
57
58     /**
59      * Creates Inputs section under App Config with Publishes, Subscribes, Parameters sections by
60      * checking Datarouter/MessageRouter/override/Dmaap values
61      *
62      * @param inputs Inputs
63      * @param onapComponentSpec Onap Component Specification
64      * @param override Parameter to Service Component Override
65      * @param isDmaap Dmaap Argument
66      * @return
67      */
68     public Map<String, Object> createAppconfig(
69         Map<String, LinkedHashMap<String, Object>> inputs,
70         OnapComponentSpec onapComponentSpec,
71         String override,
72         boolean isDmaap) {
73
74         Map<String, Object> response = new HashMap<>();
75         Appconfig appconfig = new Appconfig();
76
77         Calls[] call = new Calls[0];
78         appconfig.setService_calls(call);
79
80         Map<String, Dmaap> streamPublishes = new TreeMap<>();
81         if (onapComponentSpec.getStreams() != null) {
82             if (onapComponentSpec.getStreams().getPublishes() != null) {
83                 for (Publishes publishes : onapComponentSpec.getStreams().getPublishes()) {
84                     if (blueprintHelperService.isDataRouterType(publishes.getType())) {
85                         String config = publishes.getConfig_key();
86                         String name = config + Constants._FEED;
87                         Map<String, Object> dmaapDataRouterResponse =
88                             dmaapService.createDmaapDataRouter(inputs, config, name, isDmaap);
89                         inputs =
90                             (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse
91                                 .get("inputs");
92                         Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
93                         dmaap.setType(publishes.getType());
94                         streamPublishes.put(config, dmaap);
95                     } else if (blueprintHelperService.isMessageRouterType(publishes.getType())) {
96                         String config = publishes.getConfig_key();
97                         String name = config + Constants._TOPIC;
98                         Map<String, Object> dmaapDataRouterResponse =
99                             dmaapService
100                                 .createDmaapMessageRouter(inputs, config, 'p', name, name, isDmaap);
101                         inputs =
102                             (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse
103                                 .get("inputs");
104                         Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
105                         dmaap.setType(publishes.getType());
106                         streamPublishes.put(config, dmaap);
107                     }
108                 }
109             }
110         }
111
112         Map<String, Dmaap> streamSubscribes = new TreeMap<>();
113
114         if (onapComponentSpec.getStreams() != null) {
115             if (onapComponentSpec.getStreams().getSubscribes() != null) {
116                 for (Subscribes subscribes : onapComponentSpec.getStreams().getSubscribes()) {
117                     if (blueprintHelperService.isDataRouterType(subscribes.getType())) {
118                         String config = subscribes.getConfig_key();
119                         String name = config + Constants._FEED;
120                         Map<String, Object> dmaapDataRouterResponse =
121                             dmaapService.createDmaapDataRouter(inputs, config, name, isDmaap);
122                         inputs =
123                             (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse
124                                 .get("inputs");
125                         Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
126                         dmaap.setType(subscribes.getType());
127                         streamSubscribes.put(config, dmaap);
128                     } else if (blueprintHelperService.isMessageRouterType(subscribes.getType())) {
129                         String config = subscribes.getConfig_key();
130                         String name = config + Constants._TOPIC;
131                         Map<String, Object> dmaapDataRouterResponse =
132                             dmaapService
133                                 .createDmaapMessageRouter(inputs, config, 's', name, name, isDmaap);
134                         inputs =
135                             (Map<String, LinkedHashMap<String, Object>>) dmaapDataRouterResponse
136                                 .get("inputs");
137                         Dmaap dmaap = (Dmaap) dmaapDataRouterResponse.get("dmaap");
138                         dmaap.setType(subscribes.getType());
139                         streamSubscribes.put(config, dmaap);
140                     }
141                 }
142             }
143         }
144
145         appconfig.setStreams_publishes(streamPublishes);
146         appconfig.setStreams_subscribes(streamSubscribes);
147
148         Map<String, Object> parameters = new TreeMap<>();
149         for (Parameters p : onapComponentSpec.getParameters()) {
150             String pName = p.getName();
151             if (p.isSourced_at_deployment()) {
152                 GetInput paramInput = new GetInput();
153                 paramInput.setBpInputName(pName);
154                 parameters.put(pName, paramInput);
155                 if (!"".equals(p.getValue())) {
156                     LinkedHashMap<String, Object> pInputs =
157                         blueprintHelperService.createStringInput(p.getValue());
158                     inputs.put(pName, pInputs);
159                 } else {
160                     LinkedHashMap<String, Object> pInputs = new LinkedHashMap<>();
161                     pInputs.put("type", "string");
162                     inputs.put(pName, pInputs);
163                 }
164             } else {
165                 if ("string".equals(p.getType())) {
166                     String val = (String) p.getValue();
167                     val = '"' + val + '"';
168                     parameters.put(pName, val);
169                 } else {
170                     parameters.put(pName, p.getValue());
171                     // Updated code to resolve the issue of missing \ for collector.schema.file
172                     // parameters.put(pName, pName.equals("collector.schema.file") ?
173                     // ((String)p.getValue()).replace("\"", "\\\"") : p.getValue());
174                 }
175             }
176         }
177         if (override != null) {
178             GetInput ov = new GetInput();
179             ov.setBpInputName(Constants.SERVICE_COMPONENT_NAME_OVERRIDE);
180             parameters.put(Constants.SERVICE_COMPONENT_NAME_OVERRIDE, ov);
181             LinkedHashMap<String, Object> over = blueprintHelperService.createStringInput(override);
182             inputs.put(Constants.SERVICE_COMPONENT_NAME_OVERRIDE, over);
183         }
184         appconfig.setParams(parameters);
185
186         response.put("appconfig", appconfig);
187         response.put("inputs", inputs);
188         return response;
189     }
190 }