887d201aabb8a908d099aedd7c8baf53ff5a9e46
[bgpcep.git] / bgp / bmp-mock / src / main / java / org / opendaylight / protocol / bmp / mock / BmpMockSession.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bmp.mock;
10
11 import static org.opendaylight.protocol.util.Ipv4Util.incrementIpv4Address;
12 import static org.opendaylight.protocol.util.Ipv4Util.incrementIpv4Prefix;
13
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;
28
29 public final class BmpMockSession extends SimpleChannelInboundHandler<Notification> implements BmpSession {
30
31     private static final Logger LOG = LoggerFactory.getLogger(BmpMockSession.class);
32
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;
36
37     private final int peersCount;
38     private final int prePolicyRoutesCount;
39     private final int postPolicyRoutesCount;
40
41     private InetSocketAddress remoteAddress;
42     private Channel channel;
43
44     public BmpMockSession(final int peersCount, final int prePolicyRoutesCount, final int postPolicyRoutesCount) {
45         this.peersCount = peersCount;
46         this.prePolicyRoutesCount = prePolicyRoutesCount;
47         this.postPolicyRoutesCount = postPolicyRoutesCount;
48     }
49
50     @Override
51     public void close() throws InterruptedException {
52         LOG.info("BMP session {} is closed.", BmpMockSession.this.channel);
53         this.channel.close().sync();
54     }
55
56     @Override
57     public InetAddress getRemoteAddress() {
58         return this.remoteAddress.getAddress();
59     }
60
61     @Override
62     protected void channelRead0(final ChannelHandlerContext ctx, final Notification msg) throws Exception {
63         // nothing to read
64     }
65
66     @Override
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);
74     }
75
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);
85         }
86     }
87
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);
94         }
95     }
96
97 }