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