bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +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.pcepio.protocol.ver1; |
| 18 | |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 20 | import org.onosproject.pcepio.exceptions.PcepOutOfBoundMessageException; |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 21 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 22 | import org.onosproject.pcepio.protocol.PcepFactories; |
| 23 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 24 | import org.onosproject.pcepio.protocol.PcepMessageReader; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 25 | import org.onosproject.pcepio.protocol.PcepType; |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 26 | import org.onosproject.pcepio.types.PcepErrorDetailInfo; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 30 | /** |
| 31 | * Provides PCEP messages. |
| 32 | */ |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 33 | public abstract class PcepMessageVer1 { |
| 34 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame^] | 35 | private static final Logger log = LoggerFactory.getLogger(PcepFactories.class); |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 36 | |
| 37 | // version: 1.0 |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 38 | public static final byte WIRE_VERSION = 1; |
| 39 | public static final int MINIMUM_LENGTH = 4; |
| 40 | public static final int PACKET_VERSION = 1; |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 41 | public static final int SHIFT_FLAG = 5; |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 42 | public static final int MINIMUM_COMMON_HEADER_LENGTH = 4; |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 43 | |
| 44 | public static final PcepMessageVer1.Reader READER = new Reader(); |
| 45 | |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 46 | /** |
| 47 | * Reader class for reading PCEP messages from channel buffer. |
| 48 | */ |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 49 | static class Reader implements PcepMessageReader<PcepMessage> { |
| 50 | @Override |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 51 | public PcepMessage readFrom(ChannelBuffer cb) throws PcepParseException, PcepOutOfBoundMessageException { |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 52 | |
| 53 | if (cb.readableBytes() < MINIMUM_LENGTH) { |
| 54 | throw new PcepParseException("Packet should have minimum length: " + MINIMUM_LENGTH); |
| 55 | } |
| 56 | |
| 57 | try { |
| 58 | int start = cb.readerIndex(); |
| 59 | // fixed value property version == 1 |
| 60 | byte version = cb.readByte(); |
| 61 | version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG); |
| 62 | if (version != (byte) PACKET_VERSION) { |
| 63 | throw new PcepParseException("Wrong version. Expected=PcepVersion.Message_1(1), got=" + version); |
| 64 | } |
| 65 | |
| 66 | byte type = cb.readByte(); |
| 67 | short length = cb.readShort(); |
| 68 | cb.readerIndex(start); |
| 69 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 70 | // Check the out-of-bound message. |
| 71 | // If the message is out-of-bound then throw PcepOutOfBoundException. |
| 72 | if ((length - MINIMUM_COMMON_HEADER_LENGTH) > cb.readableBytes()) { |
| 73 | throw new PcepOutOfBoundMessageException("Message is out-of-bound."); |
| 74 | } |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 75 | |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 76 | if (type == (byte) PcepType.OPEN.getType()) { |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 77 | log.debug("OPEN MESSAGE is received"); |
Mahesh Poojary S | 1c356e3 | 2015-08-21 15:05:30 +0530 | [diff] [blame] | 78 | return PcepOpenMsgVer1.READER.readFrom(cb.readBytes(length)); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 79 | } else if (type == (byte) PcepType.KEEP_ALIVE.getType()) { |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 80 | log.debug("KEEPALIVE MESSAGE is received"); |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 81 | return PcepKeepaliveMsgVer1.READER.readFrom(cb.readBytes(length)); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 82 | } else if (type == (byte) PcepType.ERROR.getType()) { |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 83 | log.debug("ERROR MESSAGE is received"); |
Mahesh Poojary S | 1c356e3 | 2015-08-21 15:05:30 +0530 | [diff] [blame] | 84 | return PcepErrorMsgVer1.READER.readFrom(cb.readBytes(length)); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 85 | } else if (type == (byte) PcepType.CLOSE.getType()) { |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 86 | log.debug("CLOSE MESSAGE is received"); |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 87 | return PcepCloseMsgVer1.READER.readFrom(cb.readBytes(length)); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 88 | } else if (type == (byte) PcepType.REPORT.getType()) { |
| 89 | log.debug("REPORT MESSAGE is received"); |
| 90 | return PcepReportMsgVer1.READER.readFrom(cb.readBytes(length)); |
| 91 | } else if (type == (byte) PcepType.UPDATE.getType()) { |
| 92 | log.debug("UPDATE MESSAGE is received"); |
| 93 | return PcepUpdateMsgVer1.READER.readFrom(cb.readBytes(length)); |
| 94 | } else if (type == (byte) PcepType.INITIATE.getType()) { |
| 95 | log.debug("INITIATE MESSAGE is received"); |
| 96 | return PcepInitiateMsgVer1.READER.readFrom(cb.readBytes(length)); |
| 97 | } else if (type == (byte) PcepType.LS_REPORT.getType()) { |
| 98 | log.debug("LS REPORT MESSAGE is received"); |
| 99 | return PcepLSReportMsgVer1.READER.readFrom(cb.readBytes(length)); |
| 100 | } else if (type == (byte) PcepType.LABEL_RANGE_RESERV.getType()) { |
bharat saraswal | e2e7a00 | 2015-08-21 22:47:30 +0530 | [diff] [blame] | 101 | log.debug("LABEL RANGE RESERVE MESSAGE is received"); |
bharat saraswal | e2e7a00 | 2015-08-21 22:47:30 +0530 | [diff] [blame] | 102 | return PcepLabelRangeResvMsgVer1.READER.readFrom(cb.readBytes(length)); |
Mahesh Poojary S | f1bbd36 | 2016-02-25 18:19:59 +0530 | [diff] [blame] | 103 | } else if (type == (byte) PcepType.LABEL_UPDATE.getType()) { |
| 104 | log.debug("LABEL UPDATE MESSAGE is received"); |
| 105 | return PcepLabelUpdateMsgVer1.READER.readFrom(cb.readBytes(length)); |
| 106 | } else { |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 107 | throw new PcepParseException("ERROR: UNKNOWN MESSAGE is received. Msg Type: " + type); |
| 108 | } |
| 109 | } catch (IndexOutOfBoundsException e) { |
| 110 | throw new PcepParseException(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_1); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |