SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 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.pcep.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; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 27 | import org.onosproject.pcep.controller.ClientCapability; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 28 | import org.onosproject.pcep.controller.PccId; |
| 29 | import org.onosproject.pcep.controller.PcepPacketStats; |
Avantika-Huawei | 7f7376a | 2016-05-11 17:07:50 +0530 | [diff] [blame] | 30 | import org.onosproject.pcep.controller.PcepSyncStatus; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 31 | import org.onosproject.pcep.controller.driver.PcepAgent; |
| 32 | import org.onosproject.pcep.controller.driver.PcepClientDriver; |
| 33 | import org.onosproject.pcepio.protocol.PcepFactories; |
| 34 | import org.onosproject.pcepio.protocol.PcepFactory; |
| 35 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 36 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import com.google.common.base.MoreObjects; |
| 41 | |
| 42 | /** |
| 43 | * An abstract representation of an OpenFlow switch. Can be extended by others |
| 44 | * to serve as a base for their vendor specific representation of a switch. |
| 45 | */ |
| 46 | public class PcepClientImpl implements PcepClientDriver { |
| 47 | |
| 48 | protected final Logger log = LoggerFactory.getLogger(PcepClientImpl.class); |
| 49 | |
| 50 | private static final String SHUTDOWN_MSG = "Worker has already been shutdown"; |
| 51 | |
| 52 | private Channel channel; |
| 53 | protected String channelId; |
| 54 | |
| 55 | private boolean connected; |
Avantika-Huawei | 7f7376a | 2016-05-11 17:07:50 +0530 | [diff] [blame] | 56 | protected boolean startDriverHandshakeCalled; |
| 57 | protected boolean isHandShakeComplete; |
| 58 | private PcepSyncStatus lspDbSyncStatus; |
| 59 | private PcepSyncStatus labelDbSyncStatus; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 60 | private PccId pccId; |
| 61 | private PcepAgent agent; |
| 62 | |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 63 | private ClientCapability capability; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 64 | private PcepVersion pcepVersion; |
| 65 | private byte keepAliveTime; |
| 66 | private byte deadTime; |
| 67 | private byte sessionId; |
| 68 | private PcepPacketStatsImpl pktStats; |
| 69 | |
| 70 | @Override |
| 71 | public void init(PccId pccId, PcepVersion pcepVersion, PcepPacketStats pktStats) { |
| 72 | this.pccId = pccId; |
| 73 | this.pcepVersion = pcepVersion; |
| 74 | this.pktStats = (PcepPacketStatsImpl) pktStats; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public final void disconnectClient() { |
| 79 | this.channel.close(); |
| 80 | } |
| 81 | |
| 82 | @Override |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 83 | public void setCapability(ClientCapability capability) { |
| 84 | this.capability = capability; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public ClientCapability capability() { |
| 89 | return capability; |
| 90 | } |
| 91 | |
| 92 | @Override |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 93 | public final void sendMessage(PcepMessage m) { |
| 94 | log.debug("Sending message to {}", channel.getRemoteAddress()); |
| 95 | try { |
| 96 | channel.write(Collections.singletonList(m)); |
| 97 | this.pktStats.addOutPacket(); |
| 98 | } catch (RejectedExecutionException e) { |
| 99 | log.warn(e.getMessage()); |
| 100 | if (!e.getMessage().contains(SHUTDOWN_MSG)) { |
| 101 | throw e; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public final void sendMessage(List<PcepMessage> msgs) { |
| 108 | try { |
| 109 | channel.write(msgs); |
| 110 | this.pktStats.addOutPacket(msgs.size()); |
| 111 | } catch (RejectedExecutionException e) { |
| 112 | log.warn(e.getMessage()); |
| 113 | if (!e.getMessage().contains(SHUTDOWN_MSG)) { |
| 114 | throw e; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public final boolean isConnected() { |
| 121 | return this.connected; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public final void setConnected(boolean connected) { |
| 126 | this.connected = connected; |
| 127 | }; |
| 128 | |
| 129 | @Override |
| 130 | public final void setChannel(Channel channel) { |
| 131 | this.channel = channel; |
| 132 | final SocketAddress address = channel.getRemoteAddress(); |
| 133 | if (address instanceof InetSocketAddress) { |
| 134 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 135 | final IpAddress ipAddress = IpAddress.valueOf(inetAddress.getAddress()); |
| 136 | if (ipAddress.isIp4()) { |
| 137 | channelId = ipAddress.toString() + ':' + inetAddress.getPort(); |
| 138 | } else { |
| 139 | channelId = '[' + ipAddress.toString() + "]:" + inetAddress.getPort(); |
| 140 | } |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | @Override |
| 145 | public String channelId() { |
| 146 | return channelId; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public final PccId getPccId() { |
| 151 | return this.pccId; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public final String getStringId() { |
| 156 | return this.pccId.toString(); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public final void setPcVersion(PcepVersion pcepVersion) { |
| 161 | this.pcepVersion = pcepVersion; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void setPcKeepAliveTime(byte keepAliveTime) { |
| 166 | this.keepAliveTime = keepAliveTime; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void setPcDeadTime(byte deadTime) { |
| 171 | this.deadTime = deadTime; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void setPcSessionId(byte sessionId) { |
| 176 | this.sessionId = sessionId; |
| 177 | } |
| 178 | |
| 179 | @Override |
Avantika-Huawei | 7f7376a | 2016-05-11 17:07:50 +0530 | [diff] [blame] | 180 | public void setLspDbSyncStatus(PcepSyncStatus syncStatus) { |
| 181 | this.lspDbSyncStatus = syncStatus; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
Avantika-Huawei | 7f7376a | 2016-05-11 17:07:50 +0530 | [diff] [blame] | 185 | public PcepSyncStatus lspDbSyncStatus() { |
| 186 | return lspDbSyncStatus; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void setLabelDbSyncStatus(PcepSyncStatus syncStatus) { |
| 191 | this.labelDbSyncStatus = syncStatus; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public PcepSyncStatus labelDbSyncStatus() { |
| 196 | return labelDbSyncStatus; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public final void handleMessage(PcepMessage m) { |
| 201 | this.pktStats.addInPacket(); |
| 202 | this.agent.processPcepMessage(pccId, m); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public final boolean connectClient() { |
| 207 | return this.agent.addConnectedClient(pccId, this); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public final void removeConnectedClient() { |
| 212 | this.agent.removeConnectedClient(pccId); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public PcepFactory factory() { |
| 217 | return PcepFactories.getFactory(pcepVersion); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public boolean isHandshakeComplete() { |
| 222 | return isHandShakeComplete; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public final void setAgent(PcepAgent ag) { |
| 227 | if (this.agent == null) { |
| 228 | this.agent = ag; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public boolean isOptical() { |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public String toString() { |
| 239 | return MoreObjects.toStringHelper(getClass()) |
| 240 | .add("channel", channelId()) |
| 241 | .add("pccId", getPccId()) |
| 242 | .toString(); |
| 243 | } |
| 244 | } |