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.yangtools.yang.data.api.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.common.base.Optional;
25 import com.google.common.base.Preconditions;
28 * Read-only snapshot of the data tree.
30 final class InMemoryDataTree implements DataTree {
31 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
32 private static final InstanceIdentifier PUBLIC_ROOT_PATH = InstanceIdentifier.builder().build();
34 private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
35 private ModificationApplyOperation applyOper = new AlwaysFailOperation();
36 private SchemaContext currentSchemaContext;
37 private StoreMetadataNode rootNode;
39 public InMemoryDataTree(StoreMetadataNode rootNode, final SchemaContext schemaContext) {
40 this.rootNode = Preconditions.checkNotNull(rootNode);
42 if (schemaContext != null) {
43 // Also sets applyOper
44 setSchemaContext(schemaContext);
49 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
50 Preconditions.checkNotNull(newSchemaContext);
52 LOG.info("Attepting to install schema context {}", newSchemaContext);
55 * FIXME: we should walk the schema contexts, both current and new and see
56 * whether they are compatible here. Reject incompatible changes.
59 // Instantiate new apply operation, this still may fail
60 final ModificationApplyOperation newApplyOper = SchemaAwareApplyOperation.from(newSchemaContext);
62 // Ready to change the context now, make sure no operations are running
63 rwLock.writeLock().lock();
65 this.applyOper = newApplyOper;
66 this.currentSchemaContext = newSchemaContext;
68 rwLock.writeLock().unlock();
73 public InMemoryDataTreeSnapshot takeSnapshot() {
74 rwLock.readLock().lock();
76 return new InMemoryDataTreeSnapshot(currentSchemaContext, rootNode, applyOper);
78 rwLock.readLock().unlock();
83 public void validate(DataTreeModification modification) throws DataPreconditionFailedException {
84 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
86 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
87 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.of(rootNode));
91 public synchronized DataTreeCandidate prepare(DataTreeModification modification) {
92 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
94 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
95 final NodeModification root = m.getRootModification();
97 if (root.getModificationType() == ModificationType.UNMODIFIED) {
98 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
101 rwLock.writeLock().lock();
103 // FIXME: rootNode needs to be a read-write snapshot here...
104 final Optional<StoreMetadataNode> newRoot = m.getStrategy().apply(m.getRootModification(), Optional.of(rootNode), StoreUtils.increase(rootNode.getSubtreeVersion()));
105 Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
106 return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, rootNode, newRoot.get());
108 rwLock.writeLock().unlock();
113 public synchronized void commit(DataTreeCandidate candidate) {
114 if (candidate instanceof NoopDataTreeCandidate) {
118 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
119 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
121 LOG.debug("Updating Store snapshot version: {} with version:{}", rootNode.getSubtreeVersion(), c.getAfterRoot().getSubtreeVersion());
123 if (LOG.isTraceEnabled()) {
124 LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
127 // Ready to change the context now, make sure no operations are running
128 rwLock.writeLock().lock();
130 Preconditions.checkState(c.getBeforeRoot() == rootNode,
131 String.format("Store tree %s and candidate base %s differ.", rootNode, c.getBeforeRoot()));
132 this.rootNode = c.getAfterRoot();
134 rwLock.writeLock().unlock();