f04e379dd95824dd965c0ac8c5c998d9f0dafd83
[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.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;
23
24 import com.google.common.base.Optional;
25 import com.google.common.base.Preconditions;
26
27 /**
28  * Read-only snapshot of the data tree.
29  */
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();
33
34     private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
35     private ModificationApplyOperation applyOper = new AlwaysFailOperation();
36     private SchemaContext currentSchemaContext;
37     private StoreMetadataNode rootNode;
38
39     public InMemoryDataTree(StoreMetadataNode rootNode, final SchemaContext schemaContext) {
40         this.rootNode = Preconditions.checkNotNull(rootNode);
41
42         if (schemaContext != null) {
43             // Also sets applyOper
44             setSchemaContext(schemaContext);
45         }
46     }
47
48     @Override
49     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
50         Preconditions.checkNotNull(newSchemaContext);
51
52         LOG.info("Attepting to install schema context {}", newSchemaContext);
53
54         /*
55          * FIXME: we should walk the schema contexts, both current and new and see
56          *        whether they are compatible here. Reject incompatible changes.
57          */
58
59         // Instantiate new apply operation, this still may fail
60         final ModificationApplyOperation newApplyOper = SchemaAwareApplyOperation.from(newSchemaContext);
61
62         // Ready to change the context now, make sure no operations are running
63         rwLock.writeLock().lock();
64         try {
65             this.applyOper = newApplyOper;
66             this.currentSchemaContext = newSchemaContext;
67         } finally {
68             rwLock.writeLock().unlock();
69         }
70     }
71
72     @Override
73     public InMemoryDataTreeSnapshot takeSnapshot() {
74         rwLock.readLock().lock();
75         try {
76             return new InMemoryDataTreeSnapshot(currentSchemaContext, rootNode, applyOper);
77         } finally {
78             rwLock.readLock().unlock();
79         }
80     }
81
82     @Override
83     public void validate(DataTreeModification modification) throws DataPreconditionFailedException {
84         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
85
86         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
87         m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.of(rootNode));
88     }
89
90     @Override
91     public synchronized DataTreeCandidate prepare(DataTreeModification modification) {
92         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
93
94         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
95         final NodeModification root = m.getRootModification();
96
97         if (root.getModificationType() == ModificationType.UNMODIFIED) {
98             return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
99         }
100
101         rwLock.writeLock().lock();
102         try {
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());
107         } finally {
108             rwLock.writeLock().unlock();
109         }
110     }
111
112     @Override
113     public synchronized void commit(DataTreeCandidate candidate) {
114         if (candidate instanceof NoopDataTreeCandidate) {
115             return;
116         }
117
118         Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
119         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
120
121         LOG.debug("Updating Store snapshot version: {} with version:{}", rootNode.getSubtreeVersion(), c.getAfterRoot().getSubtreeVersion());
122
123         if (LOG.isTraceEnabled()) {
124             LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
125         }
126
127         // Ready to change the context now, make sure no operations are running
128         rwLock.writeLock().lock();
129         try {
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();
133         } finally {
134             rwLock.writeLock().unlock();
135         }
136     }
137 }