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.ArrayList;
12 import java.util.List;
13 import java.util.Map.Entry;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22 class ListNodeCodecContext<D extends DataObject> extends DataObjectCodecContext<D, ListSchemaNode> {
23 ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
27 ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype,
28 final Entry<Method, IdentifiableItemCodec> keyMethod) {
29 super(prototype, keyMethod);
33 public D deserialize(final NormalizedNode<?, ?> node) {
34 if (node instanceof MapEntryNode) {
35 return fromMapEntry((MapEntryNode) node);
36 } else if (node instanceof UnkeyedListEntryNode) {
37 return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
39 throw new IllegalStateException("Unsupported data type " + node.getClass());
44 protected Object deserializeObject(final NormalizedNode<?, ?> node) {
45 if (node instanceof MapNode) {
46 return fromMap((MapNode) node);
47 } else if (node instanceof MapEntryNode) {
48 return fromMapEntry((MapEntryNode) node);
49 } else if (node instanceof UnkeyedListNode) {
50 return fromUnkeyedList((UnkeyedListNode) node);
51 } else if (node instanceof UnkeyedListEntryNode) {
52 return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
54 throw new IllegalStateException("Unsupported data type " + node.getClass());
58 private List<D> fromMap(final MapNode nodes) {
59 List<D> ret = new ArrayList<>(nodes.getValue().size());
60 for (MapEntryNode node : nodes.getValue()) {
61 ret.add(fromMapEntry(node));
66 private D fromMapEntry(final MapEntryNode node) {
67 return createBindingProxy(node);
70 private D fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
71 return createBindingProxy(node);
74 private List<D> fromUnkeyedList(final UnkeyedListNode nodes) {
75 // FIXME: Could be this lazy transformed list?
76 List<D> ret = new ArrayList<>(nodes.getValue().size());
77 for (UnkeyedListEntryNode node : nodes.getValue()) {
78 ret.add(fromUnkeyedListEntry(node));