2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.protocol.bmp.mock;
11 import io.netty.channel.Channel;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.channel.ChannelFutureListener;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.SimpleChannelInboundHandler;
16 import java.net.InetAddress;
17 import java.net.InetSocketAddress;
18 import org.opendaylight.protocol.bmp.api.BmpSession;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.AdjRibInType;
22 import org.opendaylight.yangtools.yang.binding.Notification;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public final class BmpMockSession extends SimpleChannelInboundHandler<Notification> implements BmpSession {
28 private static final Logger LOG = LoggerFactory.getLogger(BmpMockSession.class);
30 private static final Ipv4Address NEXT_HOP = new Ipv4Address("1.1.1.1");
31 private static final Ipv4Prefix PREFIX = new Ipv4Prefix("1.1.1.1/32");
32 private static final Ipv4Address PEER_ADDRESS = NEXT_HOP;
34 private final int peersCount;
35 private final int prePolicyRoutesCount;
36 private final int postPolicyRoutesCount;
38 private InetSocketAddress remoteAddress;
39 private Channel channel;
41 public BmpMockSession(final int peersCount, final int prePolicyRoutesCount, final int postPolicyRoutesCount) {
42 this.peersCount = peersCount;
43 this.prePolicyRoutesCount = prePolicyRoutesCount;
44 this.postPolicyRoutesCount = postPolicyRoutesCount;
48 public void close() throws InterruptedException {
49 this.channel.close().sync();
53 public InetAddress getRemoteAddress() {
54 return this.remoteAddress.getAddress();
58 protected void channelRead0(final ChannelHandlerContext ctx, final Notification msg) throws Exception {
63 public void channelActive(final ChannelHandlerContext ctx) {
64 this.channel = ctx.channel();
65 this.channel.closeFuture().addListener(new ChannelFutureListener() {
67 public void operationComplete(final ChannelFuture future) throws Exception {
68 LOG.info("BMP session {} final successfully established.", BmpMockSession.this.channel);
71 LOG.info("BMP session {} sucesfully established.", this.channel);
72 final InetSocketAddress localAddress = (InetSocketAddress) this.channel.localAddress();
73 this.remoteAddress = (InetSocketAddress) this.channel.remoteAddress();
74 advertizePeers(this.channel, localAddress);
77 private void advertizePeers(final Channel channel, final InetSocketAddress localAddress) {
78 channel.writeAndFlush(BmpMockUtil.createInitiation());
79 Ipv4Address peerAddress = PEER_ADDRESS;
80 for (int i = 0; i < this.peersCount; i++) {
81 channel.writeAndFlush(BmpMockUtil.createPeerUp(peerAddress, localAddress.getAddress()));
82 LOG.debug("BMP router {} advertized peer {}", channel.localAddress(), peerAddress);
83 advertizeRoutes(this.prePolicyRoutesCount, AdjRibInType.PrePolicy, channel, peerAddress);
84 advertizeRoutes(this.postPolicyRoutesCount, AdjRibInType.PostPolicy, channel, peerAddress);
85 peerAddress = BmpMockUtil.incrementIpv4Address(peerAddress);
89 private static void advertizeRoutes(final int count, final AdjRibInType type, final Channel channel,
90 final Ipv4Address peerAddress) {
91 Ipv4Prefix prefix = PREFIX;
92 for (int i = 0; i < count; i++) {
93 channel.writeAndFlush(BmpMockUtil.createRouteMonitoring(peerAddress, type, prefix));
94 prefix = BmpMockUtil.incrementIpv4Prefix(prefix);