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