BUG-3888 : fix comparing ASnumbers
[bgpcep.git] / bgp / testtool / src / test / java / org / opendaylight / protocol / bgp / testtool / BGPSpeakerMock.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.bgp.testtool;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.channel.nio.NioEventLoopGroup;
12 import io.netty.util.concurrent.DefaultPromise;
13 import io.netty.util.concurrent.GlobalEventExecutor;
14 import java.net.InetSocketAddress;
15 import java.util.HashMap;
16 import java.util.Map;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
19 import org.opendaylight.protocol.bgp.rib.impl.BGPDispatcherImpl;
20 import org.opendaylight.protocol.bgp.rib.impl.BGPHandlerFactory;
21 import org.opendaylight.protocol.bgp.rib.impl.BGPServerSessionNegotiatorFactory;
22 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
23 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionProposalImpl;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionValidator;
27 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
28 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
39
40 public class BGPSpeakerMock {
41
42     private final BGPServerSessionNegotiatorFactory negotiatorFactory;
43     private final BGPHandlerFactory factory;
44     private final BGPDispatcherImpl disp;
45     private final BGPPeerRegistry peerRegistry;
46     private final Map<Class<? extends AddressFamily>, Class<? extends SubsequentAddressFamily>> tables;
47
48     private BGPSpeakerMock(final BGPServerSessionNegotiatorFactory negotiatorFactory, final BGPHandlerFactory factory,
49                            final DefaultPromise<BGPSessionImpl> defaultPromise) {
50         this.disp = new BGPDispatcherImpl(null, new NioEventLoopGroup(), new NioEventLoopGroup());
51         this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
52         this.factory = Preconditions.checkNotNull(factory);
53
54
55         this.peerRegistry = new BGPPeerRegistry() {
56             @Override
57             public void addPeer(final IpAddress ip, final ReusableBGPPeer peer, final BGPSessionPreferences prefs) {
58             }
59
60             @Override
61             public void removePeer(final IpAddress ip) {
62             }
63
64             @Override
65             public boolean isPeerConfigured(final IpAddress ip) {
66                 return true;
67             }
68
69             @Override
70             public BGPSessionListener getPeer(final IpAddress ip, final Ipv4Address sourceId, final Ipv4Address remoteId, final AsNumber asNumber, final Open open) throws BGPDocumentedException {
71                 return new SpeakerSessionListener();
72             }
73
74             @Override
75             public BGPSessionPreferences getPeerPreferences(final IpAddress ip) {
76                 return new BGPSessionProposalImpl((short) 90, new AsNumber(72L), new Ipv4Address("127.0.0.2"), BGPSpeakerMock.this.tables, new AsNumber(72L)).getProposal();
77             }
78
79             @Override
80             public void close() throws Exception {
81
82             }
83
84             @Override
85             public void removePeerSession(final IpAddress ip) {
86             }
87         };
88
89         this.tables = new HashMap<>();
90         this.tables.put(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
91         this.tables.put(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
92     }
93
94     public void main(final String[] args) {
95
96         final BGPServerSessionNegotiatorFactory snf = new BGPServerSessionNegotiatorFactory(new BGPSessionValidator() {
97             @Override
98             public void validate(final Open openObj, final BGPSessionPreferences prefs) throws BGPDocumentedException {
99                 // NOOP
100             }
101         }, this.peerRegistry);
102
103         final BGPSpeakerMock mock = new BGPSpeakerMock(snf, new BGPHandlerFactory(ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry()), new DefaultPromise<BGPSessionImpl>(GlobalEventExecutor.INSTANCE));
104
105         mock.createServer(new InetSocketAddress("127.0.0.2", 12345));
106     }
107
108     private void createServer(final InetSocketAddress address) {
109         this.disp.createServer(this.peerRegistry,address, new BGPSessionValidator() {
110             @Override
111             public void validate(final Open openObj, final BGPSessionPreferences prefs) throws BGPDocumentedException {
112                 // NOOP
113             }
114         });
115     }
116 }