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 static org.opendaylight.protocol.util.Ipv4Util.incrementIpv4Address;
12 import static org.opendaylight.protocol.util.Ipv4Util.incrementIpv4Prefix;
14 import io.netty.channel.Channel;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.ChannelFutureListener;
17 import io.netty.channel.ChannelHandlerContext;
18 import io.netty.channel.SimpleChannelInboundHandler;
19 import java.net.InetAddress;
20 import java.net.InetSocketAddress;
21 import org.opendaylight.protocol.bmp.api.BmpSession;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev150512.AdjRibInType;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public final class BmpMockSession extends SimpleChannelInboundHandler<Notification> implements BmpSession {
31 private static final Logger LOG = LoggerFactory.getLogger(BmpMockSession.class);
33 private static final Ipv4Address NEXT_HOP = new Ipv4Address("1.1.1.1");
34 private static final Ipv4Prefix PREFIX = new Ipv4Prefix("1.1.1.1/32");
35 private static final Ipv4Address PEER_ADDRESS = NEXT_HOP;
37 private final int peersCount;
38 private final int prePolicyRoutesCount;
39 private final int postPolicyRoutesCount;
41 private InetSocketAddress remoteAddress;
42 private Channel channel;
44 public BmpMockSession(final int peersCount, final int prePolicyRoutesCount, final int postPolicyRoutesCount) {
45 this.peersCount = peersCount;
46 this.prePolicyRoutesCount = prePolicyRoutesCount;
47 this.postPolicyRoutesCount = postPolicyRoutesCount;
51 public void close() throws InterruptedException {
52 LOG.info("BMP session {} is closed.", BmpMockSession.this.channel);
53 this.channel.close().sync();
57 public InetAddress getRemoteAddress() {
58 return this.remoteAddress.getAddress();
62 protected void channelRead0(final ChannelHandlerContext ctx, final Notification msg) throws Exception {
67 public void channelActive(final ChannelHandlerContext ctx) {
68 this.channel = ctx.channel();
69 this.channel.closeFuture().addListener((ChannelFutureListener) future -> LOG.info("BMP session {} final successfully established.", BmpMockSession.this.channel));
70 LOG.info("BMP session {} successfully established.", this.channel);
71 final InetSocketAddress localAddress = (InetSocketAddress) this.channel.localAddress();
72 this.remoteAddress = (InetSocketAddress) this.channel.remoteAddress();
73 advertizePeers(this.channel, localAddress);
76 private void advertizePeers(final Channel channel, final InetSocketAddress localAddress) {
77 channel.writeAndFlush(BmpMockUtil.createInitiation());
78 Ipv4Address peerAddress = PEER_ADDRESS;
79 for (int i = 0; i < this.peersCount; i++) {
80 channel.writeAndFlush(BmpMockUtil.createPeerUp(peerAddress, localAddress.getAddress()));
81 LOG.debug("BMP router {} advertized peer {}", channel.localAddress(), peerAddress);
82 advertizeRoutes(this.prePolicyRoutesCount, AdjRibInType.PrePolicy, channel, peerAddress);
83 advertizeRoutes(this.postPolicyRoutesCount, AdjRibInType.PostPolicy, channel, peerAddress);
84 peerAddress = incrementIpv4Address(peerAddress);
88 private static void advertizeRoutes(final int count, final AdjRibInType type, final Channel channel,
89 final Ipv4Address peerAddress) {
90 Ipv4Prefix prefix = PREFIX;
91 for (int i = 0; i < count; i++) {
92 channel.writeAndFlush(BmpMockUtil.createRouteMonitoring(peerAddress, type, prefix));
93 prefix = incrementIpv4Prefix(prefix);