Do not update term from unreachable members
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / transformer / NormalizedNodeBuilderWrapper.java
1 /*
2  * Copyright (c) 2015 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.cluster.datastore.node.utils.transformer;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
18 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
19
20 final class NormalizedNodeBuilderWrapper {
21     @SuppressWarnings("rawtypes")
22     private final NormalizedNodeBuilder builder;
23     private final PathArgument identifier;
24     private final DataSchemaContextNode<?> schemaNode;
25
26     NormalizedNodeBuilderWrapper(final NormalizedNodeBuilder<?, ?, ?> builder,
27             final PathArgument identifier, final @Nullable DataSchemaContextNode<?> schemaNode) {
28         this.builder = requireNonNull(builder);
29         this.identifier = requireNonNull(identifier);
30         this.schemaNode = schemaNode;
31     }
32
33     PathArgument identifier() {
34         return identifier;
35     }
36
37     @Nullable DataSchemaContextNode<?> getSchema() {
38         return schemaNode;
39     }
40
41     @Nullable DataSchemaContextNode<?> childSchema(final PathArgument child) {
42         if (schemaNode == null) {
43             return null;
44         }
45
46         checkState(builder instanceof NormalizedNodeContainerBuilder,
47             "Attempted to lookup child %s in non-container %s", schemaNode);
48         return schemaNode.getChild(child);
49     }
50
51     NormalizedNode<?, ?> build() {
52         return builder.build();
53     }
54
55     @SuppressWarnings({ "rawtypes", "unchecked" })
56     void addChild(final NormalizedNode<?, ?> child) {
57         checkState(builder instanceof NormalizedNodeContainerBuilder,
58             "Attempted to add child %s to non-container builder %s", child, builder);
59         ((NormalizedNodeContainerBuilder) builder).addChild(child);
60     }
61
62     @SuppressWarnings("unchecked")
63     void setValue(final Object value) {
64         checkState(!(builder instanceof NormalizedNodeContainerBuilder),
65             "Attempted to set value %s on container builder %s", value, builder);
66         builder.withValue(value);
67     }
68 }