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