BUG-5504: use Object.toString() without overrides in precondition
[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 javax.annotation.Nonnull;
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.TipProducingDataTree;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
20 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
21 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Read-only snapshot of the data tree.
31  */
32 final class InMemoryDataTree extends AbstractDataTreeTip implements TipProducingDataTree {
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     private final YangInstanceIdentifier rootPath;
38     private final TreeType treeType;
39
40     /**
41      * Current data store state generation.
42      */
43     private volatile DataTreeState state;
44
45     public InMemoryDataTree(final TreeNode rootNode, final TreeType treeType, final YangInstanceIdentifier rootPath, final SchemaContext schemaContext) {
46         this.treeType = Preconditions.checkNotNull(treeType, "treeType");
47         this.rootPath = Preconditions.checkNotNull(rootPath, "rootPath");
48         state = DataTreeState.createInitial(rootNode);
49         if (schemaContext != null) {
50             setSchemaContext(schemaContext);
51         }
52     }
53
54     /*
55      * This method is synchronized to guard against user attempting to install
56      * multiple contexts. Otherwise it runs in a lock-free manner.
57      */
58     @Override
59     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
60         Preconditions.checkNotNull(newSchemaContext);
61
62         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
63
64         final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
65         final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(rootPath);
66         if (rootContextNode == null) {
67             LOG.debug("Could not find root {} in new schema context, not upgrading", rootPath);
68             return;
69         }
70
71         final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
72         if (!(rootSchemaNode instanceof DataNodeContainer)) {
73             LOG.warn("Root {} resolves to non-container type {}, not upgrading", rootPath, rootSchemaNode);
74             return;
75         }
76
77         final ModificationApplyOperation rootNode;
78         if (rootSchemaNode instanceof ContainerSchemaNode) {
79             // FIXME: real root needs to enfore presence, but that require pre-population
80             rootNode = new ContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeType);
81         } else {
82             rootNode = SchemaAwareApplyOperation.from(rootSchemaNode, treeType);
83         }
84
85         DataTreeState currentState, newState;
86         do {
87             currentState = state;
88             newState = currentState.withSchemaContext(newSchemaContext, rootNode);
89         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
90     }
91
92     @Override
93     public InMemoryDataTreeSnapshot takeSnapshot() {
94         return state.newSnapshot();
95     }
96
97     @Override
98     public void commit(final DataTreeCandidate candidate) {
99         if (candidate instanceof NoopDataTreeCandidate) {
100             return;
101         }
102         Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
103         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
104
105         if (LOG.isTraceEnabled()) {
106             LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
107         }
108
109         final TreeNode newRoot = c.getTipRoot();
110         DataTreeState currentState, newState;
111         do {
112             currentState = state;
113             final TreeNode currentRoot = currentState.getRoot();
114             LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
115
116             final TreeNode oldRoot = c.getBeforeRoot();
117             if (oldRoot != currentRoot) {
118                 final String oldStr = simpleToString(oldRoot);
119                 final String currentStr = simpleToString(currentRoot);
120                 throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr + " differ.");
121             }
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     private static String simpleToString(final Object o) {
129         return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
130     }
131
132     @Override
133     public YangInstanceIdentifier getRootPath() {
134         return rootPath;
135     }
136
137     @Override
138     public String toString() {
139         return MoreObjects.toStringHelper(this).
140                 add("object", super.toString()).
141                 add("rootPath", rootPath).
142                 add("state", state).
143                 toString();
144     }
145
146     @Override
147     @Nonnull
148     protected TreeNode getTipRoot() {
149         return state.getRoot();
150     }
151 }