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