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 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();
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");
85 mdsalManager.installFlow(flowEntity);
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");
94 mdsalManager.removeFlow(flowEntity);