d8cac9d4660d89cc2e022c900d835f30785688e1
[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 java.lang.reflect.Method;
11 import java.util.List;
12 import org.opendaylight.yangtools.concepts.Codec;
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14 import org.opendaylight.yangtools.yang.binding.Identifiable;
15 import org.opendaylight.yangtools.yang.binding.Identifier;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23
24 final class KeyedListNodeCodecContext<D extends DataObject & Identifiable<?>> extends ListNodeCodecContext<D> {
25     private final Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> codec;
26     private final Method keyGetter;
27
28     KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
29         super(prototype);
30
31         this.codec = factory().getPathArgumentCodec(getBindingClass(), getSchema());
32         try {
33             this.keyGetter = getBindingClass().getMethod("getKey");
34         } catch (NoSuchMethodException e) {
35             throw new IllegalStateException("Required method not available", e);
36         }
37     }
38
39     @Override
40     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg, final List<YangInstanceIdentifier.PathArgument> builder) {
41         /*
42          * DOM Instance Identifier for list is always represent by two
43          * entries one for map and one for children. This is also true for
44          * wildcarded instance identifiers
45          */
46         if (builder == null) {
47             return;
48         }
49
50         super.addYangPathArgument(arg, builder);
51         if (arg instanceof IdentifiableItem<?, ?>) {
52             builder.add(codec.serialize((IdentifiableItem<?, ?>) arg));
53         } else {
54             // Adding wildcarded
55             super.addYangPathArgument(arg, builder);
56         }
57     }
58
59     @Override
60     @SuppressWarnings("rawtypes")
61     Object getBindingChildValue(final Method method, final NormalizedNodeContainer dom) {
62         if (dom instanceof MapEntryNode && keyGetter.equals(method)) {
63             NodeIdentifierWithPredicates identifier = ((MapEntryNode) dom).getIdentifier();
64             return codec.deserialize(identifier).getKey();
65         } else {
66             return super.getBindingChildValue(method, dom);
67         }
68     }
69
70     @Override
71     protected InstanceIdentifier.PathArgument getBindingPathArgument(final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument domArg) {
72         if (domArg instanceof NodeIdentifierWithPredicates) {
73             return codec.deserialize((NodeIdentifierWithPredicates) domArg);
74         }
75         return super.getBindingPathArgument(domArg);
76     }
77
78     @SuppressWarnings({ "rawtypes", "unchecked" })
79     NodeIdentifierWithPredicates serialize(final Identifier<?> key) {
80         return codec.serialize(new IdentifiableItem(getBindingClass(), key));
81     }
82
83     @Override
84     public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
85         if(arg instanceof IdentifiableItem) {
86             return codec.serialize((IdentifiableItem<?,?>) arg);
87         }
88         return super.serializePathArgument(arg);
89     }
90
91     @Override
92     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
93         if(arg instanceof NodeIdentifierWithPredicates) {
94             return codec.deserialize((NodeIdentifierWithPredicates) arg);
95         }
96         return super.deserializePathArgument(arg);
97     }
98
99
100 }