Bumping versions by 0.0.1 for next dev cycle
[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     public void addSubshard(final DOMDataTreeIdentifier prefix, final ForeignShardModificationContext value) {
34         childShards.put(prefix, value);
35     }
36
37     private void putNode(final YangInstanceIdentifier key, final WriteableSubshardBoundaryNode subshardNode) {
38         ModificationContextNodeBuilder<?> current = this;
39         Iterator<PathArgument> toBoundary = toRelative(key).getPathArguments().iterator();
40         while (toBoundary.hasNext()) {
41             PathArgument nextArg = toBoundary.next();
42             if (toBoundary.hasNext()) {
43                 current = getInterior(nextArg);
44             } else {
45                 current.addBoundary(nextArg, subshardNode);
46             }
47         }
48     }
49
50
51     @Override
52     ShardDataModification build(final Map<PathArgument, WriteableModificationNode> children) {
53         return new ShardDataModification(root, children, childShards);
54     }
55
56     private YangInstanceIdentifier toRelative(final YangInstanceIdentifier key) {
57         return key.relativeTo(root.getIdentifier().getRootIdentifier()).get();
58     }
59 }