Eliminate KeyedListNodeCodecContext.Unordered.getKey()
[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.reflect.Method;
16 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.Identifiable;
20 import org.opendaylight.yangtools.yang.binding.Identifier;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28
29 abstract class KeyedListNodeCodecContext<D extends DataObject & Identifiable<?>> extends ListNodeCodecContext<D> {
30     private static final class Ordered<D extends DataObject & Identifiable<?>> extends KeyedListNodeCodecContext<D> {
31         Ordered(final DataContainerCodecPrototype<ListSchemaNode> prototype, final Method keyMethod,
32                 final IdentifiableItemCodec codec) {
33             super(prototype, keyMethod, codec);
34         }
35     }
36
37     private static final class Unordered<D extends DataObject & Identifiable<?>> extends KeyedListNodeCodecContext<D> {
38         Unordered(final DataContainerCodecPrototype<ListSchemaNode> prototype, final Method keyMethod,
39                 final IdentifiableItemCodec codec) {
40             super(prototype, keyMethod, codec);
41         }
42
43         @Override
44         Object fromMap(final MapNode map, final int size) {
45             // FIXME: MDSAL-539: Make this a lazily-populated map
46             final Builder<Object, D> builder = ImmutableMap.builderWithExpectedSize(size);
47             for (MapEntryNode node : map.getValue()) {
48                 final D entry = fromMapEntry(node);
49                 builder.put(entry.key(), entry);
50             }
51             return builder.build();
52         }
53     }
54
55     private final IdentifiableItemCodec codec;
56
57     KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype,
58             final Method keyMethod, final IdentifiableItemCodec codec) {
59         super(prototype, keyMethod);
60         this.codec = requireNonNull(codec);
61     }
62
63     @SuppressWarnings("rawtypes")
64     static KeyedListNodeCodecContext create(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
65         final Class<?> bindingClass = prototype.getBindingClass();
66         final Method keyMethod;
67         try {
68             keyMethod = bindingClass.getMethod(IDENTIFIABLE_KEY_NAME);
69         } catch (NoSuchMethodException e) {
70             throw new IllegalStateException("Required method not available", e);
71         }
72
73         final ListSchemaNode schema = prototype.getSchema();
74         final IdentifiableItemCodec codec = prototype.getFactory().getPathArgumentCodec(bindingClass, schema);
75         return schema.isUserOrdered() ? new Ordered<>(prototype, keyMethod, codec)
76                 : new Unordered<>(prototype, keyMethod, codec);
77     }
78
79     @Override
80     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
81             final List<YangInstanceIdentifier.PathArgument> builder) {
82         /*
83          * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This
84          * is also true for wildcarded instance identifiers
85          */
86         if (builder == null) {
87             return;
88         }
89
90         super.addYangPathArgument(arg, builder);
91         if (arg instanceof IdentifiableItem) {
92             builder.add(codec.serialize((IdentifiableItem<?, ?>) arg));
93         } else {
94             // Adding wildcarded
95             super.addYangPathArgument(arg, builder);
96         }
97     }
98
99     @Override
100     protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
101         if (domArg instanceof NodeIdentifierWithPredicates) {
102             return codec.deserialize((NodeIdentifierWithPredicates) domArg);
103         }
104         return super.getBindingPathArgument(domArg);
105     }
106
107     @SuppressWarnings({ "rawtypes", "unchecked" })
108     NodeIdentifierWithPredicates serialize(final Identifier<?> key) {
109         return codec.serialize(IdentifiableItem.of((Class)getBindingClass(), (Identifier)key));
110     }
111
112     @NonNull Identifier<?> deserialize(final NodeIdentifierWithPredicates arg) {
113         return codec.deserializeIdentifier(arg);
114     }
115
116     @Override
117     public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
118         if (arg instanceof IdentifiableItem) {
119             return codec.serialize((IdentifiableItem<?, ?>) arg);
120         }
121         return super.serializePathArgument(arg);
122     }
123
124     @Override
125     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
126         if (arg instanceof NodeIdentifierWithPredicates) {
127             return codec.deserialize((NodeIdentifierWithPredicates) arg);
128         }
129         return super.deserializePathArgument(arg);
130     }
131 }