blob: 26ed36d85ab2ac14ff5d77bf3766f9a60c6756bb [file] [log] [blame]
Vidyashree Rama7bd3d782015-11-23 08:46:33 +05301/*
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 */
16package org.onosproject.bgp;
17
18import java.util.LinkedList;
19import java.util.concurrent.TimeUnit;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.jboss.netty.buffer.ChannelBuffers;
23import org.jboss.netty.channel.ChannelHandlerContext;
24import org.jboss.netty.channel.ChannelStateEvent;
25import org.jboss.netty.channel.SimpleChannelHandler;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053026import org.onosproject.bgpio.protocol.ver4.BgpKeepaliveMsgVer4;
27import org.onosproject.bgpio.protocol.ver4.BgpOpenMsgVer4;
28import org.onosproject.bgpio.types.BgpHeader;
29import org.onosproject.bgpio.types.BgpValueType;
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053030
31public 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 VH5dd8dbe2015-11-26 13:22:18 +053037 public static final BgpHeader DEFAULT_OPEN_HEADER = new BgpHeader(MARKER,
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053038 (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053039 LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053040 public byte version;
41 public short asNumber;
42 public short holdTime;
43 public int bgpId;
44 public boolean isLargeAsCapabilitySet;
45
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053046 final BgpOpenMsgVer4 openMessage = new BgpOpenMsgVer4();
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053047 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 VH5dd8dbe2015-11-26 13:22:18 +053063 LinkedList<BgpValueType> capabilityTlv) {
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053064 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 VH5dd8dbe2015-11-26 13:22:18 +053084 BgpOpenMsgVer4 openMsg = new BgpOpenMsgVer4(DEFAULT_OPEN_HEADER,
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053085 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 VH5dd8dbe2015-11-26 13:22:18 +053096 BgpKeepaliveMsgVer4 keepaliveMsg = new BgpKeepaliveMsgVer4();
Vidyashree Rama7bd3d782015-11-23 08:46:33 +053097 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}