BUG-509: simple cleanup of DataTreeModification
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / StoreNodeCompositeBuilder.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.md.sal.dom.store.impl.tree;
9
10 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
11 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
12
13 import com.google.common.base.Preconditions;
14 import com.google.common.primitives.UnsignedLong;
15
16 /**
17  *
18  * Helper builder
19  *
20  *
21  */
22 @SuppressWarnings("rawtypes")
23 public class StoreNodeCompositeBuilder {
24
25     private final StoreMetadataNode.Builder metadata;
26
27     private final NormalizedNodeContainerBuilder data;
28
29     private StoreNodeCompositeBuilder(final NormalizedNodeContainerBuilder nodeBuilder) {
30         this.metadata = StoreMetadataNode.builder();
31         this.data = Preconditions.checkNotNull(nodeBuilder);
32     }
33
34     public StoreNodeCompositeBuilder(NormalizedNodeContainerBuilder nodeBuilder, StoreMetadataNode currentMeta) {
35         this.metadata = StoreMetadataNode.builder(currentMeta);
36         this.data = Preconditions.checkNotNull(nodeBuilder);
37     }
38
39     @SuppressWarnings("unchecked")
40     public StoreNodeCompositeBuilder add(final StoreMetadataNode node) {
41         metadata.add(node);
42         data.addChild(node.getData());
43         return this;
44     }
45
46     @SuppressWarnings("unchecked")
47     public StoreNodeCompositeBuilder remove(PathArgument id) {
48         metadata.remove(id);
49         data.removeChild(id);
50         return this;
51     }
52
53     public StoreMetadataNode build() {
54         return metadata.setData(data.build()).build();
55     }
56
57     public static StoreNodeCompositeBuilder from(final NormalizedNodeContainerBuilder nodeBuilder) {
58         return new StoreNodeCompositeBuilder(nodeBuilder);
59     }
60
61     public static StoreNodeCompositeBuilder from(final NormalizedNodeContainerBuilder nodeBuilder, StoreMetadataNode currentMeta) {
62         return new StoreNodeCompositeBuilder(nodeBuilder, currentMeta);
63     }
64
65     @SuppressWarnings("unchecked")
66     public StoreNodeCompositeBuilder setIdentifier(final PathArgument identifier) {
67         data.withNodeIdentifier(identifier);
68         return this;
69     }
70
71     public StoreNodeCompositeBuilder setNodeVersion(final UnsignedLong nodeVersion) {
72         metadata.setNodeVersion(nodeVersion);
73         return this;
74     }
75
76     public StoreNodeCompositeBuilder setSubtreeVersion(final UnsignedLong updatedSubtreeVersion) {
77         metadata.setSubtreeVersion(updatedSubtreeVersion);
78         return this;
79     }
80 }