Add Native Kafka streams support in bp-generator
[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.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
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     @Autowired
59     private StreamService streamService;
60
61
62     /**
63      * Creates Inputs section under App Config with Publishes, Subscribes, Parameters sections by
64      * checking Datarouter/MessageRouter/override/Dmaap values
65      *
66      * @param inputs Inputs
67      * @param onapComponentSpec Onap Component Specification
68      * @param override Parameter to Service Component Override
69      * @param isDmaap Dmaap Argument
70      * @return
71      */
72     public Map<String, Object> createAppconfig(
73         Map<String, LinkedHashMap<String, Object>> inputs,
74         OnapComponentSpec onapComponentSpec,
75         String override,
76         boolean isDmaap) {
77
78         Map<String, Object> response = new HashMap<>();
79         Appconfig appconfig = new Appconfig();
80
81         Calls[] call = new Calls[0];
82         appconfig.setService_calls(call);
83
84         Map<String, BaseStream> streamPublishes = streamService.createStreamPublishes(
85             onapComponentSpec, blueprintHelperService, dmaapService, inputs, isDmaap);
86         Map<String, BaseStream> streamSubscribes = streamService.createStreamSubscribes(
87             onapComponentSpec, blueprintHelperService, dmaapService, inputs, isDmaap);
88
89         appconfig.setStreams_publishes(streamPublishes);
90         appconfig.setStreams_subscribes(streamSubscribes);
91
92         Map<String, Object> parameters = new TreeMap<>();
93         for (Parameters p : onapComponentSpec.getParameters()) {
94             String pName = p.getName();
95             if (p.isSourced_at_deployment()) {
96                 GetInput paramInput = new GetInput();
97                 paramInput.setBpInputName(pName);
98                 parameters.put(pName, paramInput);
99                 if (!"".equals(p.getValue())) {
100                     LinkedHashMap<String, Object> pInputs =
101                         blueprintHelperService.createStringInput(p.getValue());
102                     inputs.put(pName, pInputs);
103                 } else {
104                     LinkedHashMap<String, Object> pInputs = new LinkedHashMap<>();
105                     pInputs.put("type", "string");
106                     inputs.put(pName, pInputs);
107                 }
108             } else {
109                 if ("string".equals(p.getType())) {
110                     String val = (String) p.getValue();
111                     val = '"' + val + '"';
112                     parameters.put(pName, val);
113                 } else {
114                     parameters.put(pName, p.getValue());
115                     // Updated code to resolve the issue of missing \ for collector.schema.file
116                     // parameters.put(pName, pName.equals("collector.schema.file") ?
117                     // ((String)p.getValue()).replace("\"", "\\\"") : p.getValue());
118                 }
119             }
120         }
121         if (override != null) {
122             GetInput ov = new GetInput();
123             ov.setBpInputName(Constants.SERVICE_COMPONENT_NAME_OVERRIDE);
124             parameters.put(Constants.SERVICE_COMPONENT_NAME_OVERRIDE, ov);
125             LinkedHashMap<String, Object> over = blueprintHelperService.createStringInput(override);
126             inputs.put(Constants.SERVICE_COMPONENT_NAME_OVERRIDE, over);
127         }
128         appconfig.setParams(parameters);
129
130         response.put("appconfig", appconfig);
131         response.put("inputs", inputs);
132         return response;
133     }
134
135 }