Removed JMX, other unused code from configuration
[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 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;
39
40 public final class CliConfigurationImpl extends ConfigurationImpl implements ConfigurationManager {
41
42     private static final List<String> KEYS_TO_FILTER = Arrays.asList(NAMESPACE_KEY, MODE_KEY, LOAD_ORDER_KEY);
43
44     public CliConfigurationImpl() throws Exception {
45         super();
46     }
47
48     public String getConfigurationValue(Map<String, Object> input) {
49         return getConfigurationValue((ConfigurationQuery) getInput(input));
50     }
51
52     private String getConfigurationValue(ConfigurationQuery queryData) {
53
54         try {
55
56             Hint[] hints = getHints(queryData);
57
58             String[] value;
59             if (queryData.isFallback()) {
60                 value = get(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class, hints);
61             } else {
62                 value = getInternal(queryData.getTenant(), queryData.getNamespace(), queryData.getKey(), String[].class,
63                                 hints);
64             }
65
66             return ConfigurationUtils.getCommaSeparatedList(value);
67
68         } catch (Exception exception) {
69             exception.printStackTrace();
70         }
71
72         return null;
73     }
74
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]);
81     }
82
83     private Object getInput(Map<String, Object> input) {
84
85         Object toReturn = null;
86
87         try {
88
89             toReturn = Class.forName(input.get("ImplClass").toString()).newInstance();
90
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()));
95                 }
96             }
97
98         } catch (Exception exception) {
99             exception.printStackTrace();
100         }
101
102         return toReturn;
103     }
104
105     public Map<String, String> listConfiguration(Map<String, Object> input) {
106         return listConfiguration((ConfigurationQuery) getInput(input));
107     }
108
109     private Map<String, String> listConfiguration(ConfigurationQuery query) {
110
111         Map<String, String> map = new HashMap<>();
112
113         try {
114
115             Collection<String> keys = getKeys(query.getTenant(), query.getNamespace());
116             for (String key : keys) {
117                 map.put(key, getConfigurationValue(query.key(key)));
118             }
119
120         } catch (Exception exception) {
121             exception.printStackTrace();
122             return null;
123         }
124
125         return map;
126     }
127
128     private ArrayList<String> getInMemoryKeys(String tenant, String namespace) {
129
130         ArrayList<String> keys = new ArrayList<>();
131
132         try {
133
134             Iterator<String> iter = ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace).getKeys();
135             while (iter.hasNext()) {
136
137                 String key = iter.next();
138                 if (!KEYS_TO_FILTER.contains(key)) {
139                     keys.add(key);
140                 }
141             }
142
143         } catch (Exception exception) {
144             //do nothing
145         }
146
147         return keys;
148     }
149
150     @Override
151     public Collection<String> getTenants() {
152         return ConfigurationRepository.lookup().getTenants();
153     }
154
155     @Override
156     public Collection<String> getNamespaces() {
157         return ConfigurationRepository.lookup().getNamespaces();
158     }
159
160     @Override
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;
168     }
169 }