beed7771cecb298848dfdf95bcf8aa643fb0086b
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.locks.ReadWriteLock;
13 import java.util.concurrent.locks.ReentrantReadWriteLock;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Read-only snapshot of the data tree.
27  */
28 final class InMemoryDataTree implements DataTree {
29     private static final Logger LOG = LoggerFactory.getLogger(InMemoryDataTree.class);
30     private static final YangInstanceIdentifier PUBLIC_ROOT_PATH = YangInstanceIdentifier.builder().build();
31
32     private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
33     private final LatestOperationHolder operationHolder = new LatestOperationHolder();
34     private SchemaContext currentSchemaContext;
35     private TreeNode rootNode;
36
37     public InMemoryDataTree(final TreeNode rootNode, final SchemaContext schemaContext) {
38         this.rootNode = Preconditions.checkNotNull(rootNode);
39
40         if (schemaContext != null) {
41             // Also sets applyOper
42             setSchemaContext(schemaContext);
43         }
44     }
45
46     @Override
47     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
48         Preconditions.checkNotNull(newSchemaContext);
49
50         LOG.info("Attempting to install schema contexts");
51         LOG.debug("Following schema contexts will be attempted {}",newSchemaContext);
52
53         /*
54          * FIXME: we should walk the schema contexts, both current and new and see
55          *        whether they are compatible here. Reject incompatible changes.
56          */
57
58         // Instantiate new apply operation, this still may fail
59         final ModificationApplyOperation newApplyOper = SchemaAwareApplyOperation.from(newSchemaContext);
60
61         // Ready to change the context now, make sure no operations are running
62         rwLock.writeLock().lock();
63         try {
64             this.operationHolder.setCurrent(newApplyOper);
65             this.currentSchemaContext = newSchemaContext;
66         } finally {
67             rwLock.writeLock().unlock();
68         }
69     }
70
71     @Override
72     public InMemoryDataTreeSnapshot takeSnapshot() {
73         rwLock.readLock().lock();
74         try {
75             return new InMemoryDataTreeSnapshot(currentSchemaContext, rootNode, operationHolder.newSnapshot());
76         } finally {
77             rwLock.readLock().unlock();
78         }
79     }
80
81     @Override
82     public void validate(final DataTreeModification modification) throws DataValidationFailedException {
83         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
84
85         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
86         m.getStrategy().checkApplicable(PUBLIC_ROOT_PATH, m.getRootModification(), Optional.<TreeNode>of(rootNode));
87     }
88
89     @Override
90     public synchronized DataTreeCandidate prepare(final DataTreeModification modification) {
91         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
92
93         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
94         final ModifiedNode root = m.getRootModification();
95
96         if (root.getType() == ModificationType.UNMODIFIED) {
97             return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
98         }
99
100         rwLock.writeLock().lock();
101         try {
102             final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
103                     Optional.<TreeNode>of(rootNode), m.getVersion());
104             Preconditions.checkState(newRoot.isPresent(), "Apply strategy failed to produce root node");
105             return new InMemoryDataTreeCandidate(PUBLIC_ROOT_PATH, root, rootNode, newRoot.get());
106         } finally {
107             rwLock.writeLock().unlock();
108         }
109     }
110
111     @Override
112     public synchronized void commit(final DataTreeCandidate candidate) {
113         if (candidate instanceof NoopDataTreeCandidate) {
114             return;
115         }
116
117         Preconditions.checkArgument(candidate instanceof InMemoryDataTreeCandidate, "Invalid candidate class %s", candidate.getClass());
118         final InMemoryDataTreeCandidate c = (InMemoryDataTreeCandidate)candidate;
119
120         LOG.debug("Updating datastore from {} to {}", rootNode, c.getAfterRoot());
121
122         if (LOG.isTraceEnabled()) {
123             LOG.trace("Data Tree is {}", StoreUtils.toStringTree(c.getAfterRoot().getData()));
124         }
125
126         // Ready to change the context now, make sure no operations are running
127         rwLock.writeLock().lock();
128         try {
129             Preconditions.checkState(c.getBeforeRoot() == rootNode,
130                     "Store tree %s and candidate base %s differ.", rootNode, c.getBeforeRoot());
131             this.rootNode = c.getAfterRoot();
132         } finally {
133             rwLock.writeLock().unlock();
134         }
135     }
136 }