Remove redundant names in paths
[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.Nonnull;
14 import javax.annotation.Nullable;
15 import javax.annotation.PostConstruct;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.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         List<VpnToPseudoPortData> allVpnToPseudoPortData = VpnServiceChainUtils.getAllVpnToPseudoPortData(broker);
42         for (VpnToPseudoPortData vpnToPseudoPort : allVpnToPseudoPortData) {
43             add(vpnToPseudoPort.getVrfId(), vpnToPseudoPort.getVpnLportTag());
44         }
45     }
46
47     public void add(@Nonnull String vrfId, long vpnPseudoLportTag) {
48         LOG.debug("Adding vpn {} and vpnPseudoLportTag {} to VpnPseudoPortCache", vrfId, vpnPseudoLportTag);
49         cache.put(vrfId, Long.valueOf(vpnPseudoLportTag));
50     }
51
52     @Nullable
53     public Long get(@Nonnull String vrfId) {
54         return cache.get(vrfId);
55     }
56
57     @Nullable
58     public Long remove(@Nonnull String vrfId) {
59         LOG.debug("Removing vpn {} from VpnPseudoPortCache", vrfId);
60         return cache.remove(vrfId);
61     }
62 }