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 java.lang.reflect.Method;
11 import java.util.List;
12 import org.opendaylight.yangtools.concepts.Codec;
13 import org.opendaylight.yangtools.yang.binding.BindingMapping;
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;
27 private final Method keyGetter;
29 KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
32 this.codec = factory().getPathArgumentCodec(getBindingClass(), getSchema());
34 this.keyGetter = getBindingClass().getMethod(BindingMapping.IDENTIFIABLE_KEY_NAME);
35 } catch (NoSuchMethodException e) {
36 throw new IllegalStateException("Required method not available", e);
41 protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
42 final List<YangInstanceIdentifier.PathArgument> builder) {
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
47 if (builder == null) {
51 super.addYangPathArgument(arg, builder);
52 if (arg instanceof IdentifiableItem) {
53 builder.add(codec.serialize((IdentifiableItem<?, ?>) arg));
56 super.addYangPathArgument(arg, builder);
61 @SuppressWarnings("rawtypes")
62 Object getBindingChildValue(final Method method, final NormalizedNodeContainer dom) {
63 if (dom instanceof MapEntryNode && keyGetter.equals(method)) {
64 NodeIdentifierWithPredicates identifier = ((MapEntryNode) dom).getIdentifier();
65 return codec.deserialize(identifier).getKey();
67 return super.getBindingChildValue(method, dom);
71 protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
72 if (domArg instanceof NodeIdentifierWithPredicates) {
73 return codec.deserialize((NodeIdentifierWithPredicates) domArg);
75 return super.getBindingPathArgument(domArg);
78 @SuppressWarnings({ "rawtypes", "unchecked" })
79 NodeIdentifierWithPredicates serialize(final Identifier<?> key) {
80 return codec.serialize(IdentifiableItem.of((Class)getBindingClass(), (Identifier)key));
84 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
85 if (arg instanceof IdentifiableItem) {
86 return codec.serialize((IdentifiableItem<?, ?>) arg);
88 return super.serializePathArgument(arg);
92 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
93 if (arg instanceof NodeIdentifierWithPredicates) {
94 return codec.deserialize((NodeIdentifierWithPredicates) arg);
96 return super.deserializePathArgument(arg);