aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java
blob: 2cd07ed804007ffcf2c8c8fb2de1a366fcce181e (plain)
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
/*
 * 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.OnapCommandConstants.BOOLEAN_TRUE;
import static org.onap.cli.fw.conf.OnapCommandConstants.BOOLEAN_VALUE;
import static org.onap.cli.fw.conf.OnapCommandConstants.IS_INCLUDE;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.onap.cli.fw.cmd.OnapCommand;
import org.onap.cli.fw.conf.OnapCommandConfig;
import org.onap.cli.fw.conf.OnapCommandConstants;
import org.onap.cli.fw.error.OnapCommandException;
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.input.OnapCommandParameter;
import org.onap.cli.fw.input.OnapCommandParameterType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jayway.jsonpath.JsonPath;


/**
 * Provides helper method to parse Yaml files and produce required objects.
 *
 */
public class OnapCommandUtils {

    static Logger log = LoggerFactory.getLogger(OnapCommandUtils.class);
    private static Gson gson = new GsonBuilder().serializeNulls().create();

    /**
     * Private constructor.
     */
    private OnapCommandUtils() {

    }

    public static void throwOrCollect(OnapCommandException ex, List<String> list, boolean shouldCollectException)
            throws OnapCommandException {
        if (shouldCollectException) {
            list.add(ex.getMessage());
        } else {
            throw ex;
        }
    }

    public 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);
            boolean isInclude = yamlMap.containsKey(IS_INCLUDE) && yamlMap.get(IS_INCLUDE).toString().equals(BOOLEAN_TRUE);
            if (isMandatory) {
                if (!isYamlContains && isInclude) {
                    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
     */
    public static boolean validateBoolean(String toValidate) {
        return OnapCommandConfig.getCommaSeparatedList(BOOLEAN_VALUE).contains(toValidate.toLowerCase());
    }

    public static String emptySection(String section) {
        return "The section '" + section + ":' cann't be null or empty";
    }

    public static String invalidBooleanValueMessage(String section, String attribute, String value) {
        return "The value '" + value + "' of '" + attribute + "' present under '" + section + "' should be boolean";
    }

    public 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;
        }

    }

    /**
     * Create Dict from list of Parameters.
     *
     * @param inputs
     *            list of parameters
     * @return map
     */
    public static Map<String, OnapCommandParameter> getInputMap(Set<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;
    }

    /**
     * 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 lineSpl
     * @return
     */
    public static String replaceLineForSpecialValues(String lineSpl) {
        return replaceLineForSpecialValues(lineSpl, new HashMap<String, String>());
    }

    /**
     *
     * @param lineSpl
     * @param values Value for the given entry already known by the caller.
     * @return
     */
    public static String replaceLineFromResults(String line, Map <String, String> values) {
        String resultLine = "";

        if (!line.contains("$r{")) {
            return line;
        }

        int currentIdx = 0;
        while (currentIdx < line.length()) {
            int idxS = line.indexOf("$r{", currentIdx);
            if (idxS == -1) {
                resultLine += line.substring(currentIdx);
                break;
            }
            int idxE = line.indexOf("}", idxS);
            String attr = line.substring(idxS + 3, idxE);
            attr = attr.trim();

            String value = "";

            if (values.get(attr) != null) {
                value = values.get(attr);
            } else {
                value = attr;
            }

            resultLine += line.substring(currentIdx, idxS) + value;
            currentIdx = idxE + 1;
        }

        return resultLine;
    }

    public static String replaceLineForSpecialValues(String lineSpl, Map <String, String> values) {
        String resultSpl = "";

        if (!lineSpl.contains("$s{")) {
            return lineSpl;
        }

        int currentIdx = 0;
        while (currentIdx < lineSpl.length()) {
            int idxS = lineSpl.indexOf("$s{", currentIdx);
            if (idxS == -1) {
                resultSpl += lineSpl.substring(currentIdx);
                break;
            }
            int idxE = lineSpl.indexOf("}", idxS);
            String splEntry = lineSpl.substring(idxS + 3, idxE);
            splEntry = splEntry.trim();

            String value = "";

            switch (splEntry) {
                case OnapCommandConstants.SPL_ENTRY_UUID:
                    value = UUID.randomUUID().toString();
                    break;

                default:

                    if (splEntry.startsWith(OnapCommandConstants.SPL_ENTRY_ENV)) {
                        //start to read after env:ENV_VAR_NAME
                        String envVarName = splEntry.substring(4);
                        value = System.getenv(envVarName); //NOSONAR
                        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 if (splEntry.startsWith(OnapCommandConstants.SPL_ENTRY_FILE)) {
                        //start to read after file:filepath
                        String fileName = splEntry.substring(5);
                        try {
                            value = FileUtils.readFileToString(new File(fileName));
                        } catch (IOException e) {
                            //when file is not found, assign the same file:FILE_PATH
                            //so that it will given hit to user that FILE_PATH to be
                            //exist.
                            value = "";
                        }
                    } else if (splEntry.startsWith(OnapCommandConstants.SPL_ENTRY_MD5)) {
                            //start to read after md5:entryname
                            String entryName = splEntry.substring(4);
                            String content = values.get(entryName);
                            if (content != null)
                                value = OnapCommandUtils.md5(content);
                            else
                                value = splEntry;
                    } else {
                        if (values.get(splEntry) != null) {
                            value = values.get(splEntry);
                        } else {
                            value = splEntry;
                        }
                    }
            }

            resultSpl += lineSpl.substring(currentIdx, idxS) + value;
            currentIdx = idxE + 1;
        }

        return resultSpl;
    }

    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);
            }

            OnapCommandParameter param = params.get(paramName);
            if (OnapCommandParameterType.ARRAY.equals(param.getParameterType())
                    || OnapCommandParameterType.JSON.equals(param.getParameterType())
                    || OnapCommandParameterType.YAML.equals(param.getParameterType())) {
                // ignore the front and back double quotes in json body
                String va_ = params.get(paramName).getValue().toString();
                if (idxS > 0)
                    result += line.substring(currentIdx, idxS - 1) + va_;
                else
                    result += va_;
                currentIdx = idxE + 2;
            } else if (OnapCommandParameterType.MAP.equals(param.getParameterType())) {
                try {
                    String value = gson.toJson(params.get(paramName).getValue());
                    if ((idxS == 0) && (currentIdx == 0)) {
                        result = value;
                    } else {
                        result += line.substring(currentIdx, idxS - 1) + value;
                    }
                } catch (Exception e) {  // NOSONAR
                    //never occur as map is coverted to json string here
                }

                currentIdx = idxE + 2;
            }else {
                result += line.substring(currentIdx, idxS) + params.get(paramName).getValue().toString();
                currentIdx = idxE + 1;
            }
        }

        return result;
    }

    /**
     * Populate result from input parameters.
     *
     * @param resultMap
     *            map
     * @param params
     *            Map<String, OnapCommandParameter>
     * @return map
     * @throws OnapCommandException
     */
    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) {  // NOSONAR
                    // pass
                }
                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 {
        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) {
        for (OnapCommandParameter param: from.getParameters()) {
            if (!to.getParametersMap().containsKey(param.getName())) {
                to.getParameters().add(param);
            }
        }
    }

    public static String md5(String content) {
        String md5 = DigestUtils.md5Hex(content); //NOSONAR

        byte[] encodeBase64 = Base64.encodeBase64(md5.getBytes());
        return new String(encodeBase64);
    }
}