Fix sonar issues
[dcaegen2/platform.git] / mod / bpgenerator / common / src / main / java / org / onap / blueprintgenerator / service / base / BlueprintHelperService.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.base;
27
28 import org.onap.blueprintgenerator.constants.Constants;
29 import org.springframework.stereotype.Service;
30
31 import java.util.Map;
32 import java.util.LinkedHashMap;
33
34 /**
35  * @author : Ravi Mantena
36  * @date 10/16/2020 Application: DCAE/ONAP - Blueprint Generator Common Module: Used by both ONAp and DCAE Blueprint
37  * Applications Service: An interface for Common Functions used across Blueprint
38  */
39 @Service
40 public class BlueprintHelperService {
41
42     /**
43      * creates Input value by contatinating Type, Description and Default value
44      *
45      * @param type         Input Type
46      * @param description  Description
47      * @param defaultValue Default value of Type
48      * @return
49      */
50     public Map<String, Object> createInputValue(
51         String type, String description, Object defaultValue) {
52         LinkedHashMap<String, Object> inputMap = new LinkedHashMap<>();
53         inputMap.put("type", type);
54         inputMap.put("description", description);
55         inputMap.put("default", defaultValue);
56         return inputMap;
57     }
58
59     /**
60      * creates Input value by contatinating Type and Description
61      *
62      * @param type        Input Type
63      * @param description Description
64      * @return
65      */
66     public Map<String, Object> createInputValue(String type, String description) {
67         LinkedHashMap<String, Object> inputMap = new LinkedHashMap<>();
68         inputMap.put("type", type);
69         inputMap.put("description", description);
70         return inputMap;
71     }
72
73     /**
74      * creates Input value by contatinating Type and Default value
75      *
76      * @param type         Input Type
77      * @param defaultValue Default value of Type
78      * @return
79      */
80     public Map<String, Object> createInputValue(String type, Object defaultValue) {
81         LinkedHashMap<String, Object> inputMap = new LinkedHashMap<>();
82         inputMap.put("type", type);
83         inputMap.put("default", defaultValue);
84         return inputMap;
85     }
86
87     /**
88      * creates Input value by contatinating Description and Default value
89      *
90      * @param description  Description
91      * @param defaultValue Default value of Type
92      * @return
93      */
94     public Map<String, Object> createIntegerInput(String description,
95         Object defaultValue) {
96         return createInputValue(Constants.INTEGER_TYPE, description, defaultValue);
97     }
98
99     /**
100      * creates Integer Input value for given Description
101      *
102      * @param description Description
103      * @return
104      */
105     public Map<String, Object> createIntegerInput(String description) {
106         return createInputValue(Constants.INTEGER_TYPE, description);
107     }
108
109     /**
110      * creates Integer Input value for given Default value
111      *
112      * @param defaultValue Default value of Type
113      * @return
114      */
115     public Map<String, Object> createIntegerInput(Object defaultValue) {
116         return createInputValue(Constants.INTEGER_TYPE, defaultValue);
117     }
118
119     /**
120      * creates Integer Input value for given Description and Default value
121      *
122      * @param description  Description
123      * @param defaultValue Default value of Type
124      * @return
125      */
126     public Map<String, Object> createBooleanInput(String description,
127         Object defaultValue) {
128         return createInputValue(Constants.BOOLEAN_TYPE, description, defaultValue);
129     }
130
131     /**
132      * creates Boolean Input value for given Description
133      *
134      * @param description Description
135      * @return
136      */
137     public Map<String, Object> createBooleanInput(String description) {
138         return createInputValue(Constants.BOOLEAN_TYPE, description);
139     }
140
141     /**
142      * creates Boolean Input value for given Default value
143      *
144      * @param defaultValue Default value of Type
145      * @return
146      */
147     public Map<String, Object> createBooleanInput(Object defaultValue) {
148         return createInputValue(Constants.BOOLEAN_TYPE, defaultValue);
149     }
150
151     /**
152      * creates String Input value for given Default value
153      *
154      * @param description  Description
155      * @param defaultValue Default value of Type
156      * @return
157      */
158     public Map<String, Object> createStringInput(String description,
159         Object defaultValue) {
160         return createInputValue(Constants.STRING_TYPE, description, defaultValue);
161     }
162
163     /**
164      * creates String Input value for given Default value
165      *
166      * @param defaultValue Default value of Type
167      * @return
168      */
169     public Map<String, Object> createStringInput(Object defaultValue) {
170         return createInputValue(Constants.STRING_TYPE, defaultValue);
171     }
172
173     /**
174      * creates proper Input for given inputType and defaultValue.
175      * <p>
176      * Default input type: "string".
177      *
178      * @param inputType    Input type, supported: "boolean", "integer", "number"
179      * @param defaultValue Default value of Type
180      * @return
181      */
182     public Map<String, Object> createInputByType(String inputType, Object defaultValue) {
183         switch (inputType) {
184             case "boolean":
185                 return createBooleanInput(defaultValue);
186             case "integer":
187             case "number":
188                 return createIntegerInput(defaultValue);
189             default:
190                 return createStringInput(defaultValue);
191         }
192     }
193
194
195     /**
196      * Concatenates String Input values with Underscore
197      *
198      * @param firstValue  Value
199      * @param secondValue Value
200      * @return
201      */
202     public String joinUnderscore(String firstValue, String secondValue) {
203         return firstValue + "_" + secondValue;
204     }
205
206     /**
207      * Returns if the type is Data Router or not
208      *
209      * @param type Input Type
210      * @return
211      */
212     public boolean isDataRouterType(String type) {
213         return type.equals(Constants.DATA_ROUTER) || type.equals(Constants.DATAROUTER_VALUE);
214     }
215
216     /**
217      * Returns if the type is Message Router or not
218      *
219      * @param type Input Type
220      * @return
221      */
222     public boolean isMessageRouterType(String type) {
223         return type.equals(Constants.MESSAGE_ROUTER) || type.equals(Constants.MESSAGEROUTER_VALUE);
224     }
225
226     /**
227      * Returns if the type is Kafka or not
228      *
229      * @param type Input Type
230      * @return
231      */
232     public boolean isKafkaStreamType(String type) {
233         return type.equals(Constants.KAFKA_TYPE);
234     }
235
236     /**
237      * Returns name with underscore for empty input
238      *
239      * @param name Name
240      * @return
241      */
242     public String getNamePrefix(String name) {
243         return name.isEmpty() ? "" : name + "_";
244     }
245 }