While the controller is starting up, we see the following info messages which seems...
[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 org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import java.util.concurrent.locks.ReadWriteLock;
24 import java.util.concurrent.locks.ReentrantReadWriteLock;
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 ModificationApplyOperation applyOper = new AlwaysFailOperation();
35     private SchemaContext currentSchemaContext;
36     private TreeNode rootNode;
37
38     public InMemoryDataTree(final TreeNode rootNode, final SchemaContext schemaContext) {
39         this.rootNode = Preconditions.checkNotNull(rootNode);
40
41         if (schemaContext != null) {
42             // Also sets applyOper
43             setSchemaContext(schemaContext);
44         }
45     }
46
47     @Override
48     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
49         Preconditions.checkNotNull(newSchemaContext);
50
51         LOG.info("Attempting to install schema contexts");
52         LOG.debug("Following schema contexts will be attempted {}",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(final DataTreeModification modification) throws DataValidationFailedException {
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.<TreeNode>of(rootNode));
88     }
89
90     @Override
91     public synchronized DataTreeCandidate prepare(final DataTreeModification modification) {
92         Preconditions.checkArgument(modification instanceof InMemoryDataTreeModification, "Invalid modification class %s", modification.getClass());
93
94         final InMemoryDataTreeModification m = (InMemoryDataTreeModification)modification;
95         final ModifiedNode root = m.getRootModification();
96
97         if (root.getType() == ModificationType.UNMODIFIED) {
98             return new NoopDataTreeCandidate(PUBLIC_ROOT_PATH, root);
99         }
100
101         rwLock.writeLock().lock();
102         try {
103             final Optional<TreeNode> newRoot = m.getStrategy().apply(m.getRootModification(),
104                     Optional.<TreeNode>of(rootNode), rootNode.getSubtreeVersion().next());
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(final 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 datastore from {} to {}", rootNode, c.getAfterRoot());
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 }