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