2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.pcepio.protocol.ver1;
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;
29 * Provides PCEP messages.
31 public abstract class PcepMessageVer1 {
33 protected static final Logger log = LoggerFactory.getLogger(PcepFactories.class);
36 static final byte WIRE_VERSION = 1;
37 static final int MINIMUM_LENGTH = 4;
38 static final int PACKET_VERSION = 1;
39 static final byte OPEN_MSG_TYPE = 0x1;
40 static final byte KEEPALIVE_MSG_TYPE = 0x2;
41 static final byte REPORT_MSG_TYPE = 0xa;
42 static final byte TE_REPORT_MSG_TYPE = 0xe;
43 static final byte UPDATE_MSG_TYPE = 0xb;
44 static final byte INITIATE_MSG_TYPE = 0xc;
45 static final byte CLOSE_MSG_TYPE = 0x7;
46 static final byte ERROR_MSG_TYPE = 0x6;
47 static final byte LABEL_UPDATE_MSG_TYPE = 0xD;
48 static final byte LABEL_RANGE_RESV_MSG_TYPE = 0xF;
49 public static final int SHIFT_FLAG = 5;
50 static final int MINIMUM_COMMON_HEADER_LENGTH = 4;
52 public static final PcepMessageVer1.Reader READER = new Reader();
55 * Reader class for reading PCEP messages from channel buffer.
57 static class Reader implements PcepMessageReader<PcepMessage> {
59 public PcepMessage readFrom(ChannelBuffer cb) throws PcepParseException {
61 if (cb.readableBytes() < MINIMUM_LENGTH) {
62 throw new PcepParseException("Packet should have minimum length: " + MINIMUM_LENGTH);
66 int start = cb.readerIndex();
67 // fixed value property version == 1
68 byte version = cb.readByte();
69 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
70 if (version != (byte) PACKET_VERSION) {
71 throw new PcepParseException("Wrong version. Expected=PcepVersion.Message_1(1), got=" + version);
74 byte type = cb.readByte();
75 short length = cb.readShort();
76 cb.readerIndex(start);
81 log.debug("OPEN MESSAGE is received");
82 // message type value 1 means it is open message
83 return PcepOpenMsgVer1.READER.readFrom(cb.readBytes(length));
84 case KEEPALIVE_MSG_TYPE:
85 log.debug("KEEPALIVE MESSAGE is received");
86 // message type value 2 means it is Keepalive message
87 return PcepKeepaliveMsgVer1.READER.readFrom(cb.readBytes(length));
89 log.debug("ERROR MESSAGE is received");
90 // message type value 6 means it is error message
91 return PcepErrorMsgVer1.READER.readFrom(cb.readBytes(length));
93 log.debug("REPORT MESSAGE is received");
94 // message type value 10 means it is Report message
96 return PcepReportMsgVer1.READER.readFrom(cb.readBytes(length));
98 log.debug("UPDATE MESSAGE is received");
99 //message type value 11 means it is Update message
100 return PcepUpdateMsgVer1.READER.readFrom(cb.readBytes(length));
101 case INITIATE_MSG_TYPE:
102 log.debug("INITIATE MESSAGE is received");
103 //message type value 12 means it is PcInitiate message
104 return PcepInitiateMsgVer1.READER.readFrom(cb.readBytes(length));
106 log.debug("CLOSE MESSAGE is received");
107 // message type value 7 means it is Close message
108 return PcepCloseMsgVer1.READER.readFrom(cb.readBytes(length));
109 case TE_REPORT_MSG_TYPE:
110 log.debug("TE REPORT MESSAGE is received");
111 // message type value 14 means it is TE REPORT message
113 return PcepTEReportMsgVer1.READER.readFrom(cb.readBytes(length));
114 case LABEL_UPDATE_MSG_TYPE:
115 log.debug("LABEL UPDATE MESSAGE is received");
116 // message type value 13 means it is LABEL UPDATE message
118 return PcepLabelUpdateMsgVer1.READER.readFrom(cb.readBytes(length));
119 case LABEL_RANGE_RESV_MSG_TYPE:
120 log.debug("LABEL RANGE RESERVE MESSAGE is received");
121 // message type value 15 means it is LABEL RANGE RESERVE message
123 return PcepLabelRangeResvMsgVer1.READER.readFrom(cb.readBytes(length));
125 throw new PcepParseException("ERROR: UNKNOWN MESSAGE is received. Msg Type: " + type);
127 } catch (IndexOutOfBoundsException e) {
128 throw new PcepParseException(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_1);