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
8 package org.opendaylight.protocol.bmp.mock;
10 import static org.opendaylight.protocol.util.Ipv4Util.incrementIpv4Address;
11 import static org.opendaylight.protocol.util.Ipv4Util.incrementIpv4Prefix;
13 import io.netty.channel.Channel;
14 import io.netty.channel.ChannelFutureListener;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.channel.SimpleChannelInboundHandler;
17 import java.net.InetAddress;
18 import java.net.InetSocketAddress;
19 import org.opendaylight.protocol.bmp.api.BmpSession;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev180329.AdjRibInType;
23 import org.opendaylight.yangtools.yang.binding.Notification;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public final class BmpMockSession extends SimpleChannelInboundHandler<Notification> implements BmpSession {
29 private static final Logger LOG = LoggerFactory.getLogger(BmpMockSession.class);
31 private static final Ipv4AddressNoZone NEXT_HOP = new Ipv4AddressNoZone("1.1.1.1");
32 private static final Ipv4Prefix PREFIX = new Ipv4Prefix("1.1.1.1/32");
33 private static final Ipv4AddressNoZone PEER_ADDRESS = NEXT_HOP;
35 private final int peersCount;
36 private final int prePolicyRoutesCount;
37 private final int postPolicyRoutesCount;
39 private InetSocketAddress remoteAddress;
40 private Channel channel;
42 public BmpMockSession(final int peersCount, final int prePolicyRoutesCount, final int postPolicyRoutesCount) {
43 this.peersCount = peersCount;
44 this.prePolicyRoutesCount = prePolicyRoutesCount;
45 this.postPolicyRoutesCount = postPolicyRoutesCount;
49 public void close() throws InterruptedException {
50 LOG.info("BMP session {} is closed.", BmpMockSession.this.channel);
51 this.channel.close().sync();
55 public InetAddress getRemoteAddress() {
56 return this.remoteAddress.getAddress();
60 protected void channelRead0(final ChannelHandlerContext ctx, final Notification msg) throws Exception {
65 public void channelActive(final ChannelHandlerContext ctx) {
66 this.channel = ctx.channel();
67 this.channel.closeFuture().addListener((ChannelFutureListener) future ->
68 LOG.info("BMP session {} close.", 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(localAddress);
75 private void advertizePeers(final InetSocketAddress localAddress) {
76 channel.writeAndFlush(BmpMockUtil.createInitiation());
77 Ipv4AddressNoZone 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 Ipv4AddressNoZone 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);