Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-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.bgp; |
| 17 | |
| 18 | import java.util.LinkedList; |
| 19 | import java.util.concurrent.TimeUnit; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.jboss.netty.buffer.ChannelBuffers; |
| 23 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 24 | import org.jboss.netty.channel.ChannelStateEvent; |
| 25 | import org.jboss.netty.channel.SimpleChannelHandler; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 26 | import org.onosproject.bgpio.protocol.ver4.BgpKeepaliveMsgVer4; |
| 27 | import org.onosproject.bgpio.protocol.ver4.BgpOpenMsgVer4; |
| 28 | import org.onosproject.bgpio.types.BgpHeader; |
| 29 | import org.onosproject.bgpio.types.BgpValueType; |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 30 | |
| 31 | public class BgpPeerChannelHandlerTest extends SimpleChannelHandler { |
| 32 | public static final int OPEN_MSG_MINIMUM_LENGTH = 29; |
| 33 | public static final byte[] MARKER = new byte[] {(byte) 0xff, (byte) 0xff, |
| 34 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 35 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 36 | (byte) 0xff, (byte) 0xff}; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 37 | public static final BgpHeader DEFAULT_OPEN_HEADER = new BgpHeader(MARKER, |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 38 | (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 39 | LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 40 | public byte version; |
| 41 | public short asNumber; |
| 42 | public short holdTime; |
| 43 | public int bgpId; |
| 44 | public boolean isLargeAsCapabilitySet; |
| 45 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 46 | final BgpOpenMsgVer4 openMessage = new BgpOpenMsgVer4(); |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 47 | ChannelHandlerContext savedCtx; |
| 48 | |
| 49 | /** |
| 50 | * Constructor to initialize all variables of BGP Open message. |
| 51 | * |
| 52 | * @param version BGP version in open message |
| 53 | * @param asNumber AS number in open message |
| 54 | * @param holdTime hold time in open message |
| 55 | * @param bgpId BGP identifier in open message |
| 56 | * @param capabilityTlv capabilities in open message |
| 57 | */ |
| 58 | public BgpPeerChannelHandlerTest(byte version, |
| 59 | short asNumber, |
| 60 | short holdTime, |
| 61 | int bgpId, |
| 62 | boolean isLargeAsCapabilitySet, |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 63 | LinkedList<BgpValueType> capabilityTlv) { |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 64 | this.version = version; |
| 65 | this.asNumber = asNumber; |
| 66 | this.holdTime = holdTime; |
| 67 | this.bgpId = bgpId; |
| 68 | this.isLargeAsCapabilitySet = isLargeAsCapabilitySet; |
| 69 | this.capabilityTlv = capabilityTlv; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * closes the channel. |
| 74 | */ |
| 75 | void closeChannel() { |
| 76 | savedCtx.getChannel().close(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void channelConnected(ChannelHandlerContext ctx, |
| 81 | ChannelStateEvent channelEvent) throws InterruptedException { |
| 82 | this.savedCtx = ctx; |
| 83 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 84 | BgpOpenMsgVer4 openMsg = new BgpOpenMsgVer4(DEFAULT_OPEN_HEADER, |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 85 | this.version, |
| 86 | this.asNumber, |
| 87 | this.holdTime, |
| 88 | this.bgpId, |
| 89 | this.capabilityTlv); |
| 90 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 91 | openMsg.writeTo(buffer); |
| 92 | ctx.getChannel().write(buffer); |
| 93 | |
| 94 | TimeUnit.MILLISECONDS.sleep(100); |
| 95 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 96 | BgpKeepaliveMsgVer4 keepaliveMsg = new BgpKeepaliveMsgVer4(); |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 97 | ChannelBuffer buffer1 = ChannelBuffers.dynamicBuffer(); |
| 98 | keepaliveMsg.writeTo(buffer1); |
| 99 | ctx.getChannel().write(buffer1); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void channelDisconnected(ChannelHandlerContext ctx, |
| 104 | ChannelStateEvent channelEvent) { |
| 105 | //Do Nothing |
| 106 | } |
| 107 | } |