fbd9e4638fd150b688456f5149e859085979c189
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / timebucket / implementation / TimeBucketMappingTimeoutService.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc.  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.implementation.timebucket.implementation;
10
11 import org.opendaylight.lispflowmapping.implementation.MappingSystem;
12 import org.opendaylight.lispflowmapping.implementation.timebucket.containers.TimeBucketWheel;
13 import org.opendaylight.lispflowmapping.implementation.timebucket.interfaces.ISouthBoundMappingTimeoutService;
14 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Created by Shakib Ahmed on 12/1/16.
21  */
22 public class TimeBucketMappingTimeoutService implements ISouthBoundMappingTimeoutService {
23     private static final Logger LOG = LoggerFactory.getLogger(TimeBucketWheel.class);
24
25     private final TimeBucketWheel timeBucketWheel;
26
27     public TimeBucketMappingTimeoutService(int numberOfBucket, long mappingRecordValidityInMillis,
28                                            MappingSystem mappingSystem) {
29         timeBucketWheel = new TimeBucketWheel(numberOfBucket, mappingRecordValidityInMillis, mappingSystem);
30     }
31
32     @Override
33     public int addMapping(Eid key, MappingData mappingData) {
34         long timestamp = System.currentTimeMillis();
35         if (mappingData.getTimestamp() != null) {
36             timestamp = mappingData.getTimestamp().getTime();
37         }
38         return timeBucketWheel.add(key, mappingData, timestamp);
39     }
40
41     @Override
42     public int refreshMapping(Eid key, MappingData newMappingData, int presentBucketId) {
43         long timestamp = System.currentTimeMillis();
44         if (newMappingData.getTimestamp() != null) {
45             timestamp = newMappingData.getTimestamp().getTime();
46         }
47         return timeBucketWheel.refreshMappping(key, newMappingData, timestamp, presentBucketId);
48     }
49
50     @Override
51     public void removeMappingFromTimeoutService(Eid key, int presentBucketId) {
52         timeBucketWheel.removeMapping(key, presentBucketId);
53     }
54
55     @Override
56     public void removeExpiredMappings() {
57         timeBucketWheel.clearExpiredMappingAndRotate();
58     }
59 }