Use Method.getParameterCount()
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DatastoreContextIntrospector.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.primitives.Primitives;
14 import java.beans.BeanInfo;
15 import java.beans.ConstructorProperties;
16 import java.beans.IntrospectionException;
17 import java.beans.Introspector;
18 import java.beans.MethodDescriptor;
19 import java.beans.PropertyDescriptor;
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.AbstractMap.SimpleImmutableEntry;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.Set;
31 import javax.annotation.concurrent.GuardedBy;
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.commons.text.WordUtils;
34 import org.opendaylight.controller.cluster.datastore.DatastoreContext.Builder;
35 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
36 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.distributed.datastore.provider.rev140612.DataStoreProperties;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.distributed.datastore.provider.rev140612.DataStorePropertiesContainer;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Introspects on a DatastoreContext instance to set its properties via reflection.
47  * i
48  * @author Thomas Pantelis
49  */
50 public class DatastoreContextIntrospector {
51     private static final Logger LOG = LoggerFactory.getLogger(DatastoreContextIntrospector.class);
52
53     private static final Map<String, Entry<Class<?>, Method>> DATA_STORE_PROP_INFO = new HashMap<>();
54
55     private static final Map<Class<?>, Constructor<?>> CONSTRUCTORS = new HashMap<>();
56
57     private static final Map<Class<?>, Method> YANG_TYPE_GETTERS = new HashMap<>();
58
59     private static final Map<String, Method> BUILDER_SETTERS = new HashMap<>();
60
61     static {
62         try {
63             introspectDatastoreContextBuilder();
64             introspectDataStoreProperties();
65             introspectPrimitiveTypes();
66         } catch (final IntrospectionException e) {
67             LOG.error("Error initializing DatastoreContextIntrospector", e);
68         }
69     }
70
71     /**
72      * Introspects each primitive wrapper (ie Integer, Long etc) and String type to find the
73      * constructor that takes a single String argument. For primitive wrappers, this constructor
74      * converts from a String representation.
75      */
76     // Disables "Either log or rethrow this exception" sonar warning
77     @SuppressWarnings("squid:S1166")
78     private static void introspectPrimitiveTypes() {
79         final Set<Class<?>> primitives = ImmutableSet.<Class<?>>builder().addAll(
80                 Primitives.allWrapperTypes()).add(String.class).build();
81         for (final Class<?> primitive: primitives) {
82             try {
83                 processPropertyType(primitive);
84             } catch (final NoSuchMethodException e) {
85                 // Ignore primitives that can't be constructed from a String, eg Character and Void.
86             } catch (SecurityException | IntrospectionException e) {
87                 LOG.error("Error introspect primitive type {}", primitive, e);
88             }
89         }
90     }
91
92     /**
93      * Introspects the DatastoreContext.Builder class to find all its setter methods that we will
94      * invoke via reflection. We can't use the bean Introspector here as the Builder setters don't
95      * follow the bean property naming convention, ie setter prefixed with "set", so look for all
96      * the methods that return Builder.
97      */
98     private static void introspectDatastoreContextBuilder() {
99         for (final Method method: Builder.class.getMethods()) {
100             if (Builder.class.equals(method.getReturnType())) {
101                 BUILDER_SETTERS.put(method.getName(), method);
102             }
103         }
104     }
105
106     /**
107      * Introspects the DataStoreProperties interface that is generated from the DataStoreProperties
108      * yang grouping. We use the bean Introspector to find the types of all the properties defined
109      * in the interface (this is the type returned from the getter method). For each type, we find
110      * the appropriate constructor that we will use.
111      */
112     private static void introspectDataStoreProperties() throws IntrospectionException {
113         final BeanInfo beanInfo = Introspector.getBeanInfo(DataStoreProperties.class);
114         for (final PropertyDescriptor desc: beanInfo.getPropertyDescriptors()) {
115             processDataStoreProperty(desc.getName(), desc.getPropertyType(), desc.getReadMethod());
116         }
117
118         // Getter methods that return Boolean and start with "is" instead of "get" aren't recognized as
119         // properties and thus aren't returned from getPropertyDescriptors. A getter starting with
120         // "is" is only supported if it returns primitive boolean. So we'll check for these via
121         // getMethodDescriptors.
122         for (final MethodDescriptor desc: beanInfo.getMethodDescriptors()) {
123             final String methodName = desc.getName();
124             if (Boolean.class.equals(desc.getMethod().getReturnType()) && methodName.startsWith("is")) {
125                 final String propertyName = WordUtils.uncapitalize(methodName.substring(2));
126                 processDataStoreProperty(propertyName, Boolean.class, desc.getMethod());
127             }
128         }
129     }
130
131     /**
132      * Processes a property defined on the DataStoreProperties interface.
133      */
134     @SuppressWarnings("checkstyle:IllegalCatch")
135     private static void processDataStoreProperty(final String name, final Class<?> propertyType,
136             final Method readMethod) {
137         Preconditions.checkArgument(BUILDER_SETTERS.containsKey(name), String.format(
138                 "DataStoreProperties property \"%s\" does not have corresponding setter in DatastoreContext.Builder",
139                 name));
140         try {
141             processPropertyType(propertyType);
142             DATA_STORE_PROP_INFO.put(name, new SimpleImmutableEntry<>(propertyType, readMethod));
143         } catch (final Exception e) {
144             LOG.error("Error finding constructor for type {}", propertyType, e);
145         }
146     }
147
148     /**
149      * Finds the appropriate constructor for the specified type that we will use to construct
150      * instances.
151      */
152     private static void processPropertyType(final Class<?> propertyType)
153             throws NoSuchMethodException, SecurityException, IntrospectionException {
154         final Class<?> wrappedType = Primitives.wrap(propertyType);
155         if (CONSTRUCTORS.containsKey(wrappedType)) {
156             return;
157         }
158
159         // If the type is a primitive (or String type), we look for the constructor that takes a
160         // single String argument, which, for primitives, validates and converts from a String
161         // representation which is the form we get on ingress.
162         if (propertyType.isPrimitive() || Primitives.isWrapperType(propertyType) || propertyType.equals(String.class)) {
163             CONSTRUCTORS.put(wrappedType, propertyType.getConstructor(String.class));
164         } else {
165             // This must be a yang-defined type. We need to find the constructor that takes a
166             // primitive as the only argument. This will be used to construct instances to perform
167             // validation (eg range checking). The yang-generated types have a couple single-argument
168             // constructors but the one we want has the bean ConstructorProperties annotation.
169             for (final Constructor<?> ctor: propertyType.getConstructors()) {
170                 final ConstructorProperties ctorPropsAnnotation = ctor.getAnnotation(ConstructorProperties.class);
171                 if (ctor.getParameterCount() == 1 && ctorPropsAnnotation != null) {
172                     findYangTypeGetter(propertyType, ctorPropsAnnotation.value()[0]);
173                     CONSTRUCTORS.put(propertyType, ctor);
174                     break;
175                 }
176             }
177         }
178     }
179
180     /**
181      * Finds the getter method on a yang-generated type for the specified property name.
182      */
183     private static void findYangTypeGetter(final Class<?> type, final String propertyName)
184             throws IntrospectionException {
185         for (final PropertyDescriptor desc: Introspector.getBeanInfo(type).getPropertyDescriptors()) {
186             if (desc.getName().equals(propertyName)) {
187                 YANG_TYPE_GETTERS.put(type, desc.getReadMethod());
188                 return;
189             }
190         }
191
192         throw new IntrospectionException(String.format(
193                 "Getter method for constructor property %s not found for YANG type %s",
194                 propertyName, type));
195     }
196
197     @GuardedBy(value = "this")
198     private DatastoreContext context;
199     @GuardedBy(value = "this")
200     private Map<String, Object> currentProperties;
201
202     public DatastoreContextIntrospector(final DatastoreContext context,
203             final BindingNormalizedNodeSerializer bindingSerializer) {
204         final QName qname = BindingReflections.findQName(DataStorePropertiesContainer.class);
205         final DataStorePropertiesContainer defaultPropsContainer = (DataStorePropertiesContainer)
206                 bindingSerializer.fromNormalizedNode(bindingSerializer.toYangInstanceIdentifier(
207                         InstanceIdentifier.builder(DataStorePropertiesContainer.class).build()),
208                 ImmutableNodes.containerNode(qname)).getValue();
209
210         final Builder builder = DatastoreContext.newBuilderFrom(context);
211         for (Entry<String, Entry<Class<?>, Method>> entry: DATA_STORE_PROP_INFO.entrySet()) {
212             Object value;
213             try {
214                 value = entry.getValue().getValue().invoke(defaultPropsContainer);
215             } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
216                 LOG.error("Error obtaining default value for property {}", entry.getKey(), e);
217                 value = null;
218             }
219
220             if (value != null) {
221                 convertValueAndInvokeSetter(entry.getKey(), value, builder);
222             }
223         }
224
225         this.context = builder.build();
226     }
227
228     public synchronized DatastoreContext getContext() {
229         return context;
230     }
231
232     public DatastoreContextFactory newContextFactory() {
233         return new DatastoreContextFactory(this);
234     }
235
236     public synchronized DatastoreContext getShardDatastoreContext(final String forShardName) {
237         if (currentProperties == null) {
238             return context;
239         }
240
241         final Builder builder = DatastoreContext.newBuilderFrom(context);
242         final String dataStoreTypePrefix = context.getDataStoreName() + '.';
243         final String shardNamePrefix = forShardName + '.';
244
245         final List<String> keys = getSortedKeysByDatastoreType(currentProperties.keySet(), dataStoreTypePrefix);
246
247         for (String key: keys) {
248             final Object value = currentProperties.get(key);
249             if (key.startsWith(dataStoreTypePrefix)) {
250                 key = key.replaceFirst(dataStoreTypePrefix, "");
251             }
252
253             if (key.startsWith(shardNamePrefix)) {
254                 key = key.replaceFirst(shardNamePrefix, "");
255                 convertValueAndInvokeSetter(key, value.toString(), builder);
256             }
257         }
258
259         return builder.build();
260     }
261
262     /**
263      * Applies the given properties to the cached DatastoreContext and yields a new DatastoreContext
264      * instance which can be obtained via {@link #getContext()}.
265      *
266      * @param properties the properties to apply
267      * @return true if the cached DatastoreContext was updated, false otherwise.
268      */
269     public synchronized boolean update(final Map<String, Object> properties) {
270         currentProperties = null;
271         if (properties == null || properties.isEmpty()) {
272             return false;
273         }
274
275         LOG.debug("In update: properties: {}", properties);
276
277         final ImmutableMap.Builder<String, Object> mapBuilder = ImmutableMap.<String, Object>builder();
278
279         final Builder builder = DatastoreContext.newBuilderFrom(context);
280
281         final String dataStoreTypePrefix = context.getDataStoreName() + '.';
282
283         final List<String> keys = getSortedKeysByDatastoreType(properties.keySet(), dataStoreTypePrefix);
284
285         boolean updated = false;
286         for (String key: keys) {
287             final Object value = properties.get(key);
288             mapBuilder.put(key, value);
289
290             // If the key is prefixed with the data store type, strip it off.
291             if (key.startsWith(dataStoreTypePrefix)) {
292                 key = key.replaceFirst(dataStoreTypePrefix, "");
293             }
294
295             if (convertValueAndInvokeSetter(key, value.toString(), builder)) {
296                 updated = true;
297             }
298         }
299
300         currentProperties = mapBuilder.build();
301
302         if (updated) {
303             context = builder.build();
304         }
305
306         return updated;
307     }
308
309     private static ArrayList<String> getSortedKeysByDatastoreType(final Collection<String> inKeys,
310             final String dataStoreTypePrefix) {
311         // Sort the property keys by putting the names prefixed with the data store type last. This
312         // is done so data store specific settings are applied after global settings.
313         final ArrayList<String> keys = new ArrayList<>(inKeys);
314         keys.sort((key1, key2) -> key1.startsWith(dataStoreTypePrefix) ? 1 :
315             key2.startsWith(dataStoreTypePrefix) ? -1 : key1.compareTo(key2));
316         return keys;
317     }
318
319     @SuppressWarnings("checkstyle:IllegalCatch")
320     private boolean convertValueAndInvokeSetter(final String inKey, final Object inValue, final Builder builder) {
321         final String key = convertToCamelCase(inKey);
322
323         try {
324             // Convert the value to the right type.
325             final Object value = convertValue(key, inValue);
326             if (value == null) {
327                 return false;
328             }
329
330             LOG.debug("Converted value for property {}: {} ({})",
331                     key, value, value.getClass().getSimpleName());
332
333             // Call the setter method on the Builder instance.
334             final Method setter = BUILDER_SETTERS.get(key);
335             setter.invoke(builder, constructorValueRecursively(
336                     Primitives.wrap(setter.getParameterTypes()[0]), value.toString()));
337
338             return true;
339         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
340                 | InstantiationException e) {
341             LOG.error("Error converting value ({}) for property {}", inValue, key, e);
342         }
343
344         return false;
345     }
346
347     private static String convertToCamelCase(final String inString) {
348         String str = inString.trim();
349         if (StringUtils.contains(str, '-') || StringUtils.contains(str, ' ')) {
350             str = inString.replace('-', ' ');
351             str = WordUtils.capitalizeFully(str);
352             str = StringUtils.deleteWhitespace(str);
353         }
354
355         return StringUtils.uncapitalize(str);
356     }
357
358     private Object convertValue(final String name, final Object from)
359             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
360         final Entry<Class<?>, Method> propertyInfo = DATA_STORE_PROP_INFO.get(name);
361         if (propertyInfo == null) {
362             LOG.debug("Property not found for {}", name);
363             return null;
364         }
365
366         final Class<?> propertyType = propertyInfo.getKey();
367
368         LOG.debug("Type for property {}: {}, converting value {} ({})",
369                 name, propertyType.getSimpleName(), from, from.getClass().getSimpleName());
370
371         // Recurse the chain of constructors depth-first to get the resulting value. Eg, if the
372         // property type is the yang-generated NonZeroUint32Type, it's constructor takes a Long so
373         // we have to first construct a Long instance from the input value.
374         Object converted = constructorValueRecursively(propertyType, from);
375
376         // If the converted type is a yang-generated type, call the getter to obtain the actual value.
377         final Method getter = YANG_TYPE_GETTERS.get(converted.getClass());
378         if (getter != null) {
379             converted = getter.invoke(converted);
380         }
381
382         return converted;
383     }
384
385     private Object constructorValueRecursively(final Class<?> toType, final Object fromValue)
386             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
387         LOG.trace("convertValueRecursively - toType: {}, fromValue {} ({})",
388                 toType.getSimpleName(), fromValue, fromValue.getClass().getSimpleName());
389
390         if (toType.equals(fromValue.getClass())) {
391             return fromValue;
392         }
393
394         final Constructor<?> ctor = CONSTRUCTORS.get(toType);
395
396         LOG.trace("Found {}", ctor);
397
398         if (ctor == null) {
399             throw new IllegalArgumentException(String.format("Constructor not found for type %s", toType));
400         }
401
402         Object value = fromValue;
403
404         // Once we find a constructor that takes the original type as an argument, we're done recursing.
405         if (!ctor.getParameterTypes()[0].equals(fromValue.getClass())) {
406             value = constructorValueRecursively(ctor.getParameterTypes()[0], fromValue);
407         }
408
409         return ctor.newInstance(value);
410     }
411 }