2e4e5f20aba5eea6f85a61a0ba7c24d9e69fd5d3
[lispflowmapping.git] / mappingservice / clusterdao / src / main / java / org / opendaylight / lispflowmapping / clusterdao / ClusterDAOService.java
1 /*
2  * Copyright (c) 2014 Contextream, 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
9 package org.opendaylight.lispflowmapping.clusterdao;
10
11 import java.util.EnumSet;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
18
19 import org.opendaylight.controller.clustering.services.CacheConfigException;
20 import org.opendaylight.controller.clustering.services.CacheExistException;
21 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
22 import org.opendaylight.controller.clustering.services.IClusterServices;
23 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
24 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
25 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
26 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOCGroup;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class ClusterDAOService implements ILispDAO {
31
32     protected static final Logger LOG = LoggerFactory.getLogger(ClusterDAOService.class);
33     private IClusterContainerServices clusterContainerService = null;
34     private ConcurrentMap<Object, ConcurrentMap<String, Object>> data;
35     private final String CACHE_NAME = "mappingServiceCache";
36     private TimeUnit timeUnit = TimeUnit.SECONDS;
37     private int recordTimeOut = 240;
38     private int cleanInterval = 10;
39     private ScheduledExecutorService scheduler;
40
41     void setClusterContainerService(IClusterContainerServices s) {
42         this.clusterContainerService = s;
43         allocateCache();
44         retrieveCache();
45         scheduler = Executors.newScheduledThreadPool(1);
46         scheduler.scheduleAtFixedRate(new Runnable() {
47
48             public void run() {
49                 cleanOld();
50             }
51         }, 0, cleanInterval, timeUnit);
52     }
53
54     void unsetClusterContainerService(IClusterContainerServices s) {
55         LOG.trace("Cluster Service unset");
56         if (this.clusterContainerService == s) {
57             this.clusterContainerService = null;
58         }
59         scheduler.shutdownNow();
60     }
61
62     private void allocateCache() {
63         if (this.clusterContainerService == null) {
64             LOG.warn("un-initialized clusterContainerService, can't create cache");
65             return;
66         }
67         LOG.trace("Creating Cache for ClusterDAOService");
68         try {
69             this.clusterContainerService.createCache(CACHE_NAME, EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
70         } catch (CacheConfigException cce) {
71             LOG.warn("Cache couldn't be created for ClusterDAOService -  check cache mode");
72         } catch (CacheExistException cce) {
73             LOG.warn("Cache for ClusterDAOService already exists, destroy and recreate");
74         }
75         LOG.trace("Cache successfully created for ClusterDAOService");
76     }
77
78     @SuppressWarnings({ "unchecked" })
79     private void retrieveCache() {
80         if (this.clusterContainerService == null) {
81             LOG.warn("un-initialized clusterContainerService, can't retrieve cache");
82             return;
83         }
84         LOG.trace("Retrieving cache for ClusterDAOService");
85         data = (ConcurrentMap<Object, ConcurrentMap<String, Object>>) this.clusterContainerService.getCache(CACHE_NAME);
86         if (data == null) {
87             LOG.warn("Cache couldn't be retrieved for ClusterDAOService");
88         }
89         LOG.trace("Cache was successfully retrieved for ClusterDAOService");
90     }
91
92     public void getAll(IRowVisitor visitor) {
93         for (ConcurrentMap.Entry<Object, ConcurrentMap<String, Object>> keyEntry : data.entrySet()) {
94             for (Map.Entry<String, Object> valueEntry : keyEntry.getValue().entrySet()) {
95                 visitor.visitRow(keyEntry.getKey(), valueEntry.getKey(), valueEntry.getValue());
96             }
97         }
98     }
99
100     public void put(Object key, MappingEntry<?>... values) {
101         if (!data.containsKey(key)) {
102             data.put(key, new ConcurrentHashMap<String, Object>());
103         }
104         for (MappingEntry<?> entry : values) {
105             data.get(key).put(entry.getKey(), entry.getValue());
106         }
107     }
108
109     public void cleanOld() {
110         getAll(new IRowVisitor() {
111             public void visitRow(Object keyId, String valueKey, Object value) {
112                 if (value instanceof MappingServiceRLOCGroup) {
113                     MappingServiceRLOCGroup rloc = (MappingServiceRLOCGroup) value;
114                     if (isExpired(rloc)) {
115                         removeSpecific(keyId, valueKey);
116                     }
117                 }
118             }
119
120             private boolean isExpired(MappingServiceRLOCGroup rloc) {
121                 return System.currentTimeMillis() - rloc.getRegisterdDate().getTime() > TimeUnit.MILLISECONDS.convert(recordTimeOut, timeUnit);
122             }
123         });
124     }
125
126     public Object getSpecific(Object key, String valueKey) {
127         Map<String, Object> keyToValues = data.get(key);
128         if (keyToValues == null) {
129             return null;
130         }
131         return keyToValues.get(valueKey);
132     }
133
134     public Map<String, Object> get(Object key) {
135         return data.get(key);
136     }
137
138     public void remove(Object key) {
139         data.remove(key);
140     }
141
142     public void removeSpecific(Object key, String valueKey) {
143         if (data.containsKey(key) && data.get(key).containsKey(valueKey)) {
144             data.get(key).remove(valueKey);
145         }
146     }
147
148     public void removeAll() {
149         data.clear();
150     }
151
152     public TimeUnit getTimeUnit() {
153         return timeUnit;
154     }
155
156     public void setRecordTimeOut(int recordTimeOut) {
157         this.recordTimeOut = recordTimeOut;
158     }
159
160     public int getRecordTimeOut() {
161         return recordTimeOut;
162     }
163
164     public void setTimeUnit(TimeUnit timeUnit) {
165         this.timeUnit = timeUnit;
166     }
167 }