a3aabd98d474a43ca04090dd37d878dd8dd7a72a
[netvirt.git] / vpnservice / natservice / natservice-impl / src / main / java / org / opendaylight / netvirt / natservice / internal / SNATDefaultRouteProgrammer.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.natservice.internal;
9
10 import java.math.BigInteger;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.genius.mdsalutil.*;
17 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class SNATDefaultRouteProgrammer {
22
23     private static final Logger LOG = LoggerFactory.getLogger(SNATDefaultRouteProgrammer.class);
24     private IMdsalApiManager mdsalManager;
25
26     public SNATDefaultRouteProgrammer(IMdsalApiManager mdsalManager) {
27         this.mdsalManager = mdsalManager;
28     }
29
30     private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long vpnId) {
31
32         InetAddress defaultIP = null;
33
34         try {
35             defaultIP = InetAddress.getByName("0.0.0.0");
36
37         } catch (UnknownHostException e) {
38             LOG.error("UnknowHostException in buildDefNATFlowEntity. Failed  to build FIB Table Flow for Default Route to NAT table ");
39             return null;
40         }
41
42         List<MatchInfo> matches = new ArrayList<>();
43         matches.add(new MatchInfo(MatchFieldType.eth_type,
44                 new long[] { 0x0800L }));
45
46         //add match for default route "0.0.0.0/0"
47 //        matches.add(new MatchInfo(MatchFieldType.ipv4_dst, new long[] {
48 //                NatUtil.getIpAddress(defaultIP.getAddress()), 0 }));
49
50         //add match for vrfid
51         matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
52                 MetaDataUtil.getVpnIdMetadata(vpnId), MetaDataUtil.METADATA_MASK_VRFID }));
53
54         List<InstructionInfo> instructions = new ArrayList<>();
55         instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.PSNAT_TABLE }));
56
57         String flowRef = getFlowRefFib(dpId, NwConstants.L3_FIB_TABLE, vpnId);
58
59         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_FIB_TABLE, flowRef,
60                 NatConstants.DEFAULT_DNAT_FLOW_PRIORITY, flowRef, 0, 0,
61                 NwConstants.COOKIE_DNAT_TABLE, matches, instructions);
62
63         return flowEntity;
64
65
66     }
67
68     private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long bgpVpnId, long routerId) {
69
70         InetAddress defaultIP = null;
71
72         try {
73             defaultIP = InetAddress.getByName("0.0.0.0");
74
75         } catch (UnknownHostException e) {
76             LOG.error("UnknowHostException in buildDefNATFlowEntity. Failed  to build FIB Table Flow for Default Route to NAT table ");
77             return null;
78         }
79
80         List<MatchInfo> matches = new ArrayList<>();
81         matches.add(new MatchInfo(MatchFieldType.eth_type,
82                 new long[] { 0x0800L }));
83
84         //add match for default route "0.0.0.0/0"
85 //        matches.add(new MatchInfo(MatchFieldType.ipv4_dst, new long[] {
86 //                NatUtil.getIpAddress(defaultIP.getAddress()), 0 }));
87
88         //add match for vrfid
89         matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
90                 MetaDataUtil.getVpnIdMetadata(bgpVpnId), MetaDataUtil.METADATA_MASK_VRFID }));
91
92         List<InstructionInfo> instructions = new ArrayList<>();
93         instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.PSNAT_TABLE }));
94
95         String flowRef = getFlowRefFib(dpId, NwConstants.L3_FIB_TABLE, routerId);
96
97         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_FIB_TABLE, flowRef,
98                 NatConstants.DEFAULT_DNAT_FLOW_PRIORITY, flowRef, 0, 0,
99                 NwConstants.COOKIE_DNAT_TABLE, matches, instructions);
100
101         return flowEntity;
102
103
104     }
105
106     private String getFlowRefFib(BigInteger dpnId, short tableId, long routerID) {
107         return new StringBuilder().append(NatConstants.NAPT_FLOWID_PREFIX).append(dpnId).append(NatConstants.FLOWID_SEPARATOR).
108                 append(tableId).append(NatConstants.FLOWID_SEPARATOR).append(routerID).toString();
109     }
110
111     void installDefNATRouteInDPN(BigInteger dpnId, long vpnId) {
112         FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, vpnId);
113         if(flowEntity == null) {
114             LOG.error("Flow entity received is NULL. Cannot proceed with installation of Default NAT flow");
115             return;
116         }
117         NatServiceCounters.install_default_nat_flow.inc();
118         mdsalManager.installFlow(flowEntity);
119     }
120
121     void installDefNATRouteInDPN(BigInteger dpnId, long bgpVpnId, long routerId) {
122         FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, bgpVpnId, routerId);
123         if(flowEntity == null) {
124             LOG.error("Flow entity received is NULL. Cannot proceed with installation of Default NAT flow");
125             return;
126         }
127         NatServiceCounters.install_default_nat_flow.inc();
128         mdsalManager.installFlow(flowEntity);
129     }
130
131     void removeDefNATRouteInDPN(BigInteger dpnId, long vpnId) {
132         FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, vpnId);
133         if(flowEntity == null) {
134             LOG.error("Flow entity received is NULL. Cannot proceed with installation of Default NAT flow");
135             return;
136         }
137         NatServiceCounters.remove_default_nat_flow.inc();
138         mdsalManager.removeFlow(flowEntity);
139     }
140
141     void removeDefNATRouteInDPN(BigInteger dpnId, long bgpVpnId, long routerId) {
142         FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, bgpVpnId, routerId);
143         if(flowEntity == null) {
144             LOG.error("Flow entity received is NULL. Cannot proceed with installation of Default NAT flow");
145             return;
146         }
147         NatServiceCounters.remove_default_nat_flow.inc();
148         mdsalManager.removeFlow(flowEntity);
149     }
150 }