f2e7773a454565e537053f72889cc0ebfb0a3f6a
[controller.git] / opendaylight / md-sal / clustered-data-store / implementation / src / main / java / org / opendaylight / controller / datastore / internal / ClusteredDataStoreImpl.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.datastore.internal;
11
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.clustering.services.CacheConfigException;
14 import org.opendaylight.controller.clustering.services.CacheExistException;
15 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
16 import org.opendaylight.controller.clustering.services.IClusterServices;
17 import org.opendaylight.controller.datastore.ClusteredDataStore;
18 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
19 import org.opendaylight.controller.sal.common.util.Rpcs;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23
24 import java.util.Collections;
25 import java.util.EnumSet;
26 import java.util.Set;
27 import java.util.concurrent.ConcurrentMap;
28
29 /**
30  * The ClusteredDataStoreImpl stores global data to be shared across a controller cluster. It uses Clustering Services.
31  */
32 public class ClusteredDataStoreImpl implements ClusteredDataStore {
33
34
35     public static final String OPERATIONAL_DATA_CACHE = "clustered_data_store.operational_data_cache";
36     public static final String CONFIGURATION_DATA_CACHE = "clustered_data_store.configuration_data_cache";
37
38     private ConcurrentMap operationalDataCache;
39     private ConcurrentMap configurationDataCache;
40
41     public ClusteredDataStoreImpl(IClusterGlobalServices clusterGlobalServices) throws CacheExistException, CacheConfigException {
42         Preconditions.checkNotNull(clusterGlobalServices, "clusterGlobalServices cannot be null");
43
44         operationalDataCache = clusterGlobalServices.createCache(OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
45
46         if(operationalDataCache == null){
47             Preconditions.checkNotNull(operationalDataCache, "operationalDataCache cannot be null");
48         }
49
50         configurationDataCache = clusterGlobalServices.createCache(CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
51
52         if(configurationDataCache == null){
53             Preconditions.checkNotNull(configurationDataCache, "configurationDataCache cannot be null");
54         }
55
56     }
57
58     @Override
59     public DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> requestCommit(DataModification<InstanceIdentifier<? extends Object>, Object> modification) {
60         return new ClusteredDataStoreTransaction(modification);
61     }
62
63     @Override
64     public Object readOperationalData(InstanceIdentifier<? extends Object> path) {
65         Preconditions.checkNotNull(path, "path cannot be null");
66         return operationalDataCache.get(path);
67     }
68
69     @Override
70     public Object readConfigurationData(InstanceIdentifier<? extends Object> path) {
71         Preconditions.checkNotNull(path, "path cannot be null");
72         return configurationDataCache.get(path);
73     }
74
75     private RpcResult<Void> finish(final ClusteredDataStoreTransaction transaction) {
76       final DataModification<InstanceIdentifier<? extends Object>,Object> modification = transaction.getModification();
77
78       this.configurationDataCache.putAll(modification.getUpdatedConfigurationData());
79       this.operationalDataCache.putAll(modification.getUpdatedOperationalData());
80
81       for (final InstanceIdentifier<? extends Object> removal : modification.getRemovedConfigurationData()) {
82         this.configurationDataCache.remove(removal);
83       }
84
85       for (final InstanceIdentifier<? extends Object> removal : modification.getRemovedOperationalData()) {
86         this.operationalDataCache.remove(removal  );
87       }
88
89       Set<RpcError> _emptySet = Collections.<RpcError>emptySet();
90       return Rpcs.<Void>getRpcResult(true, null, _emptySet);
91     }
92
93     private RpcResult<Void> rollback(final ClusteredDataStoreTransaction transaction) {
94       Set<RpcError> _emptySet = Collections.<RpcError>emptySet();
95       return Rpcs.<Void>getRpcResult(true, null, _emptySet);
96     }
97
98     private class ClusteredDataStoreTransaction implements DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> {
99         private final DataModification<InstanceIdentifier<? extends Object>,Object> modification;
100
101         public ClusteredDataStoreTransaction(DataModification<InstanceIdentifier<? extends Object>,Object> modification){
102             Preconditions.checkNotNull(modification, "modification cannot be null");
103
104             this.modification = modification;
105         }
106
107         @Override
108         public DataModification<InstanceIdentifier<? extends Object>, Object> getModification() {
109             return this.modification;
110         }
111
112         @Override
113         public RpcResult<Void> finish() throws IllegalStateException {
114             return ClusteredDataStoreImpl.this.finish(this);
115         }
116
117         @Override
118         public RpcResult<Void> rollback() throws IllegalStateException {
119             return ClusteredDataStoreImpl.this.rollback(this);
120         }
121     }
122 }