Fixed bug when new childs were ommited during data store merge.
[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
22 class HashMapDataStore implements DataStore, AutoCloseable {
23
24
25     val Map<InstanceIdentifier, CompositeNode> configuration = new ConcurrentHashMap();
26     val Map<InstanceIdentifier, CompositeNode> operational = new ConcurrentHashMap();
27     
28     
29     
30     override containsConfigurationPath(InstanceIdentifier path) {
31         throw new UnsupportedOperationException("TODO: auto-generated method stub")
32         
33     }
34     
35     override containsOperationalPath(InstanceIdentifier path) {
36         throw new UnsupportedOperationException("TODO: auto-generated method stub")
37     }
38     
39     override getStoredConfigurationPaths() {
40         configuration.keySet
41     }
42     
43     override getStoredOperationalPaths() {
44         operational.keySet
45     }
46
47     override readConfigurationData(InstanceIdentifier path) {
48         configuration.get(path);
49     }
50
51     override readOperationalData(InstanceIdentifier path) {
52         operational.get(path);
53     }
54
55
56
57     override requestCommit(DataModification<InstanceIdentifier, CompositeNode> modification) {
58         return new HashMapDataStoreTransaction(modification, this);
59     }
60
61     def RpcResult<Void> rollback(HashMapDataStoreTransaction transaction) {
62         return Rpcs.getRpcResult(true, null, Collections.emptySet);
63     }
64
65     def RpcResult<Void> finish(HashMapDataStoreTransaction transaction) {
66         val modification = transaction.modification;
67         configuration.putAll(modification.updatedConfigurationData);
68         operational.putAll(modification.updatedOperationalData);
69
70         for (removal : modification.removedConfigurationData) {
71             remove(configuration,removal);
72         }
73         for (removal : modification.removedOperationalData) {
74             remove(operational,removal);
75         }
76         return Rpcs.getRpcResult(true, null, Collections.emptySet);
77     }
78     
79     def remove(Map<InstanceIdentifier, CompositeNode> map, InstanceIdentifier identifier) {
80         val affected = new HashSet<InstanceIdentifier>();
81         for(path : map.keySet) {
82             if(identifier.contains(path)) {
83                 affected.add(path);
84             }
85         }
86         for(pathToRemove : affected) {
87             map.remove(pathToRemove);
88         }
89         
90     }
91
92
93     override close()  {
94         // NOOP
95     }
96     
97 }
98
99 class HashMapDataStoreTransaction implements // 
100 DataCommitTransaction<InstanceIdentifier, CompositeNode> {
101     @Property
102     val DataModification<InstanceIdentifier, CompositeNode> modification
103
104     @Property
105     val HashMapDataStore datastore;
106
107     new(
108         DataModification<InstanceIdentifier, CompositeNode> modify,
109         HashMapDataStore store
110     ) {
111         _modification = modify;
112         _datastore = store;
113     }
114
115     override finish() throws IllegalStateException {
116         datastore.finish(this);
117
118     }
119
120     override getModification() {
121         this._modification;
122     }
123
124     override rollback() throws IllegalStateException {
125         datastore.rollback(this);
126     }
127 }