1
2
3
4
5
6
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 outBuffer.position(0);
162
163 ByteBuf data = wrappedBuffer(outBuffer.array());
164 DatagramPacket packet = new DatagramPacket(data, recipient);
165 LOG.debug("Sending {} on port {} to address: {}", packetType, address.getPort().getValue(), ip);
166 if (LOG.isTraceEnabled()) {
167 LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
168 }
169 channel.write(packet).addListener(new ChannelFutureListener() {
170 @Override
171 public void operationComplete(ChannelFuture future) {
172 if (future.isSuccess()) {
173 LOG.trace("Success");
174 statistics.incrementTx(packetType.getIntValue());
175 } else {
176 LOG.warn("Failed to send packet");
177 statistics.incrementTxErrors();
178 }
179 }
180 });
181 channel.flush();
182 }
183
184 public LispSouthboundStats getStats() {
185 return statistics;
186 }
187
188 @Override
189 public void setLispAddress(String address) {
190 synchronized (startLock) {
191 if (bindingAddress.equals(address)) {
192 LOG.debug("Configured LISP binding address didn't change.");
193 } else {
194 LOG.debug("Setting LISP binding address to {}", address);
195 bindingAddress = address;
196 try {
197 restart();
198 restartXtr();
199 } catch (Exception e) {
200 LOG.error("Failed to set LISP binding address: ", e);
201 }
202 }
203 }
204 }
205
206 @Override
207 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
208 listenOnXtrPort = shouldListenOnXtrPort;
209 if (listenOnXtrPort) {
210 restartXtr();
211 } else {
212 LOG.info("Shutting down xTR");
213 stopXtr();
214 }
215 }
216
217 @Override
218 public void setXtrPort(int port) {
219 this.xtrPort = port;
220 if (listenOnXtrPort) {
221 restartXtr();
222 }
223 }
224
225 @Override
226 public void close() throws Exception {
227 unloadActions();
228 eventLoopGroup.shutdownGracefully();
229 sbRpcRegistration.close();
230 }
231 }