d25db24e901cf6855fcc47852c8623f24f1041e0
[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 package org.onosproject.bgpio.protocol.ver4;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.onosproject.bgpio.exceptions.BgpParseException;
20 import org.onosproject.bgpio.protocol.BgpMessageReader;
21 import org.onosproject.bgpio.protocol.BgpMessageWriter;
22 import org.onosproject.bgpio.protocol.BgpNotificationMsg;
23 import org.onosproject.bgpio.protocol.BgpType;
24 import org.onosproject.bgpio.protocol.BgpVersion;
25 import org.onosproject.bgpio.types.BgpErrorType;
26 import org.onosproject.bgpio.types.BgpHeader;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.base.MoreObjects;
31
32 /**
33  * A NOTIFICATION message is sent when an error condition is detected. The BGP connection is closed immediately after it
34  * is sent.
35  */
36 class BgpNotificationMsgVer4 implements BgpNotificationMsg {
37
38     /*
39           0                   1                   2                   3
40           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
41           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42           | Error code    | Error subcode |   Data (variable)             |
43           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44               REFERENCE : RFC 4271
45     */
46
47     private static final Logger log = LoggerFactory.getLogger(BgpNotificationMsgVer4.class);
48
49     static final byte PACKET_VERSION = 4;
50     //BGPHeader(19) + Error code(1) + Error subcode(1)
51     static final int TOTAL_MESSAGE_MIN_LENGTH = 21;
52     static final int PACKET_MINIMUM_LENGTH = 2;
53     static final BgpType MSG_TYPE = BgpType.NOTIFICATION;
54     static final byte DEFAULT_ERRORSUBCODE = 0;
55     static final byte[] MARKER = {(byte) 0xff, (byte) 0xff, (byte) 0xff,
56             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
57             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
58             (byte) 0xff, (byte) 0xff, (byte) 0xff };
59     static final byte MESSAGE_TYPE = 3;
60     static final BgpHeader DEFAULT_MESSAGE_HEADER = new BgpHeader(MARKER, BgpHeader.DEFAULT_HEADER_LENGTH,
61                                                                   MESSAGE_TYPE);
62
63     private byte errorCode;
64     private byte errorSubCode;
65     private byte[] data;
66     private BgpHeader bgpHeader;
67     public static final BgpNotificationMsgVer4.Reader READER = new Reader();
68
69     /**
70      * Initialize fields.
71      */
72     public BgpNotificationMsgVer4() {
73         this.bgpHeader = null;
74         this.data = null;
75         this.errorCode = 0;
76         this.errorSubCode = 0;
77     }
78
79     /**
80      * Constructor to initialize parameters.
81      *
82      * @param bgpHeader BGP Header in notification message
83      * @param errorCode error code
84      * @param errorSubCode error subcode
85      * @param data field
86      */
87     public BgpNotificationMsgVer4(BgpHeader bgpHeader, byte errorCode, byte errorSubCode, byte[] data) {
88         this.bgpHeader = bgpHeader;
89         this.data = data;
90         this.errorCode = errorCode;
91         this.errorSubCode = errorSubCode;
92     }
93
94     /**
95      * Reader reads BGP Notification Message from the channel buffer.
96      */
97     static class Reader implements BgpMessageReader<BgpNotificationMsg> {
98         @Override
99         public BgpNotificationMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException {
100             byte errorCode;
101             byte errorSubCode;
102             if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
103                 throw new BgpParseException("Not enough readable bytes");
104             }
105             errorCode = cb.readByte();
106             errorSubCode = cb.readByte();
107             //Message Length = 21 + Data Length
108             int dataLength = bgpHeader.getLength() - TOTAL_MESSAGE_MIN_LENGTH;
109             byte[] data = new byte[dataLength];
110             cb.readBytes(data, 0, dataLength);
111             return new BgpNotificationMsgVer4(bgpHeader, errorCode, errorSubCode, data);
112         }
113     }
114
115     /**
116      * Builder class for BGP notification message.
117      */
118     static class Builder implements BgpNotificationMsg.Builder {
119         private byte errorCode;
120         private byte errorSubCode;
121         private byte[] data;
122         private BgpHeader bgpHeader;
123         private boolean isErrorCodeSet = false;
124         private boolean isErrorSubCodeSet = false;
125         private boolean isBGPHeaderSet = false;
126
127         @Override
128         public BgpNotificationMsg build() throws BgpParseException {
129             BgpHeader bgpHeader = this.isBGPHeaderSet ? this.bgpHeader : DEFAULT_MESSAGE_HEADER;
130             if (!this.isErrorCodeSet) {
131                 throw new BgpParseException("Error code must be present");
132             }
133
134             byte errorSubCode = this.isErrorSubCodeSet ? this.errorSubCode : DEFAULT_ERRORSUBCODE;
135             return new BgpNotificationMsgVer4(bgpHeader, this.errorCode, errorSubCode, this.data);
136         }
137
138         @Override
139         public Builder setErrorCode(byte errorCode) {
140             this.errorCode = errorCode;
141             this.isErrorCodeSet = true;
142             return this;
143         }
144
145         @Override
146         public Builder setErrorSubCode(byte errorSubCode) {
147             this.errorSubCode = errorSubCode;
148             this.isErrorSubCodeSet = true;
149             return this;
150         }
151
152         @Override
153         public Builder setData(byte[] data) {
154             if (data != null) {
155                 this.data = data;
156             }
157             return this;
158         }
159
160         @Override
161         public Builder setHeader(BgpHeader bgpMsgHeader) {
162             this.bgpHeader = bgpMsgHeader;
163             return this;
164         }
165     }
166
167     @Override
168     public BgpVersion getVersion() {
169         return BgpVersion.BGP_4;
170     }
171
172     @Override
173     public BgpType getType() {
174         return BgpType.NOTIFICATION;
175     }
176
177     @Override
178     public void writeTo(ChannelBuffer cb) throws BgpParseException {
179         WRITER.write(cb, this);
180     }
181
182     static final Writer WRITER = new Writer();
183
184     /**
185      * Writer writes BGP notification message to channel buffer.
186      */
187     static class Writer implements BgpMessageWriter<BgpNotificationMsgVer4> {
188         @Override
189         public void write(ChannelBuffer cb, BgpNotificationMsgVer4 message) throws BgpParseException {
190             int msgStartIndex = cb.writerIndex();
191             int headerLenIndex = message.bgpHeader.write(cb);
192             if (headerLenIndex <= 0) {
193                 throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR, (byte) 0, null);
194             }
195             cb.writeByte(message.errorCode);
196             cb.writeByte(message.errorSubCode);
197             cb.writeBytes(message.data);
198
199             //Update message length field in notification message
200             int length = cb.writerIndex() - msgStartIndex;
201             cb.setShort(headerLenIndex, (short) length);
202             message.bgpHeader.setLength((short) length);
203         }
204     }
205
206     @Override
207     public byte getErrorCode() {
208         return this.errorCode;
209     }
210
211     /**
212      * Sets errorcode with specified errorcode.
213      *
214      * @param errorCode field
215      */
216     public void setErrorCode(byte errorCode) {
217         this.errorCode = errorCode;
218     }
219
220     @Override
221     public byte getErrorSubCode() {
222         return this.errorSubCode;
223     }
224
225     /**
226      * Sets error subcode with specified errorSubCode.
227      *
228      * @param errorSubCode field
229      */
230     public void setErrorSubCode(byte errorSubCode) {
231         this.errorSubCode = errorSubCode;
232     }
233
234     @Override
235     public byte[] getData() {
236         return this.data;
237     }
238
239     /**
240      * Sets error data with specified data.
241      *
242      * @param data field
243      */
244     public void setData(byte[] data) {
245         this.data = data;
246     }
247
248     @Override
249     public BgpHeader getHeader() {
250         return this.bgpHeader;
251     }
252
253     @Override
254     public String toString() {
255         return MoreObjects.toStringHelper(getClass())
256                 .omitNullValues()
257                 .add("bgpHeader", bgpHeader)
258                 .add("data", data)
259                 .add("errorCode", errorCode)
260                 .add("errorSubCode", errorSubCode)
261                 .toString();
262     }
263 }