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