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