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.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;
28 public class SNATDefaultRouteProgrammer {
30 private static final Logger LOG = LoggerFactory.getLogger(SNATDefaultRouteProgrammer.class);
31 private IMdsalApiManager mdsalManager;
33 public SNATDefaultRouteProgrammer(IMdsalApiManager mdsalManager) {
34 this.mdsalManager = mdsalManager;
37 private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long vpnId) {
39 InetAddress defaultIP = null;
42 defaultIP = InetAddress.getByName("0.0.0.0");
44 } catch (UnknownHostException e) {
45 LOG.error("UnknowHostException in buildDefNATFlowEntity. Failed to build FIB Table Flow for Default Route to NAT table ");
49 List<MatchInfo> matches = new ArrayList<>();
50 matches.add(new MatchInfo(MatchFieldType.eth_type,
51 new long[] { 0x0800L }));
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 }));
58 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
59 MetaDataUtil.getVpnIdMetadata(vpnId), MetaDataUtil.METADATA_MASK_VRFID }));
61 List<InstructionInfo> instructions = new ArrayList<>();
62 instructions.add(new InstructionGotoTable(NwConstants.PSNAT_TABLE));
64 String flowRef = getFlowRefFib(dpId, NwConstants.L3_FIB_TABLE, vpnId);
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);
75 private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long bgpVpnId, long routerId) {
77 InetAddress defaultIP = null;
80 defaultIP = InetAddress.getByName("0.0.0.0");
82 } catch (UnknownHostException e) {
83 LOG.error("UnknowHostException in buildDefNATFlowEntity. Failed to build FIB Table Flow for Default Route to NAT table ");
87 List<MatchInfo> matches = new ArrayList<>();
88 matches.add(new MatchInfo(MatchFieldType.eth_type,
89 new long[] { 0x0800L }));
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 }));
96 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
97 MetaDataUtil.getVpnIdMetadata(bgpVpnId), MetaDataUtil.METADATA_MASK_VRFID }));
99 List<InstructionInfo> instructions = new ArrayList<>();
100 instructions.add(new InstructionGotoTable(NwConstants.PSNAT_TABLE));
102 String flowRef = getFlowRefFib(dpId, NwConstants.L3_FIB_TABLE, routerId);
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);
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();
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");
124 NatServiceCounters.install_default_nat_flow.inc();
125 mdsalManager.installFlow(flowEntity);
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");
134 NatServiceCounters.install_default_nat_flow.inc();
135 mdsalManager.installFlow(flowEntity);
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");
144 NatServiceCounters.remove_default_nat_flow.inc();
145 mdsalManager.removeFlow(flowEntity);
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");
154 NatServiceCounters.remove_default_nat_flow.inc();
155 mdsalManager.removeFlow(flowEntity);