BUG-509: Move DataTree concepts into separate package
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / 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.controller.md.sal.dom.store.impl.tree.data;
9
10 import java.util.concurrent.locks.ReadWriteLock;
11 import java.util.concurrent.locks.ReentrantReadWriteLock;
12
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;
22
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
25
26 /**
27  * Read-only snapshot of the data tree.
28  */
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();
32
33     private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
34     private StoreMetadataNode rootNode;
35     private SchemaContext currentSchemaContext;
36
37     public InMemoryDataTree(StoreMetadataNode rootNode, final SchemaContext schemaContext) {
38         this.rootNode = Preconditions.checkNotNull(rootNode);
39         this.currentSchemaContext = schemaContext;
40     }
41
42     @Override
43         public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
44         Preconditions.checkNotNull(newSchemaContext);
45
46         LOG.info("Attepting to install schema context {}", newSchemaContext);
47
48         /*
49          * FIXME: we should walk the schema contexts, both current and new and see
50          *        whether they are compatible here. Reject incompatible changes.
51          */
52
53         // Ready to change the context now, make sure no operations are running
54         rwLock.writeLock().lock();
55         try {
56             this.currentSchemaContext = newSchemaContext;
57         } finally {
58             rwLock.writeLock().unlock();
59         }
60     }
61
62     @Override
63         public InMemoryDataTreeSnapshot takeSnapshot() {
64         rwLock.readLock().lock();
65         try {
66             return new InMemoryDataTreeSnapshot(currentSchemaContext, rootNode);
67         } finally {
68             rwLock.readLock().unlock();
69         }
70     }
71
72         @Override
73         public void validate(DataTreeModification modification) throws DataPreconditionFailedException {
74                 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
75
76                 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
77                 m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.of(rootNode));
78         }
79
80         @Override
81         public synchronized DataTreeCandidate prepare(DataTreeModification modification) {
82                 Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
83
84                 final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
85                 final NodeModification root = m.getRootModification();
86
87         if (root.getModificationType() == ModificationType.UNMODIFIED) {
88                 return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
89         }
90
91         rwLock.writeLock().lock();
92         try {
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());
97         } finally {
98                 rwLock.writeLock().unlock();
99         }
100         }
101
102         @Override
103         public synchronized void commit(DataTreeCandidate candidate) {
104                 if (candidate instanceof NoopDataTreeCandidate) {
105                         return;
106                 }
107
108                 Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
109                 final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
110
111         LOG.debug("Updating Store snapshot version: {} with version:{}", rootNode.getSubtreeVersion(), c.getAfterRoot().getSubtreeVersion());
112
113         if (LOG.isTraceEnabled()) {
114             LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
115         }
116
117         // Ready to change the context now, make sure no operations are running
118         rwLock.writeLock().lock();
119         try {
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();
123         } finally {
124             rwLock.writeLock().unlock();
125         }
126         }
127 }