Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.artemis.impl.moas; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import io.netty.bootstrap.Bootstrap; |
| 21 | import io.netty.channel.ChannelFuture; |
| 22 | import io.netty.channel.ChannelInitializer; |
| 23 | import io.netty.channel.ChannelOption; |
| 24 | import io.netty.channel.EventLoopGroup; |
| 25 | import io.netty.channel.nio.NioEventLoopGroup; |
| 26 | import io.netty.channel.socket.SocketChannel; |
| 27 | import io.netty.channel.socket.nio.NioSocketChannel; |
| 28 | import org.apache.commons.lang.exception.ExceptionUtils; |
| 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onlab.packet.IpPrefix; |
| 31 | import org.onosproject.artemis.ArtemisPacketProcessor; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | /** |
| 36 | * MOAS Client Controller. |
| 37 | */ |
| 38 | @Beta |
| 39 | public class MoasClientController { |
| 40 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 41 | |
| 42 | private IpAddress host; |
| 43 | private EventLoopGroup workerGroup; |
| 44 | private MoasClientHandler ach; |
| 45 | private ChannelFuture channel; |
| 46 | private IpAddress localIp; |
| 47 | private IpPrefix localPrefix; |
| 48 | private ArtemisPacketProcessor packetProcessor; |
| 49 | |
| 50 | public MoasClientController(ArtemisPacketProcessor packetProcessor, |
| 51 | IpAddress host, IpAddress localIp, IpPrefix localPrefix) { |
| 52 | this.host = host; |
| 53 | this.ach = null; |
| 54 | this.localIp = localIp; |
| 55 | this.localPrefix = localPrefix; |
| 56 | this.packetProcessor = packetProcessor; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Run the MOAS client. |
| 61 | */ |
| 62 | public void run() { |
| 63 | try { |
| 64 | final Bootstrap bootstrap = createBootstrap(); |
| 65 | |
| 66 | ach = new MoasClientHandler(localIp, localPrefix, packetProcessor); |
| 67 | |
| 68 | bootstrap.handler(new ChannelInitializer<SocketChannel>() { |
| 69 | @Override |
| 70 | public void initChannel(SocketChannel ch) throws Exception { |
| 71 | ch.pipeline().addLast(ach); |
| 72 | } |
| 73 | }); |
| 74 | |
| 75 | channel = bootstrap.connect(host.toInetAddress(), 32323).sync(); |
| 76 | } catch (Exception e) { |
| 77 | log.warn(ExceptionUtils.getFullStackTrace(e)); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Bootstrap netty socket. |
| 83 | * |
| 84 | * @return bootstrap |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 85 | */ |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 86 | private Bootstrap createBootstrap() { |
| 87 | workerGroup = new NioEventLoopGroup(); |
| 88 | return new Bootstrap() |
| 89 | .group(workerGroup) |
| 90 | .channel(NioSocketChannel.class) |
| 91 | .option(ChannelOption.SO_KEEPALIVE, true); |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Stop the MOAS client. |
| 96 | */ |
| 97 | public void stop() { |
| 98 | channel.channel().close(); |
| 99 | workerGroup.shutdownGracefully(); |
| 100 | } |
| 101 | |
| 102 | } |