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.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;
25 final class KeyedListNodeCodecContext<D extends DataObject & Identifiable<?>> extends ListNodeCodecContext<D> {
26 private final IdentifiableItemCodec codec;
28 KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
29 super(prototype, keyMethod(prototype.getBindingClass()));
30 this.codec = factory().getPathArgumentCodec(getBindingClass(), getSchema());
33 private static Method keyMethod(final Class<?> bindingClass) {
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);
43 protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
44 final List<YangInstanceIdentifier.PathArgument> builder) {
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
49 if (builder == null) {
53 super.addYangPathArgument(arg, builder);
54 if (arg instanceof IdentifiableItem) {
55 builder.add(codec.serialize((IdentifiableItem<?, ?>) arg));
58 super.addYangPathArgument(arg, builder);
63 @SuppressWarnings("rawtypes")
64 Object getBindingChildValue(final String methodName, final NormalizedNodeContainer dom) {
65 if (dom instanceof MapEntryNode && IDENTIFIABLE_KEY_NAME.equals(methodName)) {
66 NodeIdentifierWithPredicates identifier = ((MapEntryNode) dom).getIdentifier();
67 return codec.deserialize(identifier).getKey();
69 return super.getBindingChildValue(methodName, dom);
73 protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
74 if (domArg instanceof NodeIdentifierWithPredicates) {
75 return codec.deserialize((NodeIdentifierWithPredicates) domArg);
77 return super.getBindingPathArgument(domArg);
80 @SuppressWarnings({ "rawtypes", "unchecked" })
81 NodeIdentifierWithPredicates serialize(final Identifier<?> key) {
82 return codec.serialize(IdentifiableItem.of((Class)getBindingClass(), (Identifier)key));
86 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
87 if (arg instanceof IdentifiableItem) {
88 return codec.serialize((IdentifiableItem<?, ?>) arg);
90 return super.serializePathArgument(arg);
94 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
95 if (arg instanceof NodeIdentifierWithPredicates) {
96 return codec.deserialize((NodeIdentifierWithPredicates) arg);
98 return super.deserializePathArgument(arg);