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.io.IOException; |
| 20 | import java.net.InetSocketAddress; |
| 21 | import java.net.SocketAddress; |
| 22 | import java.nio.channels.ClosedChannelException; |
| 23 | import java.util.Collections; |
| 24 | import java.util.Date; |
| 25 | import java.util.LinkedList; |
| 26 | import java.util.List; |
| 27 | import java.util.ListIterator; |
| 28 | import java.util.concurrent.RejectedExecutionException; |
| 29 | |
| 30 | import org.jboss.netty.channel.Channel; |
| 31 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 32 | import org.jboss.netty.channel.ChannelStateEvent; |
| 33 | import org.jboss.netty.channel.ExceptionEvent; |
| 34 | import org.jboss.netty.channel.MessageEvent; |
| 35 | import org.jboss.netty.handler.timeout.IdleState; |
| 36 | import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; |
| 37 | import org.jboss.netty.handler.timeout.IdleStateEvent; |
| 38 | import org.jboss.netty.handler.timeout.IdleStateHandler; |
| 39 | import org.jboss.netty.handler.timeout.ReadTimeoutException; |
| 40 | import org.onlab.packet.IpAddress; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 41 | import org.onosproject.pcep.controller.ClientCapability; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 42 | import org.onosproject.pcep.controller.PccId; |
| 43 | import org.onosproject.pcep.controller.driver.PcepClientDriver; |
| 44 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 45 | import org.onosproject.pcepio.protocol.PcepError; |
| 46 | import org.onosproject.pcepio.protocol.PcepErrorInfo; |
| 47 | import org.onosproject.pcepio.protocol.PcepErrorMsg; |
| 48 | import org.onosproject.pcepio.protocol.PcepErrorObject; |
| 49 | import org.onosproject.pcepio.protocol.PcepFactory; |
| 50 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 51 | import org.onosproject.pcepio.protocol.PcepOpenMsg; |
| 52 | import org.onosproject.pcepio.protocol.PcepOpenObject; |
| 53 | import org.onosproject.pcepio.protocol.PcepType; |
| 54 | import org.onosproject.pcepio.protocol.PcepVersion; |
Avantika-Huawei | 56c1184 | 2016-04-28 00:56:56 +0530 | [diff] [blame] | 55 | import org.onosproject.pcepio.types.IPv4RouterIdOfLocalNodeSubTlv; |
| 56 | import org.onosproject.pcepio.types.NodeAttributesTlv; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 57 | import org.onosproject.pcepio.types.PceccCapabilityTlv; |
| 58 | import org.onosproject.pcepio.types.StatefulPceCapabilityTlv; |
| 59 | import org.onosproject.pcepio.types.PcepErrorDetailInfo; |
| 60 | import org.onosproject.pcepio.types.PcepValueType; |
| 61 | import org.slf4j.Logger; |
| 62 | import org.slf4j.LoggerFactory; |
| 63 | |
| 64 | /** |
| 65 | * Channel handler deals with the pcc client connection and dispatches |
| 66 | * messages from client to the appropriate locations. |
| 67 | */ |
| 68 | class PcepChannelHandler extends IdleStateAwareChannelHandler { |
| 69 | static final byte DEADTIMER_MAXIMUM_VALUE = (byte) 0xFF; |
| 70 | static final byte KEEPALIVE_MULTIPLE_FOR_DEADTIMER = 4; |
| 71 | private static final Logger log = LoggerFactory.getLogger(PcepChannelHandler.class); |
| 72 | private final Controller controller; |
| 73 | private PcepClientDriver pc; |
| 74 | private PccId thispccId; |
| 75 | private Channel channel; |
| 76 | private byte sessionId = 0; |
| 77 | private byte keepAliveTime; |
| 78 | private byte deadTime; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 79 | private ClientCapability capability; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 80 | private PcepPacketStatsImpl pcepPacketStats; |
| 81 | static final int MAX_WRONG_COUNT_PACKET = 5; |
| 82 | static final int BYTE_MASK = 0xFF; |
| 83 | |
| 84 | // State needs to be volatile because the HandshakeTimeoutHandler |
| 85 | // needs to check if the handshake is complete |
| 86 | private volatile ChannelState state; |
| 87 | |
| 88 | // When a pcc client with a ip addresss is found (i.e we already have a |
| 89 | // connected client with the same ip), the new client is immediately |
| 90 | // disconnected. At that point netty callsback channelDisconnected() which |
| 91 | // proceeds to cleaup client state - we need to ensure that it does not cleanup |
| 92 | // client state for the older (still connected) client |
| 93 | private volatile Boolean duplicatePccIdFound; |
| 94 | |
| 95 | //Indicates the pcep version used by this pcc client |
| 96 | protected PcepVersion pcepVersion; |
| 97 | protected PcepFactory factory1; |
| 98 | |
| 99 | /** |
| 100 | * Create a new unconnected PcepChannelHandler. |
| 101 | * @param controller parent controller |
| 102 | */ |
| 103 | PcepChannelHandler(Controller controller) { |
| 104 | this.controller = controller; |
| 105 | this.state = ChannelState.INIT; |
| 106 | factory1 = controller.getPcepMessageFactory1(); |
| 107 | duplicatePccIdFound = Boolean.FALSE; |
| 108 | pcepPacketStats = new PcepPacketStatsImpl(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * To disconnect a PCC. |
| 113 | */ |
| 114 | public void disconnectClient() { |
| 115 | pc.disconnectClient(); |
| 116 | } |
| 117 | |
| 118 | //************************* |
| 119 | // Channel State Machine |
| 120 | //************************* |
| 121 | |
| 122 | /** |
| 123 | * The state machine for handling the client/channel state. All state |
| 124 | * transitions should happen from within the state machine (and not from other |
| 125 | * parts of the code) |
| 126 | */ |
| 127 | enum ChannelState { |
| 128 | /** |
| 129 | * Initial state before channel is connected. |
| 130 | */ |
| 131 | INIT(false) { |
| 132 | |
| 133 | }, |
| 134 | /** |
| 135 | * Once the session is established, wait for open message. |
| 136 | */ |
| 137 | OPENWAIT(false) { |
| 138 | @Override |
| 139 | void processPcepMessage(PcepChannelHandler h, PcepMessage m) throws IOException, PcepParseException { |
| 140 | |
Avantika-Huawei | 56c1184 | 2016-04-28 00:56:56 +0530 | [diff] [blame] | 141 | log.info("Message received in OPEN WAIT State"); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 142 | |
| 143 | //check for open message |
| 144 | if (m.getType() != PcepType.OPEN) { |
| 145 | // When the message type is not open message increment the wrong packet statistics |
| 146 | h.processUnknownMsg(); |
| 147 | log.debug("message is not OPEN message"); |
| 148 | } else { |
| 149 | |
| 150 | h.pcepPacketStats.addInPacket(); |
| 151 | PcepOpenMsg pOpenmsg = (PcepOpenMsg) m; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 152 | //Do Capability negotiation. |
| 153 | h.capabilityNegotiation(pOpenmsg); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 154 | log.debug("Sending handshake OPEN message"); |
| 155 | h.sessionId = pOpenmsg.getPcepOpenObject().getSessionId(); |
| 156 | h.pcepVersion = pOpenmsg.getPcepOpenObject().getVersion(); |
| 157 | |
| 158 | //setting keepalive and deadTimer |
| 159 | byte yKeepalive = pOpenmsg.getPcepOpenObject().getKeepAliveTime(); |
| 160 | byte yDeadTimer = pOpenmsg.getPcepOpenObject().getDeadTime(); |
| 161 | h.keepAliveTime = yKeepalive; |
| 162 | if (yKeepalive < yDeadTimer) { |
| 163 | h.deadTime = yDeadTimer; |
| 164 | } else { |
| 165 | if (DEADTIMER_MAXIMUM_VALUE > (yKeepalive * KEEPALIVE_MULTIPLE_FOR_DEADTIMER)) { |
| 166 | h.deadTime = (byte) (yKeepalive * KEEPALIVE_MULTIPLE_FOR_DEADTIMER); |
| 167 | } else { |
| 168 | h.deadTime = DEADTIMER_MAXIMUM_VALUE; |
| 169 | } |
| 170 | } |
Avantika-Huawei | 56c1184 | 2016-04-28 00:56:56 +0530 | [diff] [blame] | 171 | |
| 172 | LinkedList<PcepValueType> optionalTlvs = pOpenmsg.getPcepOpenObject().getOptionalTlv(); |
| 173 | for (PcepValueType optionalTlv : optionalTlvs) { |
| 174 | if (optionalTlv instanceof NodeAttributesTlv) { |
| 175 | List<PcepValueType> subTlvs = ((NodeAttributesTlv) optionalTlv) |
| 176 | .getllNodeAttributesSubTLVs(); |
| 177 | for (PcepValueType subTlv : subTlvs) { |
| 178 | if (subTlv instanceof IPv4RouterIdOfLocalNodeSubTlv) { |
| 179 | h.thispccId = PccId.pccId(IpAddress |
| 180 | .valueOf(((IPv4RouterIdOfLocalNodeSubTlv) subTlv).getInt())); |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (h.thispccId == null) { |
| 189 | final SocketAddress address = h.channel.getRemoteAddress(); |
| 190 | if (!(address instanceof InetSocketAddress)) { |
| 191 | throw new IOException("Invalid client connection. Pcc is indentifed based on IP"); |
| 192 | } |
| 193 | |
| 194 | final InetSocketAddress inetAddress = (InetSocketAddress) address; |
| 195 | h.thispccId = PccId.pccId(IpAddress.valueOf(inetAddress.getAddress())); |
| 196 | } |
| 197 | |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 198 | h.sendHandshakeOpenMessage(); |
| 199 | h.pcepPacketStats.addOutPacket(); |
| 200 | h.setState(KEEPWAIT); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | }, |
| 204 | /** |
| 205 | * Once the open messages are exchanged, wait for keep alive message. |
| 206 | */ |
| 207 | KEEPWAIT(false) { |
| 208 | @Override |
| 209 | void processPcepMessage(PcepChannelHandler h, PcepMessage m) throws IOException, PcepParseException { |
Avantika-Huawei | 56c1184 | 2016-04-28 00:56:56 +0530 | [diff] [blame] | 210 | log.info("message received in KEEPWAIT state"); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 211 | //check for keep alive message |
| 212 | if (m.getType() != PcepType.KEEP_ALIVE) { |
| 213 | // When the message type is not keep alive message increment the wrong packet statistics |
| 214 | h.processUnknownMsg(); |
Avantika-Huawei | 56c1184 | 2016-04-28 00:56:56 +0530 | [diff] [blame] | 215 | log.error("message is not KEEPALIVE message"); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 216 | } else { |
| 217 | // Set the client connected status |
| 218 | h.pcepPacketStats.addInPacket(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 219 | log.debug("sending keep alive message in KEEPWAIT state"); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 220 | h.pc = h.controller.getPcepClientInstance(h.thispccId, h.sessionId, h.pcepVersion, |
| 221 | h.pcepPacketStats); |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 222 | //Get pc instance and set capabilities |
| 223 | h.pc.setCapability(h.capability); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 224 | // set the status of pcc as connected |
| 225 | h.pc.setConnected(true); |
| 226 | h.pc.setChannel(h.channel); |
| 227 | |
| 228 | // set any other specific parameters to the pcc |
| 229 | h.pc.setPcVersion(h.pcepVersion); |
| 230 | h.pc.setPcSessionId(h.sessionId); |
| 231 | h.pc.setPcKeepAliveTime(h.keepAliveTime); |
| 232 | h.pc.setPcDeadTime(h.deadTime); |
| 233 | int keepAliveTimer = h.keepAliveTime & BYTE_MASK; |
| 234 | int deadTimer = h.deadTime & BYTE_MASK; |
| 235 | if (0 == h.keepAliveTime) { |
| 236 | h.deadTime = 0; |
| 237 | } |
| 238 | // handle keep alive and dead time |
| 239 | if (keepAliveTimer != PcepPipelineFactory.DEFAULT_KEEP_ALIVE_TIME |
| 240 | || deadTimer != PcepPipelineFactory.DEFAULT_DEAD_TIME) { |
| 241 | |
| 242 | h.channel.getPipeline().replace("idle", "idle", |
| 243 | new IdleStateHandler(PcepPipelineFactory.TIMER, deadTimer, keepAliveTimer, 0)); |
| 244 | } |
| 245 | log.debug("Dead timer : " + deadTimer); |
| 246 | log.debug("Keep alive time : " + keepAliveTimer); |
| 247 | |
| 248 | //set the state handshake completion. |
| 249 | h.sendKeepAliveMessage(); |
| 250 | h.pcepPacketStats.addOutPacket(); |
| 251 | h.setHandshakeComplete(true); |
| 252 | |
| 253 | if (!h.pc.connectClient()) { |
| 254 | disconnectDuplicate(h); |
| 255 | } else { |
| 256 | h.setState(ESTABLISHED); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | }, |
| 261 | /** |
| 262 | * Once the keep alive messages are exchanged, the state is established. |
| 263 | */ |
| 264 | ESTABLISHED(true) { |
| 265 | @Override |
| 266 | void processPcepMessage(PcepChannelHandler h, PcepMessage m) throws IOException, PcepParseException { |
| 267 | |
| 268 | //h.channel.getPipeline().remove("waittimeout"); |
| 269 | log.debug("Message received in established state " + m.getType()); |
| 270 | //dispatch the message |
| 271 | h.dispatchMessage(m); |
| 272 | } |
| 273 | }; |
| 274 | private boolean handshakeComplete; |
| 275 | |
| 276 | ChannelState(boolean handshakeComplete) { |
| 277 | this.handshakeComplete = handshakeComplete; |
| 278 | } |
| 279 | |
| 280 | void processPcepMessage(PcepChannelHandler h, PcepMessage m) throws IOException, PcepParseException { |
| 281 | // do nothing |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Is this a state in which the handshake has completed. |
| 286 | * |
| 287 | * @return true if the handshake is complete |
| 288 | */ |
| 289 | public boolean isHandshakeComplete() { |
| 290 | return this.handshakeComplete; |
| 291 | } |
| 292 | |
| 293 | protected void disconnectDuplicate(PcepChannelHandler h) { |
| 294 | log.error("Duplicated Pcc IP or incompleted cleanup - " + "disconnecting channel {}", |
| 295 | h.getClientInfoString()); |
| 296 | h.duplicatePccIdFound = Boolean.TRUE; |
| 297 | h.channel.disconnect(); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Sets handshake complete status. |
| 302 | * |
| 303 | * @param handshakeComplete status of handshake |
| 304 | */ |
| 305 | public void setHandshakeComplete(boolean handshakeComplete) { |
| 306 | this.handshakeComplete = handshakeComplete; |
| 307 | } |
| 308 | |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { |
| 313 | channel = e.getChannel(); |
| 314 | log.info("PCC connected from {}", channel.getRemoteAddress()); |
| 315 | |
| 316 | // Wait for open message from pcc client |
| 317 | setState(ChannelState.OPENWAIT); |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { |
| 322 | log.info("Pcc disconnected callback for pc:{}. Cleaning up ...", getClientInfoString()); |
| 323 | if (thispccId != null) { |
| 324 | if (!duplicatePccIdFound) { |
| 325 | // if the disconnected client (on this ChannelHandler) |
| 326 | // was not one with a duplicate-dpid, it is safe to remove all |
| 327 | // state for it at the controller. Notice that if the disconnected |
| 328 | // client was a duplicate-ip, calling the method below would clear |
| 329 | // all state for the original client (with the same ip), |
| 330 | // which we obviously don't want. |
| 331 | log.debug("{}:removal called", getClientInfoString()); |
| 332 | if (pc != null) { |
| 333 | pc.removeConnectedClient(); |
| 334 | } |
| 335 | } else { |
| 336 | // A duplicate was disconnected on this ChannelHandler, |
| 337 | // this is the same client reconnecting, but the original state was |
| 338 | // not cleaned up - XXX check liveness of original ChannelHandler |
| 339 | log.debug("{}:duplicate found", getClientInfoString()); |
| 340 | duplicatePccIdFound = Boolean.FALSE; |
| 341 | } |
| 342 | } else { |
| 343 | log.warn("no pccip in channelHandler registered for " + "disconnected client {}", getClientInfoString()); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { |
| 349 | PcepErrorMsg errMsg; |
| 350 | log.info("exceptionCaught: " + e.toString()); |
| 351 | |
| 352 | if (e.getCause() instanceof ReadTimeoutException) { |
| 353 | if (ChannelState.OPENWAIT == state) { |
| 354 | // When ReadTimeout timer is expired in OPENWAIT state, it is considered |
| 355 | // OpenWait timer. |
| 356 | errMsg = getErrorMsg(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_2); |
| 357 | log.debug("Sending PCEP-ERROR message to PCC."); |
| 358 | channel.write(Collections.singletonList(errMsg)); |
| 359 | channel.close(); |
| 360 | state = ChannelState.INIT; |
| 361 | return; |
| 362 | } else if (ChannelState.KEEPWAIT == state) { |
| 363 | // When ReadTimeout timer is expired in KEEPWAIT state, is is considered |
| 364 | // KeepWait timer. |
| 365 | errMsg = getErrorMsg(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_7); |
| 366 | log.debug("Sending PCEP-ERROR message to PCC."); |
| 367 | channel.write(Collections.singletonList(errMsg)); |
| 368 | channel.close(); |
| 369 | state = ChannelState.INIT; |
| 370 | return; |
| 371 | } |
| 372 | } else if (e.getCause() instanceof ClosedChannelException) { |
| 373 | log.debug("Channel for pc {} already closed", getClientInfoString()); |
| 374 | } else if (e.getCause() instanceof IOException) { |
| 375 | log.error("Disconnecting client {} due to IO Error: {}", getClientInfoString(), e.getCause().getMessage()); |
| 376 | if (log.isDebugEnabled()) { |
| 377 | // still print stack trace if debug is enabled |
| 378 | log.debug("StackTrace for previous Exception: ", e.getCause()); |
| 379 | } |
| 380 | channel.close(); |
| 381 | } else if (e.getCause() instanceof PcepParseException) { |
| 382 | PcepParseException errMsgParse = (PcepParseException) e.getCause(); |
| 383 | byte errorType = errMsgParse.getErrorType(); |
| 384 | byte errorValue = errMsgParse.getErrorValue(); |
| 385 | |
| 386 | if ((errorType == (byte) 0x0) && (errorValue == (byte) 0x0)) { |
| 387 | processUnknownMsg(); |
| 388 | } else { |
| 389 | errMsg = getErrorMsg(errorType, errorValue); |
| 390 | log.debug("Sending PCEP-ERROR message to PCC."); |
| 391 | channel.write(Collections.singletonList(errMsg)); |
| 392 | } |
| 393 | } else if (e.getCause() instanceof RejectedExecutionException) { |
| 394 | log.warn("Could not process message: queue full"); |
| 395 | } else { |
| 396 | log.error("Error while processing message from client " + getClientInfoString() + "state " + this.state); |
| 397 | channel.close(); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public String toString() { |
| 403 | return getClientInfoString(); |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception { |
| 408 | if (!isHandshakeComplete()) { |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | if (e.getState() == IdleState.READER_IDLE) { |
| 413 | // When no message is received on channel for read timeout, then close |
| 414 | // the channel |
| 415 | log.info("Disconnecting client {} due to read timeout", getClientInfoString()); |
| 416 | ctx.getChannel().close(); |
| 417 | } else if (e.getState() == IdleState.WRITER_IDLE) { |
| 418 | // Send keep alive message |
| 419 | log.debug("Sending keep alive message due to IdleState timeout " + pc.toString()); |
| 420 | pc.sendMessage(Collections.singletonList(pc.factory().buildKeepaliveMsg().build())); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { |
| 426 | if (e.getMessage() instanceof List) { |
| 427 | @SuppressWarnings("unchecked") |
| 428 | List<PcepMessage> msglist = (List<PcepMessage>) e.getMessage(); |
| 429 | for (PcepMessage pm : msglist) { |
| 430 | // Do the actual packet processing |
| 431 | state.processPcepMessage(this, pm); |
| 432 | } |
| 433 | } else { |
| 434 | state.processPcepMessage(this, (PcepMessage) e.getMessage()); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * To set the handshake status. |
| 440 | * |
| 441 | * @param handshakeComplete value is handshake status |
| 442 | */ |
| 443 | public void setHandshakeComplete(boolean handshakeComplete) { |
| 444 | this.state.setHandshakeComplete(handshakeComplete); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Is this a state in which the handshake has completed. |
| 449 | * |
| 450 | * @return true if the handshake is complete |
| 451 | */ |
| 452 | public boolean isHandshakeComplete() { |
| 453 | return this.state.isHandshakeComplete(); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * To handle the pcep message. |
| 458 | * |
| 459 | * @param m pcep message |
| 460 | */ |
| 461 | private void dispatchMessage(PcepMessage m) { |
| 462 | pc.handleMessage(m); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Return a string describing this client based on the already available |
| 467 | * information (ip address and/or remote socket). |
| 468 | * |
| 469 | * @return display string |
| 470 | */ |
| 471 | private String getClientInfoString() { |
| 472 | if (pc != null) { |
| 473 | return pc.toString(); |
| 474 | } |
| 475 | String channelString; |
| 476 | if (channel == null || channel.getRemoteAddress() == null) { |
| 477 | channelString = "?"; |
| 478 | } else { |
| 479 | channelString = channel.getRemoteAddress().toString(); |
| 480 | } |
| 481 | String pccIpString; |
| 482 | // TODO : implement functionality to get pcc id string |
| 483 | pccIpString = "?"; |
| 484 | return String.format("[%s PCCIP[%s]]", channelString, pccIpString); |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Update the channels state. Only called from the state machine. |
| 489 | * |
| 490 | * @param state |
| 491 | */ |
| 492 | private void setState(ChannelState state) { |
| 493 | this.state = state; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Send handshake open message. |
| 498 | * |
| 499 | * @throws IOException,PcepParseException |
| 500 | */ |
| 501 | private void sendHandshakeOpenMessage() throws IOException, PcepParseException { |
| 502 | PcepOpenObject pcepOpenobj = factory1.buildOpenObject() |
| 503 | .setSessionId(sessionId) |
| 504 | .setKeepAliveTime(keepAliveTime) |
| 505 | .setDeadTime(deadTime) |
| 506 | .build(); |
| 507 | PcepMessage msg = factory1.buildOpenMsg() |
| 508 | .setPcepOpenObj(pcepOpenobj) |
| 509 | .build(); |
| 510 | log.debug("Sending OPEN message to {}", channel.getRemoteAddress()); |
| 511 | channel.write(Collections.singletonList(msg)); |
| 512 | } |
| 513 | |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 514 | //Capability negotiation |
| 515 | private void capabilityNegotiation(PcepOpenMsg pOpenmsg) { |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 516 | LinkedList<PcepValueType> tlvList = pOpenmsg.getPcepOpenObject().getOptionalTlv(); |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 517 | boolean pceccCapability = false; |
| 518 | boolean statefulPceCapability = false; |
| 519 | boolean pcInstantiationCapability = false; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 520 | |
| 521 | ListIterator<PcepValueType> listIterator = tlvList.listIterator(); |
| 522 | while (listIterator.hasNext()) { |
| 523 | PcepValueType tlv = listIterator.next(); |
| 524 | |
| 525 | switch (tlv.getType()) { |
| 526 | case PceccCapabilityTlv.TYPE: |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 527 | pceccCapability = true; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 528 | break; |
| 529 | case StatefulPceCapabilityTlv.TYPE: |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 530 | statefulPceCapability = true; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 531 | StatefulPceCapabilityTlv stetefulPcCapTlv = (StatefulPceCapabilityTlv) tlv; |
| 532 | if (stetefulPcCapTlv.getIFlag()) { |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 533 | pcInstantiationCapability = true; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 534 | } |
| 535 | break; |
| 536 | default: |
| 537 | continue; |
| 538 | } |
| 539 | } |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 540 | this.capability = new ClientCapability(pceccCapability, statefulPceCapability, pcInstantiationCapability); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Send keep alive message. |
| 545 | * |
| 546 | * @throws IOException when channel is disconnected |
| 547 | * @throws PcepParseException while building keep alive message |
| 548 | */ |
| 549 | private void sendKeepAliveMessage() throws IOException, PcepParseException { |
| 550 | PcepMessage msg = factory1.buildKeepaliveMsg().build(); |
| 551 | log.debug("Sending KEEPALIVE message to {}", channel.getRemoteAddress()); |
| 552 | channel.write(Collections.singletonList(msg)); |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Send error message and close channel with pcc. |
| 557 | */ |
| 558 | private void sendErrMsgAndCloseChannel() { |
| 559 | // TODO send error message |
| 560 | channel.close(); |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Send error message when an invalid message is received. |
| 565 | * |
| 566 | * @throws PcepParseException while building error message |
| 567 | */ |
| 568 | private void sendErrMsgForInvalidMsg() throws PcepParseException { |
| 569 | byte errorType = 0x02; |
| 570 | byte errorValue = 0x00; |
| 571 | PcepErrorMsg errMsg = getErrorMsg(errorType, errorValue); |
| 572 | channel.write(Collections.singletonList(errMsg)); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Builds pcep error message based on error value and error type. |
| 577 | * |
| 578 | * @param errorType pcep error type |
| 579 | * @param errorValue pcep error value |
| 580 | * @return pcep error message |
| 581 | * @throws PcepParseException while bulding error message |
| 582 | */ |
| 583 | public PcepErrorMsg getErrorMsg(byte errorType, byte errorValue) throws PcepParseException { |
Sho SHIMIZU | 9b8274c | 2015-09-04 15:54:24 -0700 | [diff] [blame] | 584 | LinkedList<PcepErrorObject> llerrObj = new LinkedList<>(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 585 | PcepErrorMsg errMsg; |
| 586 | |
| 587 | PcepErrorObject errObj = factory1.buildPcepErrorObject() |
| 588 | .setErrorValue(errorValue) |
| 589 | .setErrorType(errorType) |
| 590 | .build(); |
| 591 | |
| 592 | llerrObj.add(errObj); |
| 593 | |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 594 | //If Error caught in other than Openmessage |
Sho SHIMIZU | 9b8274c | 2015-09-04 15:54:24 -0700 | [diff] [blame] | 595 | LinkedList<PcepError> llPcepErr = new LinkedList<>(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 596 | |
| 597 | PcepError pcepErr = factory1.buildPcepError() |
| 598 | .setErrorObjList(llerrObj) |
| 599 | .build(); |
| 600 | |
| 601 | llPcepErr.add(pcepErr); |
| 602 | |
| 603 | PcepErrorInfo errInfo = factory1.buildPcepErrorInfo() |
| 604 | .setPcepErrorList(llPcepErr) |
| 605 | .build(); |
| 606 | |
| 607 | errMsg = factory1.buildPcepErrorMsg() |
| 608 | .setPcepErrorInfo(errInfo) |
| 609 | .build(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 610 | return errMsg; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Process unknown pcep message received. |
| 615 | * |
| 616 | * @throws PcepParseException while building pcep error message |
| 617 | */ |
| 618 | public void processUnknownMsg() throws PcepParseException { |
| 619 | Date now = null; |
| 620 | if (pcepPacketStats.wrongPacketCount() == 0) { |
| 621 | now = new Date(); |
| 622 | pcepPacketStats.setTime(now.getTime()); |
| 623 | pcepPacketStats.addWrongPacket(); |
| 624 | sendErrMsgForInvalidMsg(); |
| 625 | } |
| 626 | |
| 627 | if (pcepPacketStats.wrongPacketCount() > 1) { |
| 628 | Date lastest = new Date(); |
| 629 | pcepPacketStats.addWrongPacket(); |
| 630 | //converting to seconds |
| 631 | if (((lastest.getTime() - pcepPacketStats.getTime()) / 1000) > 60) { |
| 632 | now = lastest; |
| 633 | pcepPacketStats.setTime(now.getTime()); |
| 634 | pcepPacketStats.resetWrongPacket(); |
| 635 | pcepPacketStats.addWrongPacket(); |
| 636 | } else if (((int) (lastest.getTime() - now.getTime()) / 1000) < 60) { |
| 637 | if (MAX_WRONG_COUNT_PACKET <= pcepPacketStats.wrongPacketCount()) { |
| 638 | //reset once wrong packet count reaches MAX_WRONG_COUNT_PACKET |
| 639 | pcepPacketStats.resetWrongPacket(); |
| 640 | // max wrong packets received send error message and close the session |
| 641 | sendErrMsgAndCloseChannel(); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | } |