2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.md.sal.binding.impl;
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;
24 abstract class ContextReferenceExtractor {
26 private static final Logger LOG = LoggerFactory.getLogger(ContextReferenceExtractor.class);
27 private static final ContextReferenceExtractor NULL_EXTRACTOR = new ContextReferenceExtractor() {
30 InstanceIdentifier<?> extract(final DataObject obj) {
36 private static final LoadingCache<Class<?>, ContextReferenceExtractor> EXTRACTORS = CacheBuilder.newBuilder()
37 .weakKeys().build(new CacheLoader<Class<?>, ContextReferenceExtractor>() {
40 public ContextReferenceExtractor load(final Class<?> key) throws Exception {
46 private static final String GET_VALUE_NAME = "getValue";
48 static ContextReferenceExtractor from(final Class<?> obj) {
49 return EXTRACTORS.getUnchecked(obj);
53 * Extract context-reference (Instance Identifier) from
56 * @param obj DataObject from which context reference
57 * should be extracted.
59 * @return Instance Identifier representing context reference
60 * or null, if data object does not contain context reference.
62 abstract @Nullable InstanceIdentifier<?> extract(DataObject obj);
65 private static ContextReferenceExtractor create(final Class<?> key) {
66 final Method contextGetter = getContextGetter(key);
67 if (contextGetter == null) {
68 return NULL_EXTRACTOR;
70 final Class<?> returnType = contextGetter.getReturnType();
72 if (InstanceIdentifier.class.isAssignableFrom(returnType)) {
73 return DirectGetterRouteContextExtractor.create(contextGetter);
75 final Method getValueMethod = findGetValueMethod(returnType,InstanceIdentifier.class);
76 if (getValueMethod != null) {
77 return GetValueRouteContextExtractor.create(contextGetter, getValueMethod);
79 LOG.warn("Class {} can not be used to determine context, falling back to NULL_EXTRACTOR.",returnType);
81 } catch (final IllegalAccessException e) {
82 LOG.warn("Class {} does not conform to Binding Specification v1. Falling back to NULL_EXTRACTOR", e);
84 return NULL_EXTRACTOR;
88 private static Method findGetValueMethod(final Class<?> type, final Class<?> returnType) {
90 final Method method = type.getMethod(GET_VALUE_NAME);
91 if(returnType.equals(method.getReturnType())) {
94 } catch (final NoSuchMethodException e) {
95 LOG.warn("Value class {} does not comform to Binding Specification v1.", type, e);
100 private static Method getContextGetter(final Class<?> key) {
101 for (final Method method : key.getMethods()) {
102 if (method.getAnnotation(RoutingContext.class) != null) {