2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.IDENTIFIABLE_KEY_NAME;
12 import java.util.List;
13 import org.opendaylight.yangtools.concepts.Codec;
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;
25 final class KeyedListNodeCodecContext<D extends DataObject & Identifiable<?>> extends ListNodeCodecContext<D> {
26 private final Codec<NodeIdentifierWithPredicates, IdentifiableItem<?, ?>> codec;
28 KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
31 final Class<D> bindingClass = getBindingClass();
32 this.codec = factory().getPathArgumentCodec(bindingClass, getSchema());
34 // This just verifies the method is present
35 bindingClass.getMethod(IDENTIFIABLE_KEY_NAME);
36 } catch (NoSuchMethodException e) {
37 throw new IllegalStateException("Required method not available", e);
42 protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
43 final List<YangInstanceIdentifier.PathArgument> builder) {
45 * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This
46 * is also true for wildcarded instance identifiers
48 if (builder == null) {
52 super.addYangPathArgument(arg, builder);
53 if (arg instanceof IdentifiableItem) {
54 builder.add(codec.serialize((IdentifiableItem<?, ?>) arg));
57 super.addYangPathArgument(arg, builder);
62 @SuppressWarnings("rawtypes")
63 Object getBindingChildValue(final String methodName, final NormalizedNodeContainer dom) {
64 if (dom instanceof MapEntryNode && IDENTIFIABLE_KEY_NAME.equals(methodName)) {
65 NodeIdentifierWithPredicates identifier = ((MapEntryNode) dom).getIdentifier();
66 return codec.deserialize(identifier).getKey();
68 return super.getBindingChildValue(methodName, dom);
72 protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
73 if (domArg instanceof NodeIdentifierWithPredicates) {
74 return codec.deserialize((NodeIdentifierWithPredicates) domArg);
76 return super.getBindingPathArgument(domArg);
79 @SuppressWarnings({ "rawtypes", "unchecked" })
80 NodeIdentifierWithPredicates serialize(final Identifier<?> key) {
81 return codec.serialize(IdentifiableItem.of((Class)getBindingClass(), (Identifier)key));
85 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
86 if (arg instanceof IdentifiableItem) {
87 return codec.serialize((IdentifiableItem<?, ?>) arg);
89 return super.serializePathArgument(arg);
93 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
94 if (arg instanceof NodeIdentifierWithPredicates) {
95 return codec.deserialize((NodeIdentifierWithPredicates) arg);
97 return super.deserializePathArgument(arg);