Migrate isFoo() callers
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / lisp / LispXtrSouthboundHandler.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.ChannelHandlerContext;
13 import io.netty.channel.SimpleChannelInboundHandler;
14 import io.netty.channel.socket.DatagramPacket;
15 import java.net.InetAddress;
16 import java.nio.ByteBuffer;
17 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
18 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
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.southbound.LispSouthboundPlugin;
23 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
24 import org.opendaylight.lispflowmapping.southbound.util.LispNotificationHelper;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMappingBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMappingBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class LispXtrSouthboundHandler extends SimpleChannelInboundHandler<DatagramPacket>
36         implements ILispSouthboundService {
37     private final LispSouthboundPlugin lispSbPlugin;
38     protected static final Logger LOG = LoggerFactory.getLogger(LispXtrSouthboundHandler.class);
39
40     public LispXtrSouthboundHandler(LispSouthboundPlugin lispSbPlugin) {
41         this.lispSbPlugin = lispSbPlugin;
42     }
43
44     public void handlePacket(DatagramPacket packet) {
45         ByteBuffer inBuffer = packet.content().nioBuffer();
46         Object lispType = MessageType.forValue((int) (ByteUtil.getUnsignedByte(inBuffer, LispMessage.Pos.TYPE) >> 4));
47         if (lispType == MessageType.MapRequest) {
48             LOG.trace("Received packet of type MapRequest for xTR");
49             handleMapRequest(inBuffer, packet.sender().getAddress());
50         } else if (lispType ==  MessageType.MapReply) {
51             LOG.trace("Received packet of type MapReply for xTR");
52             handleMapReply(inBuffer);
53         } else {
54             LOG.warn("Received unknown packet type");
55         }
56     }
57
58     @SuppressWarnings("checkstyle:IllegalCatch")
59     private void handleMapRequest(ByteBuffer inBuffer, InetAddress sourceAddress) {
60         try {
61             MapRequest request = MapRequestSerializer.getInstance().deserialize(inBuffer, sourceAddress);
62             InetAddress finalSourceAddress = MapRequestUtil.selectItrRloc(request);
63             if (finalSourceAddress == null) {
64                 throw new LispMalformedPacketException("Couldn't deserialize Map-Request, no ITR Rloc found!");
65             }
66
67             XtrRequestMappingBuilder requestMappingBuilder = new XtrRequestMappingBuilder();
68             requestMappingBuilder.setMapRequest(LispNotificationHelper.convertMapRequest(request));
69             TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
70             transportAddressBuilder.setIpAddress(
71                     LispNotificationHelper.getIpAddressBinaryFromInetAddress(finalSourceAddress));
72             transportAddressBuilder.setPort(new PortNumber(LispMessage.PORT_NUM));
73             requestMappingBuilder.setTransportAddress(transportAddressBuilder.build());
74             lispSbPlugin.sendNotificationIfPossible(requestMappingBuilder.build());
75         } catch (RuntimeException re) {
76             throw new LispMalformedPacketException(
77                     "Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re);
78         } catch (InterruptedException e) {
79             LOG.warn("Notification publication interrupted!");
80         }
81     }
82
83     @SuppressWarnings("checkstyle:IllegalCatch")
84     private void handleMapReply(ByteBuffer buffer) {
85         try {
86             MapReply reply = MapReplySerializer.getInstance().deserialize(buffer);
87
88             XtrReplyMappingBuilder replyMappingBuilder = new XtrReplyMappingBuilder();
89             replyMappingBuilder.setMapReply(LispNotificationHelper.convertMapReply(reply));
90             lispSbPlugin.sendNotificationIfPossible(replyMappingBuilder.build());
91         } catch (RuntimeException re) {
92             throw new LispMalformedPacketException(
93                     "Couldn't deserialize Map-Reply (len=" + buffer.capacity() + ")", re);
94         } catch (InterruptedException e) {
95             LOG.warn("Notification publication interrupted!");
96         }
97     }
98
99     @Override
100     protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
101         if (LOG.isTraceEnabled()) {
102             LOG.trace("Received xTR UDP packet from {}:{} with content:\n{}", msg.sender().getHostString(),
103                     msg.sender().getPort(), ByteBufUtil.prettyHexDump(msg.content()));
104         }
105         handlePacket(msg);
106     }
107
108     @Override
109     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
110         ctx.flush();
111     }
112
113     @Override
114     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
115         LOG.error("Error on channel: " + cause, cause);
116     }
117 }