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