3fcbd4f1c4af80306cd6f1c4d7f8f58d2f17b078
[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
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             return toReturn;
89         }
90         try {
91             toReturn = Class.forName(input.get("ImplClass").toString()).newInstance();
92             Method[] methods = toReturn.getClass().getMethods();
93             for (Method method : methods) {
94                 if (input.containsKey(method.getName())) {
95                     method.invoke(toReturn, input.get(method.getName()));
96                 }
97             }
98         } catch (Exception exception) {
99             LOGGER.warn("Error occurred while processing input: {}", input, exception);
100         }
101         return toReturn;
102     }
103
104     public Map<String, String> listConfiguration(Map<String, Object> input) {
105         return listConfiguration((ConfigurationQuery) getInput(input));
106     }
107
108     private Map<String, String> listConfiguration(ConfigurationQuery query) {
109         Map<String, String> map = new HashMap<>();
110         if (query != null) {
111             try {
112                 Collection<String> keys = getKeys(query.getTenant(), query.getNamespace());
113                 for (String key : keys) {
114                     map.put(key, getConfigurationValue(query.key(key)));
115                 }
116             } catch (Exception exception) {
117                 LOGGER.warn("Error occurred while processing query: {}", query, exception);
118             }
119         }
120         return map;
121     }
122
123     private ArrayList<String> getInMemoryKeys(String tenant, String namespace) {
124         ArrayList<String> keys = new ArrayList<>();
125         try {
126             Iterator<String> iter = ConfigurationRepository.lookup().getConfigurationFor(tenant, namespace).getKeys();
127             while (iter.hasNext()) {
128                 String key = iter.next();
129                 if (!KEYS_TO_FILTER.contains(key)) {
130                     keys.add(key);
131                 }
132             }
133         } catch (Exception exception) {
134             LOGGER.warn("Error occurred while searching for in-memory keys for namespace: '{}' and tenant: '{}'",
135                     namespace,
136                     tenant,
137                     exception
138             );
139         }
140         return keys;
141     }
142
143     @Override
144     public Collection<String> getTenants() {
145         return ConfigurationRepository.lookup().getTenants();
146     }
147
148     @Override
149     public Collection<String> getNamespaces() {
150         return ConfigurationRepository.lookup().getNamespaces();
151     }
152
153     @Override
154     public Collection<String> getKeys(String tenant, String namespace) {
155         Set<String> keyCollection = new HashSet<>();
156         keyCollection.addAll(getInMemoryKeys(tenant, DEFAULT_NAMESPACE));
157         keyCollection.addAll(getInMemoryKeys(tenant, namespace));
158         keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, namespace));
159         keyCollection.addAll(getInMemoryKeys(DEFAULT_TENANT, DEFAULT_NAMESPACE));
160         return keyCollection;
161     }
162 }