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