Use internal classes for codecs
[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.util.List;
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 IdentifiableItemCodec codec;
26
27     KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
28         super(prototype);
29
30         final Class<D> bindingClass = getBindingClass();
31         this.codec = factory().getPathArgumentCodec(bindingClass, getSchema());
32         try {
33             // This just verifies the method is present
34             bindingClass.getMethod(IDENTIFIABLE_KEY_NAME);
35         } catch (NoSuchMethodException e) {
36             throw new IllegalStateException("Required method not available", e);
37         }
38     }
39
40     @Override
41     protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
42             final List<YangInstanceIdentifier.PathArgument> builder) {
43         /*
44          * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This
45          * is also true for wildcarded instance identifiers
46          */
47         if (builder == null) {
48             return;
49         }
50
51         super.addYangPathArgument(arg, builder);
52         if (arg instanceof IdentifiableItem) {
53             builder.add(codec.serialize((IdentifiableItem<?, ?>) arg));
54         } else {
55             // Adding wildcarded
56             super.addYangPathArgument(arg, builder);
57         }
58     }
59
60     @Override
61     @SuppressWarnings("rawtypes")
62     Object getBindingChildValue(final String methodName, final NormalizedNodeContainer dom) {
63         if (dom instanceof MapEntryNode && IDENTIFIABLE_KEY_NAME.equals(methodName)) {
64             NodeIdentifierWithPredicates identifier = ((MapEntryNode) dom).getIdentifier();
65             return codec.deserialize(identifier).getKey();
66         }
67         return super.getBindingChildValue(methodName, dom);
68     }
69
70     @Override
71     protected InstanceIdentifier.PathArgument getBindingPathArgument(final 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(IdentifiableItem.of((Class)getBindingClass(), (Identifier)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 }