f919e234f07bb1cbf11602e69c87e0935218358c
[sdc.git] /
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 lombok.NoArgsConstructor;
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 @NoArgsConstructor
44 public final class CliConfigurationImpl extends ConfigurationImpl implements ConfigurationManager {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(CliConfigurationImpl.class);
47     private static final List<String> KEYS_TO_FILTER = Arrays.asList(NAMESPACE_KEY, MODE_KEY, LOAD_ORDER_KEY);
48
49     @Override
50     public String getConfigurationValue(Map<String, Object> input) {
51         return getConfigurationValue((ConfigurationQuery) getInput(input));
52     }
53
54     private String getConfigurationValue(ConfigurationQuery queryData) {
55         if (queryData != null) {
56             try {
57                 Hint[] hints = getHints(queryData);
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                 return ConfigurationUtils.getCommaSeparatedList(value);
66             } catch (Exception exception) {
67                 LOGGER.warn("Error occurred while processing query: {}", queryData, exception);
68             }
69         }
70         return null;
71     }
72
73     private Hint[] getHints(ConfigurationQuery query) {
74         List<Hint> hints = new ArrayList<>(Hint.values().length);
75         if (query != null) {
76             hints.add(query.isLatest() ? Hint.LATEST_LOOKUP : Hint.DEFAULT);
77             hints.add(query.isExternalLookup() ? Hint.EXTERNAL_LOOKUP : Hint.DEFAULT);
78             hints.add(query.isNodeSpecific() ? Hint.NODE_SPECIFIC : Hint.DEFAULT);
79         }
80         return hints.toArray(new Hint[0]);
81     }
82
83     private Object getInput(Map<String, Object> input) {
84         Object toReturn = null;
85         if (input == null) {
86             return toReturn;
87         }
88         try {
89             toReturn = Class.forName(input.get("ImplClass").toString()).getDeclaredConstructor().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         return toReturn;
100     }
101
102     @Override
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 }