Migrate implementation/neutron/southbound to IETF YANG model
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / lisp / LispXtrSouthboundService.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.util.LispNotificationHelper;
17 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
18 import org.opendaylight.lispflowmapping.lisp.type.LispMessageEnum;
19 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
20 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
21 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
22 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
23 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMappingBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMappingBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class LispXtrSouthboundService implements ILispSouthboundService {
34     private NotificationPublishService notificationPublishService;
35     protected static final Logger LOG = LoggerFactory.getLogger(LispXtrSouthboundService.class);
36
37     public void setNotificationProvider(NotificationPublishService nps) {
38         this.notificationPublishService = nps;
39     }
40
41     public void handlePacket(DatagramPacket packet) {
42         ByteBuffer inBuffer = ByteBuffer.wrap(packet.getData(), 0, packet.getLength());
43         Object lispType = LispMessageEnum.valueOf((byte) (ByteUtil.getUnsignedByte(inBuffer, LispMessage.Pos.TYPE) >> 4));
44         if (lispType == LispMessageEnum.MapRequest) {
45             LOG.trace("Received packet of type MapRequest for xTR");
46             handleMapRequest(inBuffer);
47         } else if (lispType ==  LispMessageEnum.MapReply){
48             LOG.trace("Received packet of type MapReply for xTR");
49             handleMapReply(inBuffer);
50         } else {
51             LOG.warn("Received unknown packet type");
52         }
53     }
54
55     private void handleMapRequest(ByteBuffer inBuffer) {
56         try {
57             MapRequest request = MapRequestSerializer.getInstance().deserialize(inBuffer);
58             InetAddress finalSourceAddress = MapRequestUtil.selectItrRloc(request);
59             if (finalSourceAddress == null) {
60                 throw new LispMalformedPacketException("Couldn't deserialize Map-Request, no ITR Rloc found!");
61             }
62
63             XtrRequestMappingBuilder requestMappingBuilder = new XtrRequestMappingBuilder();
64             requestMappingBuilder.setMapRequest(LispNotificationHelper.convertMapRequest(request));
65             TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
66             transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressFromInetAddress(finalSourceAddress));
67             transportAddressBuilder.setPort(new PortNumber(LispMessage.PORT_NUM));
68             requestMappingBuilder.setTransportAddress(transportAddressBuilder.build());
69             if (notificationPublishService != null) {
70                 notificationPublishService.putNotification(requestMappingBuilder.build());
71                 LOG.trace("MapRequest was published!");
72             } else {
73                 LOG.warn("Notification Provider is null!");
74             }
75         } catch (RuntimeException re) {
76             throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re);
77         } catch (InterruptedException e) {
78             LOG.warn("Notification publication interrupted!");
79         }
80     }
81
82     private void handleMapReply(ByteBuffer buffer) {
83         try {
84             MapReply reply = MapReplySerializer.getInstance().deserialize(buffer);
85
86             XtrReplyMappingBuilder replyMappingBuilder = new XtrReplyMappingBuilder();
87             replyMappingBuilder.setMapReply(LispNotificationHelper.convertMapReply(reply));
88
89             if (notificationPublishService != null) {
90                 notificationPublishService.putNotification(replyMappingBuilder.build());
91                 LOG.trace("MapReply was published!");
92             } else {
93                 LOG.warn("Notification Provider is null!");
94             }
95         } catch (RuntimeException re) {
96             throw new LispMalformedPacketException("Couldn't deserialize Map-Reply (len=" + buffer.capacity() + ")", re);
97         } catch (InterruptedException e) {
98             LOG.warn("Notification publication interrupted!");
99         }
100     }
101 }