2169a673fe75a06cd3e5058aa9c93d9be24ea6bd
[onosfw.git] /
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 /**
29  * Provides PCEP messages.
30  */
31 public abstract class PcepMessageVer1 {
32
33     protected static final Logger log = LoggerFactory.getLogger(PcepFactories.class);
34
35     // version: 1.0
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;
51
52     public static final PcepMessageVer1.Reader READER = new Reader();
53
54     /**
55      * Reader class for reading PCEP messages from channel buffer.
56      */
57     static class Reader implements PcepMessageReader<PcepMessage> {
58         @Override
59         public PcepMessage readFrom(ChannelBuffer cb) throws PcepParseException {
60
61             if (cb.readableBytes() < MINIMUM_LENGTH) {
62                 throw new PcepParseException("Packet should have minimum length: " + MINIMUM_LENGTH);
63             }
64
65             try {
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);
72                 }
73
74                 byte type = cb.readByte();
75                 short length = cb.readShort();
76                 cb.readerIndex(start);
77
78                 switch (type) {
79
80                 case OPEN_MSG_TYPE:
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));
88                 case ERROR_MSG_TYPE:
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));
92                 case REPORT_MSG_TYPE:
93                     log.debug("REPORT MESSAGE is received");
94                     // message type value 10 means it is Report message
95                     // return
96                     return PcepReportMsgVer1.READER.readFrom(cb.readBytes(length));
97                 case UPDATE_MSG_TYPE:
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));
105                 case CLOSE_MSG_TYPE:
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
112                     // return
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
117                     // return
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
122                     // return
123                     return PcepLabelRangeResvMsgVer1.READER.readFrom(cb.readBytes(length));
124                 default:
125                     throw new PcepParseException("ERROR: UNKNOWN MESSAGE is received. Msg Type: " + type);
126                 }
127             } catch (IndexOutOfBoundsException e) {
128                 throw new PcepParseException(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_1);
129             }
130         }
131     }
132 }