blob: 8bcd5a0e0d3362dede0589b58f40ade6ee7ce30f [file] [log] [blame]
Shashikanth VH6de20d32015-10-09 12:04:13 +05301/*
2 * Copyright 2015 Open Networking Laboratory
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.bgp.controller.impl;
18
19import java.net.InetSocketAddress;
20import java.net.SocketAddress;
21import java.util.Collections;
22import java.util.List;
23import java.util.concurrent.RejectedExecutionException;
24
25import org.jboss.netty.channel.Channel;
26import org.onlab.packet.IpAddress;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053027import org.onosproject.bgp.controller.BgpController;
28import org.onosproject.bgp.controller.BgpPeer;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053029import org.onosproject.bgp.controller.BgpSessionInfo;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053030import org.onosproject.bgpio.protocol.BgpFactories;
31import org.onosproject.bgpio.protocol.BgpFactory;
32import org.onosproject.bgpio.protocol.BgpMessage;
Shashikanth VH6de20d32015-10-09 12:04:13 +053033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import com.google.common.base.MoreObjects;
37
38/**
39 * BGPPeerImpl implements BGPPeer, maintains peer information and store updates in RIB .
40 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053041public class BgpPeerImpl implements BgpPeer {
Shashikanth VH6de20d32015-10-09 12:04:13 +053042
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053043 protected final Logger log = LoggerFactory.getLogger(BgpPeerImpl.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +053044
45 private static final String SHUTDOWN_MSG = "Worker has already been shutdown";
46
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053047 private BgpController bgpController;
Shashikanth VH6de20d32015-10-09 12:04:13 +053048 private Channel channel;
49 protected String channelId;
50 private boolean connected;
51 protected boolean isHandShakeComplete = false;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053052 private BgpSessionInfo sessionInfo;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053053 private BgpPacketStatsImpl pktStats;
Shashikanth VH6de20d32015-10-09 12:04:13 +053054
Shashikanth VH9f8afb42015-11-04 18:00:30 +053055
Shashikanth VH6de20d32015-10-09 12:04:13 +053056 @Override
Shashikanth VH9f8afb42015-11-04 18:00:30 +053057 public BgpSessionInfo sessionInfo() {
58 return sessionInfo;
59 }
60
61 /**
62 * Initialize peer.
63 *
64 *@param bgpController controller instance
65 *@param sessionInfo bgp session info
66 *@param pktStats packet statistics
67 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053068 public BgpPeerImpl(BgpController bgpController, BgpSessionInfo sessionInfo, BgpPacketStatsImpl pktStats) {
Shashikanth VH9f8afb42015-11-04 18:00:30 +053069 this.bgpController = bgpController;
70 this.sessionInfo = sessionInfo;
71 this.pktStats = pktStats;
Shashikanth VH6de20d32015-10-09 12:04:13 +053072 }
73
74 // ************************
75 // Channel related
76 // ************************
77
78 @Override
79 public final void disconnectPeer() {
80 this.channel.close();
81 }
82
83 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053084 public final void sendMessage(BgpMessage m) {
Shashikanth VH6de20d32015-10-09 12:04:13 +053085 log.debug("Sending message to {}", channel.getRemoteAddress());
86 try {
87 channel.write(Collections.singletonList(m));
88 this.pktStats.addOutPacket();
89 } catch (RejectedExecutionException e) {
90 log.warn(e.getMessage());
91 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
92 throw e;
93 }
94 }
95 }
96
97 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053098 public final void sendMessage(List<BgpMessage> msgs) {
Shashikanth VH6de20d32015-10-09 12:04:13 +053099 try {
100 channel.write(msgs);
101 this.pktStats.addOutPacket(msgs.size());
102 } catch (RejectedExecutionException e) {
103 log.warn(e.getMessage());
104 if (!e.getMessage().contains(SHUTDOWN_MSG)) {
105 throw e;
106 }
107 }
108 }
109
110 @Override
111 public final boolean isConnected() {
112 return this.connected;
113 }
114
115 @Override
116 public final void setConnected(boolean connected) {
117 this.connected = connected;
118 };
119
120 @Override
121 public final void setChannel(Channel channel) {
122 this.channel = channel;
123 final SocketAddress address = channel.getRemoteAddress();
124 if (address instanceof InetSocketAddress) {
125 final InetSocketAddress inetAddress = (InetSocketAddress) address;
126 final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress());
127 if (ipAddress.isIp4()) {
128 channelId = ipAddress.toString() + ':' + inetAddress.getPort();
129 } else {
130 channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort();
131 }
132 }
133 };
134
135 @Override
136 public final Channel getChannel() {
137 return this.channel;
138 };
139
140 @Override
141 public String channelId() {
142 return channelId;
143 }
144
Shashikanth VH6de20d32015-10-09 12:04:13 +0530145 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530146 public BgpFactory factory() {
147 return BgpFactories.getFactory(sessionInfo.remoteBgpVersion());
Shashikanth VH6de20d32015-10-09 12:04:13 +0530148 }
149
150 @Override
151 public boolean isHandshakeComplete() {
152 return isHandShakeComplete;
153 }
154
155 @Override
156 public String toString() {
Shashikanth VH9f8afb42015-11-04 18:00:30 +0530157 return MoreObjects.toStringHelper(getClass()).omitNullValues()
158 .add("channel", channelId())
159 .add("bgpId", sessionInfo().remoteBgpId()).toString();
Shashikanth VH6de20d32015-10-09 12:04:13 +0530160 }
161}