Bump versions to 13.0.4-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.annotations.VisibleForTesting;
11 import com.google.common.base.Throwables;
12 import java.lang.invoke.MethodHandle;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.MethodType;
15 import java.lang.reflect.Method;
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.opendaylight.yangtools.yang.binding.contract.Naming;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 abstract sealed class ContextReferenceExtractor {
25     @VisibleForTesting
26     static final class Direct extends ContextReferenceExtractor {
27         private final MethodHandle handle;
28
29         private Direct(final MethodHandle rawHandle) {
30             handle = rawHandle.asType(MethodType.methodType(InstanceIdentifier.class, DataObject.class));
31         }
32
33         @VisibleForTesting
34         static ContextReferenceExtractor create(final Method getterMethod) throws IllegalAccessException {
35             return new Direct(MethodHandles.publicLookup().unreflect(getterMethod));
36         }
37
38         @Override
39         InstanceIdentifier<?> extractImpl(final DataObject obj) throws Throwable {
40             return (InstanceIdentifier<?>) handle.invokeExact(obj);
41         }
42     }
43
44     @VisibleForTesting
45     static final class GetValue extends ContextReferenceExtractor {
46         private final MethodHandle contextHandle;
47         private final MethodHandle valueHandle;
48
49         private GetValue(final MethodHandle rawContextHandle, final MethodHandle rawValueHandle) {
50             contextHandle = rawContextHandle.asType(MethodType.methodType(Object.class, DataObject.class));
51             valueHandle = rawValueHandle.asType(MethodType.methodType(InstanceIdentifier.class, Object.class));
52         }
53
54         private static ContextReferenceExtractor create(final Method contextGetter, final Method getValueMethod)
55                 throws IllegalAccessException {
56             final var lookup = MethodHandles.publicLookup();
57             return new GetValue(lookup.unreflect(contextGetter), lookup.unreflect(getValueMethod));
58         }
59
60         @Override
61         InstanceIdentifier<?> extractImpl(final DataObject obj) throws Throwable {
62             final var ctx = contextHandle.invokeExact(obj);
63             return ctx == null ? null : (InstanceIdentifier<?>) valueHandle.invokeExact(ctx);
64         }
65     }
66
67     private static final Logger LOG = LoggerFactory.getLogger(ContextReferenceExtractor.class);
68
69     static @Nullable ContextReferenceExtractor of(final Class<?> type) {
70         final var contextGetter = getContextGetter(type);
71         if (contextGetter == null) {
72             return null;
73         }
74
75         final var returnType = contextGetter.getReturnType();
76         try {
77             if (InstanceIdentifier.class.isAssignableFrom(returnType)) {
78                 return Direct.create(contextGetter);
79             }
80             final var getValueMethod = findGetValueMethod(returnType, InstanceIdentifier.class);
81             if (getValueMethod != null) {
82                 return GetValue.create(contextGetter, getValueMethod);
83             } else {
84                 LOG.warn("Class {} can not be used to determine context, falling back to NULL_EXTRACTOR.", returnType);
85             }
86         } catch (final IllegalAccessException e) {
87             LOG.warn("Class {} does not conform to Binding Specification v1. Falling back to NULL_EXTRACTOR",
88                 returnType, e);
89         }
90         return null;
91     }
92
93     /**
94      * Extract context-reference (Instance Identifier) from a Binding DataObject.
95      *
96      * @param obj DataObject from which context reference should be extracted.
97      *
98      * @return Instance Identifier representing context reference or null, if data object does not contain a context
99      *         reference.
100      */
101     @SuppressWarnings("checkstyle:IllegalCatch")
102     final @Nullable InstanceIdentifier<?> extract(final DataObject obj) {
103         try {
104             return extractImpl(obj);
105         } catch (Throwable e) {
106             Throwables.throwIfUnchecked(e);
107             throw new IllegalStateException(e);
108         }
109     }
110
111     @SuppressWarnings("checkstyle:IllegalThrows")
112     abstract @Nullable InstanceIdentifier<?> extractImpl(DataObject obj) throws Throwable;
113
114     private static @Nullable Method findGetValueMethod(final Class<?> type, final Class<?> returnType) {
115         final Method method;
116         try {
117             method = type.getMethod(Naming.SCALAR_TYPE_OBJECT_GET_VALUE_NAME);
118         } catch (NoSuchMethodException e) {
119             LOG.warn("Value class {} does not comform to Binding Specification v1.", type, e);
120             return null;
121         }
122
123         if (returnType.equals(method.getReturnType())) {
124             return method;
125         }
126         return null;
127     }
128
129     private static Method getContextGetter(final Class<?> type) {
130         for (var method : type.getMethods()) {
131             if (method.getAnnotation(RoutingContext.class) != null) {
132                 return method;
133             }
134         }
135         return null;
136     }
137 }