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

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