BUG-8291: expose additional DataTreeFactory methods
[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.DataTreeConfiguration;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
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 DataTreeConfiguration treeConfig;
38     private final boolean maskMandatory;
39
40     /**
41      * Current data store state generation.
42      */
43     private volatile DataTreeState state;
44
45     InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig,
46         final SchemaContext schemaContext) {
47         this.treeConfig = Preconditions.checkNotNull(treeConfig, "treeConfig");
48         maskMandatory = true;
49         state = DataTreeState.createInitial(rootNode);
50         if (schemaContext != null) {
51             setSchemaContext(schemaContext);
52         }
53     }
54
55     InMemoryDataTree(final TreeNode rootNode, final DataTreeConfiguration treeConfig, final SchemaContext schemaContext,
56             final DataSchemaNode rootSchemaNode, final boolean maskMandatory) {
57         this.treeConfig = Preconditions.checkNotNull(treeConfig, "treeConfig");
58         this.maskMandatory = maskMandatory;
59
60         state = DataTreeState.createInitial(rootNode).withSchemaContext(schemaContext, getOperation(rootSchemaNode));
61     }
62
63     private ModificationApplyOperation getOperation(final DataSchemaNode rootSchemaNode) {
64         if (maskMandatory && rootSchemaNode instanceof ContainerSchemaNode) {
65             return new ContainerModificationStrategy((ContainerSchemaNode) rootSchemaNode, treeConfig);
66         }
67
68         return SchemaAwareApplyOperation.from(rootSchemaNode, treeConfig);
69     }
70
71     /*
72      * This method is synchronized to guard against user attempting to install
73      * multiple contexts. Otherwise it runs in a lock-free manner.
74      */
75     @Override
76     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
77         Preconditions.checkNotNull(newSchemaContext);
78
79         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
80
81         final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
82         final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(getRootPath());
83         if (rootContextNode == null) {
84             LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
85             return;
86         }
87
88         final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
89         if (!(rootSchemaNode instanceof DataNodeContainer)) {
90             LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
91             return;
92         }
93
94         final ModificationApplyOperation rootNode = getOperation(rootSchemaNode);
95         DataTreeState currentState, newState;
96         do {
97             currentState = state;
98             newState = currentState.withSchemaContext(newSchemaContext, rootNode);
99         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
100     }
101
102     @Override
103     public InMemoryDataTreeSnapshot takeSnapshot() {
104         return state.newSnapshot();
105     }
106
107     @Override
108     public void commit(final DataTreeCandidate candidate) {
109         if (candidate instanceof NoopDataTreeCandidate) {
110             return;
111         }
112         Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
113         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
114
115         if (LOG.isTraceEnabled()) {
116             LOG.trace("Data Tree is {}", NormalizedNodes.toStringTree(c.getTipRoot().getData()));
117         }
118
119         final TreeNode newRoot = c.getTipRoot();
120         DataTreeState currentState, newState;
121         do {
122             currentState = state;
123             final TreeNode currentRoot = currentState.getRoot();
124             LOG.debug("Updating datastore from {} to {}", currentRoot, newRoot);
125
126             final TreeNode oldRoot = c.getBeforeRoot();
127             if (oldRoot != currentRoot) {
128                 final String oldStr = simpleToString(oldRoot);
129                 final String currentStr = simpleToString(currentRoot);
130                 throw new IllegalStateException("Store tree " + currentStr + " and candidate base " + oldStr + " differ.");
131             }
132
133             newState = currentState.withRoot(newRoot);
134             LOG.trace("Updated state from {} to {}", currentState, newState);
135         } while (!STATE_UPDATER.compareAndSet(this, currentState, newState));
136     }
137
138     private static String simpleToString(final Object o) {
139         return o.getClass().getName() + "@" + Integer.toHexString(o.hashCode());
140     }
141
142     @Override
143     public YangInstanceIdentifier getRootPath() {
144         return treeConfig.getRootPath();
145     }
146
147     @Override
148     public String toString() {
149         return MoreObjects.toStringHelper(this).
150                 add("object", super.toString()).
151                 add("config", treeConfig).
152                 add("state", state).
153                 toString();
154     }
155
156     @Override
157     @Nonnull
158     protected TreeNode getTipRoot() {
159         return state.getRoot();
160     }
161 }