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