Binding2 runtime - Codecs impl - context - part2
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / context / UnionValueOptionContext.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.javav2.dom.codec.impl.context;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Throwables;
13 import java.lang.invoke.MethodHandle;
14 import java.lang.invoke.MethodHandles;
15 import java.lang.invoke.MethodType;
16 import java.lang.reflect.Method;
17 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.EncapsulatedValueCodec;
18 import org.opendaylight.yangtools.concepts.Codec;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Context for serializing input value of union type and deserializing objects to binding.
24  *
25  */
26 @Beta
27 public class UnionValueOptionContext {
28
29     private static final Logger LOG = LoggerFactory.getLogger(UnionValueOptionContext.class);
30
31     private static final MethodType OBJECT_TYPE = MethodType.methodType(Object.class, Object.class);
32
33     private final Class<?> bindingType;
34     private final Codec<Object, Object> codec;
35     private final MethodHandle getter;
36     private final MethodHandle unionCtor;
37
38     /**
39      * Prepare union as binding object and codec for this object, make a direct method handle of getting union
40      * type and constructor of union type for initializing it.
41      *
42      * @param unionType
43      *            - union as binding object
44      * @param valueType
45      *            - returned type of union
46      * @param getter
47      *            - method for getting union type
48      * @param codec
49      *            - codec for serialize/deserialize type of union
50      */
51     public UnionValueOptionContext(final Class<?> unionType, final Class<?> valueType, final Method getter,
52             final Codec<Object, Object> codec) {
53         this.bindingType = Preconditions.checkNotNull(valueType);
54         this.codec = Preconditions.checkNotNull(codec);
55
56         try {
57             this.getter = MethodHandles.publicLookup().unreflect(getter).asType(OBJECT_TYPE);
58         } catch (final IllegalAccessException e) {
59             throw new IllegalStateException("Failed to access method " + getter, e);
60         }
61
62         try {
63             this.unionCtor = MethodHandles.publicLookup()
64                     .findConstructor(unionType, MethodType.methodType(void.class, valueType)).asType(OBJECT_TYPE);
65         } catch (IllegalAccessException | NoSuchMethodException e) {
66             throw new IllegalStateException(
67                     String.format("Failed to access constructor for %s in type %s", valueType, unionType), e);
68         }
69     }
70
71     /**
72      * Serialize input based on prepared codec.
73      *
74      * @param input
75      *            - object to serialize
76      * @return serialized objetc
77      */
78     public Object serialize(final Object input) {
79         final Object baValue = getValueFrom(input);
80         return baValue == null ? null : codec.serialize(baValue);
81     }
82
83     /**
84      * Deserialize input object via prepared codec for invoking new object of union as binding.
85      *
86      * @param input
87      *            - input object for deserializing
88      * @return deserialized union binding type object
89      */
90     public Object deserializeUnion(final Object input) {
91         // Side-step potential exceptions by checking the type if it is available
92         if (codec instanceof EncapsulatedValueCodec && !((EncapsulatedValueCodec) codec).canAcceptObject(input)) {
93             return null;
94         }
95
96         final Object value;
97         try {
98             value = codec.deserialize(input);
99         } catch (final Exception e) {
100             LOG.debug("Codec {} failed to deserialize input {}", codec, input, e);
101             return null;
102         }
103
104         try {
105             return unionCtor.invokeExact(value);
106         } catch (final ClassCastException e) {
107             // This case can happen. e.g. NOOP_CODEC
108             LOG.debug("Failed to instantiate {} for input {} value {}", bindingType, input, value, e);
109             return null;
110         } catch (final Throwable e) {
111             throw new IllegalArgumentException("Failed to construct union for value " + value, e);
112         }
113     }
114
115     private Object getValueFrom(final Object input) {
116         try {
117             return getter.invokeExact(input);
118         } catch (final Throwable e) {
119             throw Throwables.propagate(e);
120         }
121     }
122
123     @Override
124     public int hashCode() {
125         return bindingType.hashCode();
126     }
127
128     @Override
129     public boolean equals(final Object obj) {
130         if (this == obj) {
131             return true;
132         }
133         if (!(obj instanceof UnionValueOptionContext)) {
134             return false;
135         }
136
137         final UnionValueOptionContext other = (UnionValueOptionContext) obj;
138         return bindingType.equals(other.bindingType);
139     }
140 }