0a754a5615000d81968caeca851af2978ad6acc4
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / InmemoryDOMDataTreeShardWriteTransaction.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.dom.store.inmemory;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.Iterator;
15 import org.opendaylight.mdsal.common.api.ReadFailedException;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21
22 class InmemoryDOMDataTreeShardWriteTransaction implements DOMDataTreeShardWriteTransaction {
23     private enum SimpleCursorOperation {
24         MERGE {
25             @Override
26             void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
27                     final NormalizedNode<?, ?> data) {
28                 cursor.merge(child, data);
29             }
30         },
31         DELETE {
32             @Override
33             void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
34                     final NormalizedNode<?, ?> data) {
35                 cursor.delete(child);
36             }
37         },
38         WRITE {
39             @Override
40             void applyOnLeaf(final DOMDataTreeWriteCursor cursor, final PathArgument child,
41                     final NormalizedNode<?, ?> data) {
42                 cursor.write(child, data);
43             }
44         };
45
46         abstract void applyOnLeaf(DOMDataTreeWriteCursor cursor, PathArgument child, NormalizedNode<?, ?> data);
47
48         void apply(final DOMDataTreeWriteCursor cursor, final YangInstanceIdentifier path,
49                 final NormalizedNode<?, ?> data) {
50             int enterCount = 0;
51             Iterator<PathArgument> it = path.getPathArguments().iterator();
52             while (it.hasNext()) {
53                 PathArgument currentArg = it.next();
54                 if (it.hasNext()) {
55                     // We need to enter one level deeper, we are not at leaf (modified) node
56                     cursor.enter(currentArg);
57                     enterCount++;
58                 } else {
59                     applyOnLeaf(cursor, currentArg, data);
60                 }
61             }
62             cursor.exit(enterCount);
63         }
64     }
65
66     private final ShardDataModification modification;
67     private DOMDataTreeWriteCursor cursor;
68
69     InmemoryDOMDataTreeShardWriteTransaction(final ShardDataModification root) {
70         this.modification = Preconditions.checkNotNull(root);
71     }
72
73     private DOMDataTreeWriteCursor getCursor() {
74         if (cursor == null) {
75             cursor = new ShardDataModificationCursor(modification);
76         }
77         return cursor;
78     }
79
80     void delete(final YangInstanceIdentifier path) {
81         YangInstanceIdentifier relativePath = toRelative(path);
82         Preconditions.checkArgument(!YangInstanceIdentifier.EMPTY.equals(relativePath),
83                 "Deletion of shard root is not allowed");
84         SimpleCursorOperation.DELETE.apply(getCursor(), relativePath , null);
85     }
86
87     void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
88         SimpleCursorOperation.MERGE.apply(getCursor(), toRelative(path), data);
89     }
90
91     void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
92         SimpleCursorOperation.DELETE.apply(getCursor(), toRelative(path), data);
93     }
94
95     private YangInstanceIdentifier toRelative(final YangInstanceIdentifier path) {
96         Optional<YangInstanceIdentifier> relative =
97                 path.relativeTo(modification.getPrefix().getRootIdentifier());
98         Preconditions.checkArgument(relative.isPresent());
99         return relative.get();
100     }
101
102     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
103         // FIXME: Implement this
104         return null;
105     }
106
107     public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
108         // TODO Auto-generated method stub
109         return null;
110     }
111
112
113     public Object getIdentifier() {
114         // TODO Auto-generated method stub
115         return null;
116     }
117
118     public void close() {
119         // TODO Auto-generated method stub
120     }
121
122     @Override
123     public void ready() {
124
125         modification.seal();
126
127
128         return;
129     }
130
131     public void followUp() {
132
133     }
134
135     @Override
136     public DOMDataTreeWriteCursor createCursor(final DOMDataTreeIdentifier prefix) {
137         DOMDataTreeWriteCursor ret = getCursor();
138         YangInstanceIdentifier relativePath = toRelative(prefix.getRootIdentifier());
139         ret.enter(relativePath.getPathArguments());
140         return ret;
141     }
142 }