MRI version bumpup for Aluminium
[netvirt.git] / cloud-servicechain / impl / src / main / java / org / opendaylight / netvirt / cloudservicechain / utils / VpnPseudoPortCache.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.netvirt.cloudservicechain.utils;
9
10 import java.util.List;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
13 import javax.annotation.PostConstruct;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.cloud.servicechain.state.rev160711.vpn.to.pseudo.port.list.VpnToPseudoPortData;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Manages a per-blade cache, which is fed by a clustered data change listener.
25  */
26 @Singleton
27 public class VpnPseudoPortCache {
28     private static final Logger LOG = LoggerFactory.getLogger(VpnPseudoPortCache.class);
29
30     private final DataBroker broker;
31     private final ConcurrentMap<String, Long> cache =  new ConcurrentHashMap<>();
32
33     @Inject
34     public VpnPseudoPortCache(DataBroker broker) {
35         this.broker = broker;
36     }
37
38     @PostConstruct
39     public void init() {
40         LOG.info("Initial read of Vpn to VpnPseudoPort map from Datastore");
41         try {
42             List<VpnToPseudoPortData> allVpnToPseudoPortData = VpnServiceChainUtils.getAllVpnToPseudoPortData(broker);
43             for (VpnToPseudoPortData vpnToPseudoPort : allVpnToPseudoPortData) {
44                 add(vpnToPseudoPort.getVrfId(), vpnToPseudoPort.getVpnLportTag());
45             }
46         } catch (InterruptedException | ExecutionException e) {
47             LOG.error("Error reading VPN to pseudo-port map", e);
48         }
49     }
50
51     public void add(@NonNull String vrfId, long vpnPseudoLportTag) {
52         LOG.debug("Adding vpn {} and vpnPseudoLportTag {} to VpnPseudoPortCache", vrfId, vpnPseudoLportTag);
53         cache.put(vrfId, Long.valueOf(vpnPseudoLportTag));
54     }
55
56     @Nullable
57     public Long get(@NonNull String vrfId) {
58         return cache.get(vrfId);
59     }
60
61     @Nullable
62     public Long remove(@NonNull String vrfId) {
63         LOG.debug("Removing vpn {} from VpnPseudoPortCache", vrfId);
64         return cache.remove(vrfId);
65     }
66 }