Merge "Bring some reliability in the eclipse and maven mixed builds"
[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 final ConcurrentMap operationalDataCache;
39     private final ConcurrentMap configurationDataCache;
40
41     public ClusteredDataStoreImpl(IClusterGlobalServices clusterGlobalServices) throws CacheExistException, CacheConfigException {
42         Preconditions.checkNotNull(clusterGlobalServices, "clusterGlobalServices cannot be null");
43
44         operationalDataCache = getOrCreateCache(clusterGlobalServices, OPERATIONAL_DATA_CACHE);
45
46         if(operationalDataCache == null){
47             Preconditions.checkNotNull(operationalDataCache, "operationalDataCache cannot be null");
48         }
49
50         configurationDataCache = getOrCreateCache(clusterGlobalServices, CONFIGURATION_DATA_CACHE);
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
99     private ConcurrentMap getOrCreateCache(IClusterGlobalServices clusterGlobalServices, String name) throws CacheConfigException {
100         ConcurrentMap cache = clusterGlobalServices.getCache(name);
101
102         if(cache == null) {
103             try {
104                 cache = clusterGlobalServices.createCache(name, EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
105             } catch (CacheExistException e) {
106                 cache = clusterGlobalServices.getCache(name);
107             }
108         }
109         return cache;
110     }
111
112     private class ClusteredDataStoreTransaction implements DataCommitTransaction<InstanceIdentifier<? extends Object>, Object> {
113         private final DataModification<InstanceIdentifier<? extends Object>,Object> modification;
114
115         public ClusteredDataStoreTransaction(DataModification<InstanceIdentifier<? extends Object>,Object> modification){
116             Preconditions.checkNotNull(modification, "modification cannot be null");
117
118             this.modification = modification;
119         }
120
121         @Override
122         public DataModification<InstanceIdentifier<? extends Object>, Object> getModification() {
123             return this.modification;
124         }
125
126         @Override
127         public RpcResult<Void> finish() throws IllegalStateException {
128             return ClusteredDataStoreImpl.this.finish(this);
129         }
130
131         @Override
132         public RpcResult<Void> rollback() throws IllegalStateException {
133             return ClusteredDataStoreImpl.this.rollback(this);
134         }
135     }
136 }