blob: 6f1828f0769a9657d0df1ecc34f51b3fbf029d04 [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;
26import 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;
30
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};
37 public static final BGPHeader DEFAULT_OPEN_HEADER = new BGPHeader(MARKER,
38 (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01);
39 LinkedList<BGPValueType> capabilityTlv = new LinkedList<>();
40 public byte version;
41 public short asNumber;
42 public short holdTime;
43 public int bgpId;
44 public boolean isLargeAsCapabilitySet;
45
46 final BGPOpenMsgVer4 openMessage = new BGPOpenMsgVer4();
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,
63 LinkedList<BGPValueType> capabilityTlv) {
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
84 BGPOpenMsgVer4 openMsg = new BGPOpenMsgVer4(DEFAULT_OPEN_HEADER,
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
96 BGPKeepaliveMsgVer4 keepaliveMsg = new BGPKeepaliveMsgVer4();
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}