Fix logging issues in natservice
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / EvpnNaptSwitchHA.java
1 /*
2  * Copyright (c) 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
9 package org.opendaylight.netvirt.natservice.internal;
10
11 import java.math.BigInteger;
12
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.genius.mdsalutil.FlowEntity;
18 import org.opendaylight.genius.mdsalutil.NwConstants;
19 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 @Singleton
25 public class EvpnNaptSwitchHA {
26
27     private static final Logger LOG = LoggerFactory.getLogger(EvpnNaptSwitchHA.class);
28     private final DataBroker dataBroker;
29     private final IMdsalApiManager mdsalManager;
30     private final IdManagerService idManager;
31     private final EvpnSnatFlowProgrammer evpnSnatFlowProgrammer;
32
33     @Inject
34     public EvpnNaptSwitchHA(final DataBroker dataBroker, final IMdsalApiManager mdsalManager,
35                         final EvpnSnatFlowProgrammer evpnSnatFlowProgrammer,
36                         final IdManagerService idManager) {
37         this.dataBroker = dataBroker;
38         this.mdsalManager = mdsalManager;
39         this.evpnSnatFlowProgrammer = evpnSnatFlowProgrammer;
40         this.idManager = idManager;
41     }
42
43     public void evpnRemoveSnatFlowsInOldNaptSwitch(String routerName, long routerId, String vpnName,
44                                                    BigInteger naptSwitch, WriteTransaction removeFlowInvTx) {
45         //Handling VXLAN Provider type flow removal from old NAPT switch
46         Long vpnId = NatUtil.getNetworkVpnIdFromRouterId(dataBroker, routerId);
47         if (vpnId == NatConstants.INVALID_ID) {
48             LOG.error("evpnRemoveSnatFlowsInOldNaptSwitch : Unable to retrieved vpnId for routerId {}", routerId);
49             return;
50         }
51         String rd = NatUtil.getVpnRd(dataBroker, vpnName);
52         if (rd == null) {
53             LOG.error("evpnRemoveSnatFlowsInOldNaptSwitch : Could not retrieve RD value from VPN Name {} ", vpnName);
54             return;
55         }
56         long l3Vni = NatEvpnUtil.getL3Vni(dataBroker, rd);
57         if (l3Vni == NatConstants.DEFAULT_L3VNI_VALUE) {
58             LOG.debug("evpnRemoveSnatFlowsInOldNaptSwitch : L3VNI value is not configured in Internet VPN {} and RD {} "
59                     + "Carve-out L3VNI value from OpenDaylight VXLAN VNI Pool and continue to installing "
60                     + "NAT flows", vpnName, rd);
61             l3Vni = NatOverVxlanUtil.getInternetVpnVni(idManager, vpnName, routerId).longValue();
62         }
63         String gwMacAddress = NatUtil.getExtGwMacAddFromRouterName(dataBroker, routerName);
64         if (gwMacAddress == null) {
65             LOG.error("evpnRemoveSnatFlowsInOldNaptSwitch : Unable to Get External Gateway MAC address for "
66                     + "External Router ID {} ", routerId);
67             return;
68         }
69         //Remove the L3_GW_MAC_TABLE which forwards the packet to Inbound NAPT Table (table19->44)
70         NatEvpnUtil.removeL3GwMacTableEntry(naptSwitch, vpnId, gwMacAddress, mdsalManager, removeFlowInvTx);
71
72         //Remove the PDNAT_TABLE which forwards the packet to Inbound NAPT Table (table25->44)
73         NatUtil.removePreDnatToSnatTableEntry(mdsalManager, naptSwitch, removeFlowInvTx);
74
75         //Remove the PSNAT_TABLE which forwards the packet to Outbound NAPT Table (table26->46)
76         String flowRef = getFlowRefSnat(naptSwitch, NwConstants.PSNAT_TABLE, routerName);
77         FlowEntity flowEntity = NatUtil.buildFlowEntity(naptSwitch, NwConstants.PSNAT_TABLE, flowRef);
78         LOG.info("evpnRemoveSnatFlowsInOldNaptSwitch: Remove the flow (table26->46) in table {} "
79                 + "for the old napt switch with the DPN ID {} and router ID {}",
80                 NwConstants.PSNAT_TABLE, naptSwitch, routerId);
81         mdsalManager.removeFlowToTx(flowEntity, removeFlowInvTx);
82
83         //Remove the Terminating Service table entry which forwards the packet to Inbound NAPT Table (table36->44)
84         LOG.info("evpnRemoveSnatFlowsInOldNaptSwitch : Remove the flow (table36->44) in table {} "
85                 + "for the old napt switch with the DPN ID {} and router ID {}",
86                 NwConstants.INTERNAL_TUNNEL_TABLE, naptSwitch, routerId);
87         evpnSnatFlowProgrammer.removeTunnelTableEntry(naptSwitch, l3Vni, removeFlowInvTx);
88
89         //Remove the INTERNAL_TUNNEL_TABLE entry which forwards the packet to Outbound NAPT Table (table36->46)
90         long tunnelId = NatEvpnUtil.getTunnelIdForRouter(idManager, dataBroker, routerName, routerId);
91
92         String tsFlowRef = getFlowRefTs(naptSwitch, NwConstants.INTERNAL_TUNNEL_TABLE, tunnelId);
93         FlowEntity tsNatFlowEntity = NatUtil.buildFlowEntity(naptSwitch, NwConstants.INTERNAL_TUNNEL_TABLE, tsFlowRef);
94         LOG.info("evpnRemoveSnatFlowsInOldNaptSwitch : Remove the flow in the {} for the active switch "
95                 + "with the DPN ID {} and router ID {}", NwConstants.INTERNAL_TUNNEL_TABLE, naptSwitch, routerId);
96         mdsalManager.removeFlowToTx(tsNatFlowEntity, removeFlowInvTx);
97
98     }
99
100     private String getFlowRefTs(BigInteger dpnId, short tableId, long routerID) {
101         return NatConstants.NAPT_FLOWID_PREFIX + dpnId + NatConstants.FLOWID_SEPARATOR + tableId + NatConstants
102                 .FLOWID_SEPARATOR + routerID;
103     }
104
105     private String getFlowRefSnat(BigInteger dpnId, short tableId, String routerID) {
106         return NatConstants.SNAT_FLOWID_PREFIX + dpnId + NatConstants.FLOWID_SEPARATOR + tableId + NatConstants
107                 .FLOWID_SEPARATOR + routerID;
108     }
109 }