Checkstyle: enforce 120 character limit
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / lisp / LispSouthboundHandler.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 io.netty.buffer.ByteBufUtil;
12 import io.netty.channel.ChannelHandler;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.SimpleChannelInboundHandler;
15 import io.netty.channel.socket.DatagramPacket;
16
17 import java.net.InetAddress;
18 import java.nio.ByteBuffer;
19
20 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
21 import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
22 import org.opendaylight.lispflowmapping.southbound.LispSouthboundStats;
23 import org.opendaylight.lispflowmapping.southbound.util.LispNotificationHelper;
24 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
25 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
26 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
27 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
28 import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
29 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
30 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
31 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
32 import org.opendaylight.lispflowmapping.southbound.lisp.network.PacketHeader;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMappingBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotifyBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReplyBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMappingBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 @ChannelHandler.Sharable
48 public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramPacket>
49         implements ILispSouthboundService {
50     private NotificationPublishService notificationPublishService;
51     protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundHandler.class);
52
53     private final LispSouthboundPlugin lispSbPlugin;
54     private LispSouthboundStats lispSbStats = null;
55
56     public LispSouthboundHandler(LispSouthboundPlugin lispSbPlugin) {
57         this.lispSbPlugin = lispSbPlugin;
58         if (lispSbPlugin != null) {
59             this.lispSbStats = lispSbPlugin.getStats();
60         }
61     }
62
63     public void setNotificationProvider(NotificationPublishService nps) {
64         this.notificationPublishService = nps;
65     }
66
67     public void handlePacket(DatagramPacket msg) {
68         ByteBuffer inBuffer = msg.content().nioBuffer();
69         int type = ByteUtil.getUnsignedByte(inBuffer, LispMessage.Pos.TYPE) >> 4;
70         handleStats(type);
71         Object lispType = MessageType.forValue(type);
72         if (lispType == MessageType.EncapsulatedControlMessage) {
73             LOG.trace("Received packet of type Encapsulated Control Message");
74             handleEncapsulatedControlMessage(inBuffer, msg.sender().getAddress());
75         } else if (lispType == MessageType.MapRequest) {
76             LOG.trace("Received packet of type Map-Request");
77             handleMapRequest(inBuffer, msg.sender().getPort());
78         } else if (lispType == MessageType.MapRegister) {
79             LOG.trace("Received packet of type Map-Register");
80             handleMapRegister(inBuffer, msg.sender().getAddress(), msg.sender().getPort());
81         } else if (lispType == MessageType.MapNotify) {
82             LOG.trace("Received packet of type Map-Notify");
83             handleMapNotify(inBuffer, msg.sender().getAddress(), msg.sender().getPort());
84         } else if (lispType == MessageType.MapReply) {
85             LOG.trace("Received packet of type Map-Reply");
86             handleMapReply(inBuffer, msg.sender().getAddress(), msg.sender().getPort());
87         } else {
88             LOG.warn("Received unknown LISP control packet (type " + ((lispType != null) ? lispType : type) + ")");
89         }
90     }
91
92     private void handleEncapsulatedControlMessage(ByteBuffer inBuffer, InetAddress sourceAddress) {
93         try {
94             handleMapRequest(inBuffer, extractEncapsulatedSourcePort(inBuffer));
95         } catch (RuntimeException re) {
96             throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len="
97                     + inBuffer.capacity() + ")", re);
98         }
99     }
100
101     private void handleMapRequest(ByteBuffer inBuffer, int port) {
102         try {
103             MapRequest request = MapRequestSerializer.getInstance().deserialize(inBuffer);
104             InetAddress finalSourceAddress = MapRequestUtil.selectItrRloc(request);
105             if (finalSourceAddress == null) {
106                 throw new LispMalformedPacketException("Couldn't deserialize Map-Request, no ITR Rloc found!");
107             }
108
109             RequestMappingBuilder requestMappingBuilder = new RequestMappingBuilder();
110             requestMappingBuilder.setMapRequest(LispNotificationHelper.convertMapRequest(request));
111             TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
112             transportAddressBuilder.setIpAddress(
113                     LispNotificationHelper.getIpAddressFromInetAddress(finalSourceAddress));
114             transportAddressBuilder.setPort(new PortNumber(port));
115             requestMappingBuilder.setTransportAddress(transportAddressBuilder.build());
116             if (notificationPublishService != null) {
117                 notificationPublishService.putNotification(requestMappingBuilder.build());
118                 LOG.trace("MapRequest was published!");
119             } else {
120                 LOG.warn("Notification Provider is null!");
121             }
122         } catch (RuntimeException re) {
123             throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len="
124                     + inBuffer.capacity() + ")", re);
125         } catch (InterruptedException e) {
126             LOG.warn("Notification publication interrupted!");
127         }
128     }
129
130     private int extractEncapsulatedSourcePort(ByteBuffer inBuffer) {
131         try {
132             inBuffer.position(PacketHeader.Length.LISP_ENCAPSULATION);
133             int ipType = (inBuffer.get() >> 4);
134             if (ipType == 4) {
135                 inBuffer.position(inBuffer.position() + PacketHeader.Length.IPV4 - 1);
136             } else if (ipType == 6) {
137                 inBuffer.position(inBuffer.position() + PacketHeader.Length.IPV6_NO_EXT - 1);
138             } else {
139                 throw new LispMalformedPacketException(
140                         "Couldn't deserialize Map-Request: inner packet has unknown IP version: " + ipType);
141             }
142
143             int encapsulatedSourcePort = inBuffer.getShort() & 0xFFFF;
144             inBuffer.position(inBuffer.position() + PacketHeader.Length.UDP - 2);
145             return encapsulatedSourcePort;
146         } catch (RuntimeException re) {
147             throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len="
148                     + inBuffer.capacity() + ")", re);
149         }
150     }
151
152     private void handleMapRegister(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
153         try {
154             MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(inBuffer, sourceAddress);
155             AddMappingBuilder addMappingBuilder = new AddMappingBuilder();
156             addMappingBuilder.setMapRegister(LispNotificationHelper.convertMapRegister(mapRegister));
157             TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
158             transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(sourceAddress));
159             transportAddressBuilder.setPort(new PortNumber(port));
160             addMappingBuilder.setTransportAddress(transportAddressBuilder.build());
161             if (notificationPublishService != null) {
162                 notificationPublishService.putNotification(addMappingBuilder.build());
163                 LOG.trace("MapRegister was published!");
164             } else {
165                 LOG.warn("Notification Provider is null!");
166             }
167         } catch (RuntimeException re) {
168             throw new LispMalformedPacketException("Couldn't deserialize Map-Register (len="
169                     + inBuffer.capacity() + ")", re);
170         } catch (InterruptedException e) {
171             LOG.warn("Notification publication interrupted!");
172         }
173     }
174
175     private void handleMapNotify(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
176         try {
177             MapNotify mapNotify = MapNotifySerializer.getInstance().deserialize(inBuffer);
178             GotMapNotifyBuilder gotMapNotifyBuilder = new GotMapNotifyBuilder();
179             gotMapNotifyBuilder.setMapNotify(LispNotificationHelper.convertMapNotify(mapNotify));
180             TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
181             transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(sourceAddress));
182             transportAddressBuilder.setPort(new PortNumber(port));
183             gotMapNotifyBuilder.setTransportAddress(transportAddressBuilder.build());
184             if (notificationPublishService != null) {
185                 notificationPublishService.putNotification(gotMapNotifyBuilder.build());
186                 LOG.trace("MapNotify was published!");
187             } else {
188                 LOG.warn("Notification Provider is null!");
189             }
190         } catch (RuntimeException re) {
191             throw new LispMalformedPacketException("Couldn't deserialize Map-Notify (len="
192                     + inBuffer.capacity() + ")", re);
193         } catch (InterruptedException e) {
194             LOG.warn("Notification publication interrupted!");
195         }
196     }
197
198     private void handleMapReply(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
199         try {
200             MapReply mapReply = MapReplySerializer.getInstance().deserialize(inBuffer);
201             GotMapReplyBuilder gotMapReplyBuilder = new GotMapReplyBuilder();
202             gotMapReplyBuilder.setMapReply(LispNotificationHelper.convertMapReply(mapReply));
203             TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
204             transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(sourceAddress));
205             transportAddressBuilder.setPort(new PortNumber(port));
206             gotMapReplyBuilder.setTransportAddress(transportAddressBuilder.build());
207             if (notificationPublishService != null) {
208                 notificationPublishService.putNotification(gotMapReplyBuilder.build());
209                 LOG.trace("MapReply was published!");
210             } else {
211                 LOG.warn("Notification Provider is null!");
212             }
213         } catch (RuntimeException re) {
214             throw new LispMalformedPacketException("Couldn't deserialize Map-Reply (len="
215                     + inBuffer.capacity() + ")", re);
216         } catch (InterruptedException e) {
217             LOG.warn("Notification publication interrupted!");
218         }
219     }
220
221     private void handleStats(int type) {
222         if (lispSbStats != null) {
223             if (type <= LispSouthboundStats.MAX_LISP_TYPES) {
224                 lispSbStats.incrementRx(type);
225             } else {
226                 lispSbStats.incrementRxUnknown();
227             }
228         }
229     }
230
231     @Override
232     protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
233         if (LOG.isTraceEnabled()) {
234             LOG.trace("Received UDP packet from {}:{} with content:\n{}", msg.sender().getHostString(),
235                     msg.sender().getPort(), ByteBufUtil.prettyHexDump(msg.content()));
236         }
237         handlePacket(msg);
238     }
239
240     @Override
241     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
242         ctx.flush();
243     }
244
245     @Override
246     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
247         LOG.error("Error on channel: " + cause, cause);
248     }
249 }