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