21f4b98226a22075e17c7b89faeb5b276729a231
[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.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
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.StoreTreeNodes;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Read-only snapshot of the data tree.
27  */
28 final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
29     private static final AtomicReferenceFieldUpdater<InMemoryDataTree, DataTreeState> STATE_UPDATER =
30             AtomicReferenceFieldUpdater.newUpdater(InMemoryDataTree.class, DataTreeState.class, "state");
31     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
32
33     private final YangInstanceIdentifier rootPath;
34     private final TreeType treeType;
35
36     /**
37      * Current data store state generation.
38      */
39     private volatile DataTreeState state;
40
41     public InMemoryDataTree(final TreeNode rootNode, final TreeType treeType, final YangInstanceIdentifier rootPath, final SchemaContext schemaContext) {
42         this.treeType = Preconditions.checkNotNull(treeType, "treeType");
43         this.rootPath = Preconditions.checkNotNull(rootPath, "rootPath");
44         state = DataTreeState.createInitial(rootNode);
45         if (schemaContext != null) {
46             setSchemaContext(schemaContext);
47         }
48     }
49
50     /*
51      * This method is synchronized to guard against user attempting to install
52      * multiple contexts. Otherwise it runs in a lock-free manner.
53      */
54     @Override
55     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
56         Preconditions.checkNotNull(newSchemaContext);
57
58         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
59
60         final ModificationApplyOperation op = SchemaAwareApplyOperation.from(newSchemaContext, treeType);
61         final Optional<ModificationApplyOperation> maybeRootNode = StoreTreeNodes.findNode(op, rootPath);
62         if (!maybeRootNode.isPresent()) {
63             LOG.debug("Could not find root {} in new schema context, not upgrading", rootPath);
64             return;
65         }
66
67         final ModificationApplyOperation rootNode = maybeRootNode.get();
68         if (!(rootNode instanceof AbstractNodeContainerModificationStrategy)) {
69             LOG.warn("Root {} resolves to non-container type {}, not upgrading", rootPath, rootNode);
70             return;
71         }
72
73         DataTreeState currentState, newState;
74         do {
75             currentState = state;
76             newState = currentState.withSchemaContext(newSchemaContext, (SchemaAwareApplyOperation) rootNode);
77         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
78     }
79
80     @Override
81     public InMemoryDataTreeSnapshot takeSnapshot() {
82         return state.newSnapshot();
83     }
84
85     @Override
86     public void commit(final DataTreeCandidate candidate) {
87         if (candidate instanceof NoopDataTreeCandidate) {
88             return;
89         }
90         Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
91         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
92
93         if (LOG.isTraceEnabled()) {
94             LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
95         }
96
97         final TreeNode newRoot = c.getTipRoot();
98         DataTreeState currentState, newState;
99         do {
100             currentState = state;
101             final TreeNode currentRoot = currentState.getRoot();
102             LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
103
104             final TreeNode oldRoot = c.getBeforeRoot();
105             Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
106
107             newState = currentState.withRoot(newRoot);
108             LOG.trace("Updated state from {} to {}", currentState, newState);
109         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
110     }
111
112     @Override
113     public YangInstanceIdentifier getRootPath() {
114         return rootPath;
115     }
116
117     @Override
118     public String toString() {
119         return MoreObjects.toStringHelper(this).
120                 add("object", super.toString()).
121                 add("rootPath", rootPath).
122                 add("state", state).
123                 toString();
124     }
125
126     @Override
127     protected TreeNode getTipRoot() {
128         return state.getRoot();
129     }
130 }