Bug 1572: Fixed duplicate and inconsistent allocation of Version
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / 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.yangtools.yang.data.impl.schema.tree;
9
10 import java.util.concurrent.locks.ReadWriteLock;
11 import java.util.concurrent.locks.ReentrantReadWriteLock;
12
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
20 import org.opendaylight.yangtools.yang.data.impl.schema.tree.RootModificationApplyOperation.LatestOperationHolder;
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 YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.builder().build();
34
35     private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
36     private final LatestOperationHolder operationHolder = new LatestOperationHolder();
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("Attempting to install schema contexts");
54         LOG.debug("Following schema contexts will be attempted {}",newSchemaContext);
55
56         /*
57          * FIXME: we should walk the schema contexts, both current and new and see
58          *        whether they are compatible here. Reject incompatible changes.
59          */
60
61         // Instantiate new apply operation, this still may fail
62         final ModificationApplyOperation newApplyOper = SchemaAwareApplyOperation.from(newSchemaContext);
63
64         // Ready to change the context now, make sure no operations are running
65         rwLock.writeLock().lock();
66         try {
67             this.operationHolder.setCurrent(newApplyOper);
68             this.currentSchemaContext = newSchemaContext;
69         } finally {
70             rwLock.writeLock().unlock();
71         }
72     }
73
74     @Override
75     public InMemoryDataTreeSnapshot takeSnapshot() {
76         rwLock.readLock().lock();
77         try {
78             return new InMemoryDataTreeSnapshot(currentSchemaContext, rootNode, operationHolder.newSnapshot());
79         } finally {
80             rwLock.readLock().unlock();
81         }
82     }
83
84     @Override
85     public void validate(final DataTreeModification modification) throws DataValidationFailedException {
86         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
87
88         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
89         m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.<TreeNode>of(rootNode));
90     }
91
92     @Override
93     public synchronized DataTreeCandidate prepare(final DataTreeModification modification) {
94         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
95
96         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
97         final ModifiedNode root = m.getRootModification();
98
99         if (root.getType() == ModificationType.UNMODIFIED) {
100             return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
101         }
102
103         rwLock.writeLock().lock();
104         try {
105             final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
106                     Optional.<TreeNode>of(rootNode), m.getVersion());
107             Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
108             return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, rootNode, newRoot.get());
109         } finally {
110             rwLock.writeLock().unlock();
111         }
112     }
113
114     @Override
115     public synchronized void commit(final DataTreeCandidate candidate) {
116         if (candidate instanceof NoopDataTreeCandidate) {
117             return;
118         }
119
120         Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
121         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
122
123         LOG.debug("Updating datastore from {} to {}", rootNode, c.getAfterRoot());
124
125         if (LOG.isTraceEnabled()) {
126             LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
127         }
128
129         // Ready to change the context now, make sure no operations are running
130         rwLock.writeLock().lock();
131         try {
132             Preconditions.checkState(c.getBeforeRoot() == rootNode,
133                     String.format("Store tree %s and candidate base %s differ.", rootNode, c.getBeforeRoot()));
134             this.rootNode = c.getAfterRoot();
135         } finally {
136             rwLock.writeLock().unlock();
137         }
138     }
139 }