blob: a7c2901241f2faba922caec06d569b9eea93014a [file] [log] [blame]
bharat saraswalf7364db2015-08-11 13:39:31 +05301/*
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
17package org.onosproject.pcepio.protocol.ver1;
18
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.onosproject.pcepio.exceptions.PcepParseException;
21import org.onosproject.pcepio.protocol.PcepFactories;
22import org.onosproject.pcepio.protocol.PcepMessage;
23import org.onosproject.pcepio.protocol.PcepMessageReader;
24import org.onosproject.pcepio.types.PcepErrorDetailInfo;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28public 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;
45 public static final int SHIFT_FLAG = 5;
46 static final int MINIMUM_COMMON_HEADER_LENGTH = 4;
47
48 public static final PcepMessageVer1.Reader READER = new Reader();
49
50 static class Reader implements PcepMessageReader<PcepMessage> {
51 @Override
52 public PcepMessage readFrom(ChannelBuffer cb) throws PcepParseException {
53
54 if (cb.readableBytes() < MINIMUM_LENGTH) {
55 throw new PcepParseException("Packet should have minimum length: " + MINIMUM_LENGTH);
56 }
57
58 try {
59 int start = cb.readerIndex();
60 // fixed value property version == 1
61 byte version = cb.readByte();
62 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
63 if (version != (byte) PACKET_VERSION) {
64 throw new PcepParseException("Wrong version. Expected=PcepVersion.Message_1(1), got=" + version);
65 }
66
67 byte type = cb.readByte();
68 short length = cb.readShort();
69 cb.readerIndex(start);
70
71 switch (type) {
72
73 case OPEN_MSG_TYPE:
74 log.debug("OPEN MESSAGE is received");
75 // message type value 1 means it is open message
Mahesh Poojary S1c356e32015-08-21 15:05:30 +053076 return PcepOpenMsgVer1.READER.readFrom(cb.readBytes(length));
bharat saraswalf7364db2015-08-11 13:39:31 +053077 case KEEPALIVE_MSG_TYPE:
78 log.debug("KEEPALIVE MESSAGE is received");
79 // message type value 2 means it is Keepalive message
80 return PcepKeepaliveMsgVer1.READER.readFrom(cb.readBytes(length));
81 case ERROR_MSG_TYPE:
82 log.debug("ERROR MESSAGE is received");
83 // message type value 6 means it is error message
Mahesh Poojary S1c356e32015-08-21 15:05:30 +053084 return PcepErrorMsgVer1.READER.readFrom(cb.readBytes(length));
bharat saraswalf7364db2015-08-11 13:39:31 +053085 case REPORT_MSG_TYPE:
86 log.debug("REPORT MESSAGE is received");
87 // message type value 10 means it is Report message
88 // return
89 // TODO: Read Report message from channel buffer.
90 case UPDATE_MSG_TYPE:
91 log.debug("UPDATE MESSAGE is received");
92 //message type value 11 means it is Update message
93 return PcepUpdateMsgVer1.READER.readFrom(cb.readBytes(length));
94 case INITIATE_MSG_TYPE:
95 log.debug("INITIATE MESSAGE is received");
96 //message type value 12 means it is PcInitiate message
97 return PcepInitiateMsgVer1.READER.readFrom(cb.readBytes(length));
98 case CLOSE_MSG_TYPE:
99 log.debug("CLOSE MESSAGE is received");
100 // message type value 7 means it is Close message
101 return PcepCloseMsgVer1.READER.readFrom(cb.readBytes(length));
102 case TE_REPORT_MSG_TYPE:
103 log.debug("TE REPORT MESSAGE is received");
104 // message type value 14 means it is TE REPORT message
105 // return
106 // TODO: Read TE Report message from channel buffer.
107 case LABEL_UPDATE_MSG_TYPE:
108 log.debug("LABEL UPDATE MESSAGE is received");
109 // message type value 13 means it is LABEL UPDATE message
110 // return
111 // TODO: Read Label update message from channel buffer.
112 default:
113 throw new PcepParseException("ERROR: UNKNOWN MESSAGE is received. Msg Type: " + type);
114 }
115 } catch (IndexOutOfBoundsException e) {
116 throw new PcepParseException(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_1);
117 }
118 }
119 }
120}