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