CVC: Add support for execution, schema, product
[cli.git] / framework / src / main / java / org / onap / cli / fw / cmd / product / OnapProductsListCommand.java
1 /*
2  * Copyright 2018 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.product;
18
19 import java.io.InputStream;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.onap.cli.fw.cmd.OnapCommand;
26 import org.onap.cli.fw.conf.OnapCommandConstants;
27 import org.onap.cli.fw.error.OnapCommandException;
28 import org.onap.cli.fw.schema.OnapCommandSchema;
29 import org.onap.cli.fw.schema.OnapCommandSchemaInfo;
30 import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
31 import org.yaml.snakeyaml.Yaml;
32
33 /**
34  * Product list.
35  *
36  */
37 @OnapCommandSchema(schema = "product-list.yaml")
38 public class OnapProductsListCommand extends OnapCommand {
39
40     @Override
41     protected void run() throws OnapCommandException {
42         List<OnapCommandSchemaInfo> schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(false);
43
44         Set<String> rslt = new HashSet<>();
45
46         for (OnapCommandSchemaInfo schema :  schemas) {
47             if (schema.isIgnore()) {
48                 continue;
49             }
50
51                rslt.add(schema.getProduct());
52         }
53
54         for (String product : rslt) {
55             this.getResult().getRecordsMap().get("product").getValues().add(product);
56
57             InputStream stream = this.getClass().getResourceAsStream("/" + OnapCommandConstants.SCHEMA_DIRECTORY +
58                     "/" + product + OnapCommandConstants.PRODUCT_REGISTRY_YAML);
59
60             if (stream != null) {
61                 Map<String, ?> map = (Map<String, ?>) new Yaml().load(stream);
62                 Map<String, String> productMap = (Map<String, String>) map.get("product");
63                 String description = (String) productMap.get(OnapCommandConstants.DESCRIPTION);
64                 this.getResult().getRecordsMap().get("description").getValues().add(description.trim());
65             } else {
66                 this.getResult().getRecordsMap().get("description").getValues().add("");
67             }
68         }
69     }
70 }