Merge "BUG-2962: add DataTreeTip interface and implement it"
[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 org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Read-only snapshot of the data tree.
23  */
24 final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
25     private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
26             AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
27     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
28
29     /**
30      * Current data store state generation.
31      */
32     private volatile DataTreeState state;
33
34     public InMemoryDataTree(final TreeNode rootNode, final SchemaContext schemaContext) {
35         state = DataTreeState.createInitial(rootNode);
36         if (schemaContext != null) {
37             setSchemaContext(schemaContext);
38         }
39     }
40
41     /*
42      * This method is synchronized to guard against user attempting to install
43      * multiple contexts. Otherwise it runs in a lock-free manner.
44      */
45     @Override
46     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
47         Preconditions.checkNotNull(newSchemaContext);
48
49         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
50
51         final SchemaAwareApplyOperation operation = SchemaAwareApplyOperation.from(newSchemaContext);
52
53         DataTreeState currentState, newState;
54         do {
55             currentState = state;
56             newState = currentState.withSchemaContext(newSchemaContext, operation);
57         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
58     }
59
60     @Override
61     public InMemoryDataTreeSnapshot takeSnapshot() {
62         return state.newSnapshot();
63     }
64
65     @Override
66     public void commit(final DataTreeCandidate candidate) {
67         if (candidate instanceof NoopDataTreeCandidate) {
68             return;
69         }
70
71         Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
72         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
73
74         if (LOG.isTraceEnabled()) {
75             LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
76         }
77
78         final TreeNode newRoot = c.getTipRoot();
79         DataTreeState currentState, newState;
80         do {
81             currentState = state;
82             final TreeNode currentRoot = currentState.getRoot();
83             LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
84
85             final TreeNode oldRoot = c.getBeforeRoot();
86             Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
87
88             newState = currentState.withRoot(newRoot);
89             LOG.trace("Updated state from {} to {}", currentState, newState);
90         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
91     }
92
93     @Override
94     public String toString() {
95         return MoreObjects.toStringHelper(this).add("object", super.toString()).add("state", state).toString();
96     }
97
98     @Override
99     protected TreeNode getTipRoot() {
100         return state.getRoot();
101     }
102 }