7e90f62fe37a30f5f8e332271679b6de65d89e50
[netvirt.git] /
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.netvirt.openstack.netvirt.providers.openflow13.services.arp;
10
11 import org.opendaylight.controller.liblldp.EtherTypes;
12 import org.opendaylight.controller.liblldp.Ethernet;
13 import org.opendaylight.controller.liblldp.NetUtils;
14 import org.opendaylight.controller.liblldp.PacketException;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class ArpResolverUtils {
20     private static final Logger LOG = LoggerFactory.getLogger(ArpResolverUtils.class);
21
22
23     static {
24         Ethernet.etherTypeClassMap.put(EtherTypes.ARP.shortValue(), Arp.class);
25     }
26
27     /**
28      * Tries to deserialize received packet as ARP packet with IPv4 protocol address and MAC
29      * hardware address.
30      *
31      * @param potentialArp the packet for deserialization
32      * @return ARP packet if received packet is ARP and deserialization was successful
33      */
34     public static Arp getArpFrom(PacketReceived potentialArp) {
35         byte[] payload = potentialArp.getPayload();
36         Ethernet ethPkt = new Ethernet();
37         try {
38             ethPkt.deserialize(payload, 0, payload.length * NetUtils.NumBitsInAByte);
39         } catch (PacketException e) {
40             LOG.trace("Failed to decode the incoming packet. ignoring it.");
41         }
42         if (ethPkt.getPayload() instanceof Arp) {
43             return (Arp) ethPkt.getPayload();
44         }
45         return null;
46     }
47 }