aboutsummaryrefslogtreecommitdiffstats
path: root/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/DynamicConfiguration.java
blob: 428d9ee2334a862fe8f09506108d8d7ca5a5d3e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package org.openecomp.config.api;

import java.util.Arrays;
import java.util.List;

/**
 * The type Dynamic configuration.
 *
 * @param <T> the type parameter
 */
public class DynamicConfiguration<T> {

  /**
   * The Tenant.
   */
  String tenant;
  /**
   * The Namespace.
   */
  String namespace;
  /**
   * The Key.
   */
  String key;
  /**
   * The Configuration.
   */
  Configuration configuration;
  /**
   * The Clazz.
   */
  Class clazz;
  /**
   * The Default value.
   */
  T defaultValue;

  /**
   * Gets dynamic configuration.
   *
   * @param <T>           the type parameter
   * @param tenant        the tenant
   * @param namespace     the namespace
   * @param key           the key
   * @param clazz         the clazz
   * @param defaultValue  the default value
   * @param configuration the configuration
   * @return the dynamic configuration
   */
  public static <T> DynamicConfiguration<T> getDynamicConfiguration(String tenant, String namespace,
                                                                    String key, Class<T> clazz,
                                                                    T defaultValue,
                                                                    Configuration configuration) {
    DynamicConfiguration<T> dynamicConfiguration = new DynamicConfiguration<>();
    dynamicConfiguration.tenant = tenant;
    dynamicConfiguration.namespace = namespace;
    dynamicConfiguration.key = key;
    dynamicConfiguration.clazz = clazz;
    dynamicConfiguration.defaultValue = defaultValue;
    dynamicConfiguration.configuration = configuration;
    return dynamicConfiguration;
  }

  /**
   * Gets dyn configuration.
   *
   * @param <K>           the type parameter
   * @param tenant        the tenant
   * @param namespace     the namespace
   * @param key           the key
   * @param clazz         the clazz
   * @param defaultValue  the default value
   * @param configuration the configuration
   * @return the dyn configuration
   */
  public static <K> DynamicConfiguration<List<K>> getDynConfiguration(String tenant,
                                                                      String namespace, String key,
                                                                      Class<K> clazz,
                                                                      K defaultValue,
                                                                      Configuration configuration) {
    if (clazz.isPrimitive()) {
      throw new RuntimeException(
          "Only Wrapper classes like Integer, Long, Double, "
              + "Boolean etc including String are supported.");
    }
    DynamicConfiguration<List<K>> dynamicConfiguration = new DynamicConfiguration<>();
    dynamicConfiguration.tenant = tenant;
    dynamicConfiguration.namespace = namespace;
    dynamicConfiguration.key = key;
    dynamicConfiguration.clazz = getArrayClass(clazz);
    dynamicConfiguration.defaultValue = Arrays.asList(defaultValue);
    dynamicConfiguration.configuration = configuration;
    return dynamicConfiguration;
  }

  /**
   * Gets array class.
   *
   * @param clazz the clazz
   * @return the array class
   */
  public static Class getArrayClass(Class clazz) {
    switch (clazz.getName()) {
      case "java.lang.Byte":
        return Byte[].class;
      case "java.lang.Short":
        return Short[].class;
      case "java.lang.Integer":
        return Integer[].class;
      case "java.lang.Long":
        return Long[].class;
      case "java.lang.Float":
        return Float[].class;
      case "java.lang.Double":
        return Double[].class;
      case "java.lang.Boolean":
        return Boolean[].class;
      case "java.lang.Character":
        return Character[].class;
      case "java.lang.String":
        return String[].class;
      default:
    }
    return null;
  }

  /**
   * Get t.
   *
   * @return the t
   */
  public T get() {
    Object toReturn = configuration
        .get(tenant, namespace, key, clazz, Hint.LATEST_LOOKUP, Hint.EXTERNAL_LOOKUP,
            Hint.NODE_SPECIFIC);
    if (toReturn != null && toReturn.getClass().isArray()) {
      toReturn = Arrays.asList((Object[]) toReturn);
    }
    return toReturn == null ? defaultValue : (T) toReturn;
  }

}