Code improvement for pending sonar issues
[cli.git] / framework / src / main / java / org / onap / cli / fw / cmd / product / OnapServiceListCommand.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.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.onap.cli.fw.cmd.OnapCommand;
27 import org.onap.cli.fw.conf.OnapCommandConstants;
28 import org.onap.cli.fw.error.OnapCommandException;
29 import org.onap.cli.fw.schema.OnapCommandSchema;
30 import org.onap.cli.fw.schema.OnapCommandSchemaInfo;
31 import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;
32
33 /**
34  * Service list.
35  *
36  */
37 @OnapCommandSchema(schema = "service-list.yaml")
38 public class OnapServiceListCommand extends OnapCommand {
39
40     @Override
41     protected void run() throws OnapCommandException {
42
43         String product = getParametersMap().get("product").getValue().toString();
44
45         List<OnapCommandSchemaInfo> schemas = OnapCommandDiscoveryUtils.discoverOrLoadSchemas(false);
46         Map<String, Set<String>> rslt = new HashMap<>();
47
48         for (OnapCommandSchemaInfo schema :  schemas) {
49             if (schema.isIgnore()) {
50                 continue;
51             }
52
53             if (!rslt.containsKey(schema.getProduct())) {
54                 rslt.put(schema.getProduct(), new HashSet<>());
55             }
56             rslt.get(schema.getProduct()).add(schema.getService());
57         }
58
59         InputStream stream = this.getClass().getResourceAsStream("/" +OnapCommandConstants.SCHEMA_DIRECTORY +
60                 "/" + product + OnapCommandConstants.PRODUCT_REGISTRY_YAML);
61
62         Map<String, String> serviceDescs = new HashMap<>();
63         if (stream != null) {
64             Map<String, ?> map = OnapCommandDiscoveryUtils.loadYaml(stream);
65             if (map.containsKey("services")) {
66                 List<Map<String, String>> services = (List) map.get("services");
67
68                 for (Map<String, String> service: services ) {
69                     serviceDescs.put(service.get("name"), service.get("description"));
70                 }
71             }
72         }
73
74         for (String service : rslt.getOrDefault(product, new HashSet<>())) {
75             this.getResult().getRecordsMap().get("product").getValues().add(product);
76             this.getResult().getRecordsMap().get("service").getValues().add(service);
77             this.getResult().getRecordsMap().get("description").getValues().add(
78                     serviceDescs.containsKey(service) ? serviceDescs.get(service) : "");
79         }
80     }
81 }