Deprecated Rpcs and RpcErrors helper classes
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / impl / HashMapDataStore.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.sal.dom.broker.impl;
9
10 import java.util.HashSet;
11 import java.util.Map;
12 import java.util.Set;
13 import java.util.concurrent.ConcurrentHashMap;
14
15 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
16 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
17 import org.opendaylight.controller.sal.core.api.data.DataStore;
18 import org.opendaylight.yangtools.yang.common.RpcResult;
19 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
20 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public final class HashMapDataStore implements DataStore, AutoCloseable {
26     private static final Logger LOG = LoggerFactory
27             .getLogger(HashMapDataStore.class);
28
29     private final Map<InstanceIdentifier, CompositeNode> configuration = new ConcurrentHashMap<InstanceIdentifier, CompositeNode>();
30     private final Map<InstanceIdentifier, CompositeNode> operational = new ConcurrentHashMap<InstanceIdentifier, CompositeNode>();
31
32     @Override
33     public boolean containsConfigurationPath(final InstanceIdentifier path) {
34         return configuration.containsKey(path);
35     }
36
37     @Override
38     public boolean containsOperationalPath(final InstanceIdentifier path) {
39         return operational.containsKey(path);
40     }
41
42     @Override
43     public Iterable<InstanceIdentifier> getStoredConfigurationPaths() {
44         return configuration.keySet();
45     }
46
47     @Override
48     public Iterable<InstanceIdentifier> getStoredOperationalPaths() {
49         return operational.keySet();
50     }
51
52     @Override
53     public CompositeNode readConfigurationData(final InstanceIdentifier path) {
54         LOG.trace("Reading configuration path {}", path);
55         return configuration.get(path);
56     }
57
58     @Override
59     public CompositeNode readOperationalData(InstanceIdentifier path) {
60         LOG.trace("Reading operational path {}", path);
61         return operational.get(path);
62     }
63
64     @Override
65     public DataCommitHandler.DataCommitTransaction<InstanceIdentifier, CompositeNode> requestCommit(
66             final DataModification<InstanceIdentifier, CompositeNode> modification) {
67         return new HashMapDataStoreTransaction(modification, this);
68     }
69
70     public RpcResult<Void> rollback(HashMapDataStoreTransaction transaction) {
71         return RpcResultBuilder.<Void> success().build();
72     }
73
74     public RpcResult<Void> finish(HashMapDataStoreTransaction transaction) {
75         final DataModification<InstanceIdentifier, CompositeNode> modification = transaction
76                 .getModification();
77         for (final InstanceIdentifier removal : modification
78                 .getRemovedConfigurationData()) {
79             LOG.trace("Removing configuration path {}", removal);
80             remove(configuration, removal);
81         }
82         for (final InstanceIdentifier removal : modification
83                 .getRemovedOperationalData()) {
84             LOG.trace("Removing operational path {}", removal);
85             remove(operational, removal);
86         }
87         if (LOG.isTraceEnabled()) {
88             for (final InstanceIdentifier a : modification
89                     .getUpdatedConfigurationData().keySet()) {
90                 LOG.trace("Adding configuration path {}", a);
91             }
92             for (final InstanceIdentifier a : modification
93                     .getUpdatedOperationalData().keySet()) {
94                 LOG.trace("Adding operational path {}", a);
95             }
96         }
97         configuration.putAll(modification.getUpdatedConfigurationData());
98         operational.putAll(modification.getUpdatedOperationalData());
99
100         return RpcResultBuilder.<Void> success().build();
101     }
102
103     public void remove(final Map<InstanceIdentifier, CompositeNode> map,
104             final InstanceIdentifier identifier) {
105         Set<InstanceIdentifier> affected = new HashSet<InstanceIdentifier>();
106         for (final InstanceIdentifier path : map.keySet()) {
107             if (identifier.contains(path)) {
108                 affected.add(path);
109             }
110         }
111         for (final InstanceIdentifier pathToRemove : affected) {
112             LOG.trace("Removed path {}", pathToRemove);
113             map.remove(pathToRemove);
114         }
115     }
116
117     @Override
118     public void close() {
119         // NOOP
120     }
121 }

©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.