c922ab2ae50cce79fe99dbe73606f310b0b83cde
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / ShardDataModification.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 static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.ImmutableMap;
14 import java.util.Map;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
17 import org.opendaylight.mdsal.dom.spi.shard.ForeignShardModificationContext;
18 import org.opendaylight.mdsal.dom.spi.shard.WritableNodeOperation;
19 import org.opendaylight.mdsal.dom.spi.shard.WriteCursorStrategy;
20 import org.opendaylight.mdsal.dom.spi.shard.WriteableModificationNode;
21 import org.opendaylight.mdsal.dom.spi.shard.WriteableNodeWithSubshard;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
24
25 public final class ShardDataModification extends WriteableNodeWithSubshard {
26
27     private final ShardRootModificationContext rootContext;
28     private final Map<DOMDataTreeIdentifier, ForeignShardModificationContext> childShards;
29
30     ShardDataModification(final ShardRootModificationContext boundary,
31                           final Map<PathArgument, WriteableModificationNode> subshards,
32                           final Map<DOMDataTreeIdentifier, ForeignShardModificationContext> childShards) {
33         super(subshards);
34         this.rootContext = requireNonNull(boundary);
35         this.childShards = ImmutableMap.copyOf(childShards);
36     }
37
38     @Override
39     public WriteCursorStrategy createOperation(final DOMDataTreeWriteCursor parentCursor) {
40         return new WritableNodeOperation(this, rootContext.cursor()) {
41             @Override
42             public void exit() {
43                 throw new IllegalStateException("Can not exit data tree root");
44             }
45         };
46     }
47
48     @Override
49     public PathArgument getIdentifier() {
50         return rootContext.getIdentifier().getRootIdentifier().getLastPathArgument();
51     }
52
53     DOMDataTreeIdentifier getPrefix() {
54         return rootContext.getIdentifier();
55     }
56
57     Map<DOMDataTreeIdentifier, ForeignShardModificationContext> getChildShards() {
58         return childShards;
59     }
60
61     DataTreeModification seal() {
62         final DataTreeModification rootModification = rootContext.ready();
63         for (ForeignShardModificationContext childShard : childShards.values()) {
64             childShard.ready();
65         }
66
67         return rootModification;
68     }
69
70     void closeTransactions() {
71         for (final ForeignShardModificationContext childShard : childShards.values()) {
72             childShard.closeForeignTransaction();
73         }
74     }
75
76     void closeCursor() {
77         rootContext.closeCursor();
78     }
79 }