CMD: Enhace command profile with additional macros
[cli.git] / framework / src / main / java / org / onap / cli / fw / cmd / OnapSchemaValidateCommand.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.cli.fw.cmd;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.onap.cli.fw.conf.OnapCommandConstants;
24 import org.onap.cli.fw.error.OnapCommandException;
25 import org.onap.cli.fw.input.OnapCommandParameter;
26 import org.onap.cli.fw.registrar.OnapCommandRegistrar;
27 import org.onap.cli.fw.schema.OnapCommandSchema;
28 import org.onap.cli.fw.schema.OnapCommandSchemaLoader;
29 import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
30
31 /**
32  * Validate schema command.
33  */
34 @OnapCommandSchema(schema = "schema-validate.yaml")
35 public class OnapSchemaValidateCommand extends OnapCommand {
36
37     @Override
38     protected void run() throws OnapCommandException {
39         Map<String, OnapCommandParameter> paramMap = getParametersMap();
40
41         OnapCommandParameter locationParam = paramMap.get("schema-location");
42         String location = String.valueOf(locationParam.getValue());
43
44         OnapCommandParameter interSchemaParam = paramMap.get("internal-schema");
45         boolean isInternalSchema = Boolean.parseBoolean(String.valueOf(interSchemaParam.getValue()));
46
47         if (isInternalSchema && location.startsWith("/")) {
48             location = location.substring(1);
49         }
50
51         OnapCommandParameter versionParam = paramMap.get("ocs-version");
52         String ocsVersion = String.valueOf(versionParam.getValue());
53
54         String type = OnapCommandDiscoveryUtils.identitySchemaProfileType(
55                 OnapCommandSchemaLoader.validateSchemaVersion(location, ocsVersion));
56
57         OnapCommand cmd = null;
58         if (type.equals(OnapCommandConstants.BASIC_SCHEMA_PROFILE)) {
59             cmd = new OnapCommand() {
60                 @Override
61                 protected void run() throws OnapCommandException {
62                     //This is used for enabling the validation, so no run implemented
63                 }
64             };
65         } else {
66             cmd = OnapCommandDiscoveryUtils.loadCommandClass(OnapCommandRegistrar.getRegistrar().getProfilePlugin(type));
67         }
68
69         List<String> error = cmd.initializeSchema(location, true);
70         List<String> slNumber = new ArrayList<>();
71         for (int i = 1; i <= error.size(); i++) {
72             slNumber.add(String.valueOf(i));
73         }
74         this.getResult().getRecordsMap().get("sl-no").setValues(slNumber);
75         this.getResult().getRecordsMap().get("error").setValues(error);
76     }
77
78 }