BUG-4202: Initial prototype of subshard aware Shard implementation
[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
20 final class ShardDataModification extends WriteableNodeWithSubshard {
21
22     private final ShardRootModificationContext rootContext;
23     private final Map<DOMDataTreeIdentifier, ForeignShardModificationContext> childShards;
24
25     ShardDataModification(ShardRootModificationContext boundary,
26             Map<PathArgument, WriteableModificationNode> subshards,
27             Map<DOMDataTreeIdentifier, ForeignShardModificationContext> childShards) {
28         super(subshards);
29         this.rootContext = Preconditions.checkNotNull(boundary);
30         this.childShards = ImmutableMap.copyOf(childShards);
31     }
32
33     @Override
34     WriteCursorStrategy createOperation(DOMDataTreeWriteCursor parentCursor) {
35         return new WriteableNodeOperation(this, rootContext.cursor()) {
36             @Override
37             public void exit() {
38                 throw new IllegalStateException("Can not exit data tree root");
39             }
40         };
41     }
42
43     @Override
44     public PathArgument getIdentifier() {
45         return rootContext.getIdentifier().getRootIdentifier().getLastPathArgument();
46     }
47
48     static ShardDataModification from(ShardRootModificationContext root,
49             Map<YangInstanceIdentifier, ForeignShardModificationContext> shards) {
50
51         ShardDataModificationBuilder builder = new ShardDataModificationBuilder(root);
52         for (Entry<YangInstanceIdentifier, ForeignShardModificationContext> subshard : shards.entrySet()) {
53             builder.addSubshard(subshard.getValue());
54         }
55         return builder.build();
56     }
57
58     public DOMDataTreeIdentifier getPrefix() {
59         return rootContext.getIdentifier();
60     }
61
62     public Map<DOMDataTreeIdentifier, ForeignShardModificationContext> getChildShards() {
63         return childShards;
64     }
65
66     public void seal() {
67         rootContext.ready();
68         for (ForeignShardModificationContext childShard : childShards.values()) {
69             childShard.ready();
70         }
71     }
72
73 }