a1148ac32d4c028ba62ba94bc70a0e85c9353a21
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / GetValueRouteContextExtractor.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.base.Throwables;
12 import java.lang.invoke.MethodHandle;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.MethodHandles.Lookup;
15 import java.lang.invoke.MethodType;
16 import java.lang.reflect.Method;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19
20 @Deprecated
21 final class GetValueRouteContextExtractor extends ContextReferenceExtractor {
22
23     private static final Lookup PUBLIC_LOOKUP = MethodHandles.publicLookup();
24     private final MethodHandle contextHandle;
25     private final MethodHandle valueHandle;
26
27     private GetValueRouteContextExtractor(final MethodHandle rawContextHandle, final MethodHandle rawValueHandle) {
28         contextHandle = rawContextHandle.asType(MethodType.methodType(Object.class, DataObject.class));
29         valueHandle = rawValueHandle.asType(MethodType.methodType(InstanceIdentifier.class, Object.class));
30     }
31
32     public static ContextReferenceExtractor create(final Method contextGetter, final Method getValueMethod)
33             throws IllegalAccessException {
34         final MethodHandle rawContextHandle = PUBLIC_LOOKUP.unreflect(contextGetter);
35         final MethodHandle rawValueHandle = PUBLIC_LOOKUP.unreflect(getValueMethod);
36         return new GetValueRouteContextExtractor(rawContextHandle, rawValueHandle);
37     }
38
39     @Override
40     @SuppressWarnings("checkstyle:IllegalCatch")
41     InstanceIdentifier<?> extract(final DataObject obj) {
42         try {
43             final Object ctx = contextHandle.invokeExact(obj);
44             if (ctx != null) {
45                 return (InstanceIdentifier<?>) valueHandle.invokeExact(ctx);
46             }
47             return null;
48         } catch (Throwable e) {
49             throw Throwables.propagate(e);
50         }
51     }
52 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.