453958fd871aa2e41cc1c99bc2113feb676e91e4
[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
24 abstract class ContextReferenceExtractor {
25
26     private static final Logger LOG = LoggerFactory.getLogger(ContextReferenceExtractor.class);
27     private static final ContextReferenceExtractor NULL_EXTRACTOR = new ContextReferenceExtractor() {
28
29         @Override
30         InstanceIdentifier<?> extract(final DataObject obj) {
31             return null;
32         }
33     };
34
35
36     private static final LoadingCache<Class<?>, ContextReferenceExtractor> EXTRACTORS = CacheBuilder.newBuilder()
37             .weakKeys().build(new CacheLoader<Class<?>, ContextReferenceExtractor>() {
38
39                 @Override
40                 public ContextReferenceExtractor load(final Class<?> key) throws Exception {
41                     return create(key);
42                 }
43             });
44
45
46     private static final String GET_VALUE_NAME = "getValue";
47
48     static ContextReferenceExtractor from(final Class<?> obj) {
49         return EXTRACTORS.getUnchecked(obj);
50     }
51
52     /**
53      * Extract context-reference (Instance Identifier) from
54      * Binding DataObject.
55      *
56      * @param obj DataObject from which context reference
57      * should be extracted.
58      *
59      * @return Instance Identifier representing context reference
60      * or null, if data object does not contain context reference.
61      */
62     abstract @Nullable InstanceIdentifier<?> extract(DataObject obj);
63
64     private static @Nonnull ContextReferenceExtractor create(final Class<?> key) {
65         final Method contextGetter = getContextGetter(key);
66         if (contextGetter == null) {
67             return NULL_EXTRACTOR;
68         }
69         final Class<?> returnType = contextGetter.getReturnType();
70         try {
71             if (InstanceIdentifier.class.isAssignableFrom(returnType)) {
72                 return DirectGetterRouteContextExtractor.create(contextGetter);
73             }
74             final Method getValueMethod = findGetValueMethod(returnType,InstanceIdentifier.class);
75             if (getValueMethod != null) {
76                 return GetValueRouteContextExtractor.create(contextGetter, getValueMethod);
77             } else {
78                 LOG.warn("Class {} can not be used to determine context, falling back to NULL_EXTRACTOR.",returnType);
79             }
80         } catch (final IllegalAccessException e) {
81             LOG.warn("Class {} does not conform to Binding Specification v1. Falling back to NULL_EXTRACTOR", 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
107
108
109 }