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