Fix sonar issues
[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  *  *  Copyright (c) 2021  Nokia. All rights reserved.
8  *  *  ================================================================================
9  *  *  Modifications Copyright (c) 2021 Nokia
10  *  *  ================================================================================
11  *  *  Licensed under the Apache License, Version 2.0 (the "License");
12  *  *  you may not use this file except in compliance with the License.
13  *  *  You may obtain a copy of the License at
14  *  *
15  *  *       http://www.apache.org/licenses/LICENSE-2.0
16  *  *
17  *  *  Unless required by applicable law or agreed to in writing, software
18  *  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  *  See the License for the specific language governing permissions and
21  *  *  limitations under the License.
22  *  *  ============LICENSE_END=========================================================
23  *
24  *
25  */
26
27 package org.onap.blueprintgenerator.service.common;
28
29 import org.onap.blueprintgenerator.model.common.Appconfig;
30 import org.onap.blueprintgenerator.model.common.BaseStream;
31 import org.onap.blueprintgenerator.model.common.GetInput;
32 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
33 import org.onap.blueprintgenerator.model.componentspec.common.Calls;
34 import org.onap.blueprintgenerator.model.componentspec.common.Parameters;
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 Config
47  */
48 @Service("onapAppConfigService")
49 public class AppConfigService {
50
51     @Autowired
52     private DmaapService dmaapService;
53
54     @Autowired
55     private BlueprintHelperService blueprintHelperService;
56
57     @Autowired
58     private StreamService streamService;
59
60
61     /**
62      * Creates Inputs section under App Config with Publishes, Subscribes, Parameters sections by checking
63      * Datarouter/MessageRouter/override/Dmaap values
64      *
65      * @param inputs            Inputs
66      * @param onapComponentSpec Onap Component Specification
67      * @param isDmaap           Dmaap Argument
68      * @return
69      */
70     public Map<String, Object> createAppconfig(
71         Map<String, Map<String, Object>> inputs,
72         OnapComponentSpec onapComponentSpec,
73         boolean isDmaap) {
74
75         Map<String, Object> response = new HashMap<>();
76         Appconfig appconfig = new Appconfig();
77
78         Calls[] call = new Calls[0];
79         appconfig.setService_calls(call);
80
81         Map<String, BaseStream> streamPublishes = streamService.createStreamPublishes(
82             onapComponentSpec, blueprintHelperService, dmaapService, inputs, isDmaap);
83         Map<String, BaseStream> streamSubscribes = streamService.createStreamSubscribes(
84             onapComponentSpec, blueprintHelperService, dmaapService, inputs, isDmaap);
85
86         appconfig.setStreams_publishes(streamPublishes);
87         appconfig.setStreams_subscribes(streamSubscribes);
88
89         Map<String, Object> parameters = new TreeMap<>();
90         for (Parameters p : onapComponentSpec.getParameters()) {
91             String pName = p.getName();
92             if (p.isSourced_at_deployment()) {
93                 GetInput paramInput = new GetInput();
94                 paramInput.setBpInputName(pName);
95                 parameters.put(pName, paramInput);
96                 if (!"".equals(p.getValue())) {
97                     Map<String, Object> pInputs = createInputFromParameter(p);
98                     inputs.put(pName, pInputs);
99                 } else {
100                     LinkedHashMap<String, Object> pInputs = new LinkedHashMap<>();
101                     pInputs.put("type", "string");
102                     inputs.put(pName, pInputs);
103                 }
104             } else {
105                 if ("string".equals(p.getType())) {
106                     String val = (String) p.getValue();
107                     val = '"' + val + '"';
108                     parameters.put(pName, val);
109                 } else {
110                     parameters.put(pName, p.getValue());
111                     // Updated code to resolve the issue of missing \ for collector.schema.file
112                     // parameters.put(pName, pName.equals("collector.schema.file") ?
113                     // ((String)p.getValue()).replace("\"", "\\\"") : p.getValue());
114                 }
115             }
116         }
117
118         appconfig.setParams(parameters);
119
120         response.put("appconfig", appconfig);
121         response.put("inputs", inputs);
122         return response;
123     }
124
125     private Map<String, Object> createInputFromParameter(Parameters parameter) {
126         String inputType = parameter.getType() == null ? "string" : parameter.getType();
127
128         return blueprintHelperService.createInputByType(inputType, parameter.getValue());
129     }
130 }