bf13edbd126ce438e4f43003bbfbcb57a8c06e8b
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / lisp / LispSouthboundService.java
1 /*
2  * Copyright (c) 2014 Contextream, 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.lispflowmapping.southbound.lisp;
10
11 import java.net.DatagramPacket;
12 import java.net.InetAddress;
13 import java.nio.ByteBuffer;
14
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.transportaddress.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;
41
42 public class LispSouthboundService implements ILispSouthboundService {
43     private NotificationPublishService notificationPublishService;
44     protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundService.class);
45
46     private final LispSouthboundPlugin lispSbPlugin;
47     private LispSouthboundStats lispSbStats = null;
48
49     public LispSouthboundService(LispSouthboundPlugin lispSbPlugin) {
50         this.lispSbPlugin = lispSbPlugin;
51         if (lispSbPlugin != null) {
52             this.lispSbStats = lispSbPlugin.getStats();
53         }
54     }
55
56     public void setNotificationProvider(NotificationPublishService nps) {
57         this.notificationPublishService = nps;
58     }
59
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;
63         handleStats(type);
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());
80         } else {
81             LOG.warn("Received unknown LISP control packet (type " + ((lispType != null) ? lispType : type) + ")");
82             LOG.trace("Buffer: " + ByteUtil.bytesToHex(packet.getData(), packet.getLength()));
83         }
84     }
85
86     private void handleEncapsulatedControlMessage(ByteBuffer inBuffer, InetAddress sourceAddress) {
87         try {
88             handleMapRequest(inBuffer, extractEncapsulatedSourcePort(inBuffer));
89         } catch (RuntimeException re) {
90             throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re);
91         }
92     }
93
94     private void handleMapRequest(ByteBuffer inBuffer, int port) {
95         try {
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!");
100             }
101
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!");
111             } else {
112                 LOG.warn("Notification Provider is null!");
113             }
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!");
118         }
119     }
120
121     private int extractEncapsulatedSourcePort(ByteBuffer inBuffer) {
122         try {
123             inBuffer.position(PacketHeader.Length.LISP_ENCAPSULATION);
124             int ipType = (inBuffer.get() >> 4);
125             if (ipType == 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);
129             } else {
130                 throw new LispMalformedPacketException("Couldn't deserialize Map-Request: inner packet has unknown IP version: " + ipType);
131             }
132
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);
138         }
139     }
140
141     private void handleMapRegister(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
142         try {
143             MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(inBuffer);
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!");
153             } else {
154                 LOG.warn("Notification Provider is null!");
155             }
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!");
160         }
161     }
162
163     private void handleMapNotify(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
164         try {
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!");
175             } else {
176                 LOG.warn("Notification Provider is null!");
177             }
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!");
182         }
183     }
184
185     private void handleMapReply(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
186         try {
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!");
197             } else {
198                 LOG.warn("Notification Provider is null!");
199             }
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!");
204         }
205     }
206
207     private void handleStats(int type) {
208         if (lispSbStats != null) {
209             if (type <= LispSouthboundStats.MAX_LISP_TYPES) {
210                 lispSbStats.incrementRx(type);
211             } else {
212                 lispSbStats.incrementRxUnknown();
213             }
214         }
215     }
216 }