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 protected ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype,
23 final Method... additionalMethods) {
24 super(prototype, additionalMethods);
28 public D deserialize(final NormalizedNode<?, ?> node) {
29 if (node instanceof MapEntryNode) {
30 return fromMapEntry((MapEntryNode) node);
31 } else if (node instanceof UnkeyedListEntryNode) {
32 return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
34 throw new IllegalStateException("Unsupported data type " + node.getClass());
39 protected Object deserializeObject(final NormalizedNode<?, ?> node) {
40 if (node instanceof MapNode) {
41 return fromMap((MapNode) node);
42 } else if (node instanceof MapEntryNode) {
43 return fromMapEntry((MapEntryNode) node);
44 } else if (node instanceof UnkeyedListNode) {
45 return fromUnkeyedList((UnkeyedListNode) node);
46 } else if (node instanceof UnkeyedListEntryNode) {
47 return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
49 throw new IllegalStateException("Unsupported data type " + node.getClass());
53 private List<D> fromMap(final MapNode nodes) {
54 List<D> ret = new ArrayList<>(nodes.getValue().size());
55 for (MapEntryNode node : nodes.getValue()) {
56 ret.add(fromMapEntry(node));
61 private D fromMapEntry(final MapEntryNode node) {
62 return createBindingProxy(node);
65 private D fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
66 return createBindingProxy(node);
69 private List<D> fromUnkeyedList(final UnkeyedListNode nodes) {
70 // FIXME: Could be this lazy transformed list?
71 List<D> ret = new ArrayList<>(nodes.getValue().size());
72 for (UnkeyedListEntryNode node : nodes.getValue()) {
73 ret.add(fromUnkeyedListEntry(node));