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