2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.dom.broker.impl
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
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
24 final class HashMapDataStore implements DataStore, AutoCloseable {
25 private val Logger LOG = LoggerFactory.getLogger(HashMapDataStore)
27 val Map<InstanceIdentifier, CompositeNode> configuration = new ConcurrentHashMap();
28 val Map<InstanceIdentifier, CompositeNode> operational = new ConcurrentHashMap();
32 override containsConfigurationPath(InstanceIdentifier path) {
33 return configuration.containsKey(path)
36 override containsOperationalPath(InstanceIdentifier path) {
37 return operational.containsKey(path)
40 override getStoredConfigurationPaths() {
44 override getStoredOperationalPaths() {
48 override readConfigurationData(InstanceIdentifier path) {
49 LOG.trace("Reading configuration path {}", path)
50 configuration.get(path);
53 override readOperationalData(InstanceIdentifier path) {
54 LOG.trace("Reading operational path {}", path)
55 operational.get(path);
60 override requestCommit(DataModification<InstanceIdentifier, CompositeNode> modification) {
61 return new HashMapDataStoreTransaction(modification, this);
64 def RpcResult<Void> rollback(HashMapDataStoreTransaction transaction) {
65 return Rpcs.getRpcResult(true, null, Collections.emptySet);
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);
74 for (removal : modification.removedOperationalData) {
75 LOG.trace("Removing operational path {}", removal)
76 remove(operational,removal);
78 if (LOG.isTraceEnabled()) {
79 for (a : modification.updatedConfigurationData.keySet) {
80 LOG.trace("Adding configuration path {}", a)
82 for (a : modification.updatedOperationalData.keySet) {
83 LOG.trace("Adding operational path {}", a)
86 configuration.putAll(modification.updatedConfigurationData);
87 operational.putAll(modification.updatedOperationalData);
89 return Rpcs.getRpcResult(true, null, Collections.emptySet);
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)) {
99 for(pathToRemove : affected) {
100 LOG.trace("Removed path {}", pathToRemove)
101 map.remove(pathToRemove);
113 class HashMapDataStoreTransaction implements //
114 DataCommitTransaction<InstanceIdentifier, CompositeNode> {
116 val DataModification<InstanceIdentifier, CompositeNode> modification
119 val HashMapDataStore datastore;
122 DataModification<InstanceIdentifier, CompositeNode> modify,
123 HashMapDataStore store
125 _modification = modify;
129 override finish() throws IllegalStateException {
130 datastore.finish(this);
134 override getModification() {
138 override rollback() throws IllegalStateException {
139 datastore.rollback(this);