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 com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.ImmutableMap.Builder;
15 import java.lang.reflect.Method;
16 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.Identifiable;
20 import org.opendaylight.yangtools.yang.binding.Identifier;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
29 abstract class KeyedListNodeCodecContext<D extends DataObject & Identifiable<?>> extends ListNodeCodecContext<D> {
30 private static final class Ordered<D extends DataObject & Identifiable<?>> extends KeyedListNodeCodecContext<D> {
31 Ordered(final DataContainerCodecPrototype<ListSchemaNode> prototype, final Method keyMethod,
32 final IdentifiableItemCodec codec) {
33 super(prototype, keyMethod, codec);
37 private static final class Unordered<D extends DataObject & Identifiable<?>> extends KeyedListNodeCodecContext<D> {
38 Unordered(final DataContainerCodecPrototype<ListSchemaNode> prototype, final Method keyMethod,
39 final IdentifiableItemCodec codec) {
40 super(prototype, keyMethod, codec);
44 Object fromMap(final MapNode map, final int size) {
45 // FIXME: MDSAL-539: Make this a lazily-populated map
46 final Builder<Object, D> builder = ImmutableMap.builderWithExpectedSize(size);
47 for (MapEntryNode node : map.getValue()) {
48 final D entry = fromMapEntry(node);
49 builder.put(entry.key(), entry);
51 return builder.build();
55 private final IdentifiableItemCodec codec;
57 KeyedListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype,
58 final Method keyMethod, final IdentifiableItemCodec codec) {
59 super(prototype, keyMethod);
60 this.codec = requireNonNull(codec);
63 @SuppressWarnings("rawtypes")
64 static KeyedListNodeCodecContext create(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
65 final Class<?> bindingClass = prototype.getBindingClass();
66 final Method keyMethod;
68 keyMethod = bindingClass.getMethod(IDENTIFIABLE_KEY_NAME);
69 } catch (NoSuchMethodException e) {
70 throw new IllegalStateException("Required method not available", e);
73 final ListSchemaNode schema = prototype.getSchema();
74 final IdentifiableItemCodec codec = prototype.getFactory().getPathArgumentCodec(bindingClass, schema);
75 return schema.isUserOrdered() ? new Ordered<>(prototype, keyMethod, codec)
76 : new Unordered<>(prototype, keyMethod, codec);
80 protected void addYangPathArgument(final InstanceIdentifier.PathArgument arg,
81 final List<YangInstanceIdentifier.PathArgument> builder) {
83 * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This
84 * is also true for wildcarded instance identifiers
86 if (builder == null) {
90 super.addYangPathArgument(arg, builder);
91 if (arg instanceof IdentifiableItem) {
92 builder.add(codec.serialize((IdentifiableItem<?, ?>) arg));
95 super.addYangPathArgument(arg, builder);
100 protected InstanceIdentifier.PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
101 if (domArg instanceof NodeIdentifierWithPredicates) {
102 return codec.deserialize((NodeIdentifierWithPredicates) domArg);
104 return super.getBindingPathArgument(domArg);
107 @SuppressWarnings({ "rawtypes", "unchecked" })
108 NodeIdentifierWithPredicates serialize(final Identifier<?> key) {
109 return codec.serialize(IdentifiableItem.of((Class)getBindingClass(), (Identifier)key));
112 @NonNull Identifier<?> deserialize(final NodeIdentifierWithPredicates arg) {
113 return codec.deserializeIdentifier(arg);
117 public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
118 if (arg instanceof IdentifiableItem) {
119 return codec.serialize((IdentifiableItem<?, ?>) arg);
121 return super.serializePathArgument(arg);
125 public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
126 if (arg instanceof NodeIdentifierWithPredicates) {
127 return codec.deserialize((NodeIdentifierWithPredicates) arg);
129 return super.deserializePathArgument(arg);