2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.javav2.dom.codec.impl.context;
10 import com.google.common.annotations.Beta;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.DataContainerCodecPrototype;
15 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.TreeNodeCodecContext;
16 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
25 * Codec context for serializing and deserializing list node.
31 public class ListNodeCodecContext<D extends TreeNode> extends TreeNodeCodecContext<D, ListSchemaNode> {
34 * Prepare context for list node from prototype.
37 * - codec prototype of list node
39 public ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
45 public D deserialize(@Nonnull final NormalizedNode<?, ?> node) {
46 if (node instanceof MapEntryNode) {
47 return fromMapEntry((MapEntryNode) node);
48 } else if (node instanceof UnkeyedListEntryNode) {
49 return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
51 throw new IllegalStateException("Unsupported data type " + node.getClass());
56 protected Object deserializeObject(final NormalizedNode<?, ?> node) {
57 if (node instanceof MapNode) {
58 return fromMap((MapNode) node);
59 } else if (node instanceof MapEntryNode) {
60 return fromMapEntry((MapEntryNode) node);
61 } else if (node instanceof UnkeyedListNode) {
62 return fromUnkeyedList((UnkeyedListNode) node);
63 } else if (node instanceof UnkeyedListEntryNode) {
64 return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
66 throw new IllegalStateException("Unsupported data type " + node.getClass());
70 private List<D> fromMap(final MapNode nodes) {
71 final List<D> ret = new ArrayList<>(nodes.getValue().size());
72 for (final MapEntryNode node : nodes.getValue()) {
73 ret.add(fromMapEntry(node));
78 private D fromMapEntry(final MapEntryNode node) {
79 return createBindingProxy(node);
82 private D fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
83 return createBindingProxy(node);
86 private List<D> fromUnkeyedList(final UnkeyedListNode nodes) {
87 // FIXME: Could be this lazy transformed list?
88 final List<D> ret = new ArrayList<>(nodes.getValue().size());
89 for (final UnkeyedListEntryNode node : nodes.getValue()) {
90 ret.add(fromUnkeyedListEntry(node));