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