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 java.util.Objects.requireNonNull;
11 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.IDENTIFIABLE_KEY_NAME;
13 import java.lang.reflect.Method;
14 import java.util.AbstractMap.SimpleImmutableEntry;
15 import java.util.List;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.Identifiable;
18 import org.opendaylight.yangtools.yang.binding.Identifier;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
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 private KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype,
29 final Method keyMethod, final IdentifiableItemCodec codec) {
30 super(prototype, new SimpleImmutableEntry<>(keyMethod, codec));
31 this.codec = requireNonNull(codec);
34 @SuppressWarnings("rawtypes")
35 static KeyedListNodeCodecContext create(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
36 final Class<?> bindingClass = prototype.getBindingClass();
37 final Method keyMethod;
39 keyMethod = bindingClass.getMethod(IDENTIFIABLE_KEY_NAME);
40 } catch (NoSuchMethodException e) {
41 throw new IllegalStateException("Required method not available", e);
44 final IdentifiableItemCodec codec = prototype.getFactory().getPathArgumentCodec(bindingClass,
45 prototype.getSchema());
46 return new KeyedListNodeCodecContext<>(prototype, keyMethod, codec);
50 protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
51 final List<YangInstanceIdentifier.PathArgument> builder) {
53 * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This
54 * is also true for wildcarded instance identifiers
56 if (builder == null) {
60 super.addYangPathArgument(arg, builder);
61 if (arg instanceof IdentifiableItem) {
62 builder.add(codec.serialize((IdentifiableItem<?, ?>) arg));
65 super.addYangPathArgument(arg, builder);
70 protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
71 if (domArg instanceof NodeIdentifierWithPredicates) {
72 return codec.deserialize((NodeIdentifierWithPredicates) domArg);
74 return super.getBindingPathArgument(domArg);
77 @SuppressWarnings({ "rawtypes", "unchecked" })
78 NodeIdentifierWithPredicates serialize(final Identifier<?> key) {
79 return codec.serialize(IdentifiableItem.of((Class)getBindingClass(), (Identifier)key));
83 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
84 if (arg instanceof IdentifiableItem) {
85 return codec.serialize((IdentifiableItem<?, ?>) arg);
87 return super.serializePathArgument(arg);
91 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
92 if (arg instanceof NodeIdentifierWithPredicates) {
93 return codec.deserialize((NodeIdentifierWithPredicates) arg);
95 return super.deserializePathArgument(arg);