Move transaction-invariants into producer
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / ShardDataModificationFactoryBuilder.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 java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.Map;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
16 import org.opendaylight.yangtools.concepts.Builder;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19
20 class ShardDataModificationFactoryBuilder extends ModificationContextNodeBuilder
21         implements Builder<ShardDataModificationFactory> {
22
23     private final Map<DOMDataTreeIdentifier, ForeignShardModificationContext> childShards = new HashMap<>();
24     private final DOMDataTreeIdentifier root;
25
26     ShardDataModificationFactoryBuilder(final DOMDataTreeIdentifier root) {
27         this.root = Preconditions.checkNotNull(root);
28     }
29
30     void addSubshard(final ForeignShardModificationContext value) {
31         WriteableSubshardBoundaryNode leafNode = WriteableSubshardBoundaryNode.from(value);
32         putNode(value.getIdentifier().getRootIdentifier(), leafNode);
33     }
34
35     void addSubshard(final DOMDataTreeIdentifier prefix, final ForeignShardModificationContext value) {
36         childShards.put(prefix, value);
37     }
38
39     private void putNode(final YangInstanceIdentifier key, final WriteableSubshardBoundaryNode subshardNode) {
40         final Iterator<PathArgument> toBoundary = toRelative(key).getPathArguments().iterator();
41         if (toBoundary.hasNext()) {
42             ModificationContextNodeBuilder current = this;
43             while (true) {
44                 final PathArgument nextArg = toBoundary.next();
45                 if (!toBoundary.hasNext()) {
46                     current.addBoundary(nextArg, subshardNode);
47                     break;
48                 }
49
50                 current = getInterior(nextArg);
51             }
52         }
53     }
54
55     @Override
56     public ShardDataModificationFactory build() {
57         return new ShardDataModificationFactory(root, buildChildren(), childShards);
58     }
59
60     private YangInstanceIdentifier toRelative(final YangInstanceIdentifier key) {
61         return key.relativeTo(root.getRootIdentifier()).get();
62     }
63 }