Reenable Neutron using YANG models
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / lisp / cache / MapRegisterCache.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, 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 package org.opendaylight.lispflowmapping.southbound.lisp.cache;
9
10 import java.util.Map;
11 import java.util.concurrent.ConcurrentHashMap;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.key.container.MapRegisterCacheKey;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadataBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.value.grouping.MapRegisterCacheValue;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.value.grouping.MapRegisterCacheValueBuilder;
17
18 public class MapRegisterCache {
19
20     protected final Map<MapRegisterCacheKey, MapRegisterCacheValue> cache;
21
22     public MapRegisterCache() {
23         cache = new ConcurrentHashMap<>();
24     }
25
26     public void addEntry(final MapRegisterCacheKey mapRegisterCacheKey, final MapRegisterCacheValue
27             mapRegisterCacheValue) {
28         cache.put(mapRegisterCacheKey, mapRegisterCacheValue);
29     }
30
31     public void addEntry(final Map.Entry<MapRegisterCacheKey, MapRegisterCacheValue>
32                                  mapRegisterCacheEntry) {
33         cache.put(mapRegisterCacheEntry.getKey(), mapRegisterCacheEntry.getValue());
34     }
35
36     public MapRegisterCacheValue getEntry(final MapRegisterCacheKey mapRegisterCacheKey) {
37         if (mapRegisterCacheKey != null) {
38             return cache.get(mapRegisterCacheKey);
39         }
40         return null;
41     }
42
43     public void removeEntry(final MapRegisterCacheKey mapRegisterCacheKey) {
44         if (mapRegisterCacheKey != null) {
45             cache.remove(mapRegisterCacheKey);
46         }
47     }
48
49     public MapRegisterCacheValue refreshEntry(final MapRegisterCacheKey mapRegisterCacheKey) {
50         final MapRegisterCacheValue mapRegisterCacheValueOld = getEntry(mapRegisterCacheKey);
51         final MapRegisterCacheMetadata mapRegisterCacheMetadataOld = mapRegisterCacheValueOld
52                 .getMapRegisterCacheMetadata();
53
54         final MapRegisterCacheMetadataBuilder mapRegisterCacheMetadataBuilderNew = new MapRegisterCacheMetadataBuilder
55                 (mapRegisterCacheMetadataOld);
56         mapRegisterCacheMetadataBuilderNew.setTimestamp(System.currentTimeMillis());
57
58         final MapRegisterCacheValueBuilder mapRegisterCacheValueBuilderNew = new MapRegisterCacheValueBuilder();
59         mapRegisterCacheValueBuilderNew.setPacketData(mapRegisterCacheValueOld.getPacketData());
60         mapRegisterCacheValueBuilderNew.setAuthKeyValue(mapRegisterCacheValueOld.getAuthKeyValue());
61         mapRegisterCacheValueBuilderNew.setAuthKeyType(mapRegisterCacheValueOld.getAuthKeyType());
62         mapRegisterCacheValueBuilderNew.setMapRegisterCacheMetadata(mapRegisterCacheMetadataBuilderNew.build());
63
64         return mapRegisterCacheValueBuilderNew.build();
65     }
66
67     public int cacheSize() {
68         return cache.size();
69     }
70 }