8b6e93820ee693f5f88e7abfcfff7052b2f4c4c1
[onosfw.git] /
1 /*
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
17 package org.onosproject.pcepio.protocol.ver1;
18
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;
28
29 import com.google.common.base.MoreObjects;
30
31 /**
32  * Provides PCEP keep alive message.
33  */
34 class 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
45     protected static final Logger log = LoggerFactory.getLogger(PcepKeepaliveMsgVer1.class);
46     // 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
53     /**
54      * Reader class for reading PCEP keepalive message from channel buffer.
55      */
56     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
90     /**
91      * Builder class for PCEP keepalive message.
92      */
93     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
117     /**
118      * Writer class for writing the PCEP keepalive message to channel buffer.
119      */
120     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
151     public String toString() {
152         return MoreObjects.toStringHelper(getClass()).toString();
153     }
154 }