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