[SDC-29] Amdocs OnBoard 1707 initial commit.
[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     DynamicConfiguration<List<K>> dynamicConfiguration = new DynamicConfiguration<>();
87     dynamicConfiguration.tenant = tenant;
88     dynamicConfiguration.namespace = namespace;
89     dynamicConfiguration.key = key;
90     dynamicConfiguration.clazz = getArrayClass(clazz);
91     dynamicConfiguration.defaultValue = Arrays.asList(defaultValue);
92     dynamicConfiguration.configuration = configuration;
93     return dynamicConfiguration;
94   }
95
96   /**
97    * Gets array class.
98    *
99    * @param clazz the clazz
100    * @return the array class
101    */
102   public static Class getArrayClass(Class clazz) {
103     switch (clazz.getName()) {
104       case "java.lang.Byte":
105         return Byte[].class;
106       case "java.lang.Short":
107         return Short[].class;
108       case "java.lang.Integer":
109         return Integer[].class;
110       case "java.lang.Long":
111         return Long[].class;
112       case "java.lang.Float":
113         return Float[].class;
114       case "java.lang.Double":
115         return Double[].class;
116       case "java.lang.Boolean":
117         return Boolean[].class;
118       case "java.lang.Character":
119         return Character[].class;
120       case "java.lang.String":
121         return String[].class;
122       default:
123     }
124     return null;
125   }
126
127   /**
128    * Get t.
129    *
130    * @return the t
131    */
132   public T get() {
133     Object toReturn = configuration
134         .get(tenant, namespace, key, clazz, Hint.LATEST_LOOKUP, Hint.EXTERNAL_LOOKUP,
135             Hint.NODE_SPECIFIC);
136     if (toReturn != null && toReturn.getClass().isArray()) {
137       toReturn = Arrays.asList((Object[]) toReturn);
138     }
139     return toReturn == null ? defaultValue : (T) toReturn;
140   }
141
142 }