Squash empty MapNode/UnkeyedListNode objects to null
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / KeyedListNodeCodecContext.java
1 /*
2  * Copyright (c) 2014 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.codec.impl;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.IDENTIFIABLE_KEY_NAME;
12
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableMap.Builder;
15 import java.lang.invoke.MethodHandle;
16 import java.lang.invoke.MethodHandles;
17 import java.lang.invoke.MethodType;
18 import java.lang.invoke.WrongMethodTypeException;
19 import java.lang.reflect.Method;
20 import java.util.Collection;
21 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.Identifiable;
25 import org.opendaylight.yangtools.yang.binding.Identifier;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33
34 abstract class KeyedListNodeCodecContext<D extends DataObject & Identifiable<?>> extends ListNodeCodecContext<D> {
35     private static final class Ordered<D extends DataObject & Identifiable<?>> extends KeyedListNodeCodecContext<D> {
36         Ordered(final DataContainerCodecPrototype<ListSchemaNode> prototype, final Method keyMethod,
37                 final IdentifiableItemCodec codec) {
38             super(prototype, keyMethod, codec);
39         }
40     }
41
42     private static final class Unordered<D extends DataObject & Identifiable<?>> extends KeyedListNodeCodecContext<D> {
43         private static final MethodType KEY_TYPE = MethodType.methodType(Object.class, DataObject.class);
44
45         private final MethodHandle keyHandle;
46
47         Unordered(final DataContainerCodecPrototype<ListSchemaNode> prototype, final Method keyMethod,
48                 final IdentifiableItemCodec codec) {
49             super(prototype, keyMethod, codec);
50
51             try {
52                 this.keyHandle = MethodHandles.publicLookup().unreflect(keyMethod).asType(KEY_TYPE);
53             } catch (IllegalAccessException | WrongMethodTypeException e) {
54                 throw new LinkageError("Failed to acquire method " + keyMethod, e);
55             }
56         }
57
58         @Override
59         Object fromMap(final MapNode map, final Collection<MapEntryNode> value) {
60             // FIXME: Could be this lazy transformed map?
61             final Builder<Object, D> builder = ImmutableMap.builderWithExpectedSize(value.size());
62             for (MapEntryNode node : value) {
63                 final D entry = fromMapEntry(node);
64                 builder.put(getKey(entry), entry);
65             }
66             return builder.build();
67         }
68
69         @SuppressWarnings("checkstyle:illegalCatch")
70         private Object getKey(final D entry) {
71             try {
72                 return keyHandle.invokeExact(entry);
73             } catch (Throwable e) {
74                 throw new LinkageError("Failed to extract key from " + entry, e);
75             }
76         }
77     }
78
79     private final IdentifiableItemCodec codec;
80
81     KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype,
82             final Method keyMethod, final IdentifiableItemCodec codec) {
83         super(prototype, keyMethod);
84         this.codec = requireNonNull(codec);
85     }
86
87     @SuppressWarnings("rawtypes")
88     static KeyedListNodeCodecContext create(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
89         final Class<?> bindingClass = prototype.getBindingClass();
90         final Method keyMethod;
91         try {
92             keyMethod = bindingClass.getMethod(IDENTIFIABLE_KEY_NAME);
93         } catch (NoSuchMethodException e) {
94             throw new IllegalStateException("Required method not available", e);
95         }
96
97         final ListSchemaNode schema = prototype.getSchema();
98         final IdentifiableItemCodec codec = prototype.getFactory().getPathArgumentCodec(bindingClass, schema);
99         return schema.isUserOrdered() ? new Ordered<>(prototype, keyMethod, codec)
100                 : new Unordered<>(prototype, keyMethod, codec);
101     }
102
103     @Override
104     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
105             final List<YangInstanceIdentifier.PathArgument> builder) {
106         /*
107          * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This
108          * is also true for wildcarded instance identifiers
109          */
110         if (builder == null) {
111             return;
112         }
113
114         super.addYangPathArgument(arg, builder);
115         if (arg instanceof IdentifiableItem) {
116             builder.add(codec.serialize((IdentifiableItem<?, ?>) arg));
117         } else {
118             // Adding wildcarded
119             super.addYangPathArgument(arg, builder);
120         }
121     }
122
123     @Override
124     protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
125         if (domArg instanceof NodeIdentifierWithPredicates) {
126             return codec.deserialize((NodeIdentifierWithPredicates) domArg);
127         }
128         return super.getBindingPathArgument(domArg);
129     }
130
131     @SuppressWarnings({ "rawtypes", "unchecked" })
132     NodeIdentifierWithPredicates serialize(final Identifier<?> key) {
133         return codec.serialize(IdentifiableItem.of((Class)getBindingClass(), (Identifier)key));
134     }
135
136     @NonNull Identifier<?> deserialize(final NodeIdentifierWithPredicates arg) {
137         return codec.deserializeIdentifier(arg);
138     }
139
140     @Override
141     public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
142         if (arg instanceof IdentifiableItem) {
143             return codec.serialize((IdentifiableItem<?, ?>) arg);
144         }
145         return super.serializePathArgument(arg);
146     }
147
148     @Override
149     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
150         if (arg instanceof NodeIdentifierWithPredicates) {
151             return codec.deserialize((NodeIdentifierWithPredicates) arg);
152         }
153         return super.deserializePathArgument(arg);
154     }
155 }