ActivitySpec - Correcting logger messages
[sdc.git] / common / openecomp-common-configuration-management / openecomp-configuration-management-api / src / main / java / org / openecomp / config / api / DynamicConfiguration.java
1 package org.openecomp.config.api;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 /**
7  * The type Dynamic configuration.
8  *
9  * @param <T> the type parameter
10  */
11 public class DynamicConfiguration<T> {
12
13   /**
14    * The Tenant.
15    */
16   String tenant;
17   /**
18    * The Namespace.
19    */
20   String namespace;
21   /**
22    * The Key.
23    */
24   String key;
25   /**
26    * The Configuration.
27    */
28   Configuration configuration;
29   /**
30    * The Clazz.
31    */
32   Class clazz;
33   /**
34    * The Default value.
35    */
36   T defaultValue;
37
38   /**
39    * Gets dynamic configuration.
40    *
41    * @param <T>           the type parameter
42    * @param tenant        the tenant
43    * @param namespace     the namespace
44    * @param key           the key
45    * @param clazz         the clazz
46    * @param defaultValue  the default value
47    * @param configuration the configuration
48    * @return the dynamic configuration
49    */
50   public static <T> DynamicConfiguration<T> getDynamicConfiguration(String tenant, String namespace,
51                                                                     String key, Class<T> clazz,
52                                                                     T defaultValue,
53                                                                     Configuration configuration) {
54     DynamicConfiguration<T> dynamicConfiguration = new DynamicConfiguration<>();
55     dynamicConfiguration.tenant = tenant;
56     dynamicConfiguration.namespace = namespace;
57     dynamicConfiguration.key = key;
58     dynamicConfiguration.clazz = clazz;
59     dynamicConfiguration.defaultValue = defaultValue;
60     dynamicConfiguration.configuration = configuration;
61     return dynamicConfiguration;
62   }
63
64   /**
65    * Gets dyn configuration.
66    *
67    * @param <K>           the type parameter
68    * @param tenant        the tenant
69    * @param namespace     the namespace
70    * @param key           the key
71    * @param clazz         the clazz
72    * @param defaultValue  the default value
73    * @param configuration the configuration
74    * @return the dyn configuration
75    */
76   public static <K> DynamicConfiguration<List<K>> getDynConfiguration(String tenant,
77                                                                       String namespace, String key,
78                                                                       Class<K> clazz,
79                                                                       K defaultValue,
80                                                                       Configuration configuration) {
81     if (clazz.isPrimitive()) {
82       throw new RuntimeException(
83           "Only Wrapper classes like Integer, Long, Double, "
84               + "Boolean etc including String are supported.");
85     }
86     return getDynamicConfiguration(tenant, namespace, key, getArrayClass(clazz),
87         Arrays.asList(defaultValue), configuration);
88   }
89
90   /**
91    * Gets array class.
92    *
93    * @param clazz the clazz
94    * @return the array class
95    */
96   public static Class getArrayClass(Class clazz) {
97     Class arrayClass = null;
98     switch (clazz.getName()) {
99       case "java.lang.Byte":
100         arrayClass = Byte[].class;
101         break;
102       case "java.lang.Short":
103         arrayClass = Short[].class;
104         break;
105       case "java.lang.Integer":
106         arrayClass = Integer[].class;
107         break;
108       case "java.lang.Long":
109         arrayClass = Long[].class;
110         break;
111       case "java.lang.Float":
112         arrayClass = Float[].class;
113         break;
114       case "java.lang.Double":
115         arrayClass = Double[].class;
116         break;
117       case "java.lang.Boolean":
118         arrayClass = Boolean[].class;
119         break;
120       case "java.lang.Character":
121         arrayClass = Character[].class;
122         break;
123       case "java.lang.String":
124         arrayClass = String[].class;
125         break;
126       default:
127     }
128     return arrayClass;
129   }
130
131   /**
132    * Get t.
133    *
134    * @return the t
135    */
136   public T get() {
137     Object toReturn = configuration
138         .get(tenant, namespace, key, clazz, Hint.LATEST_LOOKUP, Hint.EXTERNAL_LOOKUP,
139             Hint.NODE_SPECIFIC);
140     if (toReturn != null && toReturn.getClass().isArray()) {
141       toReturn = Arrays.asList((Object[]) toReturn);
142     }
143     return toReturn == null ? defaultValue : (T) toReturn;
144   }
145
146 }