blob: f0ff5cfa261d3fd13b541e1a309b641d4e108f14 [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.PcepKeepaliveMsg;
22import org.onosproject.pcepio.protocol.PcepMessageReader;
23import org.onosproject.pcepio.protocol.PcepMessageWriter;
24import org.onosproject.pcepio.protocol.PcepType;
25import org.onosproject.pcepio.protocol.PcepVersion;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29class PcepKeepaliveMsgVer1 implements PcepKeepaliveMsg {
30
31 /*
32 <Keepalive Message>::= <Common Header>
33
34 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
35 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 | Ver | Flags | Message-Type | Message-Length |
37 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 */
39
40 protected static final Logger log = LoggerFactory.getLogger(PcepKeepaliveMsgVer1.class);
41 // Pcep version: 1
42 public static final byte PACKET_VERSION = 1;
43 public static final int PACKET_MINIMUM_LENGTH = 4;
44 public static final PcepType MSG_TYPE = PcepType.KEEP_ALIVE;
45
46 public static final PcepKeepaliveMsgVer1.Reader READER = new Reader();
47
48 static class Reader implements PcepMessageReader<PcepKeepaliveMsg> {
49
50 @Override
51 public PcepKeepaliveMsg readFrom(ChannelBuffer cb) throws PcepParseException {
52
53 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
54 throw new PcepParseException("Packet size is less than the minimum required length.");
55 }
56 // fixed value property version == 1
57 byte version = cb.readByte();
58 version = (byte) (version >> PcepMessageVer1.SHIFT_FLAG);
59 if (version != PACKET_VERSION) {
60 throw new PcepParseException("Wrong version: Expected=PcepVersion.KEEP_ALIVE_1(2), got=" + version);
61 }
62 // fixed value property type == 2
63 byte type = cb.readByte();
64 if (type != MSG_TYPE.getType()) {
65 throw new PcepParseException("Wrong type: Expected=PcepType.KEEP_ALIVE_1(2), got=" + type);
66 }
67 short length = cb.readShort();
68 if (length < PACKET_MINIMUM_LENGTH) {
69 throw new PcepParseException("Wrong length: Expected to be >= " + PACKET_MINIMUM_LENGTH + ", was: "
70 + length);
71 }
72 return new PcepKeepaliveMsgVer1();
73 }
74 }
75
76 /**
77 * Default constructor.
78 */
79 PcepKeepaliveMsgVer1() {
80 }
81
82 static class Builder implements PcepKeepaliveMsg.Builder {
83 @Override
84 public PcepVersion getVersion() {
85 return PcepVersion.PCEP_1;
86 }
87
88 @Override
89 public PcepType getType() {
90 return PcepType.KEEP_ALIVE;
91 }
92
93 @Override
94 public PcepKeepaliveMsg build() {
95 return new PcepKeepaliveMsgVer1();
96 }
97 }
98
99 @Override
100 public void writeTo(ChannelBuffer cb) {
101 WRITER.write(cb, this);
102 }
103
104 static final Writer WRITER = new Writer();
105
106 static class Writer implements PcepMessageWriter<PcepKeepaliveMsgVer1> {
107
108 @Override
109 public void write(ChannelBuffer cb, PcepKeepaliveMsgVer1 message) {
110 int startIndex = cb.writerIndex();
111 // first 3 bits set to version
112 cb.writeByte((byte) (PACKET_VERSION << PcepMessageVer1.SHIFT_FLAG));
113 // message type
114 cb.writeByte(MSG_TYPE.getType());
115 // length is length of variable message, will be updated at the end
116 // Store the position of message
117 // length in buffer
118 int msgLenIndex = cb.writerIndex();
119 cb.writeShort((short) 0);
120 // update message length field
121 int length = cb.writerIndex() - startIndex;
122 cb.setShort(msgLenIndex, (short) length);
123 }
124 }
125
126 @Override
127 public PcepVersion getVersion() {
128 return PcepVersion.PCEP_1;
129 }
130
131 @Override
132 public PcepType getType() {
133 return MSG_TYPE;
134 }
135
136 @Override
137 public void print() {
138 log.debug("KEEPALIVE MESSAGE");
139 }
140}