Cosmetics: check in pom.xml files as _sort_pom_ wants them to be
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / DataTree.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;
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.StoreMetadataNode;
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
18 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeUtils;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.annotations.VisibleForTesting;
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 DataTree {
31     public static final class Snapshot {
32         private final SchemaContext schemaContext;
33         private final StoreMetadataNode rootNode;
34
35         @VisibleForTesting
36         Snapshot(final SchemaContext schemaContext, final StoreMetadataNode rootNode) {
37             this.schemaContext = Preconditions.checkNotNull(schemaContext);
38             this.rootNode = Preconditions.checkNotNull(rootNode);
39         }
40
41         public SchemaContext getSchemaContext() {
42             return schemaContext;
43         }
44
45         public Optional<NormalizedNode<?, ?>> readNode(final InstanceIdentifier path) {
46             return NormalizedNodeUtils.findNode(rootNode.getData(), path);
47         }
48
49         // FIXME: this is a leak of information
50         @Deprecated
51         StoreMetadataNode getRootNode() {
52             return rootNode;
53         }
54
55         @Override
56         public String toString() {
57             return rootNode.getSubtreeVersion().toString();
58         }
59     }
60
61     private static final Logger LOG = LoggerFactory.getLogger(DataTree.class);
62     private final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
63     private StoreMetadataNode rootNode;
64     private SchemaContext currentSchemaContext;
65
66     private DataTree(StoreMetadataNode rootNode, final SchemaContext schemaContext) {
67         this.rootNode = Preconditions.checkNotNull(rootNode);
68         this.currentSchemaContext = schemaContext;
69     }
70
71     public synchronized void setSchemaContext(final SchemaContext newSchemaContext) {
72         Preconditions.checkNotNull(newSchemaContext);
73
74         LOG.info("Attepting to install schema context {}", newSchemaContext);
75
76         /*
77          * FIXME: we should walk the schema contexts, both current and new and see
78          *        whether they are compatible here. Reject incompatible changes.
79          */
80
81         // Ready to change the context now, make sure no operations are running
82         rwLock.writeLock().lock();
83         try {
84             this.currentSchemaContext = newSchemaContext;
85         } finally {
86             rwLock.writeLock().unlock();
87         }
88     }
89
90     public static DataTree create(final SchemaContext schemaContext) {
91         final NodeIdentifier root = new NodeIdentifier(SchemaContext.NAME);
92         final NormalizedNode<?, ?> data = Builders.containerBuilder().withNodeIdentifier(root).build();
93
94         return new DataTree(StoreMetadataNode.createEmpty(data), schemaContext);
95     }
96
97     public Snapshot takeSnapshot() {
98         rwLock.readLock().lock();
99
100         try {
101             return new Snapshot(currentSchemaContext, rootNode);
102         } finally {
103             rwLock.readLock().unlock();
104         }
105     }
106
107     public void commitSnapshot(Snapshot currentSnapshot, StoreMetadataNode newDataTree) {
108         // Ready to change the context now, make sure no operations are running
109         rwLock.writeLock().lock();
110         try {
111             Preconditions.checkState(currentSnapshot.rootNode == rootNode,
112                     String.format("Store snapshot %s and transaction snapshot %s differ.",
113                             rootNode, currentSnapshot.rootNode));
114
115             this.rootNode = newDataTree;
116         } finally {
117             rwLock.writeLock().unlock();
118         }
119     }
120 }