8ac865617daebfdb417ad9266c5c0fc306529850
[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 @Deprecated
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     private static @NonNull ContextReferenceExtractor create(final Class<?> key) {
61         final Method contextGetter = getContextGetter(key);
62         if (contextGetter == null) {
63             return NULL_EXTRACTOR;
64         }
65         final Class<?> returnType = contextGetter.getReturnType();
66         try {
67             if (InstanceIdentifier.class.isAssignableFrom(returnType)) {
68                 return DirectGetterRouteContextExtractor.create(contextGetter);
69             }
70             final Method getValueMethod = findGetValueMethod(returnType, InstanceIdentifier.class);
71             if (getValueMethod != null) {
72                 return GetValueRouteContextExtractor.create(contextGetter, getValueMethod);
73             } else {
74                 LOG.warn("Class {} can not be used to determine context, falling back to NULL_EXTRACTOR.", returnType);
75             }
76         } catch (final IllegalAccessException e) {
77             LOG.warn("Class {} does not conform to Binding Specification v1. Falling back to NULL_EXTRACTOR",
78                 returnType, e);
79         }
80         return NULL_EXTRACTOR;
81     }
82
83     private static @Nullable Method findGetValueMethod(final Class<?> type, final Class<?> returnType) {
84         try {
85             final Method method = type.getMethod(GET_VALUE_NAME);
86             if (returnType.equals(method.getReturnType())) {
87                 return method;
88             }
89         } catch (final NoSuchMethodException e) {
90             LOG.warn("Value class {} does not comform to Binding Specification v1.", type, e);
91         }
92         return null;
93     }
94
95     private static Method getContextGetter(final Class<?> key) {
96         for (final Method method : key.getMethods()) {
97             if (method.getAnnotation(RoutingContext.class) != null) {
98                 return method;
99             }
100         }
101         return null;
102     }
103 }