1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
/*
* Copyright 2017 Huawei Technologies Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.cli.fw.input;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandInvalidParameterValue;
import org.onap.cli.fw.error.OnapCommandParameterMissing;
import org.onap.cli.fw.utils.OnapCommandUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* Oclip Command's input parameter.
*
*/
public class OnapCommandParameter {
/*
* Name, for positional parameters, the place is decided from schema file definition
*/
private String cmdName;
/*
* Description
*/
private String cmdDescription = "";
/*
* Short Option, like -f, for positional parameters, its not required.
*/
private String shortOption = null;
/*
* Long Option, like --file-path, for positional parameters, its not required.
*/
private String longOption = null;
/*
* Parameter type such as int, json, yaml, string, etc
*/
private OnapCommandParameterType parameterType = OnapCommandParameterType.STRING;
/*
* Default value
*/
private Object defaultValue;
/*
* raw default value, stored with out processing it.
*/
private String rawDefaultValue = "";
/*
* Is optional
*/
private boolean isOptional = false;
/*
* Is secured
*/
private boolean isSecured = false;
/*
* Parameter Value
*/
private Object value = null;
/*
* raw value, get stored as its without processing it.
* it will have same value as stored in yaml template
*/
private Object rawValue = null;
/*
* to support include concepts of templates, new variable
* called is_include is introduced and default its always
* true.
*/
private boolean isInclude = true;
/*
* This param is from The default input parameters file
*/
private boolean isDefaultParam = false;
public boolean isDefaultParam() {
return isDefaultParam;
}
public void setDefaultParam(boolean isDefaultParam) {
this.isDefaultParam = isDefaultParam;
}
public String getName() {
return cmdName;
}
public void setName(String name) {
this.cmdName = name;
}
public String getDescription() {
return cmdDescription;
}
public void setDescription(String description) {
this.cmdDescription = description;
}
public String getShortOption() {
return shortOption;
}
public void setShortOption(String shortOption) {
this.shortOption = shortOption;
}
public String getLongOption() {
return longOption;
}
public void setLongOption(String longOption) {
this.longOption = longOption;
}
public OnapCommandParameterType getParameterType() {
return parameterType;
}
public void setParameterType(OnapCommandParameterType parameterType) {
this.parameterType = parameterType;
if (defaultValue == null) {
switch (getParameterType()) {
case MAP:
this.defaultValue = new HashMap<String, String>();
break;
case ARRAY:
defaultValue = new ArrayList<String>();
break;
case BOOL:
defaultValue = Boolean.FALSE;
break;
case UUID:
this.defaultValue = UUID.randomUUID().toString();
break;
case JSON:
this.defaultValue = new String("{}");
break;
default:
this.defaultValue = new String("");
break;
}
}
}
/**
* Returns default value.
*
* @return string
*/
public Object getDefaultValue() {
return defaultValue;
}
/**
* check if the default value is ${ENV_VAR_NAME}.
*
* @return boolean
*/
public boolean isRawDefaultValueAnEnv() {
return this.rawDefaultValue.trim().startsWith("$s{env:") && this.rawDefaultValue.trim().endsWith("}");
}
/**
* check if the default value is $s{env:ENV_VAR_NAME} and return the ENV_VAR_NAME.
*
* @return ENV_VAR_NAME
*/
public String getEnvVarNameFromrRawDefaultValue() {
return this.rawDefaultValue.trim().substring(7, this.rawDefaultValue.length() - 1);
}
public void setRawDefaultValue(String value) throws OnapCommandInvalidParameterValue {
this.rawDefaultValue = value;
String processedValue= OnapCommandUtils.replaceLineForSpecialValues(value);
switch (parameterType) {
case MAP:
try {
defaultValue = new ObjectMapper().readValue(processedValue, Map.class);
} catch (IOException e) {
throw new OnapCommandInvalidParameterValue("Invalid default value for " + this.getName(), e);
}
break;
case ARRAY:
try {
defaultValue = new ObjectMapper().readValue(processedValue, List.class);
} catch (IOException e) {
throw new OnapCommandInvalidParameterValue("Invalid default value for " + this.getName(), e);
}
break;
case BOOL:
defaultValue = Boolean.valueOf(processedValue);
break;
default:
defaultValue = processedValue;
break;
}
}
public void setDefaultValue(Object defaultValue) throws OnapCommandInvalidParameterValue {
//check type
Class<?> clz;
switch (parameterType) {
case BOOL:
clz = Boolean.class;
break;
case MAP:
clz = Map.class;
break;
case ARRAY:
clz = Collection.class;
break;
default:
clz = String.class;
break;
}
if (clz.isInstance(defaultValue)) {
this.defaultValue = defaultValue;
} else {
throw new OnapCommandInvalidParameterValue("Invalid default value for parameter: " + this.getName());
}
}
/**
* Returns param value.
*
* @return value
* @throws OnapCommandInvalidParameterValue
* exception
*/
//mrkanag return value in the type format instead of string format
public Object getValue() {
if (value != null) {
return value;
}
return getDefaultValue();
}
public void setValue(Object value) throws OnapCommandInvalidParameterValue {
this.rawValue = value;
if (OnapCommandParameterType.URL.equals(parameterType) && !value.toString().isEmpty() && !value.toString().startsWith("http")
&& !value.toString().startsWith("/")) {
value = "/" + value;
} else if (OnapCommandParameterType.ARRAY.equals(parameterType)) {
if (!(value instanceof List)) {
throw new OnapCommandInvalidParameterValue(this.getName());
}
} else if (OnapCommandParameterType.MAP.equals(parameterType)) {
if (!(value instanceof Map)) {
throw new OnapCommandInvalidParameterValue(this.getName());
}
} else if (OnapCommandParameterType.BOOL.equals(parameterType)) {
if (!(value instanceof Boolean)) {
if (value instanceof String) {
value = Boolean.valueOf((String)value);
} else {
throw new OnapCommandInvalidParameterValue(this.getName());
}
}
}
this.value = value;
}
public boolean isOptional() {
return isOptional;
}
public void setOptional(boolean isOptional) {
this.isOptional = isOptional;
}
public boolean isSecured() {
return isSecured;
}
public void setSecured(boolean isSecured) {
this.isSecured = isSecured;
}
public boolean isInclude() {
return isInclude;
}
public void setInclude(boolean isInclude) {
this.isInclude = isInclude;
}
public static String printShortOption(String option) {
return "-" + option;
}
public static String printLongOption(String option) {
return "-" + printShortOption(option);
}
/**
* Validate parameter value.
*
* @throws OnapCommandParameterMissing
* exception
* @throws OnapCommandInvalidParameterValue
* exception
*/
public void validate() throws OnapCommandException {
// (mrkanag) empty check needs to revisit
if (!this.isOptional() && (this.getValue() == null || this.getValue().toString().isEmpty())) {
throw new OnapCommandParameterMissing(this.getName());
}
if (!this.isOptional() && OnapCommandParameterType.BINARY.equals(parameterType)) {
File file = new File(value.toString());
if (!file.isFile()) {
throw new OnapCommandInvalidParameterValue(this.getName());
}
}
// (mrkanag) validate for type supported OnapCommandParameterType using constraints
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cmdName == null) ? 0 : cmdName.hashCode());
result = prime * result + ((longOption == null) ? 0 : longOption.hashCode());
result = prime * result + ((shortOption == null) ? 0 : shortOption.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
OnapCommandParameter other = (OnapCommandParameter) obj;
if (cmdName == null) {
if (other.cmdName != null)
return false;
} else if (!cmdName.equals(other.cmdName))
return false;
if (longOption == null) {
if (other.longOption != null)
return false;
} else if (!longOption.equals(other.longOption))
return false;
if (shortOption == null) {
if (other.shortOption != null)
return false;
} else if (!shortOption.equals(other.shortOption))
return false;
return true;
}
@Override
public String toString() {
return this.getName() + ": " + this.getValue();
}
}
|