blob: fccda3e58781514537feca881e6ff85f4543c855 [file] [log] [blame]
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -07001/*
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
17package org.onosproject.artemis.impl.moas;
18
19import com.google.common.annotations.Beta;
20import io.netty.bootstrap.Bootstrap;
21import io.netty.channel.ChannelFuture;
22import io.netty.channel.ChannelInitializer;
23import io.netty.channel.ChannelOption;
24import io.netty.channel.EventLoopGroup;
25import io.netty.channel.nio.NioEventLoopGroup;
26import io.netty.channel.socket.SocketChannel;
27import io.netty.channel.socket.nio.NioSocketChannel;
28import org.apache.commons.lang.exception.ExceptionUtils;
29import org.onlab.packet.IpAddress;
30import org.onlab.packet.IpPrefix;
31import org.onosproject.artemis.ArtemisPacketProcessor;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35/**
36 * MOAS Client Controller.
37 */
38@Beta
39public 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
85 * @throws Exception exception
86 */
87 private Bootstrap createBootstrap() throws Exception {
88 try {
89 workerGroup = new NioEventLoopGroup();
90 return new Bootstrap()
91 .group(workerGroup)
92 .channel(NioSocketChannel.class)
93 .option(ChannelOption.SO_KEEPALIVE, true);
94 } catch (Exception e) {
95 throw new Exception(e);
96 }
97 }
98
99 /**
100 * Stop the MOAS client.
101 */
102 public void stop() {
103 channel.channel().close();
104 workerGroup.shutdownGracefully();
105 }
106
107}