3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.datastore.internal;
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;
24 import java.util.Collections;
25 import java.util.EnumSet;
27 import java.util.concurrent.ConcurrentMap;
30 * The ClusteredDataStoreImpl stores global data to be shared across a controller cluster. It uses Clustering Services.
32 public class ClusteredDataStoreImpl implements ClusteredDataStore {
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";
38 private ConcurrentMap operationalDataCache;
39 private ConcurrentMap configurationDataCache;
41 public ClusteredDataStoreImpl(IClusterGlobalServices clusterGlobalServices) throws CacheExistException, CacheConfigException {
42 Preconditions.checkNotNull(clusterGlobalServices, "clusterGlobalServices cannot be null");
44 operationalDataCache = clusterGlobalServices.createCache(OPERATIONAL_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
46 if(operationalDataCache == null){
47 Preconditions.checkNotNull(operationalDataCache, "operationalDataCache cannot be null");
50 configurationDataCache = clusterGlobalServices.createCache(CONFIGURATION_DATA_CACHE, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
52 if(configurationDataCache == null){
53 Preconditions.checkNotNull(configurationDataCache, "configurationDataCache cannot be null");
59 public DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> requestCommit(DataModification<InstanceIdentifier<? extends Object>, Object> modification) {
60 return new ClusteredDataStoreTransaction(modification);
64 public Object readOperationalData(InstanceIdentifier<? extends Object> path) {
65 Preconditions.checkNotNull(path, "path cannot be null");
66 return operationalDataCache.get(path);
70 public Object readConfigurationData(InstanceIdentifier<? extends Object> path) {
71 Preconditions.checkNotNull(path, "path cannot be null");
72 return configurationDataCache.get(path);
75 private RpcResult<Void> finish(final ClusteredDataStoreTransaction transaction) {
76 final DataModification<InstanceIdentifier<? extends Object>,Object> modification = transaction.getModification();
78 this.configurationDataCache.putAll(modification.getUpdatedConfigurationData());
79 this.operationalDataCache.putAll(modification.getUpdatedOperationalData());
81 for (final InstanceIdentifier<? extends Object> removal : modification.getRemovedConfigurationData()) {
82 this.configurationDataCache.remove(removal);
85 for (final InstanceIdentifier<? extends Object> removal : modification.getRemovedOperationalData()) {
86 this.operationalDataCache.remove(removal );
89 Set<RpcError> _emptySet = Collections.<RpcError>emptySet();
90 return Rpcs.<Void>getRpcResult(true, null, _emptySet);
93 private RpcResult<Void> rollback(final ClusteredDataStoreTransaction transaction) {
94 Set<RpcError> _emptySet = Collections.<RpcError>emptySet();
95 return Rpcs.<Void>getRpcResult(true, null, _emptySet);
98 private class ClusteredDataStoreTransaction implements DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> {
99 private final DataModification<InstanceIdentifier<? extends Object>,Object> modification;
101 public ClusteredDataStoreTransaction(DataModification<InstanceIdentifier<? extends Object>,Object> modification){
102 Preconditions.checkNotNull(modification, "modification cannot be null");
104 this.modification = modification;
108 public DataModification<InstanceIdentifier<? extends Object>, Object> getModification() {
109 return this.modification;
113 public RpcResult<Void> finish() throws IllegalStateException {
114 return ClusteredDataStoreImpl.this.finish(this);
118 public RpcResult<Void> rollback() throws IllegalStateException {
119 return ClusteredDataStoreImpl.this.rollback(this);