12835ccf0a235ec2a312e32313818525425c6f36
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / impl / HashMapDataStore.xtend
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.sal.dom.broker.impl
9
10 import org.opendaylight.controller.md.sal.common.api.data.DataModification
11 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction
12 import org.opendaylight.yangtools.yang.common.RpcResult
13 import java.util.Map
14 import java.util.concurrent.ConcurrentHashMap
15 import org.opendaylight.controller.sal.common.util.Rpcs
16 import java.util.Collections
17 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode
19 import org.opendaylight.controller.sal.core.api.data.DataStore
20 import java.util.HashSet
21 import org.slf4j.LoggerFactory
22 import org.slf4j.Logger
23
24 final class HashMapDataStore implements DataStore, AutoCloseable {
25     private val Logger LOG = LoggerFactory.getLogger(HashMapDataStore)
26
27     val Map<InstanceIdentifier, CompositeNode> configuration = new ConcurrentHashMap();
28     val Map<InstanceIdentifier, CompositeNode> operational = new ConcurrentHashMap();
29     
30     
31     
32     override containsConfigurationPath(InstanceIdentifier path) {
33         return configuration.containsKey(path)
34     }
35     
36     override containsOperationalPath(InstanceIdentifier path) {
37         return operational.containsKey(path)
38     }
39     
40     override getStoredConfigurationPaths() {
41         configuration.keySet
42     }
43     
44     override getStoredOperationalPaths() {
45         operational.keySet
46     }
47
48     override readConfigurationData(InstanceIdentifier path) {
49         LOG.trace("Reading configuration path {}", path)
50         configuration.get(path);
51     }
52
53     override readOperationalData(InstanceIdentifier path) {
54         LOG.trace("Reading operational path {}", path)
55         operational.get(path);
56     }
57
58
59
60     override requestCommit(DataModification<InstanceIdentifier, CompositeNode> modification) {
61         return new HashMapDataStoreTransaction(modification, this);
62     }
63
64     def RpcResult<Void> rollback(HashMapDataStoreTransaction transaction) {
65         return Rpcs.getRpcResult(true, null, Collections.emptySet);
66     }
67
68     def RpcResult<Void> finish(HashMapDataStoreTransaction transaction) {
69         val modification = transaction.modification;
70         for (removal : modification.removedConfigurationData) {
71             LOG.trace("Removing configuration path {}", removal)
72             remove(configuration,removal);
73         }
74         for (removal : modification.removedOperationalData) {
75             LOG.trace("Removing operational path {}", removal)
76             remove(operational,removal);
77         }
78         if (LOG.isTraceEnabled()) {
79             for (a : modification.updatedConfigurationData.keySet) {
80                 LOG.trace("Adding configuration path {}", a)
81             }
82             for (a : modification.updatedOperationalData.keySet) {
83                 LOG.trace("Adding operational path {}", a)
84             }
85         }
86         configuration.putAll(modification.updatedConfigurationData);
87         operational.putAll(modification.updatedOperationalData);
88
89         return Rpcs.getRpcResult(true, null, Collections.emptySet);
90     }
91     
92     def remove(Map<InstanceIdentifier, CompositeNode> map, InstanceIdentifier identifier) {
93         val affected = new HashSet<InstanceIdentifier>();
94         for(path : map.keySet) {
95             if(identifier.contains(path)) {
96                 affected.add(path);
97             }
98         }
99         for(pathToRemove : affected) {
100             LOG.trace("Removed path {}", pathToRemove)
101             map.remove(pathToRemove);
102         }
103         
104     }
105
106
107     override close()  {
108         // NOOP
109     }
110     
111 }
112
113 class HashMapDataStoreTransaction implements // 
114 DataCommitTransaction<InstanceIdentifier, CompositeNode> {
115     @Property
116     val DataModification<InstanceIdentifier, CompositeNode> modification
117
118     @Property
119     val HashMapDataStore datastore;
120
121     new(
122         DataModification<InstanceIdentifier, CompositeNode> modify,
123         HashMapDataStore store
124     ) {
125         _modification = modify;
126         _datastore = store;
127     }
128
129     override finish() throws IllegalStateException {
130         datastore.finish(this);
131
132     }
133
134     override getModification() {
135         this._modification;
136     }
137
138     override rollback() throws IllegalStateException {
139         datastore.rollback(this);
140     }
141 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.