From 280f8015d06af1f41a3ef12e8300801c7a5e0d54 Mon Sep 17 00:00:00 2001 From: AviZi Date: Fri, 9 Jun 2017 02:39:56 +0300 Subject: [SDC-29] Amdocs OnBoard 1707 initial commit. Change-Id: Ie4d12a3f574008b792899b368a0902a8b46b5370 Signed-off-by: AviZi --- .../main/java/org/openecomp/config/api/Config.java | 21 + .../org/openecomp/config/api/Configuration.java | 831 +++++++++++++++++++++ .../config/api/ConfigurationChangeListener.java | 15 + .../openecomp/config/api/ConfigurationManager.java | 98 +++ .../openecomp/config/api/DynamicConfiguration.java | 142 ++++ .../main/java/org/openecomp/config/api/Hint.java | 18 + 6 files changed, 1125 insertions(+) create mode 100644 common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Config.java create mode 100644 common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Configuration.java create mode 100644 common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/ConfigurationChangeListener.java create mode 100644 common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/ConfigurationManager.java create mode 100644 common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/DynamicConfiguration.java create mode 100644 common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Hint.java (limited to 'common/openecomp-common-configuration-management/openecomp-configuration-management-api/src') diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Config.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Config.java new file mode 100644 index 0000000000..2859ce639b --- /dev/null +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Config.java @@ -0,0 +1,21 @@ +package org.openecomp.config.api; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The interface Config. + */ +@Target({ElementType.TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Config { + + /** + * Key string. + * + * @return the string + */ + String key() default ""; +} diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Configuration.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Configuration.java new file mode 100644 index 0000000000..4d82dad965 --- /dev/null +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Configuration.java @@ -0,0 +1,831 @@ +package org.openecomp.config.api; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * The interface Configuration. + */ +public interface Configuration { + /** + * The constant tenant. + */ + public static ThreadLocal tenant = new ThreadLocal(); + + /** + * Sets tenant id. + * + * @param id the id + */ + public static void setTenantId(String id) { + if (id != null && id.trim().length() > 0) { + tenant.set(id); + } + } + + /** + * Gets as string. + * + * @param key the key + * @return the as string + */ + public default String getAsString(String key) { + return getAsString(null, key); + } + + /** + * Gets as string. + * + * @param namespace the namespace + * @param key the key + * @return the as string + */ + public default String getAsString(String namespace, String key) { + return getAsString(tenant.get(), namespace, key); + } + + /** + * Gets as string. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as string + */ + public default String getAsString(String tenantId, String namespace, String key) { + return get(tenantId, namespace, key, String.class); + } + + /** + * Gets as byte value. + * + * @param key the key + * @return the as byte value + */ + public default Byte getAsByteValue(String key) { + return getAsByteValue(null, key); + } + + /** + * Gets as byte value. + * + * @param namespace the namespace + * @param key the key + * @return the as byte value + */ + public default Byte getAsByteValue(String namespace, String key) { + return getAsByteValue(tenant.get(), namespace, key); + } + + /** + * Gets as byte value. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as byte value + */ + public default Byte getAsByteValue(String tenantId, String namespace, String key) { + return get(tenantId, namespace, key, Byte.class); + } + + /** + * Gets as short value. + * + * @param key the key + * @return the as short value + */ + public default Short getAsShortValue(String key) { + return getAsShortValue(null, key); + } + + /** + * Gets as short value. + * + * @param namespace the namespace + * @param key the key + * @return the as short value + */ + public default Short getAsShortValue(String namespace, String key) { + return getAsShortValue(tenant.get(), namespace, key); + } + + /** + * Gets as short value. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as short value + */ + public default Short getAsShortValue(String tenantId, String namespace, String key) { + return get(tenantId, namespace, key, Short.class); + } + + /** + * Gets as integer value. + * + * @param key the key + * @return the as integer value + */ + public default Integer getAsIntegerValue(String key) { + return getAsIntegerValue(null, key); + } + + /** + * Gets as integer value. + * + * @param namespace the namespace + * @param key the key + * @return the as integer value + */ + public default Integer getAsIntegerValue(String namespace, String key) { + return getAsIntegerValue(tenant.get(), namespace, key); + } + + /** + * Gets as integer value. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as integer value + */ + public default Integer getAsIntegerValue(String tenantId, String namespace, String key) { + return get(tenantId, namespace, key, Integer.class); + } + + /** + * Gets as long value. + * + * @param key the key + * @return the as long value + */ + public default Long getAsLongValue(String key) { + return getAsLongValue(null, key); + } + + /** + * Gets as long value. + * + * @param namespace the namespace + * @param key the key + * @return the as long value + */ + public default Long getAsLongValue(String namespace, String key) { + return getAsLongValue(tenant.get(), namespace, key); + } + + /** + * Gets as long value. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as long value + */ + public default Long getAsLongValue(String tenantId, String namespace, String key) { + return get(tenantId, namespace, key, Long.class); + } + + /** + * Gets as float value. + * + * @param key the key + * @return the as float value + */ + public default Float getAsFloatValue(String key) { + return getAsFloatValue(null, key); + } + + /** + * Gets as float value. + * + * @param namespace the namespace + * @param key the key + * @return the as float value + */ + public default Float getAsFloatValue(String namespace, String key) { + return getAsFloatValue(tenant.get(), namespace, key); + } + + /** + * Gets as float value. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as float value + */ + public default Float getAsFloatValue(String tenantId, String namespace, String key) { + return get(tenantId, namespace, key, Float.class); + } + + /** + * Gets as double value. + * + * @param key the key + * @return the as double value + */ + public default Double getAsDoubleValue(String key) { + return getAsDoubleValue(null, key); + } + + /** + * Gets as double value. + * + * @param namespace the namespace + * @param key the key + * @return the as double value + */ + public default Double getAsDoubleValue(String namespace, String key) { + return getAsDoubleValue(tenant.get(), namespace, key); + } + + /** + * Gets as double value. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as double value + */ + public default Double getAsDoubleValue(String tenantId, String namespace, String key) { + return get(tenantId, namespace, key, Double.class); + } + + /** + * Gets as boolean value. + * + * @param key the key + * @return the as boolean value + */ + public default Boolean getAsBooleanValue(String key) { + return getAsBooleanValue(null, key); + } + + /** + * Gets as boolean value. + * + * @param namespace the namespace + * @param key the key + * @return the as boolean value + */ + public default Boolean getAsBooleanValue(String namespace, String key) { + return getAsBooleanValue(tenant.get(), namespace, key); + } + + /** + * Gets as boolean value. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as boolean value + */ + public default Boolean getAsBooleanValue(String tenantId, String namespace, String key) { + return get(tenantId, namespace, key, Boolean.class); + } + + /** + * Gets as char value. + * + * @param key the key + * @return the as char value + */ + public default Character getAsCharValue(String key) { + return getAsCharValue(null, key); + } + + /** + * Gets as char value. + * + * @param namespace the namespace + * @param key the key + * @return the as char value + */ + public default Character getAsCharValue(String namespace, String key) { + return getAsCharValue(tenant.get(), namespace, key); + } + + /** + * Gets as char value. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as char value + */ + public default Character getAsCharValue(String tenantId, String namespace, String key) { + return get(tenantId, namespace, key, Character.class); + } + + /** + * Populate configuration t. + * + * @param the type parameter + * @param clazz the clazz + * @return the t + */ + public default T populateConfiguration(Class clazz) { + return populateConfiguration(null, clazz); + } + + /** + * Populate configuration t. + * + * @param the type parameter + * @param namespace the namespace + * @param clazz the clazz + * @return the t + */ + public default T populateConfiguration(String namespace, Class clazz) { + return populateConfiguration(tenant.get(), namespace, clazz); + } + + /** + * Populate configuration t. + * + * @param the type parameter + * @param tenantId the tenant id + * @param namespace the namespace + * @param clazz the clazz + * @return the t + */ + public default T populateConfiguration(String tenantId, String namespace, Class clazz) { + return get(tenantId, namespace, null, clazz, Hint.EXTERNAL_LOOKUP); + } + + /** + * Gets dynamic configuration. + * + * @param the type parameter + * @param key the key + * @param clazz the clazz + * @param defaultValue the default value + * @return the dynamic configuration + */ + public default DynamicConfiguration getDynamicConfiguration(String key, Class clazz, + T defaultValue) { + return getDynamicConfiguration(null, key, clazz, defaultValue); + } + + /** + * Gets dynamic configuration. + * + * @param the type parameter + * @param namespace the namespace + * @param key the key + * @param clazz the clazz + * @param defaultValue the default value + * @return the dynamic configuration + */ + public default DynamicConfiguration getDynamicConfiguration(String namespace, String key, + Class clazz, + T defaultValue) { + return getDynamicConfiguration(tenant.get(), namespace, key, clazz, defaultValue); + } + + /** + * Gets dynamic configuration. + * + * @param the type parameter + * @param tenant the tenant + * @param namespace the namespace + * @param key the key + * @param clazz the clazz + * @param defaultValue the default value + * @return the dynamic configuration + */ + public default DynamicConfiguration getDynamicConfiguration(String tenant, + String namespace, String key, + Class clazz, + T defaultValue) { + return DynamicConfiguration + .getDynamicConfiguration(tenant, namespace, key, clazz, defaultValue, this); + } + + /** + * Gets dynamic configuration values. + * + * @param the type parameter + * @param key the key + * @param clazz the clazz + * @param defaultValue the default value + * @return the dynamic configuration values + */ + public default DynamicConfiguration> getDynamicConfigurationValues(String key, + Class clazz, + T defaultValue) { + return getDynamicConfigurationValues(null, key, clazz, defaultValue); + } + + /** + * Gets dynamic configuration values. + * + * @param the type parameter + * @param namespace the namespace + * @param key the key + * @param clazz the clazz + * @param defaultValue the default value + * @return the dynamic configuration values + */ + public default DynamicConfiguration> getDynamicConfigurationValues(String namespace, + String key, + Class clazz, + T defaultValue) { + return getDynamicConfigurationValues(tenant.get(), namespace, key, clazz, defaultValue); + } + + /** + * Gets dynamic configuration values. + * + * @param the type parameter + * @param tenant the tenant + * @param namespace the namespace + * @param key the key + * @param clazz the clazz + * @param defaultValue the default value + * @return the dynamic configuration values + */ + public default DynamicConfiguration> getDynamicConfigurationValues(String tenant, + String namespace, + String key, + Class clazz, + T defaultValue) { + return DynamicConfiguration + .getDynConfiguration(tenant, namespace, key, clazz, defaultValue, this); + } + + /** + * Gets as string values. + * + * @param key the key + * @return the as string values + */ + public default List getAsStringValues(String key) { + return getAsStringValues(null, key); + } + + /** + * Gets as string values. + * + * @param namespace the namespace + * @param key the key + * @return the as string values + */ + public default List getAsStringValues(String namespace, String key) { + return getAsStringValues(tenant.get(), namespace, key); + } + + /** + * Gets as string values. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as string values + */ + public default List getAsStringValues(String tenantId, String namespace, String key) { + String[] tempArray = get(tenantId, namespace, key, String[].class); + return tempArray == null ? Arrays.asList() : Arrays.asList(tempArray); + } + + /** + * Gets as byte values. + * + * @param key the key + * @return the as byte values + */ + public default List getAsByteValues(String key) { + return getAsByteValues(null, key); + } + + /** + * Gets as byte values. + * + * @param namespace the namespace + * @param key the key + * @return the as byte values + */ + public default List getAsByteValues(String namespace, String key) { + return getAsByteValues(tenant.get(), namespace, key); + } + + /** + * Gets as byte values. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as byte values + */ + public default List getAsByteValues(String tenantId, String namespace, String key) { + Byte[] tempArray = get(tenantId, namespace, key, Byte[].class); + return tempArray == null ? Arrays.asList() : Arrays.asList(tempArray); + } + + /** + * Gets as short values. + * + * @param key the key + * @return the as short values + */ + public default List getAsShortValues(String key) { + return getAsShortValues(null, key); + } + + /** + * Gets as short values. + * + * @param namespace the namespace + * @param key the key + * @return the as short values + */ + public default List getAsShortValues(String namespace, String key) { + return getAsShortValues(tenant.get(), namespace, key); + } + + /** + * Gets as short values. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as short values + */ + public default List getAsShortValues(String tenantId, String namespace, String key) { + Short[] tempArray = get(tenantId, namespace, key, Short[].class); + return tempArray == null ? Arrays.asList() : Arrays.asList(tempArray); + } + + /** + * Gets as integer values. + * + * @param key the key + * @return the as integer values + */ + public default List getAsIntegerValues(String key) { + return getAsIntegerValues(null, key); + } + + /** + * Gets as integer values. + * + * @param namespace the namespace + * @param key the key + * @return the as integer values + */ + public default List getAsIntegerValues(String namespace, String key) { + return getAsIntegerValues(tenant.get(), namespace, key); + } + + /** + * Gets as integer values. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as integer values + */ + public default List getAsIntegerValues(String tenantId, String namespace, String key) { + Integer[] tempArray = get(tenantId, namespace, key, Integer[].class); + return tempArray == null ? Arrays.asList() : Arrays.asList(tempArray); + } + + /** + * Gets as double values. + * + * @param key the key + * @return the as double values + */ + public default List getAsDoubleValues(String key) { + return getAsDoubleValues(null, key); + } + + /** + * Gets as double values. + * + * @param namespace the namespace + * @param key the key + * @return the as double values + */ + public default List getAsDoubleValues(String namespace, String key) { + return getAsDoubleValues(tenant.get(), namespace, key); + } + + /** + * Gets as double values. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as double values + */ + public default List getAsDoubleValues(String tenantId, String namespace, String key) { + Double[] tempArray = get(tenantId, namespace, key, Double[].class); + return tempArray == null ? Arrays.asList() : Arrays.asList(tempArray); + } + + /** + * Gets as float values. + * + * @param key the key + * @return the as float values + */ + public default List getAsFloatValues(String key) { + return getAsFloatValues(null, key); + } + + /** + * Gets as float values. + * + * @param namespace the namespace + * @param key the key + * @return the as float values + */ + public default List getAsFloatValues(String namespace, String key) { + return getAsFloatValues(tenant.get(), namespace, key); + } + + /** + * Gets as float values. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as float values + */ + public default List getAsFloatValues(String tenantId, String namespace, String key) { + Float[] tempArray = get(tenantId, namespace, key, Float[].class); + return tempArray == null ? Arrays.asList() : Arrays.asList(tempArray); + } + + /** + * Gets as boolean values. + * + * @param key the key + * @return the as boolean values + */ + public default List getAsBooleanValues(String key) { + return getAsBooleanValues(null, key); + } + + /** + * Gets as boolean values. + * + * @param namespace the namespace + * @param key the key + * @return the as boolean values + */ + public default List getAsBooleanValues(String namespace, String key) { + return getAsBooleanValues(tenant.get(), namespace, key); + } + + /** + * Gets as boolean values. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as boolean values + */ + public default List getAsBooleanValues(String tenantId, String namespace, String key) { + Boolean[] tempArray = get(tenantId, namespace, key, Boolean[].class); + return tempArray == null ? Arrays.asList() : Arrays.asList(tempArray); + } + + /** + * Gets as character values. + * + * @param key the key + * @return the as character values + */ + public default List getAsCharacterValues(String key) { + return getAsCharacterValues(null, key); + } + + /** + * Gets as character values. + * + * @param namespace the namespace + * @param key the key + * @return the as character values + */ + public default List getAsCharacterValues(String namespace, String key) { + return getAsCharacterValues(tenant.get(), namespace, key); + } + + /** + * Gets as character values. + * + * @param tenantId the tenant id + * @param namespace the namespace + * @param key the key + * @return the as character values + */ + public default List getAsCharacterValues(String tenantId, String namespace, + String key) { + Character[] tempArray = get(tenantId, namespace, key, Character[].class); + return tempArray == null ? Arrays.asList() : Arrays.asList(tempArray); + } + + /** + * Get t. + * + * @param the type parameter + * @param tenant the tenant + * @param namespace the namespace + * @param key the key + * @param clazz the clazz + * @param hints the hints + * @return the t + */ + public T get(String tenant, String namespace, String key, Class clazz, Hint... hints); + + /** + * Add configuration change listener. + * + * @param key the key + * @param myself the myself + */ + public default void addConfigurationChangeListener(String key, + ConfigurationChangeListener myself) { + addConfigurationChangeListener(null, key, myself); + } + + /** + * Add configuration change listener. + * + * @param namespace the namespace + * @param key the key + * @param myself the myself + */ + public default void addConfigurationChangeListener(String namespace, String key, + ConfigurationChangeListener myself) { + addConfigurationChangeListener(tenant.get(), namespace, key, myself); + } + + /** + * Add configuration change listener. + * + * @param tenant the tenant + * @param namespace the namespace + * @param key the key + * @param myself the myself + */ + public void addConfigurationChangeListener(String tenant, String namespace, String key, + ConfigurationChangeListener myself); + + /** + * Remove configuration change listener. + * + * @param key the key + * @param myself the myself + */ + public default void removeConfigurationChangeListener(String key, + ConfigurationChangeListener myself) { + removeConfigurationChangeListener(null, key, myself); + } + + /** + * Remove configuration change listener. + * + * @param namespace the namespace + * @param key the key + * @param myself the myself + */ + public default void removeConfigurationChangeListener(String namespace, String key, + ConfigurationChangeListener myself) { + removeConfigurationChangeListener(tenant.get(), namespace, key, myself); + } + + /** + * Remove configuration change listener. + * + * @param tenant the tenant + * @param namespace the namespace + * @param key the key + * @param myself the myself + */ + public void removeConfigurationChangeListener(String tenant, String namespace, String key, + ConfigurationChangeListener myself); + + public default Map populateMap(String key, Class clazz){ + return populateMap(null, key, clazz); + } + public default Map populateMap(String namespace, String key, Class clazz){ + return populateMap(tenant.get(), namespace, key, clazz); + } + public Map populateMap(String tenantId, String namespace, String key, Class clazz); + + public default Map generateMap(String key){ + return generateMap(null, key); + } + public default Map generateMap(String namespace, String key){ + return generateMap(tenant.get(), namespace, key); + } + public Map generateMap(String tenantId, String namespace, String key); + +} diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/ConfigurationChangeListener.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/ConfigurationChangeListener.java new file mode 100644 index 0000000000..57814bba44 --- /dev/null +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/ConfigurationChangeListener.java @@ -0,0 +1,15 @@ +package org.openecomp.config.api; + +public interface ConfigurationChangeListener { + + public default void notify(String tenantId, String component, String key, Object oldValue, + Object newValue) { + } + + public default void notify(String component, String key, Object oldValue, Object newValue) { + System.out.println("HIT"); + } + + public default void notify(String key, Object oldValue, Object newValue) { + } +} diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/ConfigurationManager.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/ConfigurationManager.java new file mode 100644 index 0000000000..ffa9255f2c --- /dev/null +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/ConfigurationManager.java @@ -0,0 +1,98 @@ +package org.openecomp.config.api; + + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Proxy; +import java.util.Collection; +import java.util.Map; +import java.util.ServiceLoader; + +/** + * The interface Configuration manager. + */ +public interface ConfigurationManager extends Configuration { + + /** + * The constant config. + */ + public static final Configuration config = lookup(); + + /** + * Lookup configuration. + * + * @return the configuration + */ + public static Configuration lookup() { + if (config == null) { + ServiceLoader loader = ServiceLoader.load(ConfigurationManager.class); + for (ConfigurationManager configuration : loader) { + return (Configuration) Proxy.newProxyInstance(ConfigurationManager.class.getClassLoader(), + new Class[]{Configuration.class}, (object, method, args) -> { + try { + return method.invoke(configuration, args); + } catch (InvocationTargetException ite) { + throw ite.getTargetException(); + } + }); + } + } + return config; + } + + /** + * Gets configuration value. + * + * @param queryData the query data + * @return the configuration value + */ + public String getConfigurationValue(Map queryData); + + /** + * Update configuration value. + * + * @param updateData the update data + */ + public void updateConfigurationValue(Map updateData); + + /** + * List configuration map. + * + * @param query the query + * @return the map + */ + public Map listConfiguration(Map query); + + /** + * Update configuration values boolean. + * + * @param tenant the tenant + * @param namespace the namespace + * @param configKeyValueStore the config key value store + * @return the boolean + */ + public boolean updateConfigurationValues(String tenant, String namespace, + Map configKeyValueStore); + + /** + * Gets tenants. + * + * @return the tenants + */ + public Collection getTenants(); + + /** + * Gets namespaces. + * + * @return the namespaces + */ + public Collection getNamespaces(); + + /** + * Gets keys. + * + * @param tenant the tenant + * @param namespace the namespace + * @return the keys + */ + public Collection getKeys(String tenant, String namespace); +} diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/DynamicConfiguration.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/DynamicConfiguration.java new file mode 100644 index 0000000000..428d9ee233 --- /dev/null +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/DynamicConfiguration.java @@ -0,0 +1,142 @@ +package org.openecomp.config.api; + +import java.util.Arrays; +import java.util.List; + +/** + * The type Dynamic configuration. + * + * @param the type parameter + */ +public class DynamicConfiguration { + + /** + * 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 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 DynamicConfiguration getDynamicConfiguration(String tenant, String namespace, + String key, Class clazz, + T defaultValue, + Configuration configuration) { + DynamicConfiguration 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 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 DynamicConfiguration> getDynConfiguration(String tenant, + String namespace, String key, + Class 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> 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; + } + +} diff --git a/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Hint.java b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Hint.java new file mode 100644 index 0000000000..c3f54bd183 --- /dev/null +++ b/common/openecomp-common-configuration-management/openecomp-configuration-management-api/src/main/java/org/openecomp/config/api/Hint.java @@ -0,0 +1,18 @@ +package org.openecomp.config.api; + +public enum Hint { + + DEFAULT(0b0), LATEST_LOOKUP(0b1), EXTERNAL_LOOKUP(0b10), NODE_SPECIFIC(0b100); + + private final int hint; + + private Hint(int hnt) { + hint = hnt; + } + + public int value() { + return hint; + } + + +} -- cgit 1.2.3-korg