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; |
| 27 | import org.onosproject.bgp.controller.BGPId; |
| 28 | import org.onosproject.bgp.controller.BGPPacketStats; |
| 29 | import org.onosproject.bgp.controller.BGPPeer; |
| 30 | import org.onosproject.bgpio.protocol.BGPMessage; |
| 31 | import org.onosproject.bgpio.protocol.BGPVersion; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import com.google.common.base.MoreObjects; |
| 36 | |
| 37 | /** |
| 38 | * BGPPeerImpl implements BGPPeer, maintains peer information and store updates in RIB . |
| 39 | */ |
| 40 | public class BGPPeerImpl implements BGPPeer { |
| 41 | |
| 42 | protected final Logger log = LoggerFactory.getLogger(BGPPeerImpl.class); |
| 43 | |
| 44 | private static final String SHUTDOWN_MSG = "Worker has already been shutdown"; |
| 45 | |
| 46 | private Channel channel; |
| 47 | protected String channelId; |
| 48 | private boolean connected; |
| 49 | protected boolean isHandShakeComplete = false; |
| 50 | public BGPSessionInfo sessionInfo; |
| 51 | private BGPPacketStatsImpl pktStats; |
| 52 | |
| 53 | @Override |
| 54 | public void init(BGPId bgpId, BGPVersion bgpVersion, BGPPacketStats pktStats) { |
| 55 | this.sessionInfo.setRemoteBgpId(bgpId); |
| 56 | this.sessionInfo.setRemoteBgpVersion(bgpVersion); |
| 57 | this.pktStats = (BGPPacketStatsImpl) pktStats; |
| 58 | this.sessionInfo = new BGPSessionInfo(); |
| 59 | } |
| 60 | |
| 61 | // ************************ |
| 62 | // Channel related |
| 63 | // ************************ |
| 64 | |
| 65 | @Override |
| 66 | public final void disconnectPeer() { |
| 67 | this.channel.close(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public final void sendMessage(BGPMessage m) { |
| 72 | log.debug("Sending message to {}", channel.getRemoteAddress()); |
| 73 | try { |
| 74 | channel.write(Collections.singletonList(m)); |
| 75 | this.pktStats.addOutPacket(); |
| 76 | } catch (RejectedExecutionException e) { |
| 77 | log.warn(e.getMessage()); |
| 78 | if (!e.getMessage().contains(SHUTDOWN_MSG)) { |
| 79 | throw e; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public final void sendMessage(List<BGPMessage> msgs) { |
| 86 | try { |
| 87 | channel.write(msgs); |
| 88 | this.pktStats.addOutPacket(msgs.size()); |
| 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 boolean isConnected() { |
| 99 | return this.connected; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public final void setConnected(boolean connected) { |
| 104 | this.connected = connected; |
| 105 | }; |
| 106 | |
| 107 | @Override |
| 108 | public final void setChannel(Channel channel) { |
| 109 | this.channel = channel; |
| 110 | final SocketAddress address = channel.getRemoteAddress(); |
| 111 | if (address instanceof InetSocketAddress) { |
| 112 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 113 | final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); |
| 114 | if (ipAddress.isIp4()) { |
| 115 | channelId = ipAddress.toString() + ':' + inetAddress.getPort(); |
| 116 | } else { |
| 117 | channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort(); |
| 118 | } |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | @Override |
| 123 | public final Channel getChannel() { |
| 124 | return this.channel; |
| 125 | }; |
| 126 | |
| 127 | @Override |
| 128 | public String channelId() { |
| 129 | return channelId; |
| 130 | } |
| 131 | |
| 132 | // ************************ |
| 133 | // BGP Peer features related |
| 134 | // ************************ |
| 135 | |
| 136 | @Override |
| 137 | public final BGPId getBGPId() { |
| 138 | return this.sessionInfo.getRemoteBgpId(); |
| 139 | }; |
| 140 | |
| 141 | @Override |
| 142 | public final String getStringId() { |
| 143 | return this.sessionInfo.getRemoteBgpId().toString(); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public final void setBgpPeerVersion(BGPVersion peerVersion) { |
| 148 | this.sessionInfo.setRemoteBgpVersion(peerVersion); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void setBgpPeerASNum(short peerASNum) { |
| 153 | this.sessionInfo.setRemoteBgpASNum(peerASNum); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void setBgpPeerHoldTime(short peerHoldTime) { |
| 158 | this.sessionInfo.setRemoteBgpHoldTime(peerHoldTime); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void setBgpPeerIdentifier(int peerIdentifier) { |
| 163 | this.sessionInfo.setRemoteBgpIdentifier(peerIdentifier); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public int getBgpPeerIdentifier() { |
| 168 | return this.sessionInfo.getRemoteBgpIdentifier(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public int getNegotiatedHoldTime() { |
| 173 | return this.sessionInfo.getNegotiatedholdTime(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void setNegotiatedHoldTime(short negotiatedHoldTime) { |
| 178 | this.sessionInfo.setNegotiatedholdTime(negotiatedHoldTime); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean isHandshakeComplete() { |
| 183 | return isHandShakeComplete; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public String toString() { |
| 188 | return MoreObjects.toStringHelper(getClass()).omitNullValues().add("channel", channelId()) |
| 189 | .add("bgpId", getBGPId()).toString(); |
| 190 | } |
| 191 | } |