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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
|
/*
* 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.utils;
import static org.onap.cli.fw.conf.Constants.ATTRIBUTES;
import static org.onap.cli.fw.conf.Constants.BODY;
import static org.onap.cli.fw.conf.Constants.BOOLEAN_VALUE;
import static org.onap.cli.fw.conf.Constants.HEADERS;
import static org.onap.cli.fw.conf.Constants.HTTP;
import static org.onap.cli.fw.conf.Constants.HTTP_BODY_FAILED_PARSING;
import static org.onap.cli.fw.conf.Constants.HTTP_BODY_JSON_EMPTY;
import static org.onap.cli.fw.conf.Constants.HTTP_METHODS;
import static org.onap.cli.fw.conf.Constants.HTTP_REQUEST_MANDATORY_PARAMS;
import static org.onap.cli.fw.conf.Constants.HTTP_REQUEST_PARAMS;
import static org.onap.cli.fw.conf.Constants.HTTP_SUCCESS_CODE_INVALID;
import static org.onap.cli.fw.conf.Constants.METHOD;
import static org.onap.cli.fw.conf.Constants.NAME;
import static org.onap.cli.fw.conf.Constants.PARAMETERS;
import static org.onap.cli.fw.conf.Constants.QUERIES;
import static org.onap.cli.fw.conf.Constants.REQUEST;
import static org.onap.cli.fw.conf.Constants.RESULTS;
import static org.onap.cli.fw.conf.Constants.RESULT_MAP;
import static org.onap.cli.fw.conf.Constants.URI;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.onap.cli.fw.OnapCommand;
import org.onap.cli.fw.cmd.OnapHttpCommand;
import org.onap.cli.fw.conf.Constants;
import org.onap.cli.fw.conf.OnapCommandConfg;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandHttpHeaderNotFound;
import org.onap.cli.fw.error.OnapCommandHttpInvalidResponseBody;
import org.onap.cli.fw.error.OnapCommandHttpInvalidResultMap;
import org.onap.cli.fw.error.OnapCommandInvalidParameterValue;
import org.onap.cli.fw.error.OnapCommandParameterNotFound;
import org.onap.cli.fw.error.OnapCommandResultEmpty;
import org.onap.cli.fw.error.OnapCommandResultMapProcessingFailed;
import org.onap.cli.fw.http.HttpInput;
import org.onap.cli.fw.http.HttpResult;
import org.onap.cli.fw.input.OnapCommandParameter;
import org.onap.cli.fw.input.ParameterType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
/**
* Provides helper method to parse Yaml files and produce required objects.
*
*/
public class OnapCommandUtils {
static Logger LOG = LoggerFactory.getLogger(OnapCommandUtils.class);
/**
* Private constructor.
*/
private OnapCommandUtils() {
}
static void throwOrCollect(OnapCommandException ex, List<String> list, boolean shouldCollectException)
throws OnapCommandException {
if (shouldCollectException) {
list.add(ex.getMessage());
} else {
throw ex;
}
}
static void validateTags(List<String> schemaErrors, Map<String, ?> yamlMap, List<String> totalParams,
List<String> mandatoryParams, String section) {
// mrkanag capture invalid entries as well
for (String param : totalParams) {
boolean isMandatory = mandatoryParams.contains(param);
boolean isYamlContains = yamlMap.containsKey(param);
if (isMandatory) {
if (!isYamlContains) {
schemaErrors.add("Mandatory attribute '" + param + "' is missing under '" + section + "'");
} else {
String value = String.valueOf(yamlMap.get(param));
if (value == null || value.isEmpty()) {
schemaErrors.add("Mandatory attribute '" + param + "' under '" + section
+ "' shouldn't be null or empty");
}
}
}
}
}
/**
* Validate Boolean.
*
* @param toValidate
* string
* @return boolean
*/
static boolean validateBoolean(String toValidate) {
return OnapCommandConfg.getSchemaAttrInfo(BOOLEAN_VALUE).contains(toValidate.toLowerCase());
}
private static String emptySection(String section) {
return "The section '" + section + ":' cann't be null or empty";
}
static String invalidBooleanValueMessage(String section, String attribute, String value) {
return "The value '" + value + "' of '" + attribute + "' present under '" + section + "' should be boolean";
}
private static Set<String> validateHttpQueries(Map<String, Object> requestMap) {
Map<String, Object> queries = (Map<String, Object>) requestMap.get(QUERIES);
Set<String> queryParamNames = new HashSet<>();
if (queries != null) {
for (Entry<String, Object> entry : queries.entrySet()) {
parseParameters(String.valueOf(entry.getValue()), queryParamNames);
}
}
return queryParamNames;
}
private static Set<String> validateHttpHeaders(Map<String, Object> requestMap) {
Map<String, Object> headers = (Map<String, Object>) requestMap.get(HEADERS);
Set<String> headerParamNames = new HashSet<>();
if (headers != null) {
for (Entry<String, Object> entry : headers.entrySet()) {
parseParameters(String.valueOf(entry.getValue()), headerParamNames);
}
}
return headerParamNames;
}
private static Set<String> validateHttpBody(List<String> errorList, Map<String, Object> requestMap) {
Set<String> bodyParamNames = new HashSet<>();
Object bodyString = requestMap.get(BODY);
if (bodyString == null) {
return bodyParamNames;
}
String body = String.valueOf(bodyString);
JSONObject obj = null;
try {
obj = new ObjectMapper().readValue(body, JSONObject.class);
} catch (IOException e1) { // NOSONAR
errorList.add(HTTP_BODY_FAILED_PARSING);
}
if (obj == null || "".equals(obj.toString())) {
errorList.add(HTTP_BODY_JSON_EMPTY);
}
parseParameters(body, bodyParamNames);
return bodyParamNames;
}
private static Set<String> validateHttpUri(List<String> errorList, Map<String, Object> requestMap) {
Set<String> uriParamNames = new HashSet<>();
String uri = (String) requestMap.get(URI);
if (uri == null || uri.isEmpty()) {
errorList.add(emptySection(URI));
return uriParamNames;
}
parseParameters(uri, uriParamNames);
return uriParamNames;
}
private static void parseParameters(String line, Set<String> paramNames) {
int currentIdx = 0;
while (currentIdx < line.length()) {
int idxS = line.indexOf("${", currentIdx);
if (idxS == -1) {
break;
}
int idxE = line.indexOf("}", idxS);
String paramName = line.substring(idxS + 2, idxE);
paramNames.add(paramName.trim());
currentIdx = idxE + 1;
}
}
private static Set<String> getRequestParams(Map<String, ?> yamlMap) {
Set<String> set = new HashSet<>();
@SuppressWarnings("unchecked")
List<Map<String, Object>> inputParams = (List<Map<String, Object>>) yamlMap.get(PARAMETERS);
if (inputParams != null) {
for (Map<String, Object> map : inputParams) {
for (Entry<String, Object> entry : map.entrySet()) {
Object key = entry.getKey();
if (NAME.equals(key)) {
set.add(String.valueOf(entry.getValue()));
break;
}
}
}
}
return set;
}
static void validateHttpResultMap(List<String> errorList, Map<String, ?> values) throws OnapCommandException {
Map<String, ?> valMap = (Map<String, ?>) values.get(HTTP);
List<Map<String, String>> attributes = (List<Map<String, String>>) ((Map<String, ?>)values.get(RESULTS)).get(ATTRIBUTES);
Set<String> resultMapParams = ((Map<String, String>) valMap.get(RESULT_MAP)).keySet();
Set<String> resultAttNames = attributes.stream().map(map -> map.get(NAME))
.collect(Collectors.toSet());
List<String> invaliResultMapParams = resultMapParams.stream()
.filter(p -> !resultAttNames.contains(p)).collect(Collectors.toList());
if (!invaliResultMapParams.isEmpty()) {
throwOrCollect(new OnapCommandHttpInvalidResultMap(invaliResultMapParams), errorList, true);
}
}
static void validateHttpSccessCodes(List<String> errorList, List<Object> requestSuccessCodes) {
if (requestSuccessCodes == null || requestSuccessCodes.isEmpty()) {
errorList.add(HTTP_SUCCESS_CODE_INVALID);
return;
}
for (Object successCode : requestSuccessCodes) {
Integer code = (Integer) successCode;
if (code < 200 || code >= 300) {
if ( code != 404) {
errorList.add(HTTP_SUCCESS_CODE_INVALID);
}
}
}
}
static ArrayList<String> validateHttpSchemaSection(Map<String, ?> values) {
ArrayList<String> errorList = new ArrayList<>();
Map<String, ?> map = (Map<String, ?>) values.get(HTTP);
Map<String, Object> requestMap = (Map<String, Object>) map.get(REQUEST);
if (requestMap != null && !requestMap.isEmpty()) {
validateTags(errorList, requestMap, OnapCommandConfg.getSchemaAttrInfo(HTTP_REQUEST_PARAMS),
OnapCommandConfg.getSchemaAttrInfo(HTTP_REQUEST_MANDATORY_PARAMS), REQUEST);
String method = (String) requestMap.get(METHOD);
if (method != null && !method.isEmpty()) {
if (!OnapCommandConfg.getSchemaAttrInfo(HTTP_METHODS).contains(method.toLowerCase())) {
errorList.add("Attribute '" + METHOD + "' under '" + REQUEST + "' is invalid, correct types are "
+ OnapCommandConfg.getSchemaAttrInfo(HTTP_METHODS).toString());
}
} else {
errorList.add("Http request method cann't be null or empty");
}
Set<String> requestParams = getRequestParams(values);
Set<String> uriParams = validateHttpUri(errorList, requestMap);
Set<String> bodyParams = validateHttpBody(errorList, requestMap);
Set<String> headerParams = validateHttpHeaders(requestMap);
Set<String> queryParams = validateHttpQueries(requestMap);
HashSet<String> totoalParams = new HashSet<>(uriParams);
totoalParams.addAll(bodyParams);
totoalParams.addAll(headerParams);
totoalParams.addAll(queryParams);
List<String> nonDeclaredParams = totoalParams.stream().filter(param -> !requestParams.contains(param))
.collect(Collectors.toList());
nonDeclaredParams.stream().forEach(p -> errorList.add("The parameter '" + p
+ "' declared under 'parameters:' section is not mapped into request section."));
} else {
errorList.add(emptySection(REQUEST));
}
return errorList;
}
/**
* Create Dict from list of Parameters.
*
* @param inputs
* list of parameters
* @return map
*/
public static Map<String, OnapCommandParameter> getInputMap(List<OnapCommandParameter> inputs) {
Map<String, OnapCommandParameter> map = new HashMap<>();
for (OnapCommandParameter param : inputs) {
map.put(param.getName(), param);
}
return map;
}
/**
* sort the set.
*
* @param col
* set
* @return list
*/
public static List<String> sort(Set<String> col) {
List<String> results = new ArrayList<>();
results.addAll(col);
Collections.sort(results);
return results;
}
/**
* Flatten the json list.
*
* @param jsons
* list json strings
* @return list
*/
public static List<String> jsonFlatten(List<String> jsons) {
List<String> results = new ArrayList<>();
for (String json : jsons) {
try {
results.add(JsonPath.parse(json).jsonString());
} catch (Exception e) { // NOSONAR
results.add(json);
}
}
return results;
}
/**
* Construct method name.
*
* @param name
* name
* @param prefix
* prefix
* @return string
*/
public static String formMethodNameFromAttributeName(String name, String prefix) {
if (name == null || name.isEmpty()) {
return name;
}
String methodName = prefix;
for (String tk : name.split("-")) {
methodName += Character.toString(tk.charAt(0)).toUpperCase();
methodName += tk.substring(1);
}
return methodName;
}
/**
* There are unique values like uuid is supported, so when input, output (default) values has
* these special entries, then it will get replaced with it's value
*
* @param line
* @return
*/
public static String replaceLineForSpecialValues(String line) {
String result = "";
if (!line.contains("$s{")) {
return line;
}
int currentIdx = 0;
while (currentIdx < line.length()) {
int idxS = line.indexOf("$s{", currentIdx);
if (idxS == -1) {
result += line.substring(currentIdx);
break;
}
int idxE = line.indexOf("}", idxS);
String splEntry = line.substring(idxS + 3, idxE);
splEntry = splEntry.trim();
String value = "";
switch (splEntry) {
case Constants.SPL_ENTRY_UUID:
value = UUID.randomUUID().toString();
break;
default:
if (splEntry.startsWith(Constants.SPL_ENTRY_ENV)) {
//start to read after env:ENV_VAR_NAME
String envVarName = splEntry.substring(4);
value = System.getenv(envVarName);
if (value == null) {
//when env is not defined, assign the same env:ENV_VAR_NAME
//so that it will given hit to user that ENV_VAR_NAME to be
//defined.
value = splEntry;
}
} else {
value = splEntry;
}
}
result += line.substring(currentIdx, idxS) + value;
currentIdx = idxE + 1;
}
return result;
}
public static String replaceLineFromInputParameters(String line, Map<String, OnapCommandParameter> params)
throws OnapCommandException {
String result = "";
if (!line.contains("${")) {
return line;
}
int currentIdx = 0;
while (currentIdx < line.length()) {
int idxS = line.indexOf("${", currentIdx);
if (idxS == -1) {
result += line.substring(currentIdx);
break;
}
int idxE = line.indexOf("}", idxS);
String paramName = line.substring(idxS + 2, idxE);
paramName = paramName.trim();
if (!params.containsKey(paramName)) {
throw new OnapCommandParameterNotFound(paramName);
}
String value = params.get(paramName).getValue().toString();
OnapCommandParameter param = params.get(paramName);
if (ParameterType.ARRAY.equals(param.getParameterType())
|| ParameterType.MAP.equals(param.getParameterType())
|| ParameterType.JSON.equals(param.getParameterType())
|| ParameterType.YAML.equals(param.getParameterType())) {
// ignore the front and back double quotes in json body
result += line.substring(currentIdx, idxS - 1) + value;
currentIdx = idxE + 2;
} else {
result += line.substring(currentIdx, idxS) + value;
currentIdx = idxE + 1;
}
}
return result;
}
private static ArrayList<String> replaceLineFromOutputResults(String line, HttpResult resultHttp)
throws OnapCommandHttpHeaderNotFound, OnapCommandHttpInvalidResponseBody,
OnapCommandResultMapProcessingFailed, OnapCommandResultEmpty {
String headerProcessedLine = "";
ArrayList<String> result = new ArrayList<>();
if (!line.contains("$b{") && !line.contains("$h{")) {
result.add(line);
return result;
}
/**
* In case of empty response body [] or {}
**/
if (resultHttp.getBody().length() <= 2) {
return result;
}
/**
* Process headers macros : line: $h{abc}-$b{$.[*].xyz} , After processing line will be [abc's
* value]-$b{$.[*].xyz}
**/
int currentIdx = 0;
while (currentIdx < line.length()) {
int idxS = line.indexOf("$h{", currentIdx);
if (idxS == -1) {
headerProcessedLine += line.substring(currentIdx);
break;
}
int idxE = line.indexOf("}", idxS);
String headerName = line.substring(idxS + 3, idxE);
headerName = headerName.trim();
if (!resultHttp.getRespHeaders().containsKey(headerName)) {
throw new OnapCommandHttpHeaderNotFound(headerName);
}
String value = resultHttp.getRespHeaders().get(headerName);
headerProcessedLine += line.substring(currentIdx, idxS) + value;
currentIdx = idxE + 1;
}
// Process body jsonpath macros
List<Object> values = new ArrayList<>();
String bodyProcessedPattern = "";
currentIdx = 0;
int maxRows = 1; // in normal case, only one row will be there
while (currentIdx < headerProcessedLine.length()) {
int idxS = headerProcessedLine.indexOf("$b{", currentIdx);
if (idxS == -1) {
bodyProcessedPattern += headerProcessedLine.substring(currentIdx);
break;
}
int idxE = headerProcessedLine.indexOf("}", idxS);
String jsonPath = headerProcessedLine.substring(idxS + 3, idxE);
jsonPath = jsonPath.trim();
try {
// JSONArray or String
Object value = JsonPath.read(resultHttp.getBody(), jsonPath);
if (value instanceof JSONArray) {
JSONArray arr = (JSONArray) value;
if (arr.size() > maxRows) {
maxRows = arr.size();
}
}
bodyProcessedPattern += headerProcessedLine.substring(currentIdx, idxS) + "%s";
values.add(value);
currentIdx = idxE + 1;
} catch (Exception e) {
throw new OnapCommandHttpInvalidResponseBody(jsonPath, e);
}
}
if (bodyProcessedPattern.isEmpty()) {
result.add(headerProcessedLine);
return result;
} else {
for (int i = 0; i < maxRows; i++) {
currentIdx = 0;
String bodyProcessedLine = "";
int positionalIdx = 0; // %s positional idx
while (currentIdx < bodyProcessedPattern.length()) {
int idxS = bodyProcessedPattern.indexOf("%s", currentIdx);
if (idxS == -1) {
bodyProcessedLine += bodyProcessedPattern.substring(currentIdx);
break;
}
int idxE = idxS + 2; // %s
try {
Object value = values.get(positionalIdx);
String valueS = String.valueOf(value);
if (value instanceof JSONArray) {
JSONArray arr = (JSONArray) value;
if (!arr.isEmpty()) {
valueS = arr.get(i).toString();
} else {
throw new OnapCommandResultEmpty();
}
}
bodyProcessedLine += bodyProcessedPattern.substring(currentIdx, idxS) + valueS;
currentIdx = idxE;
positionalIdx++;
} catch (OnapCommandResultEmpty e) {
throw e;
} catch (Exception e) {
throw new OnapCommandResultMapProcessingFailed(line, e);
}
}
result.add(bodyProcessedLine);
}
return result;
}
}
/**
* Set argument to param value.
*
* @param params
* map
* @param input
* HttpInput
* @return HttpInput
* @throws OnapCommandParameterNotFound
* exception
* @throws OnapCommandInvalidParameterValue
* exception
*/
public static HttpInput populateParameters(Map<String, OnapCommandParameter> params, HttpInput input)
throws OnapCommandException {
HttpInput inp = new HttpInput();
for (OnapCommandParameter param : params.values()) {
if (ParameterType.BINARY.equals(param.getParameterType())) {
inp.setBinaryData(true);
break;
}
}
inp.setBody(replaceLineFromInputParameters(input.getBody(), params));
inp.setUri(replaceLineFromInputParameters(input.getUri(), params));
inp.setMethod(input.getMethod().toLowerCase());
for (String h : input.getReqHeaders().keySet()) {
String value = input.getReqHeaders().get(h);
inp.getReqHeaders().put(h, replaceLineFromInputParameters(value, params));
}
for (String h : input.getReqQueries().keySet()) {
String value = input.getReqQueries().get(h);
inp.getReqQueries().put(h, replaceLineFromInputParameters(value, params));
}
return inp;
}
/**
* Populate result.
*
* @param resultMap
* map
* @param resultHttp
* HttpResult
* @return map
* @throws OnapCommandHttpHeaderNotFound
* header not found exception
* @throws OnapCommandHttpInvalidResponseBody
* invalid response body exception
* @throws OnapCommandResultMapProcessingFailed
* map processing failed exception
*/
public static Map<String, ArrayList<String>> populateOutputs(Map<String, String> resultMap, HttpResult resultHttp)
throws OnapCommandException {
Map<String, ArrayList<String>> resultsProcessed = new HashMap<>();
for (Entry<String, String> entry : resultMap.entrySet()) {
String key = entry.getKey();
try {
resultsProcessed.put(key, replaceLineFromOutputResults(resultMap.get(key), resultHttp));
} catch(OnapCommandResultEmpty e) {
// pass // NOSONAR
}
}
return resultsProcessed;
}
/**
* Populate result from input parameters.
*
* @param resultMap
* map
* @param params
* Map<String, OnapCommandParameter>
* @return map
* @throws OnapCommandHttpHeaderNotFound
* header not found exception
* @throws OnapCommandHttpInvalidResponseBody
* invalid response body exception
* @throws OnapCommandResultMapProcessingFailed
* map processing failed exception
*/
public static Map<String, ArrayList<String>> populateOutputsFromInputParameters(
Map<String, ArrayList<String>> resultMap,
Map<String, OnapCommandParameter> params)
throws OnapCommandException {
Map<String, ArrayList<String>> resultsProcessed = new HashMap<>();
for (Entry<String, ArrayList<String>> entry : resultMap.entrySet()) {
String key = entry.getKey();
resultsProcessed.put(key, new ArrayList<>());
for (String value: entry.getValue()) {
try {
value = replaceLineFromInputParameters(value, params);
} catch(OnapCommandResultEmpty e) {
// pass // NOSONAR
}
resultsProcessed.get(key).add(value);
}
}
return resultsProcessed;
}
/**
* Copy the parameters across the commands, mainly used for catalog, login and logout commands
*
* @throws OnapCommandInvalidParameterValue
*/
public static void copyParamsFrom(OnapCommand from, OnapCommand to, Map<String, String> paramOverride) throws OnapCommandInvalidParameterValue {
for (OnapCommandParameter param: to.getParameters()) {
OnapCommandParameter fromParam = from.getParametersMap().get(param.getName());
if (fromParam != null) {
param.setValue(fromParam.getValue());
param.setDefaultValue(fromParam.getDefaultValue());
}
if (paramOverride.containsKey(param.getName())) {
param.setValue(paramOverride.get(param.getName()));
}
}
}
public static void copyParamsFrom(OnapCommand from, OnapCommand to) throws OnapCommandInvalidParameterValue {
OnapCommandUtils.copyParamsFrom(from, to, new HashMap<String, String>());
}
/**
* Copy param schema from source command to destination command, useful in adding login command params into command
* @param from
* @param to
* @throws OnapCommandException
*/
public static void copyParamSchemasFrom(OnapCommand from, OnapCommand to) throws OnapCommandException {
for (OnapCommandParameter param: from.getParameters()) {
if (!to.getParametersMap().containsKey(param.getName())) {
to.getParameters().add(param);
}
}
}
}
|