2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.natservice.internal;
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;
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;
27 public class SNATDefaultRouteProgrammer {
29 private static final Logger LOG = LoggerFactory.getLogger(SNATDefaultRouteProgrammer.class);
30 private IMdsalApiManager mdsalManager;
32 public SNATDefaultRouteProgrammer(IMdsalApiManager mdsalManager) {
33 this.mdsalManager = mdsalManager;
36 private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long vpnId) {
38 InetAddress defaultIP = null;
41 defaultIP = InetAddress.getByName("0.0.0.0");
43 } catch (UnknownHostException e) {
44 LOG.error("UnknowHostException in buildDefNATFlowEntity. Failed to build FIB Table Flow for Default Route to NAT table ");
48 List<MatchInfo> matches = new ArrayList<MatchInfo>();
49 matches.add(new MatchInfo(MatchFieldType.eth_type,
50 new long[] { 0x0800L }));
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 }));
57 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
58 BigInteger.valueOf(vpnId), MetaDataUtil.METADATA_MASK_VRFID }));
60 List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
61 instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NatConstants.PSNAT_TABLE }));
63 String flowRef = getFlowRefFib(dpId, NatConstants.L3_FIB_TABLE, vpnId);
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);
74 private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long bgpVpnId, long routerId) {
76 InetAddress defaultIP = null;
79 defaultIP = InetAddress.getByName("0.0.0.0");
81 } catch (UnknownHostException e) {
82 LOG.error("UnknowHostException in buildDefNATFlowEntity. Failed to build FIB Table Flow for Default Route to NAT table ");
86 List<MatchInfo> matches = new ArrayList<MatchInfo>();
87 matches.add(new MatchInfo(MatchFieldType.eth_type,
88 new long[] { 0x0800L }));
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 }));
95 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
96 BigInteger.valueOf(bgpVpnId), MetaDataUtil.METADATA_MASK_VRFID }));
98 List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
99 instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NatConstants.PSNAT_TABLE }));
101 String flowRef = getFlowRefFib(dpId, NatConstants.L3_FIB_TABLE, routerId);
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);
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();
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");
123 mdsalManager.installFlow(flowEntity);
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");
132 mdsalManager.installFlow(flowEntity);
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");
141 mdsalManager.removeFlow(flowEntity);
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");
150 mdsalManager.removeFlow(flowEntity);