Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 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; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 19 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 20 | import org.onosproject.bgpio.protocol.BgpKeepaliveMsg; |
| 21 | import org.onosproject.bgpio.protocol.BgpMessageReader; |
| 22 | import org.onosproject.bgpio.protocol.BgpMessageWriter; |
| 23 | import org.onosproject.bgpio.types.BgpHeader; |
| 24 | import org.onosproject.bgpio.protocol.BgpType; |
| 25 | import org.onosproject.bgpio.protocol.BgpVersion; |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import com.google.common.base.MoreObjects; |
| 30 | |
| 31 | /** |
| 32 | * Provides BGP keep alive message. |
| 33 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 34 | public class BgpKeepaliveMsgVer4 implements BgpKeepaliveMsg { |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | <Keepalive Message>::= <Common Header> |
| 38 | A KEEPALIVE message consists of only the message header and has a |
| 39 | length of 19 octets. |
| 40 | |
| 41 | 0 1 2 3 |
| 42 | 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 |
| 43 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 44 | | | |
| 45 | + + |
| 46 | | | |
| 47 | + + |
| 48 | | Marker | |
| 49 | + + |
| 50 | | | |
| 51 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 52 | | Length | Type | |
| 53 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 54 | |
| 55 | REFERENCE : RFC 4271 |
| 56 | */ |
| 57 | |
| 58 | protected static final Logger log = LoggerFactory |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 59 | .getLogger(BgpKeepaliveMsgVer4.class); |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 60 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 61 | private BgpHeader bgpMsgHeader; |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 62 | public static final byte PACKET_VERSION = 4; |
| 63 | public static final int PACKET_MINIMUM_LENGTH = 19; |
| 64 | public static final int MARKER_LENGTH = 16; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 65 | public static final BgpType MSG_TYPE = BgpType.KEEP_ALIVE; |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 66 | public static byte[] marker = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 67 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 68 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 69 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; |
| 70 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 71 | public static final BgpKeepaliveMsgVer4.Reader READER = new Reader(); |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Reader class for reading BGP keepalive message from channel buffer. |
| 75 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 76 | static class Reader implements BgpMessageReader<BgpKeepaliveMsg> { |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 77 | |
| 78 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 79 | public BgpKeepaliveMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) |
| 80 | throws BgpParseException { |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 81 | |
| 82 | /* bgpHeader is not required in case of keepalive message and |
| 83 | Header is already read and no other fields except header in keepalive message.*/ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 84 | return new BgpKeepaliveMsgVer4(); |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Default constructor. |
| 90 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 91 | public BgpKeepaliveMsgVer4() { |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Builder class for BGP keepalive message. |
| 96 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 97 | static class Builder implements BgpKeepaliveMsg.Builder { |
| 98 | BgpHeader bgpMsgHeader; |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 99 | |
| 100 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 101 | public Builder setHeader(BgpHeader bgpMsgHeader) { |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 102 | this.bgpMsgHeader = bgpMsgHeader; |
| 103 | return this; |
| 104 | } |
| 105 | |
| 106 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 107 | public BgpKeepaliveMsg build() { |
| 108 | return new BgpKeepaliveMsgVer4(); |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void writeTo(ChannelBuffer cb) { |
| 114 | WRITER.write(cb, this); |
| 115 | } |
| 116 | |
| 117 | static final Writer WRITER = new Writer(); |
| 118 | |
| 119 | /** |
| 120 | * Writer class for writing the BGP keepalive message to channel buffer. |
| 121 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 122 | static class Writer implements BgpMessageWriter<BgpKeepaliveMsgVer4> { |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 123 | |
| 124 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 125 | public void write(ChannelBuffer cb, BgpKeepaliveMsgVer4 message) { |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 126 | |
| 127 | // write marker |
| 128 | cb.writeBytes(marker, 0, MARKER_LENGTH); |
| 129 | |
| 130 | // write length of header |
| 131 | cb.writeShort(PACKET_MINIMUM_LENGTH); |
| 132 | |
| 133 | // write the type of message |
| 134 | cb.writeByte(MSG_TYPE.getType()); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 139 | public BgpVersion getVersion() { |
| 140 | return BgpVersion.BGP_4; |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 144 | public BgpType getType() { |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 145 | return MSG_TYPE; |
| 146 | } |
| 147 | |
| 148 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 149 | public BgpHeader getHeader() { |
Vidyashree Rama | e59ada1 | 2015-09-28 17:08:31 +0530 | [diff] [blame] | 150 | return this.bgpMsgHeader; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public String toString() { |
| 155 | return MoreObjects.toStringHelper(getClass()).toString(); |
| 156 | } |
| 157 | } |