31711cd7be0abb00918a680b9771d92fb4593639
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / 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.mdsal.binding.dom.adapter;
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 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     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) throws Exception {
39                     return create(key);
40                 }
41
42                 private @NonNull ContextReferenceExtractor create(final Class<?> key) {
43                     final Method contextGetter = getContextGetter(key);
44                     if (contextGetter == null) {
45                         return NULL_EXTRACTOR;
46                     }
47                     final Class<?> returnType = contextGetter.getReturnType();
48                     try {
49                         if (InstanceIdentifier.class.isAssignableFrom(returnType)) {
50                             return DirectGetterRouteContextExtractor.create(contextGetter);
51                         }
52                         final Method getValueMethod = findGetValueMethod(returnType, InstanceIdentifier.class);
53                         if (getValueMethod != null) {
54                             return GetValueRouteContextExtractor.create(contextGetter, getValueMethod);
55                         } else {
56                             LOG.warn("Class {} can not be used to determine context, falling back to NULL_EXTRACTOR.",
57                                     returnType);
58                         }
59                     } catch (final IllegalAccessException e) {
60                         LOG.warn(
61                                 "Class {} does not conform to Binding Specification v1. Falling back to NULL_EXTRACTOR",
62                                 returnType, e);
63                     }
64                     return NULL_EXTRACTOR;
65                 }
66
67                 private Method getContextGetter(final Class<?> key) {
68                     for (final Method method : key.getMethods()) {
69                         if (method.getAnnotation(RoutingContext.class) != null) {
70                             return method;
71                         }
72                     }
73                     return null;
74                 }
75             });
76
77
78     private static final String GET_VALUE_NAME = "getValue";
79
80     static ContextReferenceExtractor from(final Class<?> obj) {
81         return EXTRACTORS.getUnchecked(obj);
82     }
83
84     /**
85      * Extract context-reference (Instance Identifier) from a Binding DataObject.
86      *
87      * @param obj DataObject from which context reference should be extracted.
88      *
89      * @return Instance Identifier representing context reference or null, if data object does not contain a context
90      *         reference.
91      */
92     abstract @Nullable InstanceIdentifier<?> extract(DataObject obj);
93
94     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
95             justification = "https://github.com/spotbugs/spotbugs/issues/811")
96     private static @Nullable Method findGetValueMethod(final Class<?> type, final Class<?> returnType) {
97         try {
98             final Method method = type.getMethod(GET_VALUE_NAME);
99             if (returnType.equals(method.getReturnType())) {
100                 return method;
101             }
102         } catch (final NoSuchMethodException e) {
103             LOG.warn("Value class {} does not comform to Binding Specification v1.", type, e);
104         }
105         return null;
106     }
107 }