blob: 45f746344420a0c83584d2af43f04681afa65514 [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 VH9f8afb42015-11-04 18:00:30 +053027import org.onosproject.bgp.controller.BGPController;
Shashikanth VH6de20d32015-10-09 12:04:13 +053028import org.onosproject.bgp.controller.BGPPeer;
Shashikanth VH9f8afb42015-11-04 18:00:30 +053029import org.onosproject.bgp.controller.BgpSessionInfo;
30import org.onosproject.bgpio.protocol.BGPFactories;
31import org.onosproject.bgpio.protocol.BGPFactory;
Shashikanth VH6de20d32015-10-09 12:04:13 +053032import 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 */
41public class BGPPeerImpl implements BGPPeer {
42
43 protected final Logger log = LoggerFactory.getLogger(BGPPeerImpl.class);
44
45 private static final String SHUTDOWN_MSG = "Worker has already been shutdown";
46
Shashikanth VH9f8afb42015-11-04 18:00:30 +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 VH6de20d32015-10-09 12:04:13 +053053 private BGPPacketStatsImpl pktStats;
54
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 */
68 public BGPPeerImpl(BGPController bgpController, BgpSessionInfo sessionInfo, BGPPacketStatsImpl pktStats) {
69 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
84 public final void sendMessage(BGPMessage m) {
85 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
98 public final void sendMessage(List<BGPMessage> msgs) {
99 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 VH9f8afb42015-11-04 18:00:30 +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}