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 / ShardDataModificationBuilder.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 java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.Map;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17
18 class ShardDataModificationBuilder extends ModificationContextNodeBuilder<ShardDataModification> {
19
20     private final ShardRootModificationContext root;
21     private final Map<DOMDataTreeIdentifier, ForeignShardModificationContext> childShards =
22             new HashMap<>();
23
24     public ShardDataModificationBuilder(final ShardRootModificationContext root) {
25         this.root = root;
26     }
27
28     public void addSubshard(final ForeignShardModificationContext value) {
29         WriteableSubshardBoundaryNode leafNode = WriteableSubshardBoundaryNode.from(value);
30         putNode(value.getIdentifier().getRootIdentifier(), leafNode);
31     }
32
33     private void putNode(final YangInstanceIdentifier key, final WriteableSubshardBoundaryNode subshardNode) {
34         ModificationContextNodeBuilder<?> current = this;
35         Iterator<PathArgument> toBoundary = toRelative(key).getPathArguments().iterator();
36         while (toBoundary.hasNext()) {
37             PathArgument nextArg = toBoundary.next();
38             if (toBoundary.hasNext()) {
39                 current = getInterior(nextArg);
40             } else {
41                 current.addBoundary(nextArg, subshardNode);
42             }
43         }
44     }
45
46
47     @Override
48     ShardDataModification build(final Map<PathArgument, WriteableModificationNode> children) {
49         return new ShardDataModification(root, children, childShards);
50     }
51
52     private YangInstanceIdentifier toRelative(final YangInstanceIdentifier key) {
53         return key.relativeTo(root.getIdentifier().getRootIdentifier()).get();
54     }
55 }