Moving vpn-instance yang from VPNMgr > NeutronVPN
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / NatOverVxlanUtil.java
1 /*
2  * Copyright © 2017 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.natservice.internal;
9
10 import java.util.concurrent.ExecutionException;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import org.opendaylight.genius.networkutils.VniUtils;
14 import org.opendaylight.yangtools.yang.common.Uint32;
15 import org.opendaylight.yangtools.yang.common.Uint64;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 @Singleton
20 public class NatOverVxlanUtil {
21
22     private static final Logger LOG = LoggerFactory.getLogger(NatOverVxlanUtil.class);
23
24     private VniUtils vniUtils;
25
26     @Inject
27     public NatOverVxlanUtil(final VniUtils vniUtils) {
28         this.vniUtils = vniUtils;
29     }
30
31     public Uint32 getInternetVpnVni(String vpnUuid, Uint32 vpnid) {
32         Uint32 internetVpnVni = Uint32.valueOf(getVNI(vpnUuid).longValue());
33         if (internetVpnVni.longValue() == 0) {
34             LOG.warn("getInternetVpnVni : Unable to obtain Internet Vpn VNI from VNI POOL for Vpn {}."
35                     + "Will use tunnel_id {} as Internet VNI", vpnUuid, vpnid);
36             return vpnid;
37         }
38
39         return internetVpnVni;
40     }
41
42     public Uint64 getRouterVni(String routerName, Uint32 routerId) {
43         Uint64 routerVni = getVNI(routerName);
44         if (routerVni.longValue() == 0) {
45             LOG.warn("getRouterVni : Unable to obtain Router VNI from VNI POOL for router {}."
46                     + "Router ID will be used as tun_id", routerName);
47             return Uint64.valueOf(routerId);
48         }
49         return routerVni;
50     }
51
52     public void releaseVNI(String vniKey) {
53         try {
54             vniUtils.releaseVNI(vniKey);
55         } catch (InterruptedException | ExecutionException e) {
56             LOG.error("releaseVNI : Exception in release VNI for Key {}", vniKey, e);
57         }
58     }
59
60     private Uint64 getVNI(String vniKey) {
61         try {
62             return vniUtils.getVNI(vniKey);
63         } catch (InterruptedException | ExecutionException e) {
64             LOG.error("getVNI : Exception in get VNI for key {}", vniKey, e);
65         }
66         return Uint64.ZERO;
67     }
68 }