2 * Copyright (c) 2014 Contextream, Inc. 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
9 package org.opendaylight.lispflowmapping.southbound.lisp;
11 import java.net.DatagramPacket;
12 import java.net.InetAddress;
13 import java.nio.ByteBuffer;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
16 import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
17 import org.opendaylight.lispflowmapping.southbound.LispSouthboundStats;
18 import org.opendaylight.lispflowmapping.southbound.util.LispNotificationHelper;
19 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
20 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
21 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
22 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
23 import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
24 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
25 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
26 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
27 import org.opendaylight.lispflowmapping.southbound.lisp.network.PacketHeader;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMappingBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotifyBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReplyBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMappingBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class LispSouthboundService implements ILispSouthboundService {
43 private NotificationPublishService notificationPublishService;
44 protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundService.class);
46 private final LispSouthboundPlugin lispSbPlugin;
47 private LispSouthboundStats lispSbStats = null;
49 public LispSouthboundService(LispSouthboundPlugin lispSbPlugin) {
50 this.lispSbPlugin = lispSbPlugin;
51 if (lispSbPlugin != null) {
52 this.lispSbStats = lispSbPlugin.getStats();
56 public void setNotificationProvider(NotificationPublishService nps) {
57 this.notificationPublishService = nps;
60 public void handlePacket(DatagramPacket packet) {
61 ByteBuffer inBuffer = ByteBuffer.wrap(packet.getData(), 0, packet.getLength());
62 int type = ByteUtil.getUnsignedByte(inBuffer, LispMessage.Pos.TYPE) >> 4;
64 Object lispType = MessageType.forValue(type);
65 if (lispType == MessageType.EncapsulatedControlMessage) {
66 LOG.trace("Received packet of type Encapsulated Control Message");
67 handleEncapsulatedControlMessage(inBuffer, packet.getAddress());
68 } else if (lispType == MessageType.MapRequest) {
69 LOG.trace("Received packet of type Map-Request");
70 handleMapRequest(inBuffer, packet.getPort());
71 } else if (lispType == MessageType.MapRegister) {
72 LOG.trace("Received packet of type Map-Register");
73 handleMapRegister(inBuffer, packet.getAddress(), packet.getPort());
74 } else if (lispType == MessageType.MapNotify) {
75 LOG.trace("Received packet of type Map-Notify");
76 handleMapNotify(inBuffer, packet.getAddress(), packet.getPort());
77 } else if (lispType == MessageType.MapReply) {
78 LOG.trace("Received packet of type Map-Reply");
79 handleMapReply(inBuffer, packet.getAddress(), packet.getPort());
81 LOG.warn("Received unknown LISP control packet (type " + ((lispType != null) ? lispType : type) + ")");
82 LOG.trace("Buffer: " + ByteUtil.bytesToHex(packet.getData(), packet.getLength()));
86 private void handleEncapsulatedControlMessage(ByteBuffer inBuffer, InetAddress sourceAddress) {
88 handleMapRequest(inBuffer, extractEncapsulatedSourcePort(inBuffer));
89 } catch (RuntimeException re) {
90 throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re);
94 private void handleMapRequest(ByteBuffer inBuffer, int port) {
96 MapRequest request = MapRequestSerializer.getInstance().deserialize(inBuffer);
97 InetAddress finalSourceAddress = MapRequestUtil.selectItrRloc(request);
98 if (finalSourceAddress == null) {
99 throw new LispMalformedPacketException("Couldn't deserialize Map-Request, no ITR Rloc found!");
102 RequestMappingBuilder requestMappingBuilder = new RequestMappingBuilder();
103 requestMappingBuilder.setMapRequest(LispNotificationHelper.convertMapRequest(request));
104 TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
105 transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(finalSourceAddress));
106 transportAddressBuilder.setPort(new PortNumber(port));
107 requestMappingBuilder.setTransportAddress(transportAddressBuilder.build());
108 if (notificationPublishService != null) {
109 notificationPublishService.putNotification(requestMappingBuilder.build());
110 LOG.trace("MapRequest was published!");
112 LOG.warn("Notification Provider is null!");
114 } catch (RuntimeException re) {
115 throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re);
116 } catch (InterruptedException e) {
117 LOG.warn("Notification publication interrupted!");
121 private int extractEncapsulatedSourcePort(ByteBuffer inBuffer) {
123 inBuffer.position(PacketHeader.Length.LISP_ENCAPSULATION);
124 int ipType = (inBuffer.get() >> 4);
126 inBuffer.position(inBuffer.position() + PacketHeader.Length.IPV4 - 1);
127 } else if (ipType == 6) {
128 inBuffer.position(inBuffer.position() + PacketHeader.Length.IPV6_NO_EXT - 1);
130 throw new LispMalformedPacketException("Couldn't deserialize Map-Request: inner packet has unknown IP version: " + ipType);
133 int encapsulatedSourcePort = inBuffer.getShort() & 0xFFFF;
134 inBuffer.position(inBuffer.position() + PacketHeader.Length.UDP - 2);
135 return encapsulatedSourcePort;
136 } catch (RuntimeException re) {
137 throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re);
141 private void handleMapRegister(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
143 MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(inBuffer, sourceAddress);
144 AddMappingBuilder addMappingBuilder = new AddMappingBuilder();
145 addMappingBuilder.setMapRegister(LispNotificationHelper.convertMapRegister(mapRegister));
146 TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
147 transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(sourceAddress));
148 transportAddressBuilder.setPort(new PortNumber(port));
149 addMappingBuilder.setTransportAddress(transportAddressBuilder.build());
150 if (notificationPublishService != null) {
151 notificationPublishService.putNotification(addMappingBuilder.build());
152 LOG.trace("MapRegister was published!");
154 LOG.warn("Notification Provider is null!");
156 } catch (RuntimeException re) {
157 throw new LispMalformedPacketException("Couldn't deserialize Map-Register (len=" + inBuffer.capacity() + ")", re);
158 } catch (InterruptedException e) {
159 LOG.warn("Notification publication interrupted!");
163 private void handleMapNotify(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
165 MapNotify mapNotify = MapNotifySerializer.getInstance().deserialize(inBuffer);
166 GotMapNotifyBuilder gotMapNotifyBuilder = new GotMapNotifyBuilder();
167 gotMapNotifyBuilder.setMapNotify(LispNotificationHelper.convertMapNotify(mapNotify));
168 TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
169 transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(sourceAddress));
170 transportAddressBuilder.setPort(new PortNumber(port));
171 gotMapNotifyBuilder.setTransportAddress(transportAddressBuilder.build());
172 if (notificationPublishService != null) {
173 notificationPublishService.putNotification(gotMapNotifyBuilder.build());
174 LOG.trace("MapNotify was published!");
176 LOG.warn("Notification Provider is null!");
178 } catch (RuntimeException re) {
179 throw new LispMalformedPacketException("Couldn't deserialize Map-Notify (len=" + inBuffer.capacity() + ")", re);
180 } catch (InterruptedException e) {
181 LOG.warn("Notification publication interrupted!");
185 private void handleMapReply(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
187 MapReply mapReply = MapReplySerializer.getInstance().deserialize(inBuffer);
188 GotMapReplyBuilder gotMapReplyBuilder = new GotMapReplyBuilder();
189 gotMapReplyBuilder.setMapReply(LispNotificationHelper.convertMapReply(mapReply));
190 TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
191 transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(sourceAddress));
192 transportAddressBuilder.setPort(new PortNumber(port));
193 gotMapReplyBuilder.setTransportAddress(transportAddressBuilder.build());
194 if (notificationPublishService != null) {
195 notificationPublishService.putNotification(gotMapReplyBuilder.build());
196 LOG.trace("MapReply was published!");
198 LOG.warn("Notification Provider is null!");
200 } catch (RuntimeException re) {
201 throw new LispMalformedPacketException("Couldn't deserialize Map-Reply (len=" + inBuffer.capacity() + ")", re);
202 } catch (InterruptedException e) {
203 LOG.warn("Notification publication interrupted!");
207 private void handleStats(int type) {
208 if (lispSbStats != null) {
209 if (type <= LispSouthboundStats.MAX_LISP_TYPES) {
210 lispSbStats.incrementRx(type);
212 lispSbStats.incrementRxUnknown();