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