BUG 1975 - yang unkeyed list is transformed to map node
[yangtools.git] / yang / yang-data-operations / src / main / java / org / opendaylight / yangtools / yang / data / operations / UnkeyedListNodeModification.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.operations;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Lists;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
19 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
20
21 public class UnkeyedListNodeModification implements Modification<ListSchemaNode, UnkeyedListNode> {
22
23     public static final MapEntryNodeModification MAP_ENTRY_NODE_MODIFICATION = new MapEntryNodeModification();
24
25     @Override
26     public Optional<UnkeyedListNode> modify(ListSchemaNode schema, Optional<UnkeyedListNode> actual,
27             Optional<UnkeyedListNode> modification, OperationStack operationStack) throws DataModificationException {
28
29         // Merge or None operation on parent, leaving actual if modification not present
30         if (modification.isPresent() == false)
31             return actual;
32
33         List<UnkeyedListEntryNode> resultNodes = Lists.newArrayList();
34         if (actual.isPresent())
35             resultNodes = unkeyedListEntries(actual.get());
36
37         // TODO implement ordering for modification nodes
38
39         for (UnkeyedListEntryNode unkeyedListEntryModification : modification.get().getValue()) {
40
41             operationStack.enteringNode(unkeyedListEntryModification);
42
43             YangInstanceIdentifier.NodeIdentifier entryKey = unkeyedListEntryModification.getIdentifier();
44
45             switch (operationStack.getCurrentOperation()) {
46             case NONE:
47                 break;
48             // DataModificationException.DataMissingException.check(schema.getQName(), actual, mapEntryModification);
49             case MERGE:
50             case CREATE: {
51                 DataModificationException.DataExistsException.check(schema.getQName(), actual,
52                         unkeyedListEntryModification);
53                 resultNodes.add(unkeyedListEntryModification);
54             }
55             case REPLACE: {
56                 break;
57             }
58             case DELETE: {
59                 // DataModificationException.DataMissingException.check(schema.getQName(), actual,
60                 // unkeyedListEntryModification);
61                 break;
62             }
63             case REMOVE: {
64                 break;
65             }
66             default:
67                 throw new UnsupportedOperationException(
68                         String.format("Unable to perform operation: %s on: %s, unknown",
69                                 operationStack.getCurrentOperation(), schema));
70             }
71
72             operationStack.exitingNode(unkeyedListEntryModification);
73         }
74         return build(schema, resultNodes);
75     }
76
77     private Optional<UnkeyedListNode> build(ListSchemaNode schema, List<UnkeyedListEntryNode> resultNodes) {
78         if (resultNodes.isEmpty())
79             return Optional.absent();
80
81         CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> b = Builders.unkeyedListBuilder();
82         b.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
83
84         for (UnkeyedListEntryNode child : resultNodes) {
85             b.withChild(child);
86         }
87
88         return Optional.of(b.build());
89     }
90
91     private List<UnkeyedListEntryNode> unkeyedListEntries(UnkeyedListNode unkeyedListNode) {
92         List<UnkeyedListEntryNode> unkeyedListEntries = Lists.newArrayList();
93
94         for (UnkeyedListEntryNode unkeyedListEntryNode : unkeyedListNode.getValue()) {
95             unkeyedListEntries.add(unkeyedListEntryNode);
96         }
97
98         return unkeyedListEntries;
99     }
100
101 }