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