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.PcepMessageReader;
22 import org.onosproject.pcepio.protocol.PcepMessageWriter;
23 import org.onosproject.pcepio.protocol.PcepOpenMsg;
24 import org.onosproject.pcepio.protocol.PcepOpenObject;
25 import org.onosproject.pcepio.protocol.PcepType;
26 import org.onosproject.pcepio.protocol.PcepVersion;
27 import org.onosproject.pcepio.types.PcepErrorDetailInfo;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.common.base.MoreObjects;
34 * Provides PCEP open message.
36 public class PcepOpenMsgVer1 implements PcepOpenMsg {
39 * <Open Message>::= <Common Header> <OPEN>
41 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
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 | Ver | Flags | Message-Type | Message-Length |
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 | Object-Class | OT |Res|P|I| Object Length (bytes) |
46 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 | Ver | Flags | Keepalive | DeadTimer | SID |
48 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 protected static final Logger log = LoggerFactory.getLogger(PcepOpenMsgVer1.class);
57 public static final byte PACKET_VERSION = 1;
58 public static final int PACKET_MINIMUM_LENGTH = 12;
59 public static final PcepType MSG_TYPE = PcepType.OPEN;
60 private PcepOpenObject pcepOpenObj;
62 public static final PcepOpenMsgVer1.Reader READER = new Reader();
65 * Constructor to initialize PcepOpenObject.
67 * @param pcepOpenObj PCEP-OPEN-OBJECT
69 public PcepOpenMsgVer1(PcepOpenObject pcepOpenObj) {
70 this.pcepOpenObj = pcepOpenObj;
74 public PcepOpenObject getPcepOpenObject() {
75 return this.pcepOpenObj;
79 public void setPcepOpenObject(PcepOpenObject pcepOpenObj) {
80 this.pcepOpenObj = pcepOpenObj;
84 public PcepVersion getVersion() {
85 return PcepVersion.PCEP_1;
89 public PcepType getType() {
94 * Reader class for reading PCEP open message from channel buffer.
96 public static class Reader implements PcepMessageReader<PcepOpenMsg> {
99 public PcepOpenMsg readFrom(ChannelBuffer cb) throws PcepParseException {
101 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
102 throw new PcepParseException("Packet size is less than the minimum length.");
105 byte version = cb.readByte();
106 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
107 if (version != PACKET_VERSION) {
108 log.error("[readFrom] Invalid version: " + version);
109 throw new PcepParseException(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_1);
111 // fixed value property type == 1
112 byte type = cb.readByte();
114 if (type != MSG_TYPE.getType()) {
115 log.error("[readFrom] Unexpected type: " + type);
116 throw new PcepParseException(PcepErrorDetailInfo.ERROR_TYPE_1, PcepErrorDetailInfo.ERROR_VALUE_1);
118 int length = cb.readShort();
119 if (length < PACKET_MINIMUM_LENGTH) {
120 throw new PcepParseException(
121 "Wrong length: Expected to be >= " + PACKET_MINIMUM_LENGTH + ", was: " + length);
123 return new PcepOpenMsgVer1(PcepOpenObjectVer1.read(cb));
128 * Builder class for PCEP open message.
130 static class Builder implements PcepOpenMsg.Builder {
132 private PcepOpenObject pcepOpenObj;
135 public PcepOpenMsg build() throws PcepParseException {
136 if (!(pcepOpenObj instanceof PcepOpenObjectVer1)) {
137 throw new NullPointerException("PcepOpenObject is null.");
139 return new PcepOpenMsgVer1(pcepOpenObj);
143 public PcepVersion getVersion() {
144 return PcepVersion.PCEP_1;
148 public PcepType getType() {
149 return PcepType.OPEN;
153 public PcepOpenObject getPcepOpenObj() {
154 return this.pcepOpenObj;
158 public Builder setPcepOpenObj(PcepOpenObject obj) {
159 this.pcepOpenObj = obj;
165 public void writeTo(ChannelBuffer cb) throws PcepParseException {
166 WRITER.write(cb, this);
169 public static final Writer WRITER = new Writer();
172 * Writer class for writing PCEP opne message to channel buffer.
174 public static class Writer implements PcepMessageWriter<PcepOpenMsgVer1> {
177 public void write(ChannelBuffer cb, PcepOpenMsgVer1 message) throws PcepParseException {
178 int startIndex = cb.writerIndex();
179 // first 3 bits set to version
180 cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
182 cb.writeByte(MSG_TYPE.getType());
183 // length is length of variable message, will be updated at the end
184 // Store the position of message
187 int msgLenIndex = cb.writerIndex();
190 message.getPcepOpenObject().write(cb);
192 // update message length field
193 int iLength = cb.writerIndex() - startIndex;
194 cb.setShort(msgLenIndex, (short) iLength);
199 public String toString() {
200 return MoreObjects.toStringHelper(getClass())
201 .add("OpenObject", pcepOpenObj)