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