Use managed transactions in natservice-impl
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / VxlanGreConntrackBasedSnatService.java
1 /*
2  * Copyright © 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.natservice.internal;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
17 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
18 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
19 import org.opendaylight.genius.mdsalutil.ActionInfo;
20 import org.opendaylight.genius.mdsalutil.BucketInfo;
21 import org.opendaylight.genius.mdsalutil.GroupEntity;
22 import org.opendaylight.genius.mdsalutil.InstructionInfo;
23 import org.opendaylight.genius.mdsalutil.MDSALUtil;
24 import org.opendaylight.genius.mdsalutil.MatchInfo;
25 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
26 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
27 import org.opendaylight.genius.mdsalutil.NwConstants;
28 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
29 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack;
30 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack.NxCtAction;
31 import org.opendaylight.genius.mdsalutil.actions.ActionNxLoadInPort;
32 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
33 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldMeta;
34 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldTunnelId;
35 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
36 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
37 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
38 import org.opendaylight.genius.mdsalutil.matches.MatchIpv4Destination;
39 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
40 import org.opendaylight.genius.mdsalutil.matches.MatchTunnelId;
41 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchCtState;
42 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
43 import org.opendaylight.netvirt.elanmanager.api.IElanService;
44 import org.opendaylight.netvirt.vpnmanager.api.IVpnFootprintService;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rpcs.rev160406.ItmRpcService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.types.rev160517.IpPrefixOrAddress;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatFlags;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.NxActionNatRangePresent;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public class VxlanGreConntrackBasedSnatService extends ConntrackBasedSnatService {
60
61     private static final Logger LOG = LoggerFactory.getLogger(VxlanGreConntrackBasedSnatService.class);
62     private final ExternalRoutersListener externalRouterListener;
63     private final IElanService elanManager;
64     private final ManagedNewTransactionRunner txRunner;
65
66     public VxlanGreConntrackBasedSnatService(DataBroker dataBroker, IMdsalApiManager mdsalManager,
67                                              ItmRpcService itmManager, OdlInterfaceRpcService odlInterfaceRpcService,
68                                              IdManagerService idManager, NAPTSwitchSelector naptSwitchSelector,
69                                              ExternalRoutersListener externalRouterListener, IElanService elanManager,
70                                              IInterfaceManager interfaceManager,
71                                              IVpnFootprintService vpnFootprintService) {
72         super(dataBroker, mdsalManager, itmManager, idManager, naptSwitchSelector, odlInterfaceRpcService,
73                 interfaceManager, vpnFootprintService);
74         this.externalRouterListener = externalRouterListener;
75         this.elanManager = elanManager;
76         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
77     }
78
79     @Override
80     public boolean handleSnatAllSwitch(Routers routers, BigInteger primarySwitchId,  int addOrRemove) {
81         ProviderTypes extNwProviderType = NatUtil.getProviderTypefromNetworkId(dataBroker, routers.getNetworkId());
82         LOG.debug("VxlanGreConntrackBasedSnatService: handleSnatAllSwitch ProviderTypes {}", extNwProviderType);
83         if (extNwProviderType == ProviderTypes.FLAT || extNwProviderType == ProviderTypes.VLAN) {
84             return false;
85         }
86         return super.handleSnatAllSwitch(routers, primarySwitchId, addOrRemove);
87     }
88
89     @Override
90     public boolean handleSnat(Routers routers, BigInteger primarySwitchId, BigInteger dpnId,  int addOrRemove) {
91         ProviderTypes extNwProviderType = NatUtil.getProviderTypefromNetworkId(dataBroker, routers.getNetworkId());
92         LOG.debug("VxlanGreConntrackBasedSnatService: handleSnat ProviderTypes {}", extNwProviderType);
93         if (extNwProviderType == ProviderTypes.FLAT || extNwProviderType == ProviderTypes.VLAN) {
94             return false;
95         }
96         return super.handleSnat(routers, primarySwitchId, dpnId, addOrRemove);
97     }
98
99     @Override
100     protected void installSnatSpecificEntriesForNaptSwitch(Routers routers, BigInteger dpnId, int addOrRemove) {
101         LOG.info("installSnatSpecificEntriesForNaptSwitch for router {}",
102                 routers.getRouterName());
103         String routerName = routers.getRouterName();
104         Long routerId = NatUtil.getVpnId(dataBroker, routerName);
105         int elanId = NatUtil.getElanInstanceByName(routers.getNetworkId().getValue(), dataBroker)
106                 .getElanTag().intValue();
107         /* Install Outbound NAT entries */
108
109         installSnatMissEntryForPrimrySwch(dpnId, routerId, elanId, addOrRemove);
110         installTerminatingServiceTblEntryForVxlanGre(dpnId, routerName, routerId, elanId, addOrRemove);
111         //Long extNetVpnId = NatUtil.getNetworkVpnIdFromRouterId(dataBroker, routerId);
112         Uuid vpnUuid = NatUtil.getVpnIdfromNetworkId(dataBroker, routers.getNetworkId());
113         if (vpnUuid == null) {
114             LOG.error("installSnatSpecificEntriesForNaptSwitch: Unable to retrieve external vpn_id for "
115                     + "external network {} with routerId {}", routers.getNetworkId(), routerId);
116             return;
117         }
118         Long extNetVpnId = NatUtil.getVpnId(dataBroker, vpnUuid.getValue());
119         /*//Long extNetVpnId = NatUtil.getAssociatedVPN(dataBroker, routers.getNetworkId(), LOG);
120         if (extNetVpnId == NatConstants.INVALID_ID && addOrRemove == NwConstants.ADD_FLOW) {
121             LOG.error("installSnatSpecificEntriesForNaptSwitch: Unable to retrieve external vpn_id for "
122                     + "external network {} with routerId {}", routers.getNetworkId(), routerId);
123             return;
124         }*/
125         LOG.info("installSnatSpecificEntriesForNaptSwitch: external network vpn_id {} for router {}",
126                 extNetVpnId, routers.getRouterName());
127         List<ExternalIps> externalIps = routers.getExternalIps();
128         createOutboundTblTrackEntryForVxlanGre(dpnId, routerId, extNetVpnId, addOrRemove);
129         createOutboundTblEntryForVxlanGre(dpnId, routerId, extNetVpnId, externalIps, elanId, addOrRemove);
130         installNaptPfibFlowForVxlanGre(routers, dpnId, extNetVpnId, addOrRemove);
131         installNaptPfibEntry(dpnId, routerId, addOrRemove);
132
133         //Install Inbound NAT entries
134         installInboundEntryForVxlanGre(dpnId, routerId, extNetVpnId, externalIps, elanId, addOrRemove);
135         if (externalIps.isEmpty()) {
136             LOG.error("installSnatSpecificEntriesForNaptSwitch: No externalIP present for router {}",
137                     routerName);
138             return;
139         }
140         //The logic now handle only one external IP per router, others if present will be ignored.
141         String externalIp = NatUtil.validateAndAddNetworkMask(externalIps.get(0).getIpAddress());
142         ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
143             if (addOrRemove == NwConstants.ADD_FLOW) {
144                 externalRouterListener.handleSnatReverseTraffic(dpnId, routers, routerId, routerName, externalIp, tx);
145             } else {
146                 externalRouterListener.clearFibTsAndReverseTraffic(dpnId, routerId, routers.getNetworkId(),
147                         Collections.singletonList(externalIp), null, routers.getExtGwMacAddress(), tx);
148             }
149         }), LOG, "Error installing SNAT-specific entries for NAPT switch");
150     }
151
152     protected void createOutboundTblTrackEntryForVxlanGre(BigInteger dpnId, Long routerId, Long extNetVpnId,
153                                                int addOrRemove) {
154         LOG.info("createOutboundTblTrackEntryForVxlanGre: Install Outbound tracking table flow on dpId {} for "
155                 + "routerId {}", dpnId, routerId);
156         List<MatchInfoBase> matches = new ArrayList<>();
157         matches.add(MatchEthernetType.IPV4);
158         matches.add(new NxMatchCtState(SNAT_CT_STATE, SNAT_CT_STATE_MASK));
159         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
160
161         ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
162         if (addOrRemove == NwConstants.ADD_FLOW) {
163             ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
164                     .getVpnIdMetadata(extNetVpnId));
165             listActionInfo.add(actionSetFieldMeta);
166         }
167         ArrayList<InstructionInfo> instructionInfo = new ArrayList<>();
168         listActionInfo.add(new ActionNxResubmit(NwConstants.NAPT_PFIB_TABLE));
169         instructionInfo.add(new InstructionApplyActions(listActionInfo));
170
171         String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
172         flowRef += "trkest";
173         syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY, flowRef,
174                 NwConstants.COOKIE_SNAT_TABLE, matches, instructionInfo, addOrRemove);
175
176     }
177
178     protected void createOutboundTblEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNetVpnId,
179                                                      List<ExternalIps> externalIps, int elanId, int addOrRemove) {
180         LOG.info("createOutboundTblEntryForVxlanGre: Install Outbound table flow on dpId {} for routerId {}", dpnId,
181                 routerId);
182         List<MatchInfoBase> matches = new ArrayList<>();
183         matches.add(MatchEthernetType.IPV4);
184         matches.add(new NxMatchCtState(TRACKED_NEW_CT_STATE, TRACKED_NEW_CT_MASK));
185         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
186         if (externalIps.isEmpty()) {
187             LOG.error("createOutboundTblEntryForVxlanGre: No externalIP present for routerId {}",
188                     routerId);
189             return;
190         }
191         //The logic now handle only one external IP per router, others if present will be ignored.
192         String externalIp = externalIps.get(0).getIpAddress();
193         List<ActionInfo> actionsInfos = new ArrayList<>();
194         if (addOrRemove == NwConstants.ADD_FLOW) {
195             ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
196                     .getVpnIdMetadata(extNetVpnId));
197             actionsInfos.add(actionSetFieldMeta);
198         }
199         List<ActionNxConntrack.NxCtAction> ctActionsListCommit = new ArrayList<>();
200         int rangePresent = NxActionNatRangePresent.NXNATRANGEIPV4MIN.getIntValue();
201         int flags = NxActionNatFlags.NXNATFSRC.getIntValue();
202         ActionNxConntrack.NxCtAction nxCtActionCommit = new ActionNxConntrack.NxNat(0, flags, rangePresent,
203                 new IpPrefixOrAddress(externalIp.toCharArray()).getIpAddress(),
204                 null,0, 0);
205         ctActionsListCommit.add(nxCtActionCommit);
206         int ctCommitFlag = 1;
207         ActionNxConntrack actionNxConntrackSubmit = new ActionNxConntrack(ctCommitFlag, 0, elanId,
208                 NwConstants.NAPT_PFIB_TABLE, ctActionsListCommit);
209         actionsInfos.add(actionNxConntrackSubmit);
210         List<InstructionInfo> instructions = new ArrayList<>();
211         instructions.add(new InstructionApplyActions(actionsInfos));
212         String flowRef = getFlowRef(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId);
213         syncFlow(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, flowRef,  NatConstants.SNAT_NEW_FLOW_PRIORITY,
214                 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
215     }
216
217     protected void installNaptPfibFlowForVxlanGre(Routers routers, BigInteger dpnId, Long extNetVpnId,
218                                                   int addOrRemove) {
219         LOG.info("installNaptPfibFlowForVxlanGre: Install Napt preFibFlow on dpId {} with matching extNetVpnId {} "
220                 + "for router {}", dpnId, extNetVpnId, routers.getRouterName());
221         List<MatchInfoBase> matches = new ArrayList<>();
222         matches.add(MatchEthernetType.IPV4);
223         if (addOrRemove == NwConstants.ADD_FLOW) {
224             matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extNetVpnId),
225                     MetaDataUtil.METADATA_MASK_VRFID));
226         }
227         ArrayList<ActionInfo> listActionInfo = new ArrayList<>();
228         ArrayList<InstructionInfo> instructions = new ArrayList<>();
229         listActionInfo.add(new ActionNxLoadInPort(BigInteger.ZERO));
230         listActionInfo.add(new ActionNxResubmit(NwConstants.L3_FIB_TABLE));
231         instructions.add(new InstructionApplyActions(listActionInfo));
232         String flowRef = getFlowRef(dpnId, NwConstants.NAPT_PFIB_TABLE, extNetVpnId);
233         syncFlow(dpnId, NwConstants.NAPT_PFIB_TABLE, flowRef, NatConstants.SNAT_TRK_FLOW_PRIORITY,
234                 flowRef, NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
235     }
236
237     protected void installInboundEntryForVxlanGre(BigInteger dpnId, long routerId, Long extNeVpnId,
238                                                   List<ExternalIps> externalIps, int elanId, int addOrRemove) {
239         LOG.info("installInboundEntryForVxlanGre:  Install Inbound table entry on dpId {} for routerId {}",
240                 dpnId, routerId);
241         List<MatchInfoBase> matches = new ArrayList<>();
242         matches.add(MatchEthernetType.IPV4);
243         if (externalIps.isEmpty()) {
244             LOG.error("installInboundEntryForVxlanGre : createInboundTblEntry no externalIP present for routerId {}",
245                     routerId);
246             return;
247         }
248         String externalIp = externalIps.get(0).getIpAddress();
249         matches.add(new MatchIpv4Destination(externalIp,"32"));
250         if (addOrRemove == NwConstants.ADD_FLOW) {
251             matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(extNeVpnId),
252                     MetaDataUtil.METADATA_MASK_VRFID));
253         }
254         List<ActionInfo> actionsInfos = new ArrayList<>();
255         List<ActionNxConntrack.NxCtAction> ctActionsList = new ArrayList<>();
256         ActionNxConntrack.NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
257         ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
258                 .getVpnIdMetadata(routerId));
259         actionsInfos.add(actionSetFieldMeta);
260         ctActionsList.add(nxCtAction);
261         ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
262                 .NAPT_PFIB_TABLE,ctActionsList);
263
264         actionsInfos.add(actionNxConntrack);
265         List<InstructionInfo> instructions = new ArrayList<>();
266         instructions.add(new InstructionApplyActions(actionsInfos));
267         String flowRef = getFlowRef(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId);
268         syncFlow(dpnId, NwConstants.INBOUND_NAPT_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
269                 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
270     }
271
272     protected void installTerminatingServiceTblEntryForVxlanGre(BigInteger dpnId, String routerName,
273             Long  routerId, int elanId, int addOrRemove) {
274         LOG.info("installTerminatingServiceTblEntryForVxlanGre : creating entry for"
275                 + "Terminating Service Table for switch {}, routerId {}", dpnId, routerId);
276         List<MatchInfo> matches = new ArrayList<>();
277         matches.add(MatchEthernetType.IPV4);
278
279         BigInteger tunnelId = BigInteger.valueOf(routerId);
280         if (elanManager.isOpenStackVniSemanticsEnforced()) {
281             tunnelId = NatOverVxlanUtil.getRouterVni(idManager, routerName, routerId);
282         }
283         matches.add(new MatchTunnelId(tunnelId));
284
285         List<ActionInfo> actionsInfos = new ArrayList<>();
286         List<NxCtAction> ctActionsList = new ArrayList<>();
287         NxCtAction nxCtAction = new ActionNxConntrack.NxNat(0, 0, 0,null, null,0, 0);
288         ctActionsList.add(nxCtAction);
289         ActionNxConntrack actionNxConntrack = new ActionNxConntrack(0, 0, elanId, NwConstants
290                 .OUTBOUND_NAPT_TABLE,ctActionsList);
291         ActionSetFieldMeta actionSetFieldMeta = new ActionSetFieldMeta(MetaDataUtil
292                 .getVpnIdMetadata(routerId.longValue()));
293         actionsInfos.add(actionSetFieldMeta);
294         actionsInfos.add(actionNxConntrack);
295         List<InstructionInfo> instructions = new ArrayList<>();
296         instructions.add(new InstructionApplyActions(actionsInfos));
297         String flowRef = getFlowRef(dpnId, NwConstants.INTERNAL_TUNNEL_TABLE, routerId.longValue());
298         syncFlow(dpnId,  NwConstants.INTERNAL_TUNNEL_TABLE, flowRef, NatConstants.DEFAULT_TS_FLOW_PRIORITY, flowRef,
299                  NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
300
301     }
302
303     protected void installSnatMissEntry(BigInteger dpnId, Long routerId, String routerName, BigInteger primarySwitchId,
304             int addOrRemove) {
305         LOG.debug("installSnatMissEntry : Installing SNAT miss entry in switch {}", dpnId);
306         List<ActionInfo> listActionInfoPrimary = new ArrayList<>();
307         String ifNamePrimary = getTunnelInterfaceName(dpnId, primarySwitchId);
308         List<BucketInfo> listBucketInfo = new ArrayList<>();
309         if (ifNamePrimary != null) {
310             LOG.debug("installSnatMissEntry : On Non- Napt switch , Primary Tunnel interface is {}", ifNamePrimary);
311             listActionInfoPrimary = NatUtil.getEgressActionsForInterface(odlInterfaceRpcService, itmManager,
312                     interfaceManager, ifNamePrimary, routerId);
313         }
314         BucketInfo bucketPrimary = new BucketInfo(listActionInfoPrimary);
315         listBucketInfo.add(0, bucketPrimary);
316         LOG.debug("installSnatMissEntry : installSnatMissEntry called for dpnId {} with primaryBucket {} ", dpnId,
317                 listBucketInfo.get(0));
318         // Install the select group
319         long groupId = createGroupId(getGroupIdKey(routerName));
320         GroupEntity groupEntity = MDSALUtil.buildGroupEntity(dpnId, groupId, routerName, GroupTypes.GroupAll,
321                 listBucketInfo);
322         LOG.debug("installSnatMissEntry : installing the SNAT to NAPT GroupEntity:{}", groupEntity);
323         mdsalManager.installGroup(groupEntity);
324         // Install miss entry pointing to group
325         LOG.debug("installSnatMissEntry : buildSnatFlowEntity is called for dpId {}, routerName {} and groupId {}",
326                 dpnId, routerName, groupId);
327         List<MatchInfo> matches = new ArrayList<>();
328         matches.add(new MatchEthernetType(0x0800L));
329         matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerId), MetaDataUtil.METADATA_MASK_VRFID));
330
331         List<ActionInfo> actionsInfo = new ArrayList<>();
332
333         BigInteger tunnelId = BigInteger.valueOf(routerId);
334         if (elanManager.isOpenStackVniSemanticsEnforced()) {
335             tunnelId = NatOverVxlanUtil.getRouterVni(idManager, routerName, routerId);
336         }
337
338         actionsInfo.add(new ActionSetFieldTunnelId(tunnelId));
339         LOG.debug("AbstractSnatService : Setting the tunnel to the list of action infos {}", actionsInfo);
340         actionsInfo.add(new ActionGroup(groupId));
341         List<InstructionInfo> instructions = new ArrayList<>();
342         instructions.add(new InstructionApplyActions(actionsInfo));
343         String flowRef = getFlowRef(dpnId, NwConstants.PSNAT_TABLE, routerId);
344         syncFlow(dpnId, NwConstants.PSNAT_TABLE, flowRef,  NatConstants.DEFAULT_PSNAT_FLOW_PRIORITY, flowRef,
345                 NwConstants.COOKIE_SNAT_TABLE, matches, instructions, addOrRemove);
346     }
347
348 }