Bump versions by 0.1.0 for next dev cycle
[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 FlowEntity buildDefNATFlowEntity(BigInteger dpId, long bgpVpnId, long routerId) {
75
76         InetAddress defaultIP = null;
77
78         try {
79             defaultIP = InetAddress.getByName("0.0.0.0");
80
81         } catch (UnknownHostException e) {
82             LOG.error("UnknowHostException in buildDefNATFlowEntity. Failed  to build FIB Table Flow for Default Route to NAT table ");
83             return null;
84         }
85
86         List<MatchInfo> matches = new ArrayList<MatchInfo>();
87         matches.add(new MatchInfo(MatchFieldType.eth_type,
88                 new long[] { 0x0800L }));
89
90         //add match for default route "0.0.0.0/0"
91 //        matches.add(new MatchInfo(MatchFieldType.ipv4_dst, new long[] {
92 //                NatUtil.getIpAddress(defaultIP.getAddress()), 0 }));
93
94         //add match for vrfid
95         matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
96                 BigInteger.valueOf(bgpVpnId), MetaDataUtil.METADATA_MASK_VRFID }));
97
98         List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
99         instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NatConstants.PSNAT_TABLE }));
100
101         String flowRef = getFlowRefFib(dpId, NatConstants.L3_FIB_TABLE, routerId);
102
103         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NatConstants.L3_FIB_TABLE, flowRef,
104                 NatConstants.DEFAULT_DNAT_FLOW_PRIORITY, flowRef, 0, 0,
105                 NatConstants.COOKIE_DNAT_TABLE, matches, instructions);
106
107         return flowEntity;
108
109
110     }
111
112     private String getFlowRefFib(BigInteger dpnId, short tableId, long routerID) {
113         return new StringBuilder().append(NatConstants.NAPT_FLOWID_PREFIX).append(dpnId).append(NatConstants.FLOWID_SEPARATOR).
114                 append(tableId).append(NatConstants.FLOWID_SEPARATOR).append(routerID).toString();
115     }
116
117     void installDefNATRouteInDPN(BigInteger dpnId, long vpnId) {
118         FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, vpnId);
119         if(flowEntity == null) {
120             LOG.error("Flow entity received is NULL. Cannot proceed with installation of Default NAT flow");
121             return;
122         }
123         mdsalManager.installFlow(flowEntity);
124     }
125
126     void installDefNATRouteInDPN(BigInteger dpnId, long bgpVpnId, long routerId) {
127         FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, bgpVpnId, routerId);
128         if(flowEntity == null) {
129             LOG.error("Flow entity received is NULL. Cannot proceed with installation of Default NAT flow");
130             return;
131         }
132         mdsalManager.installFlow(flowEntity);
133     }
134
135     void removeDefNATRouteInDPN(BigInteger dpnId, long vpnId) {
136         FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, vpnId);
137         if(flowEntity == null) {
138             LOG.error("Flow entity received is NULL. Cannot proceed with installation of Default NAT flow");
139             return;
140         }
141         mdsalManager.removeFlow(flowEntity);
142     }
143
144     void removeDefNATRouteInDPN(BigInteger dpnId, long bgpVpnId, long routerId) {
145         FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, bgpVpnId, routerId);
146         if(flowEntity == null) {
147             LOG.error("Flow entity received is NULL. Cannot proceed with installation of Default NAT flow");
148             return;
149         }
150         mdsalManager.removeFlow(flowEntity);
151     }
152
153 }