Cleanup checkstyle warnings and turn enforcement on in yang-data-impl
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTree.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.yangtools.yang.data.impl.schema.tree;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
20 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
21 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Read-only snapshot of the data tree.
31  */
32 final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
33     private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
34             AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
35     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
36
37     private final DataTreeConfiguration treeConfig;
38     private final boolean maskMandatory;
39
40     /**
41      * Current data store state generation.
42      */
43     private volatile DataTreeState state;
44
45     InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
46         final SchemaContext schemaContext) {
47         this.treeConfig = Preconditions.checkNotNull(treeConfig, "treeConfig");
48         maskMandatory = true;
49         state = DataTreeState.createInitial(rootNode);
50         if (schemaContext != null) {
51             setSchemaContext(schemaContext);
52         }
53     }
54
55     InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
56             final SchemaContext schemaContext, final DataSchemaNode rootSchemaNode, final boolean maskMandatory) {
57         this.treeConfig = Preconditions.checkNotNull(treeConfig, "treeConfig");
58         this.maskMandatory = maskMandatory;
59
60         state = DataTreeState.createInitial(rootNode).withSchemaContext(schemaContext, getOperation(rootSchemaNode));
61     }
62
63     private ModificationApplyOperation getOperation(final DataSchemaNode rootSchemaNode) {
64         if (maskMandatory && rootSchemaNode instanceof ContainerSchemaNode) {
65             return new ContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeConfig);
66         }
67
68         return SchemaAwareApplyOperation.from(rootSchemaNode, treeConfig);
69     }
70
71     /*
72      * This method is synchronized to guard against user attempting to install
73      * multiple contexts. Otherwise it runs in a lock-free manner.
74      */
75     @Override
76     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
77         Preconditions.checkNotNull(newSchemaContext);
78
79         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
80
81         final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
82         final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(getRootPath());
83         if (rootContextNode == null) {
84             LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
85             return;
86         }
87
88         final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
89         if (!(rootSchemaNode instanceof DataNodeContainer)) {
90             LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
91             return;
92         }
93
94         final ModificationApplyOperation rootNode = getOperation(rootSchemaNode);
95         DataTreeState currentState;
96         DataTreeState newState;
97         do {
98             currentState = state;
99             newState = currentState.withSchemaContext(newSchemaContext, rootNode);
100         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
101     }
102
103     @Override
104     public InMemoryDataTreeSnapshot takeSnapshot() {
105         return state.newSnapshot();
106     }
107
108     @Override
109     public void commit(final DataTreeCandidate candidate) {
110         if (candidate instanceof NoopDataTreeCandidate) {
111             return;
112         }
113         Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s",
114             candidate.getClass());
115         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
116
117         if (LOG.isTraceEnabled()) {
118             LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
119         }
120
121         final TreeNode newRoot = c.getTipRoot();
122         DataTreeState currentState;
123         DataTreeState newState;
124         do {
125             currentState = state;
126             final TreeNode currentRoot = currentState.getRoot();
127             LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
128
129             final TreeNode oldRoot = c.getBeforeRoot();
130             if (oldRoot != currentRoot) {
131                 final String oldStr = simpleToString(oldRoot);
132                 final String currentStr = simpleToString(currentRoot);
133                 throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr
134                     + " differ.");
135             }
136
137             newState = currentState.withRoot(newRoot);
138             LOG.trace("Updated state from {} to {}", currentState, newState);
139         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
140     }
141
142     private static String simpleToString(final Object obj) {
143         return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
144     }
145
146     @Override
147     public YangInstanceIdentifier getRootPath() {
148         return treeConfig.getRootPath();
149     }
150
151     @Override
152     public String toString() {
153         return MoreObjects.toStringHelper(this)
154                 .add("object", super.toString())
155                 .add("config", treeConfig)
156                 .add("state", state)
157                 .toString();
158     }
159
160     @Override
161     @Nonnull
162     protected TreeNode getTipRoot() {
163         return state.getRoot();
164     }
165 }