Fix unprocessed NPE
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-core / src / main / java / org / onap / config / impl / CliConfigurationImpl.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
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.config.impl;
18
19 import static org.onap.config.Constants.DEFAULT_NAMESPACE;
20 import static org.onap.config.Constants.DEFAULT_TENANT;
21 import static org.onap.config.Constants.LOAD_ORDER_KEY;
22 import static org.onap.config.Constants.MODE_KEY;
23 import static org.onap.config.Constants.NAMESPACE_KEY;
24
25 import java.lang.reflect.Method;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35
36 import org.onap.config.ConfigurationUtils;
37 import org.onap.config.api.ConfigurationManager;
38 import org.onap.config.api.Hint;
39 import org.onap.config.type.ConfigurationQuery;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public final class CliConfigurationImpl extends ConfigurationImpl implements ConfigurationManager {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(CliConfigurationImpl.class);
46     private static final List<String> KEYS_TO_FILTER = Arrays.asList(NAMESPACE_KEY, MODE_KEY, LOAD_ORDER_KEY);
47
48     public CliConfigurationImpl() {
49         super();
50     }
51
52     public String getConfigurationValue(Map<String, Object> input) {
53         return getConfigurationValue((ConfigurationQuery) getInput(input));
54     }
55
56     private String getConfigurationValue(ConfigurationQuery queryData) {
57         if (queryData != null) {
58             try {
59                 Hint[] hints = getHints(queryData);
60                 String[] value;
61                 if (queryData.isFallback()) {
62                     value = get(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, hints);
63                 } else {
64                     value = getInternal(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class,
65                             hints);
66                 }
67                 return ConfigurationUtils.getCommaSeparatedList(value);
68             } catch (Exception exception) {
69                 LOGGER.warn("Error occurred while processing query: {}", queryData, exception);
70             }
71         }
72         return null;
73     }
74
75     private Hint[] getHints(ConfigurationQuery query) {
76         List<Hint> hints = new ArrayList<>(Hint.values().length);
77         if (query != null) {
78             hints.add(query.isLatest() ? Hint.LATEST_LOOKUP : Hint.DEFAULT);
79             hints.add(query.isExternalLookup() ? Hint.EXTERNAL_LOOKUP : Hint.DEFAULT);
80             hints.add(query.isNodeSpecific() ? Hint.NODE_SPECIFIC : Hint.DEFAULT);
81         }
82         return hints.toArray(new Hint[0]);
83     }
84
85     private Object getInput(Map<String, Object> input) {
86         Object toReturn = null;
87         if (input != null) {
88             try {
89                 toReturn = Class.forName(input.get("ImplClass").toString()).newInstance();
90                 Method[] methods = toReturn.getClass().getMethods();
91                 for (Method method : methods) {
92                     if (input.containsKey(method.getName())) {
93                         method.invoke(toReturn, input.get(method.getName()));
94                     }
95                 }
96             } catch (Exception exception) {
97                 LOGGER.warn("Error occurred while processing input: {}", input, exception);
98             }
99         }
100         return toReturn;
101     }
102
103     public Map<String, String> listConfiguration(Map<String, Object> input) {
104         return listConfiguration((ConfigurationQuery) getInput(input));
105     }
106
107     private Map<String, String> listConfiguration(ConfigurationQuery query) {
108         Map<String, String> map = new HashMap<>();
109         if (query != null) {
110             try {
111                 Collection<String> keys = getKeys(query.getTenant(), query.getNamespace());
112                 for (String key : keys) {
113                     map.put(key, getConfigurationValue(query.key(key)));
114                 }
115             } catch (Exception exception) {
116                 LOGGER.warn("Error occurred while processing query: {}", query, exception);
117             }
118         }
119         return map;
120     }
121
122     private ArrayList<String> getInMemoryKeys(String tenant, String namespace) {
123         ArrayList<String> keys = new ArrayList<>();
124         try {
125             Iterator<String> iter = ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace).getKeys();
126             while (iter.hasNext()) {
127                 String key = iter.next();
128                 if (!KEYS_TO_FILTER.contains(key)) {
129                     keys.add(key);
130                 }
131             }
132         } catch (Exception exception) {
133             LOGGER.warn("Error occurred while searching for in-memory keys for namespace: '{}' and tenant: '{}'",
134                     namespace,
135                     tenant,
136                     exception
137             );
138         }
139         return keys;
140     }
141
142     @Override
143     public Collection<String> getTenants() {
144         return ConfigurationRepository.lookup().getTenants();
145     }
146
147     @Override
148     public Collection<String> getNamespaces() {
149         return ConfigurationRepository.lookup().getNamespaces();
150     }
151
152     @Override
153     public Collection<String> getKeys(String tenant, String namespace) {
154         Set<String> keyCollection = new HashSet<>();
155         keyCollection.addAll(getInMemoryKeys(tenant, DEFAULT_NAMESPACE));
156         keyCollection.addAll(getInMemoryKeys(tenant, namespace));
157         keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, namespace));
158         keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, DEFAULT_NAMESPACE));
159         return keyCollection;
160     }
161 }