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