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.netvirt.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.genius.mdsalutil.*;
17 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public class SNATDefaultRouteProgrammer {
23 private static final Logger LOG = LoggerFactory.getLogger(SNATDefaultRouteProgrammer.class);
24 private IMdsalApiManager mdsalManager;
26 public SNATDefaultRouteProgrammer(IMdsalApiManager mdsalManager) {
27 this.mdsalManager = mdsalManager;
30 private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long vpnId) {
32 InetAddress defaultIP = null;
35 defaultIP = InetAddress.getByName("0.0.0.0");
37 } catch (UnknownHostException e) {
38 LOG.error("UnknowHostException in buildDefNATFlowEntity. Failed to build FIB Table Flow for Default Route to NAT table ");
42 List<MatchInfo> matches = new ArrayList<>();
43 matches.add(new MatchInfo(MatchFieldType.eth_type,
44 new long[] { 0x0800L }));
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 }));
51 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
52 MetaDataUtil.getVpnIdMetadata(vpnId), MetaDataUtil.METADATA_MASK_VRFID }));
54 List<InstructionInfo> instructions = new ArrayList<>();
55 instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.PSNAT_TABLE }));
57 String flowRef = getFlowRefFib(dpId, NwConstants.L3_FIB_TABLE, vpnId);
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);
68 private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long bgpVpnId, long routerId) {
70 InetAddress defaultIP = null;
73 defaultIP = InetAddress.getByName("0.0.0.0");
75 } catch (UnknownHostException e) {
76 LOG.error("UnknowHostException in buildDefNATFlowEntity. Failed to build FIB Table Flow for Default Route to NAT table ");
80 List<MatchInfo> matches = new ArrayList<>();
81 matches.add(new MatchInfo(MatchFieldType.eth_type,
82 new long[] { 0x0800L }));
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 }));
89 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
90 MetaDataUtil.getVpnIdMetadata(bgpVpnId), MetaDataUtil.METADATA_MASK_VRFID }));
92 List<InstructionInfo> instructions = new ArrayList<>();
93 instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.PSNAT_TABLE }));
95 String flowRef = getFlowRefFib(dpId, NwConstants.L3_FIB_TABLE, routerId);
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);
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();
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");
117 NatServiceCounters.install_default_nat_flow.inc();
118 mdsalManager.installFlow(flowEntity);
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");
127 NatServiceCounters.install_default_nat_flow.inc();
128 mdsalManager.installFlow(flowEntity);
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");
137 NatServiceCounters.remove_default_nat_flow.inc();
138 mdsalManager.removeFlow(flowEntity);
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");
147 NatServiceCounters.remove_default_nat_flow.inc();
148 mdsalManager.removeFlow(flowEntity);