Index getter methods by String
[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.util.List;
11 import org.opendaylight.yangtools.concepts.Codec;
12 import org.opendaylight.yangtools.yang.binding.DataObject;
13 import org.opendaylight.yangtools.yang.binding.Identifiable;
14 import org.opendaylight.yangtools.yang.binding.Identifier;
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22
23 final class KeyedListNodeCodecContext<D extends DataObject & Identifiable<?>> extends ListNodeCodecContext<D> {
24     private final Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> codec;
25
26     KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
27         super(prototype);
28
29         final Class<D> bindingClass = getBindingClass();
30         this.codec = factory().getPathArgumentCodec(bindingClass, getSchema());
31         try {
32             // This just verifies the method is present
33             bindingClass.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,
41             final List<YangInstanceIdentifier.PathArgument> builder) {
42         /*
43          * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This
44          * is also true for 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 String methodName, final NormalizedNodeContainer dom) {
62         if (dom instanceof MapEntryNode && "getKey".equals(methodName)) {
63             NodeIdentifierWithPredicates identifier = ((MapEntryNode) dom).getIdentifier();
64             return codec.deserialize(identifier).getKey();
65         }
66         return super.getBindingChildValue(methodName, dom);
67     }
68
69     @Override
70     protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
71         if (domArg instanceof NodeIdentifierWithPredicates) {
72             return codec.deserialize((NodeIdentifierWithPredicates) domArg);
73         }
74         return super.getBindingPathArgument(domArg);
75     }
76
77     @SuppressWarnings({ "rawtypes", "unchecked" })
78     NodeIdentifierWithPredicates serialize(final Identifier<?> key) {
79         return codec.serialize(new IdentifiableItem(getBindingClass(), key));
80     }
81
82     @Override
83     public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
84         if (arg instanceof IdentifiableItem) {
85             return codec.serialize((IdentifiableItem<?,?>) arg);
86         }
87         return super.serializePathArgument(arg);
88     }
89
90     @Override
91     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
92         if (arg instanceof NodeIdentifierWithPredicates) {
93             return codec.deserialize((NodeIdentifierWithPredicates) arg);
94         }
95         return super.deserializePathArgument(arg);
96     }
97 }