c881db5bd740b6f5748c3d14ada033f0f8965d23
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / arp / ArpTasker.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.arp;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import java.math.BigInteger;
14 import java.util.List;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.atomic.AtomicLong;
18
19 import javax.annotation.Nullable;
20
21 import org.apache.commons.lang3.tuple.ImmutablePair;
22 import org.apache.commons.lang3.tuple.Pair;
23 import org.apache.commons.net.util.SubnetUtils;
24 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
28 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
31 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
32 import org.opendaylight.groupbasedpolicy.util.IidFactory;
33 import org.opendaylight.openflowplugin.api.OFConstants;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2ContextId;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Builder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Key;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayL3Context;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfig;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.ExternalInterfaces;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2BridgeDomain;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomain;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Subnet;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
94 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
95 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
96 import org.opendaylight.yangtools.yang.common.RpcResult;
97 import org.slf4j.Logger;
98 import org.slf4j.LoggerFactory;
99
100 import com.google.common.base.Optional;
101 import com.google.common.collect.HashMultimap;
102 import com.google.common.collect.ImmutableList;
103 import com.google.common.collect.ImmutableSetMultimap;
104 import com.google.common.collect.SetMultimap;
105 import com.google.common.util.concurrent.FutureCallback;
106 import com.google.common.util.concurrent.Futures;
107 import com.google.common.util.concurrent.JdkFutureAdapters;
108 import com.google.common.util.concurrent.ListenableFuture;
109
110 public class ArpTasker implements PacketProcessingListener {
111
112     private static final Logger LOG = LoggerFactory.getLogger(ArpTasker.class);
113     private static final short TABEL_FOR_ARP_FLOW = 0;
114     private static final String ARP_REPLY_TO_CONTROLLER_FLOW_NAME = "arpReplyToController";
115     private static final int ARP_REPLY_TO_CONTROLLER_FLOW_PRIORITY = 10000;
116     private static final Instruction SEND_TO_CONTROLLER_INSTRUCTION;
117     private static final MacAddress SENDER_MAC = new MacAddress("B4:71:13:06:52:D9");
118     private final ArpSender arpSender;
119     private final SalFlowService flowService;
120     private final DataBroker dataProvider;
121     private final AtomicLong flowCookie = new AtomicLong();
122     private final ConcurrentHashMap<String, Pair<RemoveFlowInput, EndpointL3Key>> arpRemoveFlowInputAndL3EpKeyById = new ConcurrentHashMap<>();
123
124     static {
125         ApplyActions applyActions = new ApplyActionsBuilder().setAction(
126                 ImmutableList.of(ArpFlowFactory.createSendToControllerAction(0))).build();
127         SEND_TO_CONTROLLER_INSTRUCTION = new InstructionBuilder().setOrder(0)
128             .setInstruction(new ApplyActionsCaseBuilder().setApplyActions(applyActions).build())
129             .build();
130     }
131
132     public ArpTasker(RpcProviderRegistry rpcRegistry, DataBroker dataProvider) {
133         this.dataProvider = checkNotNull(dataProvider);
134         checkNotNull(rpcRegistry);
135         PacketProcessingService packetProcessingService = rpcRegistry.getRpcService(PacketProcessingService.class);
136         if (packetProcessingService != null) {
137             LOG.info("{} was found.", PacketProcessingService.class.getSimpleName());
138             this.arpSender = new ArpSender(packetProcessingService);
139         } else {
140             LOG.info("Missing service {}", PacketProcessingService.class.getSimpleName());
141             this.arpSender = null;
142         }
143         flowService = rpcRegistry.getRpcService(SalFlowService.class);
144     }
145
146     @Override
147     public void onPacketReceived(PacketReceived potentialArp) {
148         Arp arp = null;
149         try {
150             arp = ArpResolverUtils.getArpFrom(potentialArp);
151         } catch (Exception e) {
152             LOG.trace(
153                     "Failed to decode potential ARP packet. This could occur when other than ARP packet was received.",
154                     e);
155             return;
156         }
157         if (arp.getOperation() != ArpOperation.REPLY.intValue()) {
158             LOG.trace("ARP packet is not REPLY.");
159             return;
160         }
161         if (LOG.isTraceEnabled()) {
162             LOG.trace("ARP REPLY received - {}", ArpUtils.getArpToStringFormat(arp));
163         }
164         NodeKey nodeKey = potentialArp.getIngress().getValue().firstKeyOf(Node.class, NodeKey.class);
165         if (nodeKey == null) {
166             LOG.info("Unknown source node of ARP packet: {}", potentialArp);
167             return;
168         }
169         Ipv4Address spa = ArpUtils.bytesToIp(arp.getSenderProtocolAddress());
170         MacAddress sha = ArpUtils.bytesToMac(arp.getSenderHardwareAddress());
171         Pair<RemoveFlowInput, EndpointL3Key> removeFlowInputAndL3EpKey = arpRemoveFlowInputAndL3EpKeyById.get(createKey(
172                 nodeKey.getId(), spa));
173         flowService.removeFlow(removeFlowInputAndL3EpKey.getLeft());
174         final EndpointL3Key l3EpKey = removeFlowInputAndL3EpKey.getRight();
175         ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
176         InstanceIdentifier<EndpointL3> l3EpIid = IidFactory.l3EndpointIid(l3EpKey.getL3Context(),
177                 l3EpKey.getIpAddress());
178         Optional<EndpointL3> potentialL3Ep = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, l3EpIid, rwTx);
179         if (!potentialL3Ep.isPresent()) {
180             LOG.info("L3 endpoint {} where MAC should be added does not exist anymore.", l3EpKey);
181             rwTx.cancel();
182             return;
183         }
184         final EndpointL3Builder updatedL3EpBuilder = new EndpointL3Builder(potentialL3Ep.get()).setMacAddress(sha)
185             .setTimestamp(System.currentTimeMillis());
186         EndpointL3 updatedL3Ep = updatedL3EpBuilder.build();
187         L2BridgeDomainId l2BdId = resolveL2BridgeDomainId(updatedL3Ep, rwTx);
188         if (l2BdId != null) {
189             updatedL3Ep = updatedL3EpBuilder.setL2Context(l2BdId).build();
190             EndpointBuilder newEpBuilder = new EndpointBuilder(updatedL3Ep).setKey(new EndpointKey(l2BdId, sha));
191             OfOverlayL3Context augmentation = updatedL3Ep.getAugmentation(OfOverlayL3Context.class);
192             if (augmentation != null) {
193                 newEpBuilder.addAugmentation(OfOverlayContext.class, new OfOverlayContextBuilder(augmentation).build());
194             }
195             Endpoint newEp = newEpBuilder.build();
196             rwTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.endpointIid(l2BdId, sha), newEp);
197             LOG.trace("Endpoint was created {}", newEp);
198         }
199         rwTx.put(LogicalDatastoreType.OPERATIONAL, l3EpIid, updatedL3Ep);
200         LOG.trace("MAC was added to L3 endpoint {}", updatedL3Ep);
201         rwTx.submit();
202     }
203
204     private @Nullable L2BridgeDomainId resolveL2BridgeDomainId(EndpointL3 l3Ep, ReadTransaction rTx) {
205         TenantId tenantId = l3Ep.getTenant();
206         Subnet subnetOfL3Ep = readSubnet(l3Ep, rTx);
207         if (subnetOfL3Ep == null) {
208             return null;
209         }
210         ContextId parentOfSubnet = subnetOfL3Ep.getParent();
211         if (parentOfSubnet == null) {
212             return null;
213         }
214         L2ContextId l2ContextId = new L2ContextId(parentOfSubnet);
215         Optional<L2BridgeDomain> potentialL2Bd = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
216                 IidFactory.l2BridgeDomainIid(tenantId, new L2BridgeDomainId(l2ContextId)), rTx);
217         if (potentialL2Bd.isPresent()) {
218             return potentialL2Bd.get().getId();
219         }
220         Optional<L2FloodDomain> potentialL2Fd = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
221                 IidFactory.l2FloodDomainIid(tenantId, new L2FloodDomainId(l2ContextId)), rTx);
222         if (!potentialL2Fd.isPresent()) {
223             return null;
224         }
225         return potentialL2Fd.get().getParent();
226     }
227
228     /**
229      * Uses ARP to get MAC for the given L3 endpoint. Tries to find MAC for IP from {@link EndpointL3#getKey()}.<br>
230      * {@link EndpointL3#getNetworkContainment()} has to point to a {@link Subnet}.<br>
231      * ARP Request is sent from all node connectors obtaining from {@link OfOverlayNodeConfig#getExternalInterfaces()}<br>
232      * MAC address obtained from ARP reply is added to the given L3 endpoint (if still exits).<br>
233      * Also an {@link Endpoint} is created based on MAC If the subnet from network containment point to {@link L2BridgeDomain} directly or throught {@link L2FloodDomain}.
234      * @param l3Ep the L3 endpoint which needs to have an MAC address
235      */
236     public void addMacForL3EpAndCreateEp(final EndpointL3 l3Ep) {
237         final Ipv4Address tpa = getIPv4Addresses(l3Ep);
238         if (tpa == null) {
239             LOG.debug("L3 endpoint {} does not contain IPv4 address.", l3Ep.getKey());
240             return;
241         }
242         ReadOnlyTransaction rTx = dataProvider.newReadOnlyTransaction();
243         final SetMultimap<Node, NodeConnectorId> extIfacesByNode = readNodesWithExternalIfaces(rTx);
244         if (extIfacesByNode.isEmpty()) {
245             LOG.debug("No node with external interface was found.");
246             rTx.close();
247             return;
248         }
249         Ipv4Address senderIpAddress = createSenderIpAddress(l3Ep, rTx);
250         if (senderIpAddress == null) {
251             LOG.warn("Cannot create sender IPv4 address for L3 endpoint {}", l3Ep);
252             rTx.close();
253             return;
254         }
255         rTx.close();
256
257         final ArpMessageAddress senderAddress = new ArpMessageAddress(SENDER_MAC, senderIpAddress);
258         final Flow arpReplyToControllerFlow = createArpReplyToControllerFlow(senderAddress, tpa);
259         for (final Node node : extIfacesByNode.keySet()) {
260             // check if flow with packet-in exists
261             final InstanceIdentifier<Node> nodeIid = InstanceIdentifier.builder(Nodes.class)
262                 .child(Node.class, node.getKey())
263                 .build();
264             final InstanceIdentifier<Flow> flowIid = createFlowIid(arpReplyToControllerFlow, nodeIid);
265             final NodeRef nodeRef = new NodeRef(nodeIid);
266             Future<RpcResult<AddFlowOutput>> futureAddFlowResult = flowService.addFlow(new AddFlowInputBuilder(
267                     arpReplyToControllerFlow).setFlowRef(new FlowRef(flowIid)).setNode(nodeRef).build());
268             Futures.addCallback(JdkFutureAdapters.listenInPoolThread(futureAddFlowResult),
269                     new FutureCallback<RpcResult<AddFlowOutput>>() {
270
271                         @Override
272                         public void onSuccess(RpcResult<AddFlowOutput> result) {
273                             if (!result.isSuccessful()) {
274                                 LOG.warn("ARP Reply to Controller flow was not created: {} \nErrors: {}", flowIid,
275                                         result.getErrors());
276                                 return;
277                             }
278                             LOG.debug("ARP Reply to Controller flow was created: {}", flowIid);
279                             arpRemoveFlowInputAndL3EpKeyById.put(
280                                     createKey(node.getId(), tpa),
281                                     new ImmutablePair<>(new RemoveFlowInputBuilder(arpReplyToControllerFlow).setNode(
282                                             nodeRef).build(), l3Ep.getKey()));
283                             for (NodeConnectorId egressNc : extIfacesByNode.get(node)) {
284                                 KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> egressNcIid = nodeIid.child(
285                                         NodeConnector.class, new NodeConnectorKey(egressNc));
286                                 ListenableFuture<RpcResult<Void>> futureSendArpResult = arpSender.sendArp(
287                                         senderAddress, tpa, egressNcIid);
288                                 Futures.addCallback(futureSendArpResult, logResult(tpa, egressNcIid));
289                             }
290                         }
291
292                         @Override
293                         public void onFailure(Throwable t) {
294                             LOG.warn("ARP Reply to Controller flow was not created: {}", flowIid, t);
295                         }
296                     });
297         }
298     }
299
300     private static @Nullable Ipv4Address getIPv4Addresses(EndpointL3 l3ep) {
301         IpAddress ipAddress = l3ep.getKey().getIpAddress();
302         if (ipAddress.getIpv4Address() == null) {
303             return null;
304         }
305         return ipAddress.getIpv4Address();
306     }
307
308     private SetMultimap<Node, NodeConnectorId> readNodesWithExternalIfaces(ReadTransaction rTx) {
309         Optional<Nodes> potentialNodes = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
310                 InstanceIdentifier.builder(Nodes.class).build(), rTx);
311         if (!potentialNodes.isPresent() && potentialNodes.get().getNode() != null) {
312             return ImmutableSetMultimap.of();
313         }
314         List<Node> nodes = potentialNodes.get().getNode();
315         SetMultimap<Node, NodeConnectorId> extIfacesByNode = HashMultimap.create();
316         for (Node node : nodes) {
317             Optional<Node> potentialNodeFromOper = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
318                     InstanceIdentifier.builder(Nodes.class).child(Node.class, node.getKey()).build(), rTx);
319             if (!potentialNodeFromOper.isPresent()) {
320                 LOG.debug("Node exists in CONF DS but not in OPER DS. Node from CONF: {}", node);
321                 continue;
322             }
323             OfOverlayNodeConfig ofOverlayNode = node.getAugmentation(OfOverlayNodeConfig.class);
324             if (ofOverlayNode != null) {
325                 List<ExternalInterfaces> externalIfaces = ofOverlayNode.getExternalInterfaces();
326                 if (externalIfaces != null) {
327                     for (ExternalInterfaces extIface : externalIfaces) {
328                         extIfacesByNode.put(node, extIface.getNodeConnectorId());
329                     }
330                 }
331             }
332         }
333         return extIfacesByNode;
334     }
335
336     private @Nullable Ipv4Address createSenderIpAddress(EndpointL3 l3Ep, ReadTransaction rTx) {
337         Subnet subnetOfL3Ep = readSubnet(l3Ep, rTx);
338         if (subnetOfL3Ep == null) {
339             return null;
340         }
341         SubnetInfo subnetInfo = new SubnetUtils(subnetOfL3Ep.getIpPrefix().getIpv4Prefix().getValue()).getInfo();
342         String senderIp = subnetInfo.getLowAddress();
343         if (senderIp.equals(l3Ep.getKey().getIpAddress().getIpv4Address().getValue())) {
344             senderIp = subnetInfo.getHighAddress();
345         }
346         return new Ipv4Address(senderIp);
347     }
348
349     private @Nullable Subnet readSubnet(EndpointL3 l3Ep, ReadTransaction rTx) {
350         NetworkDomainId l3EpNetworkContainment = l3Ep.getNetworkContainment();
351         if (l3EpNetworkContainment == null) {
352             LOG.debug("L3 endpoint {} does not contain network containment.", l3Ep.getKey());
353             return null;
354         }
355         if (l3Ep.getTenant() == null) {
356             LOG.debug("L3 endpoint {} does not contain tenat.", l3Ep.getKey());
357             return null;
358         }
359         Optional<Subnet> potentialSubnet = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
360                 IidFactory.subnetIid(l3Ep.getTenant(), new SubnetId(l3EpNetworkContainment)), rTx);
361         if (!potentialSubnet.isPresent()) {
362             LOG.debug(
363                     "Network containment {} of L3 endpoint {} does not point to a subnet or the subnet does not exist.",
364                     l3EpNetworkContainment.getValue(), l3Ep.getKey());
365             return null;
366         }
367         return potentialSubnet.get();
368     }
369
370     private Flow createArpReplyToControllerFlow(ArpMessageAddress senderAddress, Ipv4Address ipForRequestedMac) {
371         checkNotNull(senderAddress);
372         checkNotNull(ipForRequestedMac);
373         FlowBuilder arpFlow = new FlowBuilder().setTableId(TABEL_FOR_ARP_FLOW)
374             .setFlowName(ARP_REPLY_TO_CONTROLLER_FLOW_NAME)
375             .setPriority(ARP_REPLY_TO_CONTROLLER_FLOW_PRIORITY)
376             .setBufferId(OFConstants.OFP_NO_BUFFER)
377             .setIdleTimeout(0)
378             .setHardTimeout(0)
379             .setCookie(new FlowCookie(BigInteger.valueOf(flowCookie.incrementAndGet())))
380             .setFlags(new FlowModFlags(false, false, false, false, false));
381         EthernetMatch ethernetMatch = ArpFlowFactory.createEthernetMatch(senderAddress.getHardwareAddress());
382         ArpMatch arpMatch = ArpFlowFactory.createArpMatch(senderAddress, ipForRequestedMac);
383         Match match = new MatchBuilder().setEthernetMatch(ethernetMatch).setLayer3Match(arpMatch).build();
384         arpFlow.setMatch(match);
385         arpFlow.setInstructions(new InstructionsBuilder().setInstruction(
386                 ImmutableList.of(SEND_TO_CONTROLLER_INSTRUCTION)).build());
387         arpFlow.setId(createFlowId(ethernetMatch, arpMatch));
388         return arpFlow.build();
389     }
390
391     private FlowId createFlowId(EthernetMatch ethernetMatch, ArpMatch arpMatch) {
392         StringBuilder sb = new StringBuilder();
393         sb.append(ARP_REPLY_TO_CONTROLLER_FLOW_NAME);
394         sb.append("|").append(ethernetMatch);
395         sb.append("|").append(arpMatch);
396         return new FlowId(sb.toString());
397     }
398
399     private static InstanceIdentifier<Flow> createFlowIid(Flow flow, InstanceIdentifier<Node> nodeIid) {
400         return nodeIid.builder()
401             .augmentation(FlowCapableNode.class)
402             .child(Table.class, new TableKey(flow.getTableId()))
403             .child(Flow.class, new FlowKey(flow.getId()))
404             .build();
405     }
406
407     private FutureCallback<RpcResult<Void>> logResult(final Ipv4Address tpa,
408             final KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> egressNcIid) {
409         return new FutureCallback<RpcResult<Void>>() {
410
411             @Override
412             public void onSuccess(RpcResult<Void> result) {
413                 LOG.debug("ARP Request for IP {} was sent from {}.", tpa.getValue(), egressNcIid);
414             }
415
416             @Override
417             public void onFailure(Throwable t) {
418                 LOG.warn("ARP Request for IP {} was NOT sent from {}.", tpa.getValue(), egressNcIid);
419             }
420         };
421     }
422
423     private static String createKey(NodeId node, Ipv4Address ip) {
424         return node.getValue() + "_" + "_" + ip.getValue();
425     }
426
427 }