2 * Copyright © 2016, 2017 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 com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.List;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
24 import org.opendaylight.genius.mdsalutil.FlowEntity;
25 import org.opendaylight.genius.mdsalutil.InstructionInfo;
26 import org.opendaylight.genius.mdsalutil.MDSALUtil;
27 import org.opendaylight.genius.mdsalutil.MatchInfo;
28 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
29 import org.opendaylight.genius.mdsalutil.NwConstants;
30 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
31 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
32 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
33 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.subnets.Subnets;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 public class SNATDefaultRouteProgrammer {
46 private static final Logger LOG = LoggerFactory.getLogger(SNATDefaultRouteProgrammer.class);
47 private final IMdsalApiManager mdsalManager;
48 private final DataBroker dataBroker;
49 private final IdManagerService idManager;
50 private final ExternalNetworkGroupInstaller extNetGroupInstaller;
53 public SNATDefaultRouteProgrammer(final IMdsalApiManager mdsalManager, final DataBroker dataBroker,
54 final IdManagerService idManager, final ExternalNetworkGroupInstaller extNetGroupInstaller) {
55 this.mdsalManager = mdsalManager;
56 this.dataBroker = dataBroker;
57 this.idManager = idManager;
58 this.extNetGroupInstaller = extNetGroupInstaller;
61 private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long vpnId) {
62 InetAddress defaultIP = null;
64 defaultIP = InetAddress.getByName("0.0.0.0");
65 } catch (UnknownHostException e) {
66 LOG.error("buildDefNATFlowEntity : Failed to build FIB Table Flow for "
67 + "Default Route to NAT table", e);
71 List<MatchInfo> matches = new ArrayList<>();
72 matches.add(MatchEthernetType.IPV4);
74 //add match for default route "0.0.0.0/0"
75 // matches.add(new MatchInfo(MatchFieldType.ipv4_dst, new long[] {
76 // NatUtil.getIpAddress(defaultIP.getAddress()), 0 }));
79 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(vpnId), MetaDataUtil.METADATA_MASK_VRFID));
81 List<InstructionInfo> instructions = new ArrayList<>();
82 instructions.add(new InstructionGotoTable(NwConstants.PSNAT_TABLE));
84 String flowRef = NatUtil.getFlowRef(dpId, NwConstants.L3_FIB_TABLE, defaultIP, vpnId);
86 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_FIB_TABLE, flowRef,
87 NatConstants.DEFAULT_DNAT_FLOW_PRIORITY, flowRef, 0, 0,
88 NwConstants.COOKIE_DNAT_TABLE, matches, instructions);
93 private FlowEntity buildDefNATFlowEntity(BigInteger dpId, long bgpVpnId, long routerId) {
94 InetAddress defaultIP = null;
96 defaultIP = InetAddress.getByName("0.0.0.0");
97 } catch (UnknownHostException e) {
98 LOG.error("buildDefNATFlowEntity : Failed to build FIB Table Flow for "
99 + "Default Route to NAT table", e);
103 List<MatchInfo> matches = new ArrayList<>();
104 matches.add(MatchEthernetType.IPV4);
106 //add match for default route "0.0.0.0/0"
107 // matches.add(new MatchInfo(MatchFieldType.ipv4_dst, new long[] {
108 // NatUtil.getIpAddress(defaultIP.getAddress()), 0 }));
110 //add match for vrfid
111 matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(bgpVpnId), MetaDataUtil.METADATA_MASK_VRFID));
113 List<InstructionInfo> instructions = new ArrayList<>();
114 instructions.add(new InstructionGotoTable(NwConstants.PSNAT_TABLE));
116 String flowRef = NatUtil.getFlowRef(dpId, NwConstants.L3_FIB_TABLE, defaultIP, routerId);
118 FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_FIB_TABLE, flowRef,
119 NatConstants.DEFAULT_DNAT_FLOW_PRIORITY, flowRef, 0, 0,
120 NwConstants.COOKIE_DNAT_TABLE, matches, instructions);
127 void installDefNATRouteInDPN(BigInteger dpnId, long vpnId, WriteTransaction writeFlowInvTx) {
128 FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, vpnId);
129 if (flowEntity == null) {
130 LOG.error("installDefNATRouteInDPN : Flow entity received is NULL."
131 + "Cannot proceed with installation of Default NAT flow");
134 NatServiceCounters.install_default_nat_flow.inc();
135 mdsalManager.addFlowToTx(flowEntity, writeFlowInvTx);
138 void installDefNATRouteInDPN(BigInteger dpnId, long bgpVpnId, long routerId, WriteTransaction writeFlowInvTx) {
139 FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, bgpVpnId, routerId);
140 if (flowEntity == null) {
141 LOG.error("installDefNATRouteInDPN : Flow entity received is NULL."
142 + "Cannot proceed with installation of Default NAT flow");
145 NatServiceCounters.install_default_nat_flow.inc();
146 mdsalManager.addFlowToTx(flowEntity, writeFlowInvTx);
149 void installDefNATRouteInDPN(BigInteger dpnId, long vpnId, String subnetId) {
150 FlowEntity flowEntity = NatUtil.buildDefaultNATFlowEntityForExternalSubnet(dpnId, vpnId, subnetId, idManager);
151 if (flowEntity == null) {
152 LOG.error("installDefNATRouteInDPN : Flow entity received is NULL."
153 + "Cannot proceed with installation of Default NAT flow");
156 NatServiceCounters.install_default_nat_flow.inc();
157 mdsalManager.installFlow(flowEntity);
160 void removeDefNATRouteInDPN(BigInteger dpnId, long vpnId, WriteTransaction writeFlowInvTx) {
161 FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, vpnId);
162 if (flowEntity == null) {
163 LOG.error("removeDefNATRouteInDPN : Flow entity received is NULL."
164 + "Cannot proceed with installation of Default NAT flow");
167 NatServiceCounters.remove_default_nat_flow.inc();
168 mdsalManager.removeFlowToTx(flowEntity, writeFlowInvTx);
171 void removeDefNATRouteInDPN(BigInteger dpnId, long bgpVpnId, long routerId, WriteTransaction writeFlowInvTx) {
172 FlowEntity flowEntity = buildDefNATFlowEntity(dpnId, bgpVpnId, routerId);
173 if (flowEntity == null) {
174 LOG.error("removeDefNATRouteInDPN : Flow entity received is NULL."
175 + "Cannot proceed with installation of Default NAT flow");
178 NatServiceCounters.remove_default_nat_flow.inc();
179 mdsalManager.removeFlowToTx(flowEntity, writeFlowInvTx);
182 void addOrDelDefaultFibRouteToSNATForSubnet(Subnets subnet, String networkId, int flowAction, long vpnId) {
183 String subnetId = subnet.getId().getValue();
184 InstanceIdentifier<VpnInstanceOpDataEntry> networkVpnInstanceIdentifier =
185 NatUtil.getVpnInstanceOpDataIdentifier(networkId);
186 Optional<VpnInstanceOpDataEntry> networkVpnInstanceOp =
187 SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
188 LogicalDatastoreType.OPERATIONAL, networkVpnInstanceIdentifier);
190 if (!networkVpnInstanceOp.isPresent()) {
191 LOG.debug("addOrDelDefaultFibRouteToSNATForSubnet : Cannot create/remove default FIB route to SNAT flow "
192 + "for subnet {} vpn-instance-op-data entry for network {} does not exist",
193 subnetId, networkId);
197 List<VpnToDpnList> dpnListInVpn = networkVpnInstanceOp.get().getVpnToDpnList();
198 if (dpnListInVpn == null) {
199 LOG.debug("addOrDelDefaultFibRouteToSNATForSubnet : Will not add/remove default NAT flow for subnet {} "
200 + "no dpn set for vpn instance {}", subnetId, networkVpnInstanceOp.get());
205 for (VpnToDpnList dpn : dpnListInVpn) {
206 String macAddress = NatUtil.getSubnetGwMac(dataBroker, subnet.getId(), networkId);
207 extNetGroupInstaller.installExtNetGroupEntry(new Uuid(networkId), subnet.getId(),
208 dpn.getDpnId(), macAddress);
209 FlowEntity flowEntity = NatUtil.buildDefaultNATFlowEntityForExternalSubnet(dpn.getDpnId(),
210 vpnId, subnetId, idManager);
211 if (flowAction == NwConstants.ADD_FLOW || flowAction == NwConstants.MOD_FLOW) {
212 LOG.info("addOrDelDefaultFibRouteToSNATForSubnet : Installing flow {} for subnetId {},"
213 + "vpnId {} on dpn {}", flowEntity, subnetId, vpnId, dpn.getDpnId());
214 mdsalManager.installFlow(flowEntity);
216 LOG.info("addOrDelDefaultFibRouteToSNATForSubnet : Removing flow for subnetId {},"
217 + "vpnId {} with dpn {}", subnetId, vpnId, dpn);
218 mdsalManager.removeFlow(flowEntity);