4653ca399eeab995bd5890cb1a4dd7a41c9075d0
[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 terms of the Eclipse
5  * Public License v1.0 which accompanies this distribution, and is available at
6  * 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 javax.annotation.Nonnull;
15 import javax.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
23 abstract class ContextReferenceExtractor {
24
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
35     private static final LoadingCache<Class<?>, ContextReferenceExtractor> EXTRACTORS = CacheBuilder.newBuilder()
36             .weakKeys().build(new CacheLoader<Class<?>, ContextReferenceExtractor>() {
37
38                 @Override
39                 public ContextReferenceExtractor load(final Class<?> key) throws Exception {
40                     return create(key);
41                 }
42             });
43
44
45     private static final String GET_VALUE_NAME = "getValue";
46
47     static ContextReferenceExtractor from(final Class<?> obj) {
48         return EXTRACTORS.getUnchecked(obj);
49     }
50
51     /**
52      * Extract context-reference (Instance Identifier) from
53      * Binding DataObject.
54      *
55      * @param obj DataObject from which context reference
56      * should be extracted.
57      *
58      * @return Instance Identifier representing context reference
59      * or null, if data object does not contain context reference.
60      */
61     abstract @Nullable InstanceIdentifier<?> extract(DataObject obj);
62
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", e);
81         }
82         return NULL_EXTRACTOR;
83     }
84
85     private static @Nullable Method findGetValueMethod(final Class<?> type, final Class<?> returnType) {
86         try {
87             final Method method = type.getMethod(GET_VALUE_NAME);
88             if(returnType.equals(method.getReturnType())) {
89                 return method;
90             }
91         } catch (final NoSuchMethodException e) {
92             LOG.warn("Value class {} does not comform to Binding Specification v1.", type, e);
93         }
94         return null;
95     }
96
97     private static Method getContextGetter(final Class<?> key) {
98         for (final Method method : key.getMethods()) {
99             if (method.getAnnotation(RoutingContext.class) != null) {
100                 return method;
101             }
102         }
103         return null;
104     }
105
106
107
108 }