Clean YangStatementParserListenerImpl up
[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.NodeIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
18 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
19
20 public class UnkeyedListNodeModification implements Modification<ListSchemaNode, UnkeyedListNode> {
21
22     public static final MapEntryNodeModification MAP_ENTRY_NODE_MODIFICATION = new MapEntryNodeModification();
23
24     @Override
25     public Optional<UnkeyedListNode> modify(ListSchemaNode schema, Optional<UnkeyedListNode> actual,
26             Optional<UnkeyedListNode> modification, OperationStack operationStack) throws DataModificationException {
27
28         // Merge or None operation on parent, leaving actual if modification not present
29         if (!modification.isPresent()) {
30             return actual;
31         }
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             switch (operationStack.getCurrentOperation()) {
44             case NONE:
45                 break;
46             case MERGE:
47             case CREATE: {
48                 DataModificationException.DataExistsException.check(schema.getQName(), actual,
49                         unkeyedListEntryModification);
50                 resultNodes.add(unkeyedListEntryModification);
51             }
52             case REPLACE: {
53                 break;
54             }
55             case DELETE: {
56                 break;
57             }
58             case REMOVE: {
59                 break;
60             }
61             default:
62                 throw new UnsupportedOperationException(
63                         String.format("Unable to perform operation: %s on: %s, unknown",
64                                 operationStack.getCurrentOperation(), schema));
65             }
66
67             operationStack.exitingNode(unkeyedListEntryModification);
68         }
69         return build(schema, resultNodes);
70     }
71
72     private Optional<UnkeyedListNode> build(ListSchemaNode schema, List<UnkeyedListEntryNode> resultNodes) {
73         if (resultNodes.isEmpty()) {
74             return Optional.absent();
75         }
76
77         CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> b = Builders.unkeyedListBuilder();
78         b.withNodeIdentifier(new NodeIdentifier(schema.getQName()));
79
80         for (UnkeyedListEntryNode child : resultNodes) {
81             b.withChild(child);
82         }
83
84         return Optional.of(b.build());
85     }
86
87     private List<UnkeyedListEntryNode> unkeyedListEntries(UnkeyedListNode unkeyedListNode) {
88         List<UnkeyedListEntryNode> unkeyedListEntries = Lists.newArrayList();
89
90         for (UnkeyedListEntryNode unkeyedListEntryNode : unkeyedListNode.getValue()) {
91             unkeyedListEntries.add(unkeyedListEntryNode);
92         }
93
94         return unkeyedListEntries;
95     }
96
97 }