bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
| 20 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 21 | import org.onosproject.pcepio.protocol.PcepFactories; |
| 22 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 23 | import org.onosproject.pcepio.protocol.PcepMessageReader; |
| 24 | import org.onosproject.pcepio.types.PcepErrorDetailInfo; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | public abstract class PcepMessageVer1 { |
| 29 | |
| 30 | protected static final Logger log = LoggerFactory.getLogger(PcepFactories.class); |
| 31 | |
| 32 | // version: 1.0 |
| 33 | static final byte WIRE_VERSION = 1; |
| 34 | static final int MINIMUM_LENGTH = 4; |
| 35 | static final int PACKET_VERSION = 1; |
| 36 | static final byte OPEN_MSG_TYPE = 0x1; |
| 37 | static final byte KEEPALIVE_MSG_TYPE = 0x2; |
| 38 | static final byte REPORT_MSG_TYPE = 0xa; |
| 39 | static final byte TE_REPORT_MSG_TYPE = 0xe; |
| 40 | static final byte UPDATE_MSG_TYPE = 0xb; |
| 41 | static final byte INITIATE_MSG_TYPE = 0xc; |
| 42 | static final byte CLOSE_MSG_TYPE = 0x7; |
| 43 | static final byte ERROR_MSG_TYPE = 0x6; |
| 44 | static final byte LABEL_UPDATE_MSG_TYPE = 0xD; |
bharat saraswal | e2e7a00 | 2015-08-21 22:47:30 +0530 | [diff] [blame^] | 45 | static final byte LABEL_RANGE_RESV_MSG_TYPE = 0xF; |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 46 | public static final int SHIFT_FLAG = 5; |
| 47 | static final int MINIMUM_COMMON_HEADER_LENGTH = 4; |
| 48 | |
| 49 | public static final PcepMessageVer1.Reader READER = new Reader(); |
| 50 | |
| 51 | static class Reader implements PcepMessageReader<PcepMessage> { |
| 52 | @Override |
| 53 | public PcepMessage readFrom(ChannelBuffer cb) throws PcepParseException { |
| 54 | |
| 55 | if (cb.readableBytes() < MINIMUM_LENGTH) { |
| 56 | throw new PcepParseException("Packet should have minimum length: " + MINIMUM_LENGTH); |
| 57 | } |
| 58 | |
| 59 | try { |
| 60 | int start = cb.readerIndex(); |
| 61 | // fixed value property version == 1 |
| 62 | byte version = cb.readByte(); |
| 63 | version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG); |
| 64 | if (version != (byte) PACKET_VERSION) { |
| 65 | throw new PcepParseException("Wrong version. Expected=PcepVersion.Message_1(1), got=" + version); |
| 66 | } |
| 67 | |
| 68 | byte type = cb.readByte(); |
| 69 | short length = cb.readShort(); |
| 70 | cb.readerIndex(start); |
| 71 | |
| 72 | switch (type) { |
| 73 | |
| 74 | case OPEN_MSG_TYPE: |
| 75 | log.debug("OPEN MESSAGE is received"); |
| 76 | // message type value 1 means it is open message |
Mahesh Poojary S | 1c356e3 | 2015-08-21 15:05:30 +0530 | [diff] [blame] | 77 | return PcepOpenMsgVer1.READER.readFrom(cb.readBytes(length)); |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 78 | case KEEPALIVE_MSG_TYPE: |
| 79 | log.debug("KEEPALIVE MESSAGE is received"); |
| 80 | // message type value 2 means it is Keepalive message |
| 81 | return PcepKeepaliveMsgVer1.READER.readFrom(cb.readBytes(length)); |
| 82 | case ERROR_MSG_TYPE: |
| 83 | log.debug("ERROR MESSAGE is received"); |
| 84 | // message type value 6 means it is error message |
Mahesh Poojary S | 1c356e3 | 2015-08-21 15:05:30 +0530 | [diff] [blame] | 85 | return PcepErrorMsgVer1.READER.readFrom(cb.readBytes(length)); |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 86 | case REPORT_MSG_TYPE: |
| 87 | log.debug("REPORT MESSAGE is received"); |
| 88 | // message type value 10 means it is Report message |
| 89 | // return |
bharat saraswal | e2e7a00 | 2015-08-21 22:47:30 +0530 | [diff] [blame^] | 90 | return PcepReportMsgVer1.READER.readFrom(cb.readBytes(length)); |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 91 | case UPDATE_MSG_TYPE: |
| 92 | log.debug("UPDATE MESSAGE is received"); |
| 93 | //message type value 11 means it is Update message |
| 94 | return PcepUpdateMsgVer1.READER.readFrom(cb.readBytes(length)); |
| 95 | case INITIATE_MSG_TYPE: |
| 96 | log.debug("INITIATE MESSAGE is received"); |
| 97 | //message type value 12 means it is PcInitiate message |
| 98 | return PcepInitiateMsgVer1.READER.readFrom(cb.readBytes(length)); |
| 99 | case CLOSE_MSG_TYPE: |
| 100 | log.debug("CLOSE MESSAGE is received"); |
| 101 | // message type value 7 means it is Close message |
| 102 | return PcepCloseMsgVer1.READER.readFrom(cb.readBytes(length)); |
| 103 | case TE_REPORT_MSG_TYPE: |
| 104 | log.debug("TE REPORT MESSAGE is received"); |
| 105 | // message type value 14 means it is TE REPORT message |
| 106 | // return |
bharat saraswal | e2e7a00 | 2015-08-21 22:47:30 +0530 | [diff] [blame^] | 107 | return PcepTEReportMsgVer1.READER.readFrom(cb.readBytes(length)); |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 108 | case LABEL_UPDATE_MSG_TYPE: |
| 109 | log.debug("LABEL UPDATE MESSAGE is received"); |
| 110 | // message type value 13 means it is LABEL UPDATE message |
| 111 | // return |
bharat saraswal | e2e7a00 | 2015-08-21 22:47:30 +0530 | [diff] [blame^] | 112 | return PcepLabelUpdateMsgVer1.READER.readFrom(cb.readBytes(length)); |
| 113 | case LABEL_RANGE_RESV_MSG_TYPE: |
| 114 | log.debug("LABEL RANGE RESERVE MESSAGE is received"); |
| 115 | // message type value 15 means it is LABEL RANGE RESERVE message |
| 116 | // return |
| 117 | return PcepLabelRangeResvMsgVer1.READER.readFrom(cb.readBytes(length)); |
bharat saraswal | f7364db | 2015-08-11 13:39:31 +0530 | [diff] [blame] | 118 | default: |
| 119 | throw new PcepParseException("ERROR: UNKNOWN MESSAGE is received. Msg Type: " + type); |
| 120 | } |
| 121 | } catch (IndexOutOfBoundsException e) { |
| 122 | throw new PcepParseException(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_1); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |