Reduce JSR305 proliferation
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / ContextReferenceExtractor.java
1 /*
2  * Copyright (c) 2015 Cisco 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.md.sal.binding.impl;
9
10 import com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import java.lang.reflect.Method;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 abstract class ContextReferenceExtractor {
23     private static final Logger LOG = LoggerFactory.getLogger(ContextReferenceExtractor.class);
24     private static final ContextReferenceExtractor NULL_EXTRACTOR = new ContextReferenceExtractor() {
25
26         @Override
27         InstanceIdentifier<?> extract(final DataObject obj) {
28             return null;
29         }
30     };
31
32     private static final LoadingCache<Class<?>, ContextReferenceExtractor> EXTRACTORS = CacheBuilder.newBuilder()
33             .weakKeys().build(new CacheLoader<Class<?>, ContextReferenceExtractor>() {
34
35                 @Override
36                 public ContextReferenceExtractor load(final Class<?> key) {
37                     return create(key);
38                 }
39             });
40
41
42     private static final String GET_VALUE_NAME = "getValue";
43
44     static ContextReferenceExtractor from(final Class<?> obj) {
45         return EXTRACTORS.getUnchecked(obj);
46     }
47
48     /**
49      * Extract context-reference (Instance Identifier) from
50      * Binding DataObject.
51      *
52      * @param obj DataObject from which context reference should be extracted.
53      *
54      * @return Instance Identifier representing context reference or null, if data object does not contain
55      *     context reference.
56      */
57     abstract @Nullable InstanceIdentifier<?> extract(DataObject obj);
58
59     private static @NonNull ContextReferenceExtractor create(final Class<?> key) {
60         final Method contextGetter = getContextGetter(key);
61         if (contextGetter == null) {
62             return NULL_EXTRACTOR;
63         }
64         final Class<?> returnType = contextGetter.getReturnType();
65         try {
66             if (InstanceIdentifier.class.isAssignableFrom(returnType)) {
67                 return DirectGetterRouteContextExtractor.create(contextGetter);
68             }
69             final Method getValueMethod = findGetValueMethod(returnType,InstanceIdentifier.class);
70             if (getValueMethod != null) {
71                 return GetValueRouteContextExtractor.create(contextGetter, getValueMethod);
72             } else {
73                 LOG.warn("Class {} can not be used to determine context, falling back to NULL_EXTRACTOR.",returnType);
74             }
75         } catch (final IllegalAccessException e) {
76             LOG.warn("Class {} does not conform to Binding Specification v1. Falling back to NULL_EXTRACTOR", e);
77         }
78         return NULL_EXTRACTOR;
79     }
80
81     private static @Nullable Method findGetValueMethod(final Class<?> type, final Class<?> returnType) {
82         try {
83             final Method method = type.getMethod(GET_VALUE_NAME);
84             if (returnType.equals(method.getReturnType())) {
85                 return method;
86             }
87         } catch (final NoSuchMethodException e) {
88             LOG.warn("Value class {} does not comform to Binding Specification v1.", type, e);
89         }
90         return null;
91     }
92
93     private static Method getContextGetter(final Class<?> key) {
94         for (final Method method : key.getMethods()) {
95             if (method.getAnnotation(RoutingContext.class) != null) {
96                 return method;
97             }
98         }
99         return null;
100     }
101 }