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