BUG-2470: implement InMemoryDataTree.toString()
[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.tree.DataTree;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
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.info("Attempting to install schema contexts");
57         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
58
59         final SchemaAwareApplyOperation operation = SchemaAwareApplyOperation.from(newSchemaContext);
60
61         DataTreeState currentState, newState;
62         do {
63             currentState = state;
64             newState = currentState.withSchemaContext(newSchemaContext, operation);
65         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
66     }
67
68     @Override
69     public InMemoryDataTreeSnapshot takeSnapshot() {
70         return state.newSnapshot();
71     }
72
73     @Override
74     public void validate(final DataTreeModification modification) throws DataValidationFailedException {
75         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
76         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
77
78         m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.<TreeNode>of(state.getRoot()));
79     }
80
81     @Override
82     public DataTreeCandidate prepare(final DataTreeModification modification) {
83         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
84
85         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
86         final ModifiedNode root = m.getRootModification();
87
88         if (root.getType() == ModificationType.UNMODIFIED) {
89             return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
90         }
91
92         final TreeNode currentRoot = state.getRoot();
93         final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
94             Optional.<TreeNode>of(currentRoot), m.getVersion());
95         Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
96         return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, currentRoot, newRoot.get());
97     }
98
99     @Override
100     public void commit(final DataTreeCandidate candidate) {
101         if (candidate instanceof NoopDataTreeCandidate) {
102             return;
103         }
104
105         Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
106         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
107
108         if (LOG.isTraceEnabled()) {
109             LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
110         }
111
112         final TreeNode newRoot = c.getAfterRoot();
113         DataTreeState currentState, newState;
114         do {
115             currentState = state;
116             final TreeNode currentRoot = currentState.getRoot();
117             LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
118
119             final TreeNode oldRoot = c.getBeforeRoot();
120             Preconditions.checkState(oldRoot == currentRoot, "Store tree %s and candidate base %s differ.", currentRoot, oldRoot);
121
122             newState = currentState.withRoot(newRoot);
123             LOG.trace("Updated state from {} to {}", currentState, newState);
124         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
125     }
126
127     @Override
128     public String toString() {
129         return Objects.toStringHelper(this).add("object", super.toString()).add("state", state).toString();
130     }
131 }