Remove BindingAwareBroker
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / LispSouthboundPlugin.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;
10
11 import static io.netty.buffer.Unpooled.wrappedBuffer;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.ChannelFutureListener;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.channel.socket.DatagramPacket;
20 import io.netty.channel.socket.nio.NioDatagramChannel;
21 import io.netty.util.concurrent.DefaultThreadFactory;
22
23 import java.net.InetAddress;
24 import java.net.InetSocketAddress;
25 import java.nio.ByteBuffer;
26 import java.util.concurrent.ThreadFactory;
27 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
29 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
30 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
31 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
32 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundHandler;
33 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.common.net.InetAddresses;
41
42 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable {
43     protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
44
45     private static Object startLock = new Object();
46     private LispSouthboundHandler lispSouthboundHandler;
47     private LispXtrSouthboundHandler lispXtrSouthboundHandler;
48     private NotificationPublishService notificationPublishService;
49     private RpcProviderRegistry rpcRegistry;
50     private NioDatagramChannel channel;
51     private volatile String bindingAddress = "0.0.0.0";
52     private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
53     private volatile boolean listenOnXtrPort = false;
54     private RpcRegistration<OdlLispSbService> sbRpcRegistration;
55     private NioDatagramChannel xtrChannel;
56     private LispSouthboundStats statistics = new LispSouthboundStats();
57     private ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
58     private EventLoopGroup eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
59
60
61     public void init() {
62         LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
63         final LispSouthboundRPC sbRpcHandler = new LispSouthboundRPC(this);
64
65         sbRpcRegistration = rpcRegistry.addRpcImplementation(OdlLispSbService.class, sbRpcHandler);
66
67         synchronized (startLock) {
68             lispSouthboundHandler = new LispSouthboundHandler(this);
69             lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
70             lispSouthboundHandler.setNotificationProvider(this.notificationPublishService);
71             lispXtrSouthboundHandler.setNotificationProvider(this.notificationPublishService);
72
73             start();
74             startXtr();
75
76             LOG.info("LISP (RFC6830) Southbound Plugin is up!");
77         }
78     }
79
80     private void start() {
81         try {
82             Bootstrap bootstrap = new Bootstrap();
83             bootstrap.group(eventLoopGroup);
84             bootstrap.channel(NioDatagramChannel.class);
85             bootstrap.handler(lispSouthboundHandler);
86             channel = (NioDatagramChannel) bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
87         } catch (Exception e) {
88             LOG.error("Failed to open main socket ", e);
89         }
90     }
91
92     private void startXtr() {
93         if (listenOnXtrPort) {
94             try {
95                 Bootstrap xtrBootstrap = new Bootstrap();
96                 xtrBootstrap.group(eventLoopGroup);
97                 xtrBootstrap.channel(NioDatagramChannel.class);
98                 xtrBootstrap.handler(lispXtrSouthboundHandler);
99                 xtrChannel = (NioDatagramChannel) xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
100             } catch (Exception e) {
101                 LOG.error("Failed to open xTR socket ", e);
102             }
103         }
104     }
105
106     private void stop() {
107         try {
108             channel.close().sync();
109             channel = null;
110         } catch (Exception e) {
111             LOG.error("Failed to close main socket ", e);
112         }
113     }
114
115     private void stopXtr() {
116         if (listenOnXtrPort) {
117             try {
118                 xtrChannel.close().sync();
119                 xtrChannel = null;
120             } catch (Exception e) {
121                 LOG.error("Failed to close xTR socket ", e);
122             }
123         }
124     }
125
126     private void restart() {
127         LOG.info("Reloading");
128         stop();
129         start();
130     }
131
132     private void restartXtr() {
133         LOG.info("Reloading xTR");
134         stopXtr();
135         startXtr();
136     }
137
138     public void setNotificationPublishService(NotificationPublishService notificationService) {
139         this.notificationPublishService = notificationService;
140     }
141
142     public void setRpcRegistryDependency(RpcProviderRegistry rpcRegistry) {
143         this.rpcRegistry = rpcRegistry;
144     }
145
146     private void unloadActions() {
147         lispSouthboundHandler = null;
148         lispXtrSouthboundHandler = null;
149         bindingAddress = "0.0.0.0";
150
151         stop();
152         stopXtr();
153
154         LOG.info("LISP (RFC6830) Southbound Plugin is down!");
155     }
156
157     public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
158             final MessageType packetType) {
159         InetAddress ip = InetAddresses.forString(new String(address.getIpAddress().getValue()));
160         InetSocketAddress recipient = new InetSocketAddress(ip, address.getPort().getValue());
161         // the wrappedBuffer() method doesn't copy data, so this conversion shouldn't hurt performance
162         ByteBuf data = wrappedBuffer(outBuffer.array());
163         DatagramPacket packet = new DatagramPacket(data, recipient);
164         LOG.debug("Sending {} on port {} to address: {}", packetType, address.getPort().getValue(), ip);
165         if (LOG.isTraceEnabled()) {
166             LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
167         }
168         channel.write(packet).addListener(new ChannelFutureListener() {
169             @Override
170             public void operationComplete(ChannelFuture future) {
171                 if (future.isSuccess()) {
172                     LOG.trace("Success");
173                     statistics.incrementTx(packetType.getIntValue());
174                 } else {
175                     LOG.warn("Failed to send packet");
176                     statistics.incrementTxErrors();
177                 }
178             }
179         });
180         channel.flush();
181     }
182
183     public LispSouthboundStats getStats() {
184         return statistics;
185     }
186
187     @Override
188     public void setLispAddress(String address) {
189         synchronized (startLock) {
190             if (bindingAddress.equals(address)) {
191                 LOG.debug("Configured LISP binding address didn't change.");
192             } else {
193                 LOG.debug("Setting LISP binding address to {}", address);
194                 bindingAddress = address;
195                 try {
196                     restart();
197                     restartXtr();
198                 } catch (Exception e) {
199                     LOG.error("Failed to set LISP binding address: ", e);
200                 }
201             }
202         }
203     }
204
205     @Override
206     public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
207         listenOnXtrPort = shouldListenOnXtrPort;
208         if (listenOnXtrPort) {
209             restartXtr();
210         } else {
211             LOG.info("Shutting down xTR");
212             stopXtr();
213         }
214     }
215
216     @Override
217     public void setXtrPort(int port) {
218         this.xtrPort = port;
219         if (listenOnXtrPort) {
220             restartXtr();
221         }
222     }
223
224     @Override
225     public void close() throws Exception {
226         unloadActions();
227         eventLoopGroup.shutdownGracefully();
228         sbRpcRegistration.close();
229     }
230 }