Support for SNAT and DNAT features in L3 forwarding services.
[vpnservice.git] / natservice / natservice-impl / src / main / java / org / opendaylight / vpnservice / 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.vpnservice.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.vpnservice.mdsalutil.FlowEntity;
17 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
18 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
19 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
20 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
21 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
22 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
23 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class SNATDefaultRouteProgrammer {
28
29     private static final Logger LOG = LoggerFactory.getLogger(SNATDefaultRouteProgrammer.class);
30     private IMdsalApiManager mdsalManager;
31
32     public SNATDefaultRouteProgrammer(IMdsalApiManager mdsalManager) {
33         this.mdsalManager = mdsalManager;
34     }
35
36     private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long vpnId) {
37
38         InetAddress defaultIP = null;
39
40         try {
41             defaultIP = InetAddress.getByName("0.0.0.0");
42
43         } catch (UnknownHostException e) {
44             LOG.error("UnknowHostException in buildDefNATFlowEntity. Failed  to build FIB Table Flow for Default Route to NAT table ");
45             return null;
46         }
47
48         List<MatchInfo> matches = new ArrayList<MatchInfo>();
49         matches.add(new MatchInfo(MatchFieldType.eth_type,
50                 new long[] { 0x0800L }));
51
52         //add match for default route "0.0.0.0/0"
53 //        matches.add(new MatchInfo(MatchFieldType.ipv4_dst, new long[] {
54 //                NatUtil.getIpAddress(defaultIP.getAddress()), 0 }));
55
56         //add match for vrfid
57         matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
58                 BigInteger.valueOf(vpnId), MetaDataUtil.METADATA_MASK_VRFID }));
59
60         List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
61         instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NatConstants.PSNAT_TABLE }));
62
63         String flowRef = getFlowRefFib(dpId, NatConstants.L3_FIB_TABLE, vpnId);
64
65         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NatConstants.L3_FIB_TABLE, flowRef,
66                 NatConstants.DEFAULT_DNAT_FLOW_PRIORITY, flowRef, 0, 0,
67                 NatConstants.COOKIE_DNAT_TABLE, matches, instructions);
68
69         return flowEntity;
70
71
72     }
73
74     private String getFlowRefFib(BigInteger dpnId, short tableId, long routerID) {
75         return new StringBuilder().append(NatConstants.NAPT_FLOWID_PREFIX).append(dpnId).append(NatConstants.FLOWID_SEPARATOR).
76                 append(tableId).append(NatConstants.FLOWID_SEPARATOR).append(routerID).toString();
77     }
78
79     void installDefNATRouteInDPN(BigInteger dpnId, long vpnId) {
80         FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, vpnId);
81         if(flowEntity == null) {
82             LOG.error("Flow entity received is NULL. Cannot proceed with installation of Default NAT flow");
83             return;
84         }
85         mdsalManager.installFlow(flowEntity);
86     }
87
88     void removeDefNATRouteInDPN(BigInteger dpnId, long vpnId) {
89         FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, vpnId);
90         if(flowEntity == null) {
91             LOG.error("Flow entity received is NULL. Cannot proceed with installation of Default NAT flow");
92             return;
93         }
94         mdsalManager.removeFlow(flowEntity);
95     }
96
97 }