c2973a493fa2968749613f3469be90a9d4af0c12
[sdc.git] / common / onap-common-configuration-management / onap-configuration-management-api / src / main / java / org / onap / config / api / DynamicConfiguration.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.api;
18
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
22
23
24 public class DynamicConfiguration<T> {
25
26     private String tenant;
27
28     private String namespace;
29
30     private String key;
31
32     private Configuration configuration;
33
34     private Class clazz;
35
36     private T defaultValue;
37
38     public static <K> DynamicConfiguration<List<K>> getDynConfiguration(String tenant, String namespace, String key,
39             Class<K> clazz, K defaultValue, Configuration configuration) {
40         if (clazz.isPrimitive()) {
41             throw new RuntimeException("Only Wrapper classes like Integer, Long, Double, "
42                                                + "Boolean etc including String are supported.");
43         }
44         return getDynamicConfiguration(tenant, namespace, key, getArrayClass(clazz),
45                 Collections.singletonList(defaultValue), configuration);
46     }
47
48     public static <T> DynamicConfiguration<T> getDynamicConfiguration(String tenant, String namespace, String key,
49             Class<T> clazz, T defaultValue, Configuration configuration) {
50         DynamicConfiguration<T> dynamicConfiguration = new DynamicConfiguration<>();
51         dynamicConfiguration.tenant = tenant;
52         dynamicConfiguration.namespace = namespace;
53         dynamicConfiguration.key = key;
54         dynamicConfiguration.clazz = clazz;
55         dynamicConfiguration.defaultValue = defaultValue;
56         dynamicConfiguration.configuration = configuration;
57         return dynamicConfiguration;
58     }
59
60     public static Class getArrayClass(Class clazz) {
61         Class arrayClass = null;
62         switch (clazz.getName()) {
63             case "java.lang.Byte":
64                 arrayClass = Byte[].class;
65                 break;
66             case "java.lang.Short":
67                 arrayClass = Short[].class;
68                 break;
69             case "java.lang.Integer":
70                 arrayClass = Integer[].class;
71                 break;
72             case "java.lang.Long":
73                 arrayClass = Long[].class;
74                 break;
75             case "java.lang.Float":
76                 arrayClass = Float[].class;
77                 break;
78             case "java.lang.Double":
79                 arrayClass = Double[].class;
80                 break;
81             case "java.lang.Boolean":
82                 arrayClass = Boolean[].class;
83                 break;
84             case "java.lang.Character":
85                 arrayClass = Character[].class;
86                 break;
87             case "java.lang.String":
88                 arrayClass = String[].class;
89                 break;
90             default:
91         }
92         return arrayClass;
93     }
94
95     public T get() {
96         Object toReturn = configuration.get(tenant, namespace, key, clazz, Hint.LATEST_LOOKUP, Hint.EXTERNAL_LOOKUP,
97                 Hint.NODE_SPECIFIC);
98         if (toReturn != null && toReturn.getClass().isArray()) {
99             toReturn = Arrays.asList((Object[]) toReturn);
100         }
101         return toReturn == null ? defaultValue : (T) toReturn;
102     }
103
104 }