Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.bgp.controller.impl; |
| 18 | |
| 19 | import java.net.InetSocketAddress; |
| 20 | import java.net.SocketAddress; |
| 21 | import java.util.Collections; |
| 22 | import java.util.List; |
| 23 | import java.util.concurrent.RejectedExecutionException; |
| 24 | |
| 25 | import org.jboss.netty.channel.Channel; |
| 26 | import org.onlab.packet.IpAddress; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 27 | import org.onosproject.bgp.controller.BgpController; |
| 28 | import org.onosproject.bgp.controller.BgpPeer; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 29 | import org.onosproject.bgp.controller.BgpSessionInfo; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 30 | import org.onosproject.bgpio.protocol.BgpFactories; |
| 31 | import org.onosproject.bgpio.protocol.BgpFactory; |
| 32 | import org.onosproject.bgpio.protocol.BgpMessage; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import com.google.common.base.MoreObjects; |
| 37 | |
| 38 | /** |
| 39 | * BGPPeerImpl implements BGPPeer, maintains peer information and store updates in RIB . |
| 40 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 41 | public class BgpPeerImpl implements BgpPeer { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 42 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 43 | protected final Logger log = LoggerFactory.getLogger(BgpPeerImpl.class); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 44 | |
| 45 | private static final String SHUTDOWN_MSG = "Worker has already been shutdown"; |
| 46 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 47 | private BgpController bgpController; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 48 | private Channel channel; |
| 49 | protected String channelId; |
| 50 | private boolean connected; |
| 51 | protected boolean isHandShakeComplete = false; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 52 | private BgpSessionInfo sessionInfo; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 53 | private BgpPacketStatsImpl pktStats; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 54 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 55 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 56 | @Override |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 57 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 68 | public BgpPeerImpl(BgpController bgpController, BgpSessionInfo sessionInfo, BgpPacketStatsImpl pktStats) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 69 | this.bgpController = bgpController; |
| 70 | this.sessionInfo = sessionInfo; |
| 71 | this.pktStats = pktStats; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // ************************ |
| 75 | // Channel related |
| 76 | // ************************ |
| 77 | |
| 78 | @Override |
| 79 | public final void disconnectPeer() { |
| 80 | this.channel.close(); |
| 81 | } |
| 82 | |
| 83 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 84 | public final void sendMessage(BgpMessage m) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 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 |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 98 | public final void sendMessage(List<BgpMessage> msgs) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 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 VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 145 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame^] | 146 | public BgpFactory factory() { |
| 147 | return BgpFactories.getFactory(sessionInfo.remoteBgpVersion()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public boolean isHandshakeComplete() { |
| 152 | return isHandShakeComplete; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public String toString() { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 157 | return MoreObjects.toStringHelper(getClass()).omitNullValues() |
| 158 | .add("channel", channelId()) |
| 159 | .add("bgpId", sessionInfo().remoteBgpId()).toString(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 160 | } |
| 161 | } |