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.PcepKeepaliveMsg;
22 import org.onosproject.pcepio.protocol.PcepMessageReader;
23 import org.onosproject.pcepio.protocol.PcepMessageWriter;
24 import org.onosproject.pcepio.protocol.PcepType;
25 import org.onosproject.pcepio.protocol.PcepVersion;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.google.common.base.MoreObjects;
32 * Provides PCEP keep alive message.
34 class PcepKeepaliveMsgVer1 implements PcepKeepaliveMsg {
37 <Keepalive Message>::= <Common Header>
39 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 | Ver | Flags | Message-Type | Message-Length |
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 protected static final Logger log = LoggerFactory.getLogger(PcepKeepaliveMsgVer1.class);
47 public static final byte PACKET_VERSION = 1;
48 public static final int PACKET_MINIMUM_LENGTH = 4;
49 public static final PcepType MSG_TYPE = PcepType.KEEP_ALIVE;
51 public static final PcepKeepaliveMsgVer1.Reader READER = new Reader();
54 * Reader class for reading PCEP keepalive message from channel buffer.
56 static class Reader implements PcepMessageReader<PcepKeepaliveMsg> {
59 public PcepKeepaliveMsg readFrom(ChannelBuffer cb) throws PcepParseException {
61 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
62 throw new PcepParseException("Packet size is less than the minimum required length.");
64 // fixed value property version == 1
65 byte version = cb.readByte();
66 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
67 if (version != PACKET_VERSION) {
68 throw new PcepParseException("Wrong version: Expected=PcepVersion.KEEP_ALIVE_1(2), got=" + version);
70 // fixed value property type == 2
71 byte type = cb.readByte();
72 if (type != MSG_TYPE.getType()) {
73 throw new PcepParseException("Wrong type: Expected=PcepType.KEEP_ALIVE_1(2), got=" + type);
75 short length = cb.readShort();
76 if (length < PACKET_MINIMUM_LENGTH) {
77 throw new PcepParseException("Wrong length: Expected to be >= " + PACKET_MINIMUM_LENGTH + ", was: "
80 return new PcepKeepaliveMsgVer1();
85 * Default constructor.
87 PcepKeepaliveMsgVer1() {
91 * Builder class for PCEP keepalive message.
93 static class Builder implements PcepKeepaliveMsg.Builder {
95 public PcepVersion getVersion() {
96 return PcepVersion.PCEP_1;
100 public PcepType getType() {
101 return PcepType.KEEP_ALIVE;
105 public PcepKeepaliveMsg build() {
106 return new PcepKeepaliveMsgVer1();
111 public void writeTo(ChannelBuffer cb) {
112 WRITER.write(cb, this);
115 static final Writer WRITER = new Writer();
118 * Writer class for writing the PCEP keepalive message to channel buffer.
120 static class Writer implements PcepMessageWriter<PcepKeepaliveMsgVer1> {
123 public void write(ChannelBuffer cb, PcepKeepaliveMsgVer1 message) {
124 int startIndex = cb.writerIndex();
125 // first 3 bits set to version
126 cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
128 cb.writeByte(MSG_TYPE.getType());
129 // length is length of variable message, will be updated at the end
130 // Store the position of message
132 int msgLenIndex = cb.writerIndex();
133 cb.writeShort((short) 0);
134 // update message length field
135 int length = cb.writerIndex() - startIndex;
136 cb.setShort(msgLenIndex, (short) length);
141 public PcepVersion getVersion() {
142 return PcepVersion.PCEP_1;
146 public PcepType getType() {
151 public String toString() {
152 return MoreObjects.toStringHelper(getClass()).toString();