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