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.MBEAN_NAME;
23 import static org.onap.config.Constants.MODE_KEY;
24 import static org.onap.config.Constants.NAMESPACE_KEY;
26 import java.lang.management.ManagementFactory;
27 import java.lang.reflect.Method;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.Iterator;
37 import javax.management.MBeanServer;
38 import javax.management.ObjectName;
39 import javax.management.StandardMBean;
40 import org.onap.config.ConfigurationUtils;
41 import org.onap.config.api.ConfigurationManager;
42 import org.onap.config.api.Hint;
43 import org.onap.config.type.ConfigurationQuery;
45 public final class CliConfigurationImpl extends ConfigurationImpl implements ConfigurationManager {
47 public CliConfigurationImpl() throws Exception {
48 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
49 ObjectName name = new ObjectName(MBEAN_NAME);
50 if (mbs.isRegistered(name)) {
51 mbs.unregisterMBean(name);
53 mbs.registerMBean(new StandardMBean(this, ConfigurationManager.class), name);
56 public String getConfigurationValue(Map<String, Object> input) {
57 return getConfigurationValue((ConfigurationQuery) getInput(input));
60 private String getConfigurationValue(ConfigurationQuery queryData) {
62 if (queryData.isFallback()) {
63 return ConfigurationUtils.getCommaSeparatedList(
64 get(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class,
65 queryData.isLatest() ? Hint.LATEST_LOOKUP : Hint.DEFAULT,
66 queryData.isExternalLookup() ? Hint.EXTERNAL_LOOKUP : Hint.DEFAULT,
67 queryData.isNodeSpecific() ? Hint.NODE_SPECIFIC : Hint.DEFAULT));
70 getInternal(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class,
71 queryData.isLatest() ? Hint.LATEST_LOOKUP : Hint.DEFAULT,
72 queryData.isExternalLookup() ? Hint.EXTERNAL_LOOKUP : Hint.DEFAULT,
73 queryData.isNodeSpecific() ? Hint.NODE_SPECIFIC : Hint.DEFAULT);
74 return ConfigurationUtils
75 .getCommaSeparatedList(list == null ? Collections.emptyList() : Arrays.asList(list));
77 } catch (Exception exception) {
78 exception.printStackTrace();
83 private Object getInput(Map<String, Object> input) {
85 Object toReturn = null;
88 toReturn = Class.forName(input.get("ImplClass").toString()).newInstance();
89 Method[] methods = toReturn.getClass().getMethods();
90 for (Method method : methods) {
91 if (input.containsKey(method.getName())) {
92 method.invoke(toReturn, input.get(method.getName()));
95 } catch (Exception exception) {
96 exception.printStackTrace();
102 public Map<String, String> listConfiguration(Map<String, Object> input) {
103 return listConfiguration((ConfigurationQuery) getInput(input));
106 private Map<String, String> listConfiguration(ConfigurationQuery query) {
107 Map<String, String> map = new HashMap<>();
109 Collection<String> keys = getKeys(query.getTenant(), query.getNamespace());
110 for (String key : keys) {
111 map.put(key, getConfigurationValue(query.key(key)));
113 } catch (Exception exception) {
114 exception.printStackTrace();
120 private ArrayList<String> getInMemoryKeys(String tenant, String namespace) {
122 ArrayList<String> keys = new ArrayList<>();
125 Iterator<String> iter = ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace).getKeys();
126 while (iter.hasNext()) {
127 String key = iter.next();
128 if (!(key.equals(NAMESPACE_KEY) || key.equals(MODE_KEY) || key.equals(LOAD_ORDER_KEY))) {
132 } catch (Exception exception) {
140 public Collection<String> getTenants() {
141 return ConfigurationRepository.lookup().getTenants();
145 public Collection<String> getNamespaces() {
146 return ConfigurationRepository.lookup().getNamespaces();
150 public Collection<String> getKeys(String tenant, String namespace) {
151 Set<String> keyCollection = new HashSet<>();
152 keyCollection.addAll(getInMemoryKeys(tenant, DEFAULT_NAMESPACE));
153 keyCollection.addAll(getInMemoryKeys(tenant, namespace));
154 keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, namespace));
155 keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, DEFAULT_NAMESPACE));
156 return keyCollection;