2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.config.impl;
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;
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;
35 import org.onap.config.ConfigurationUtils;
36 import org.onap.config.api.ConfigurationManager;
37 import org.onap.config.api.Hint;
38 import org.onap.config.type.ConfigurationQuery;
40 public final class CliConfigurationImpl extends ConfigurationImpl implements ConfigurationManager {
42 private static final List<String> KEYS_TO_FILTER = Arrays.asList(NAMESPACE_KEY, MODE_KEY, LOAD_ORDER_KEY);
44 public CliConfigurationImpl() throws Exception {
48 public String getConfigurationValue(Map<String, Object> input) {
49 return getConfigurationValue((ConfigurationQuery) getInput(input));
52 private String getConfigurationValue(ConfigurationQuery queryData) {
56 Hint[] hints = getHints(queryData);
59 if (queryData.isFallback()) {
60 value = get(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, hints);
62 value = getInternal(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class,
66 return ConfigurationUtils.getCommaSeparatedList(value);
68 } catch (Exception exception) {
69 exception.printStackTrace();
75 private Hint[] getHints(ConfigurationQuery query) {
76 List<Hint> hints = new ArrayList<>(Hint.values().length);
77 hints.add(query.isLatest() ? Hint.LATEST_LOOKUP : Hint.DEFAULT);
78 hints.add(query.isExternalLookup() ? Hint.EXTERNAL_LOOKUP : Hint.DEFAULT);
79 hints.add(query.isNodeSpecific() ? Hint.NODE_SPECIFIC : Hint.DEFAULT);
80 return hints.toArray(new Hint[0]);
83 private Object getInput(Map<String, Object> input) {
85 Object toReturn = null;
89 toReturn = Class.forName(input.get("ImplClass").toString()).newInstance();
91 Method[] methods = toReturn.getClass().getMethods();
92 for (Method method : methods) {
93 if (input.containsKey(method.getName())) {
94 method.invoke(toReturn, input.get(method.getName()));
98 } catch (Exception exception) {
99 exception.printStackTrace();
105 public Map<String, String> listConfiguration(Map<String, Object> input) {
106 return listConfiguration((ConfigurationQuery) getInput(input));
109 private Map<String, String> listConfiguration(ConfigurationQuery query) {
111 Map<String, String> map = new HashMap<>();
115 Collection<String> keys = getKeys(query.getTenant(), query.getNamespace());
116 for (String key : keys) {
117 map.put(key, getConfigurationValue(query.key(key)));
120 } catch (Exception exception) {
121 exception.printStackTrace();
128 private ArrayList<String> getInMemoryKeys(String tenant, String namespace) {
130 ArrayList<String> keys = new ArrayList<>();
134 Iterator<String> iter = ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace).getKeys();
135 while (iter.hasNext()) {
137 String key = iter.next();
138 if (!KEYS_TO_FILTER.contains(key)) {
143 } catch (Exception exception) {
151 public Collection<String> getTenants() {
152 return ConfigurationRepository.lookup().getTenants();
156 public Collection<String> getNamespaces() {
157 return ConfigurationRepository.lookup().getNamespaces();
161 public Collection<String> getKeys(String tenant, String namespace) {
162 Set<String> keyCollection = new HashSet<>();
163 keyCollection.addAll(getInMemoryKeys(tenant, DEFAULT_NAMESPACE));
164 keyCollection.addAll(getInMemoryKeys(tenant, namespace));
165 keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, namespace));
166 keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, DEFAULT_NAMESPACE));
167 return keyCollection;