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.StoreUtils;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
27 * Read-only snapshot of the data tree.
29 final class InMemoryDataTree implements DataTree {
30 private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
31 private static final InstanceIdentifier PUBLIC_ROOT_PATH = InstanceIdentifier.builder().build();
33 private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
34 private StoreMetadataNode rootNode;
35 private SchemaContext currentSchemaContext;
37 public InMemoryDataTree(StoreMetadataNode rootNode, final SchemaContext schemaContext) {
38 this.rootNode = Preconditions.checkNotNull(rootNode);
39 this.currentSchemaContext = schemaContext;
43 public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
44 Preconditions.checkNotNull(newSchemaContext);
46 LOG.info("Attepting to install schema context {}", newSchemaContext);
49 * FIXME: we should walk the schema contexts, both current and new and see
50 * whether they are compatible here. Reject incompatible changes.
53 // Ready to change the context now, make sure no operations are running
54 rwLock.writeLock().lock();
56 this.currentSchemaContext = newSchemaContext;
58 rwLock.writeLock().unlock();
63 public InMemoryDataTreeSnapshot takeSnapshot() {
64 rwLock.readLock().lock();
66 return new InMemoryDataTreeSnapshot(currentSchemaContext, rootNode);
68 rwLock.readLock().unlock();
73 public void validate(DataTreeModification modification) throws DataPreconditionFailedException {
74 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
76 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
77 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.of(rootNode));
81 public synchronized DataTreeCandidate prepare(DataTreeModification modification) {
82 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
84 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
85 final NodeModification root = m.getRootModification();
87 if (root.getModificationType() == ModificationType.UNMODIFIED) {
88 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
91 rwLock.writeLock().lock();
93 // FIXME: rootNode needs to be a read-write snapshot here...
94 final Optional<StoreMetadataNode> newRoot = m.getStrategy().apply(m.getRootModification(), Optional.of(rootNode), StoreUtils.increase(rootNode.getSubtreeVersion()));
95 Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
96 return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, rootNode, newRoot.get());
98 rwLock.writeLock().unlock();
103 public synchronized void commit(DataTreeCandidate candidate) {
104 if (candidate instanceof NoopDataTreeCandidate) {
108 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
109 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
111 LOG.debug("Updating Store snapshot version: {} with version:{}", rootNode.getSubtreeVersion(), c.getAfterRoot().getSubtreeVersion());
113 if (LOG.isTraceEnabled()) {
114 LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
117 // Ready to change the context now, make sure no operations are running
118 rwLock.writeLock().lock();
120 Preconditions.checkState(c.getBeforeRoot() == rootNode,
121 String.format("Store snapshot %s and transaction snapshot %s differ.", rootNode, c.getBeforeRoot()));
122 this.rootNode = c.getAfterRoot();
124 rwLock.writeLock().unlock();