blob: ff930a43c3c09ed1c02c43bba3222ea61d6bd995 [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;
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
bharat saraswale1806302015-08-21 12:16:46 +053029import com.google.common.base.MoreObjects;
30
Phanendra Manda51fb9c22015-09-01 16:17:41 +053031/**
32 * Provides PCEP keep alive message.
33 */
bharat saraswalf7364db2015-08-11 13:39:31 +053034class PcepKeepaliveMsgVer1 implements PcepKeepaliveMsg {
35
36 /*
37 <Keepalive Message>::= <Common Header>
38
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 */
44
Ray Milkey9c9cde42018-01-12 14:22:06 -080045 private static final Logger log = LoggerFactory.getLogger(PcepKeepaliveMsgVer1.class);
bharat saraswalf7364db2015-08-11 13:39:31 +053046 // Pcep version: 1
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;
50
51 public static final PcepKeepaliveMsgVer1.Reader READER = new Reader();
52
Phanendra Manda51fb9c22015-09-01 16:17:41 +053053 /**
54 * Reader class for reading PCEP keepalive message from channel buffer.
55 */
bharat saraswalf7364db2015-08-11 13:39:31 +053056 static class Reader implements PcepMessageReader<PcepKeepaliveMsg> {
57
58 @Override
59 public PcepKeepaliveMsg readFrom(ChannelBuffer cb) throws PcepParseException {
60
61 if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
62 throw new PcepParseException("Packet size is less than the minimum required length.");
63 }
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);
69 }
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);
74 }
75 short length = cb.readShort();
76 if (length < PACKET_MINIMUM_LENGTH) {
77 throw new PcepParseException("Wrong length: Expected to be >= " + PACKET_MINIMUM_LENGTH + ", was: "
78 + length);
79 }
80 return new PcepKeepaliveMsgVer1();
81 }
82 }
83
84 /**
85 * Default constructor.
86 */
87 PcepKeepaliveMsgVer1() {
88 }
89
Phanendra Manda51fb9c22015-09-01 16:17:41 +053090 /**
91 * Builder class for PCEP keepalive message.
92 */
bharat saraswalf7364db2015-08-11 13:39:31 +053093 static class Builder implements PcepKeepaliveMsg.Builder {
94 @Override
95 public PcepVersion getVersion() {
96 return PcepVersion.PCEP_1;
97 }
98
99 @Override
100 public PcepType getType() {
101 return PcepType.KEEP_ALIVE;
102 }
103
104 @Override
105 public PcepKeepaliveMsg build() {
106 return new PcepKeepaliveMsgVer1();
107 }
108 }
109
110 @Override
111 public void writeTo(ChannelBuffer cb) {
112 WRITER.write(cb, this);
113 }
114
115 static final Writer WRITER = new Writer();
116
Phanendra Manda51fb9c22015-09-01 16:17:41 +0530117 /**
118 * Writer class for writing the PCEP keepalive message to channel buffer.
119 */
bharat saraswalf7364db2015-08-11 13:39:31 +0530120 static class Writer implements PcepMessageWriter<PcepKeepaliveMsgVer1> {
121
122 @Override
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));
127 // message type
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
131 // length in buffer
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);
137 }
138 }
139
140 @Override
141 public PcepVersion getVersion() {
142 return PcepVersion.PCEP_1;
143 }
144
145 @Override
146 public PcepType getType() {
147 return MSG_TYPE;
148 }
149
150 @Override
bharat saraswale1806302015-08-21 12:16:46 +0530151 public String toString() {
152 return MoreObjects.toStringHelper(getClass()).toString();
bharat saraswalf7364db2015-08-11 13:39:31 +0530153 }
154}