2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.store.impl.tree.data;
10 import java.util.concurrent.locks.ReadWriteLock;
11 import java.util.concurrent.locks.ReentrantReadWriteLock;
13 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataPreconditionFailedException;
14 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTree;
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTreeCandidate;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.DataTreeModification;
17 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationType;
18 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreUtils;
19 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.common.base.Optional;
26 import com.google.common.base.Preconditions;
29 * Read-only snapshot of the data tree.
31 final class InMemoryDataTree implements DataTree {
32 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
33 private static final InstanceIdentifier PUBLIC_ROOT_PATH = InstanceIdentifier.builder().build();
35 private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
36 private ModificationApplyOperation applyOper = new AlwaysFailOperation();
37 private SchemaContext currentSchemaContext;
38 private TreeNode rootNode;
40 public InMemoryDataTree(final TreeNode rootNode, final SchemaContext schemaContext) {
41 this.rootNode = Preconditions.checkNotNull(rootNode);
43 if (schemaContext != null) {
44 // Also sets applyOper
45 setSchemaContext(schemaContext);
50 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
51 Preconditions.checkNotNull(newSchemaContext);
53 LOG.info("Attepting to install schema context {}", newSchemaContext);
56 * FIXME: we should walk the schema contexts, both current and new and see
57 * whether they are compatible here. Reject incompatible changes.
60 // Instantiate new apply operation, this still may fail
61 final ModificationApplyOperation newApplyOper = SchemaAwareApplyOperation.from(newSchemaContext);
63 // Ready to change the context now, make sure no operations are running
64 rwLock.writeLock().lock();
66 this.applyOper = newApplyOper;
67 this.currentSchemaContext = newSchemaContext;
69 rwLock.writeLock().unlock();
74 public InMemoryDataTreeSnapshot takeSnapshot() {
75 rwLock.readLock().lock();
77 return new InMemoryDataTreeSnapshot(currentSchemaContext, rootNode, applyOper);
79 rwLock.readLock().unlock();
84 public void validate(final DataTreeModification modification) throws DataPreconditionFailedException {
85 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
87 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
88 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.<TreeNode>of(rootNode));
92 public synchronized DataTreeCandidate prepare(final DataTreeModification modification) {
93 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
95 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
96 final ModifiedNode root = m.getRootModification();
98 if (root.getType() == ModificationType.UNMODIFIED) {
99 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
102 rwLock.writeLock().lock();
104 final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
105 Optional.<TreeNode>of(rootNode), rootNode.getSubtreeVersion().next());
106 Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
107 return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, rootNode, newRoot.get());
109 rwLock.writeLock().unlock();
114 public synchronized void commit(final DataTreeCandidate candidate) {
115 if (candidate instanceof NoopDataTreeCandidate) {
119 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
120 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
122 LOG.debug("Updating datastore from {} to {}", rootNode, c.getAfterRoot());
124 if (LOG.isTraceEnabled()) {
125 LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
128 // Ready to change the context now, make sure no operations are running
129 rwLock.writeLock().lock();
131 Preconditions.checkState(c.getBeforeRoot() == rootNode,
132 String.format("Store tree %s and candidate base %s differ.", rootNode, c.getBeforeRoot()));
133 this.rootNode = c.getAfterRoot();
135 rwLock.writeLock().unlock();