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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.