blob: c2be8e55d2d0a611b0bd0e3e3141468d5e920937 [file] [log] [blame]
bharat saraswalf7364db2015-08-11 13:39:31 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
bharat saraswalf7364db2015-08-11 13:39:31 +05303 *
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;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053020import org.onosproject.pcepio.exceptions.PcepOutOfBoundMessageException;
bharat saraswalf7364db2015-08-11 13:39:31 +053021import org.onosproject.pcepio.exceptions.PcepParseException;
22import org.onosproject.pcepio.protocol.PcepFactories;
23import org.onosproject.pcepio.protocol.PcepMessage;
24import org.onosproject.pcepio.protocol.PcepMessageReader;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053025import org.onosproject.pcepio.protocol.PcepType;
bharat saraswalf7364db2015-08-11 13:39:31 +053026import org.onosproject.pcepio.types.PcepErrorDetailInfo;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Phanendra Manda51fb9c22015-09-01 16:17:41 +053030/**
31 * Provides PCEP messages.
32 */
bharat saraswalf7364db2015-08-11 13:39:31 +053033public abstract class PcepMessageVer1 {
34
Ray Milkey9c9cde42018-01-12 14:22:06 -080035 private static final Logger log = LoggerFactory.getLogger(PcepFactories.class);
bharat saraswalf7364db2015-08-11 13:39:31 +053036
37 // version: 1.0
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053038 public static final byte WIRE_VERSION = 1;
39 public static final int MINIMUM_LENGTH = 4;
40 public static final int PACKET_VERSION = 1;
bharat saraswalf7364db2015-08-11 13:39:31 +053041 public static final int SHIFT_FLAG = 5;
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053042 public static final int MINIMUM_COMMON_HEADER_LENGTH = 4;
bharat saraswalf7364db2015-08-11 13:39:31 +053043
44 public static final PcepMessageVer1.Reader READER = new Reader();
45
Phanendra Manda51fb9c22015-09-01 16:17:41 +053046 /**
47 * Reader class for reading PCEP messages from channel buffer.
48 */
bharat saraswalf7364db2015-08-11 13:39:31 +053049 static class Reader implements PcepMessageReader<PcepMessage> {
50 @Override
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053051 public PcepMessage readFrom(ChannelBuffer cb) throws PcepParseException, PcepOutOfBoundMessageException {
bharat saraswalf7364db2015-08-11 13:39:31 +053052
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 Sf1bbd362016-02-25 18:19:59 +053070 // 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 saraswalf7364db2015-08-11 13:39:31 +053075
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053076 if (type == (byte) PcepType.OPEN.getType()) {
bharat saraswalf7364db2015-08-11 13:39:31 +053077 log.debug("OPEN MESSAGE is received");
Mahesh Poojary S1c356e32015-08-21 15:05:30 +053078 return PcepOpenMsgVer1.READER.readFrom(cb.readBytes(length));
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053079 } else if (type == (byte) PcepType.KEEP_ALIVE.getType()) {
bharat saraswalf7364db2015-08-11 13:39:31 +053080 log.debug("KEEPALIVE MESSAGE is received");
bharat saraswalf7364db2015-08-11 13:39:31 +053081 return PcepKeepaliveMsgVer1.READER.readFrom(cb.readBytes(length));
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053082 } else if (type == (byte) PcepType.ERROR.getType()) {
bharat saraswalf7364db2015-08-11 13:39:31 +053083 log.debug("ERROR MESSAGE is received");
Mahesh Poojary S1c356e32015-08-21 15:05:30 +053084 return PcepErrorMsgVer1.READER.readFrom(cb.readBytes(length));
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053085 } else if (type == (byte) PcepType.CLOSE.getType()) {
bharat saraswalf7364db2015-08-11 13:39:31 +053086 log.debug("CLOSE MESSAGE is received");
bharat saraswalf7364db2015-08-11 13:39:31 +053087 return PcepCloseMsgVer1.READER.readFrom(cb.readBytes(length));
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +053088 } 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 saraswale2e7a002015-08-21 22:47:30 +0530101 log.debug("LABEL RANGE RESERVE MESSAGE is received");
bharat saraswale2e7a002015-08-21 22:47:30 +0530102 return PcepLabelRangeResvMsgVer1.READER.readFrom(cb.readBytes(length));
Mahesh Poojary Sf1bbd362016-02-25 18:19:59 +0530103 } 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 saraswalf7364db2015-08-11 13:39:31 +0530107 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}