/* * Copyright (c) 2013 Contextream, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.lispflowmapping.southbound.lisp; import java.net.DatagramPacket; import java.net.InetAddress; import java.nio.ByteBuffer; import org.opendaylight.controller.sal.binding.api.NotificationProviderService; import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage; import org.opendaylight.lispflowmapping.implementation.serializer.LispMessageEnum; import org.opendaylight.lispflowmapping.implementation.serializer.MapRegisterSerializer; import org.opendaylight.lispflowmapping.implementation.serializer.MapRequestSerializer; import org.opendaylight.lispflowmapping.implementation.util.ByteUtil; import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException; import org.opendaylight.lispflowmapping.southbound.lisp.network.PacketHeader; import org.opendaylight.lispflowmapping.type.lisp.MapRegister; import org.opendaylight.lispflowmapping.type.lisp.MapRequest; import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress; import org.opendaylight.lispflowmapping.type.lisp.address.LispIPAddress; import org.opendaylight.lispflowmapping.type.sbplugin.MapRegisterNotification; import org.opendaylight.lispflowmapping.type.sbplugin.MapRequestNotification; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LispSouthboundService implements ILispSouthboundService { private NotificationProviderService notificationProvider; protected static final Logger logger = LoggerFactory.getLogger(LispSouthboundService.class); public void setNotificationProvider(NotificationProviderService nps) { this.notificationProvider = nps; } public void handlePacket(DatagramPacket packet) { ByteBuffer inBuffer = ByteBuffer.wrap(packet.getData(), 0, packet.getLength()); Object lispType = LispMessageEnum.valueOf((byte) (ByteUtil.getUnsignedByte(inBuffer, LispMessage.Pos.TYPE) >> 4)); if (lispType == LispMessageEnum.EncapsulatedControlMessage) { logger.debug("Recieved packet of type EncapsulatedControlMessage"); handleEncapsulatedControlMessage(inBuffer, packet.getAddress()); } else if (lispType == LispMessageEnum.MapRequest) { logger.debug("Recieved packet of type MapRequest"); handleMapRequest(inBuffer); } else if (lispType == LispMessageEnum.MapRegister) { logger.debug("Recieved packet of type MapRegister"); handleMapRegister(inBuffer, packet.getAddress()); } logger.debug("Recieved unknown packet type"); } private void handleEncapsulatedControlMessage(ByteBuffer inBuffer, InetAddress sourceAddress) { try { extractEncapsulatedSourcePort(inBuffer); handleMapRequest(inBuffer); } catch (RuntimeException re) { throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re); } } private void handleMapRequest(ByteBuffer inBuffer) { try { MapRequest request = MapRequestSerializer.getInstance().deserialize(inBuffer); InetAddress finalSourceAddress = null; for (LispAddress address : request.getItrRlocs()) { if (address instanceof LispIPAddress) { finalSourceAddress = (((LispIPAddress) address).getAddress()); break; } } if (finalSourceAddress == null) { throw new LispMalformedPacketException("Couldn't deserialize Map-Request, no ITR Rloc found!"); } MapRequestNotification requestNotification = new MapRequestNotification(request, finalSourceAddress); if (notificationProvider != null) { notificationProvider.publish(requestNotification); logger.debug("MapRequest was published!"); } else { logger.error("Notification Provider is null!"); } } catch (RuntimeException re) { throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re); } } private int extractEncapsulatedSourcePort(ByteBuffer inBuffer) { try { inBuffer.position(PacketHeader.Length.LISP_ENCAPSULATION); int ipType = (inBuffer.get() >> 4); if (ipType == 4) { inBuffer.position(inBuffer.position() + PacketHeader.Length.IPV4 - 1); } else { inBuffer.position(inBuffer.position() + PacketHeader.Length.IPV6_NO_EXT - 1); } int encapsulatedSourcePort = inBuffer.getShort() & 0xFFFF; inBuffer.position(inBuffer.position() + PacketHeader.Length.UDP - 2); return encapsulatedSourcePort; } catch (RuntimeException re) { throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re); } } private void handleMapRegister(ByteBuffer inBuffer, InetAddress sourceAddress) { try { MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(inBuffer); MapRegisterNotification registerNotification = new MapRegisterNotification(mapRegister, sourceAddress); if (notificationProvider != null) { notificationProvider.publish(registerNotification); logger.debug("MapRegister was published!"); } else { logger.error("Notification Provider is null!"); } } catch (RuntimeException re) { throw new LispMalformedPacketException("Couldn't deserialize Map-Register (len=" + inBuffer.capacity() + ")", re); } } }