X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FDatastoreContextIntrospector.java;h=a6f5f019307097835d3bc699ccb723da737d0e97;hp=1f0911851089d37aaf33e8d4350c39307394ffbd;hb=b65e66f7b1bafb0d0c5fbe1c569835eb890f672a;hpb=925cb4a228d0fda99c7bfeb432eb25285a223887 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreContextIntrospector.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreContextIntrospector.java index 1f09118510..a6f5f01930 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreContextIntrospector.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreContextIntrospector.java @@ -7,7 +7,8 @@ */ package org.opendaylight.controller.cluster.datastore; -import com.google.common.base.Preconditions; +import static com.google.common.base.Preconditions.checkArgument; + import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.primitives.Primitives; @@ -18,19 +19,27 @@ import java.beans.Introspector; import java.beans.MethodDescriptor; import java.beans.PropertyDescriptor; import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.AbstractMap.SimpleImmutableEntry; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.Dictionary; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; +import java.util.function.Function; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.text.WordUtils; +import org.apache.commons.text.WordUtils; +import org.checkerframework.checker.lock.qual.GuardedBy; import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.distributed.datastore.provider.rev140612.DataStoreProperties; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.distributed.datastore.provider.rev140612.DataStorePropertiesContainer; +import org.opendaylight.yangtools.yang.common.Uint16; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint64; +import org.opendaylight.yangtools.yang.common.Uint8; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,7 +51,7 @@ import org.slf4j.LoggerFactory; public class DatastoreContextIntrospector { private static final Logger LOG = LoggerFactory.getLogger(DatastoreContextIntrospector.class); - private static final Map> DATA_STORE_PROP_TYPES = new HashMap<>(); + private static final Map, Method>> DATA_STORE_PROP_INFO = new HashMap<>(); private static final Map, Constructor> CONSTRUCTORS = new HashMap<>(); @@ -50,12 +59,20 @@ public class DatastoreContextIntrospector { private static final Map BUILDER_SETTERS = new HashMap<>(); + private static final ImmutableMap, Function> UINT_FACTORIES = + ImmutableMap., Function>builder() + .put(Uint8.class, Uint8::valueOf) + .put(Uint16.class, Uint16::valueOf) + .put(Uint32.class, Uint32::valueOf) + .put(Uint64.class, Uint64::valueOf) + .build(); + static { try { introspectDatastoreContextBuilder(); introspectDataStoreProperties(); introspectPrimitiveTypes(); - } catch (IntrospectionException e) { + } catch (final IntrospectionException e) { LOG.error("Error initializing DatastoreContextIntrospector", e); } } @@ -65,16 +82,18 @@ public class DatastoreContextIntrospector { * constructor that takes a single String argument. For primitive wrappers, this constructor * converts from a String representation. */ - @SuppressWarnings("checkstyle:IllegalCatch") + // Disables "Either log or rethrow this exception" sonar warning + @SuppressWarnings("squid:S1166") private static void introspectPrimitiveTypes() { - - Set> primitives = ImmutableSet.>builder().addAll( + final Set> primitives = ImmutableSet.>builder().addAll( Primitives.allWrapperTypes()).add(String.class).build(); - for (Class primitive: primitives) { + for (final Class primitive: primitives) { try { processPropertyType(primitive); - } catch (Exception e) { + } catch (final NoSuchMethodException e) { // Ignore primitives that can't be constructed from a String, eg Character and Void. + } catch (SecurityException | IntrospectionException e) { + LOG.error("Error introspect primitive type {}", primitive, e); } } } @@ -86,7 +105,7 @@ public class DatastoreContextIntrospector { * the methods that return Builder. */ private static void introspectDatastoreContextBuilder() { - for (Method method: Builder.class.getMethods()) { + for (final Method method: Builder.class.getMethods()) { if (Builder.class.equals(method.getReturnType())) { BUILDER_SETTERS.put(method.getName(), method); } @@ -100,20 +119,20 @@ public class DatastoreContextIntrospector { * the appropriate constructor that we will use. */ private static void introspectDataStoreProperties() throws IntrospectionException { - BeanInfo beanInfo = Introspector.getBeanInfo(DataStoreProperties.class); - for (PropertyDescriptor desc: beanInfo.getPropertyDescriptors()) { - processDataStoreProperty(desc.getName(), desc.getPropertyType()); + final BeanInfo beanInfo = Introspector.getBeanInfo(DataStoreProperties.class); + for (final PropertyDescriptor desc: beanInfo.getPropertyDescriptors()) { + processDataStoreProperty(desc.getName(), desc.getPropertyType(), desc.getReadMethod()); } // Getter methods that return Boolean and start with "is" instead of "get" aren't recognized as // properties and thus aren't returned from getPropertyDescriptors. A getter starting with // "is" is only supported if it returns primitive boolean. So we'll check for these via // getMethodDescriptors. - for (MethodDescriptor desc: beanInfo.getMethodDescriptors()) { - String methodName = desc.getName(); + for (final MethodDescriptor desc: beanInfo.getMethodDescriptors()) { + final String methodName = desc.getName(); if (Boolean.class.equals(desc.getMethod().getReturnType()) && methodName.startsWith("is")) { - String propertyName = WordUtils.uncapitalize(methodName.substring(2)); - processDataStoreProperty(propertyName, Boolean.class); + final String propertyName = WordUtils.uncapitalize(methodName.substring(2)); + processDataStoreProperty(propertyName, Boolean.class, desc.getMethod()); } } } @@ -122,14 +141,15 @@ public class DatastoreContextIntrospector { * Processes a property defined on the DataStoreProperties interface. */ @SuppressWarnings("checkstyle:IllegalCatch") - private static void processDataStoreProperty(String name, Class propertyType) { - Preconditions.checkArgument(BUILDER_SETTERS.containsKey(name), String.format( + private static void processDataStoreProperty(final String name, final Class propertyType, + final Method readMethod) { + checkArgument(BUILDER_SETTERS.containsKey(name), "DataStoreProperties property \"%s\" does not have corresponding setter in DatastoreContext.Builder", - name)); + name); try { processPropertyType(propertyType); - DATA_STORE_PROP_TYPES.put(name, propertyType); - } catch (Exception e) { + DATA_STORE_PROP_INFO.put(name, new SimpleImmutableEntry<>(propertyType, readMethod)); + } catch (final Exception e) { LOG.error("Error finding constructor for type {}", propertyType, e); } } @@ -138,8 +158,9 @@ public class DatastoreContextIntrospector { * Finds the appropriate constructor for the specified type that we will use to construct * instances. */ - private static void processPropertyType(Class propertyType) throws Exception { - Class wrappedType = Primitives.wrap(propertyType); + private static void processPropertyType(final Class propertyType) + throws NoSuchMethodException, SecurityException, IntrospectionException { + final Class wrappedType = Primitives.wrap(propertyType); if (CONSTRUCTORS.containsKey(wrappedType)) { return; } @@ -154,9 +175,9 @@ public class DatastoreContextIntrospector { // primitive as the only argument. This will be used to construct instances to perform // validation (eg range checking). The yang-generated types have a couple single-argument // constructors but the one we want has the bean ConstructorProperties annotation. - for (Constructor ctor: propertyType.getConstructors()) { - ConstructorProperties ctorPropsAnnotation = ctor.getAnnotation(ConstructorProperties.class); - if (ctor.getParameterTypes().length == 1 && ctorPropsAnnotation != null) { + for (final Constructor ctor: propertyType.getConstructors()) { + final ConstructorProperties ctorPropsAnnotation = ctor.getAnnotation(ConstructorProperties.class); + if (ctor.getParameterCount() == 1 && ctorPropsAnnotation != null) { findYangTypeGetter(propertyType, ctorPropsAnnotation.value()[0]); CONSTRUCTORS.put(propertyType, ctor); break; @@ -168,28 +189,47 @@ public class DatastoreContextIntrospector { /** * Finds the getter method on a yang-generated type for the specified property name. */ - private static void findYangTypeGetter(Class type, String propertyName) - throws Exception { - for (PropertyDescriptor desc: Introspector.getBeanInfo(type).getPropertyDescriptors()) { + private static void findYangTypeGetter(final Class type, final String propertyName) + throws IntrospectionException { + for (final PropertyDescriptor desc: Introspector.getBeanInfo(type).getPropertyDescriptors()) { if (desc.getName().equals(propertyName)) { YANG_TYPE_GETTERS.put(type, desc.getReadMethod()); return; } } - throw new IllegalArgumentException(String.format( + throw new IntrospectionException(String.format( "Getter method for constructor property %s not found for YANG type %s", propertyName, type)); } + @GuardedBy(value = "this") private DatastoreContext context; + @GuardedBy(value = "this") private Map currentProperties; - public DatastoreContextIntrospector(DatastoreContext context) { - this.context = context; + public DatastoreContextIntrospector(final DatastoreContext context, + final DataStorePropertiesContainer defaultPropsContainer) { + + final Builder builder = DatastoreContext.newBuilderFrom(context); + for (Entry, Method>> entry: DATA_STORE_PROP_INFO.entrySet()) { + Object value; + try { + value = entry.getValue().getValue().invoke(defaultPropsContainer); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + LOG.error("Error obtaining default value for property {}", entry.getKey(), e); + value = null; + } + + if (value != null) { + convertValueAndInvokeSetter(entry.getKey(), value, builder); + } + } + + this.context = builder.build(); } - public DatastoreContext getContext() { + public synchronized DatastoreContext getContext() { return context; } @@ -197,26 +237,26 @@ public class DatastoreContextIntrospector { return new DatastoreContextFactory(this); } - public synchronized DatastoreContext getShardDatastoreContext(String forShardName) { + public synchronized DatastoreContext getShardDatastoreContext(final String forShardName) { if (currentProperties == null) { return context; } - Builder builder = DatastoreContext.newBuilderFrom(context); - String dataStoreTypePrefix = context.getDataStoreName() + '.'; + final Builder builder = DatastoreContext.newBuilderFrom(context); + final String dataStoreTypePrefix = context.getDataStoreName() + '.'; final String shardNamePrefix = forShardName + '.'; - List keys = getSortedKeysByDatastoreType(currentProperties.keySet(), dataStoreTypePrefix); + final List keys = getSortedKeysByDatastoreType(currentProperties.keySet(), dataStoreTypePrefix); for (String key: keys) { - Object value = currentProperties.get(key); + final Object value = currentProperties.get(key); if (key.startsWith(dataStoreTypePrefix)) { key = key.replaceFirst(dataStoreTypePrefix, ""); } if (key.startsWith(shardNamePrefix)) { key = key.replaceFirst(shardNamePrefix, ""); - convertValueAndInvokeSetter(key, value, builder); + convertValueAndInvokeSetter(key, value.toString(), builder); } } @@ -230,7 +270,7 @@ public class DatastoreContextIntrospector { * @param properties the properties to apply * @return true if the cached DatastoreContext was updated, false otherwise. */ - public synchronized boolean update(Dictionary properties) { + public synchronized boolean update(final Map properties) { currentProperties = null; if (properties == null || properties.isEmpty()) { return false; @@ -238,17 +278,17 @@ public class DatastoreContextIntrospector { LOG.debug("In update: properties: {}", properties); - ImmutableMap.Builder mapBuilder = ImmutableMap.builder(); + final ImmutableMap.Builder mapBuilder = ImmutableMap.builder(); - Builder builder = DatastoreContext.newBuilderFrom(context); + final Builder builder = DatastoreContext.newBuilderFrom(context); final String dataStoreTypePrefix = context.getDataStoreName() + '.'; - List keys = getSortedKeysByDatastoreType(Collections.list(properties.keys()), dataStoreTypePrefix); + final List keys = getSortedKeysByDatastoreType(properties.keySet(), dataStoreTypePrefix); boolean updated = false; for (String key: keys) { - Object value = properties.get(key); + final Object value = properties.get(key); mapBuilder.put(key, value); // If the key is prefixed with the data store type, strip it off. @@ -256,7 +296,7 @@ public class DatastoreContextIntrospector { key = key.replaceFirst(dataStoreTypePrefix, ""); } - if (convertValueAndInvokeSetter(key, value, builder)) { + if (convertValueAndInvokeSetter(key, value.toString(), builder)) { updated = true; } } @@ -270,23 +310,23 @@ public class DatastoreContextIntrospector { return updated; } - private static ArrayList getSortedKeysByDatastoreType(Collection inKeys, + private static ArrayList getSortedKeysByDatastoreType(final Collection inKeys, final String dataStoreTypePrefix) { // Sort the property keys by putting the names prefixed with the data store type last. This // is done so data store specific settings are applied after global settings. - ArrayList keys = new ArrayList<>(inKeys); - Collections.sort(keys, (key1, key2) -> key1.startsWith(dataStoreTypePrefix) ? 1 : - key2.startsWith(dataStoreTypePrefix) ? -1 : key1.compareTo(key2)); + final ArrayList keys = new ArrayList<>(inKeys); + keys.sort((key1, key2) -> key1.startsWith(dataStoreTypePrefix) ? 1 : + key2.startsWith(dataStoreTypePrefix) ? -1 : key1.compareTo(key2)); return keys; } @SuppressWarnings("checkstyle:IllegalCatch") - private boolean convertValueAndInvokeSetter(String inKey, Object inValue, Builder builder) { - String key = convertToCamelCase(inKey); + private boolean convertValueAndInvokeSetter(final String inKey, final Object inValue, final Builder builder) { + final String key = convertToCamelCase(inKey); try { // Convert the value to the right type. - Object value = convertValue(key, inValue); + final Object value = convertValue(key, inValue); if (value == null) { return false; } @@ -295,19 +335,20 @@ public class DatastoreContextIntrospector { key, value, value.getClass().getSimpleName()); // Call the setter method on the Builder instance. - Method setter = BUILDER_SETTERS.get(key); + final Method setter = BUILDER_SETTERS.get(key); setter.invoke(builder, constructorValueRecursively( Primitives.wrap(setter.getParameterTypes()[0]), value.toString())); return true; - } catch (Exception e) { + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | InstantiationException e) { LOG.error("Error converting value ({}) for property {}", inValue, key, e); } return false; } - private static String convertToCamelCase(String inString) { + private static String convertToCamelCase(final String inString) { String str = inString.trim(); if (StringUtils.contains(str, '-') || StringUtils.contains(str, ' ')) { str = inString.replace('-', ' '); @@ -318,23 +359,26 @@ public class DatastoreContextIntrospector { return StringUtils.uncapitalize(str); } - private Object convertValue(String name, Object from) throws Exception { - Class propertyType = DATA_STORE_PROP_TYPES.get(name); - if (propertyType == null) { + private Object convertValue(final String name, final Object from) + throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + final Entry, Method> propertyInfo = DATA_STORE_PROP_INFO.get(name); + if (propertyInfo == null) { LOG.debug("Property not found for {}", name); return null; } + final Class propertyType = propertyInfo.getKey(); + LOG.debug("Type for property {}: {}, converting value {} ({})", name, propertyType.getSimpleName(), from, from.getClass().getSimpleName()); // Recurse the chain of constructors depth-first to get the resulting value. Eg, if the // property type is the yang-generated NonZeroUint32Type, it's constructor takes a Long so // we have to first construct a Long instance from the input value. - Object converted = constructorValueRecursively(propertyType, from.toString()); + Object converted = constructorValueRecursively(propertyType, from); // If the converted type is a yang-generated type, call the getter to obtain the actual value. - Method getter = YANG_TYPE_GETTERS.get(converted.getClass()); + final Method getter = YANG_TYPE_GETTERS.get(converted.getClass()); if (getter != null) { converted = getter.invoke(converted); } @@ -342,23 +386,32 @@ public class DatastoreContextIntrospector { return converted; } - private Object constructorValueRecursively(Class toType, Object fromValue) throws Exception { + private Object constructorValueRecursively(final Class toType, final Object fromValue) + throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { LOG.trace("convertValueRecursively - toType: {}, fromValue {} ({})", toType.getSimpleName(), fromValue, fromValue.getClass().getSimpleName()); - Constructor ctor = CONSTRUCTORS.get(toType); - - LOG.trace("Found {}", ctor); + if (toType.equals(fromValue.getClass())) { + return fromValue; + } + final Constructor ctor = CONSTRUCTORS.get(toType); if (ctor == null) { + if (fromValue instanceof String) { + final Function factory = UINT_FACTORIES.get(toType); + if (factory != null) { + return factory.apply((String) fromValue); + } + } + throw new IllegalArgumentException(String.format("Constructor not found for type %s", toType)); } + LOG.trace("Found {}", ctor); Object value = fromValue; - // Since the original input type is a String, once we find a constructor that takes a String - // argument, we're done recursing. - if (!ctor.getParameterTypes()[0].equals(String.class)) { + // Once we find a constructor that takes the original type as an argument, we're done recursing. + if (!ctor.getParameterTypes()[0].equals(fromValue.getClass())) { value = constructorValueRecursively(ctor.getParameterTypes()[0], fromValue); }