Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / config / AbstractConfig.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.bgp.rib.impl.config;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyInt;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18
19 import com.google.common.util.concurrent.CheckedFuture;
20 import io.netty.util.concurrent.Future;
21 import java.net.InetSocketAddress;
22 import java.util.Collections;
23 import java.util.concurrent.Executor;
24 import org.junit.Before;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
31 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
32 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
33 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
34 import org.opendaylight.protocol.bgp.rib.impl.BGPPeerTrackerImpl;
35 import org.opendaylight.protocol.bgp.rib.impl.DefaultRibPoliciesMockTest;
36 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
37 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
38 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
41 import org.opendaylight.protocol.bgp.rib.spi.BGPPeerTracker;
42 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
43 import org.opendaylight.protocol.concepts.KeyMapping;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Rib;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
54 import org.opendaylight.yangtools.concepts.ListenerRegistration;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
59 import org.osgi.framework.ServiceRegistration;
60
61 class AbstractConfig extends DefaultRibPoliciesMockTest {
62     protected static final AsNumber AS = new AsNumber(72L);
63     protected static final AsNumber lOCAL_AS = new AsNumber(73L);
64     protected static final RibId RIB_ID = new RibId("test");
65     static final TablesKey TABLES_KEY = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
66     @Mock
67     protected RIB rib;
68     @Mock
69     protected BGPTableTypeRegistryConsumer tableTypeRegistry;
70     @Mock
71     protected DOMTransactionChain domTx;
72     @Mock
73     protected BGPDispatcher dispatcher;
74     @Mock
75     protected ServiceRegistration<?> serviceRegistration;
76     @Mock
77     protected BGPPeerRegistry bgpPeerRegistry;
78     @Mock
79     protected ListenerRegistration<?> listener;
80     @Mock
81     protected Future<?> future;
82     @Mock
83     protected DOMDataWriteTransaction domDW;
84     @Mock
85     protected PeerGroupConfigLoader peerGroupLoader;
86     @Mock
87     private DOMDataTreeChangeService dataTreeChangeService;
88     private BGPPeerTracker peerTracker = new BGPPeerTrackerImpl();
89
90     @Before
91     public void setUp() throws Exception {
92         super.setUp();
93         doReturn(InstanceIdentifier.create(BgpRib.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight
94                 .params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib.class, new RibKey(RIB_ID))).when(this.rib)
95                 .getInstanceIdentifier();
96         doReturn(this.domTx).when(this.rib).createPeerDOMChain(any(TransactionChainListener.class));
97         doAnswer(invocation -> {
98             final Object[] args = invocation.getArguments();
99             return getDataBroker().createTransactionChain((TransactionChainListener) args[0]);
100         }).when(this.rib).createPeerChain(any(TransactionChainListener.class));
101
102         doReturn(getDataBroker()).when(this.rib).getDataBroker();
103         doReturn(AS).when(this.rib).getLocalAs();
104         doReturn(mock(RIBSupportContextRegistry.class)).when(this.rib).getRibSupportContext();
105         doReturn(Collections.emptySet()).when(this.rib).getLocalTablesKeys();
106         doNothing().when(this.domTx).close();
107         doReturn(this.domDW).when(this.domTx).newWriteOnlyTransaction();
108         doNothing().when(this.domDW).put(eq(LogicalDatastoreType.OPERATIONAL),
109                 any(YangInstanceIdentifier.class), any(MapEntryNode.class));
110         doNothing().when(this.domDW).delete(eq(LogicalDatastoreType.OPERATIONAL),
111                 any(YangInstanceIdentifier.class));
112         doNothing().when(this.domDW).merge(eq(LogicalDatastoreType.OPERATIONAL),
113                 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
114         final CheckedFuture<?, ?> checkedFuture = mock(CheckedFuture.class);
115         doAnswer(invocation -> {
116             final Runnable callback = (Runnable) invocation.getArguments()[0];
117             callback.run();
118             return null;
119         }).when(checkedFuture).addListener(Mockito.any(Runnable.class), Mockito.any(Executor.class));
120         doReturn(checkedFuture).when(this.domDW).submit();
121         doReturn(null).when(checkedFuture).checkedGet();
122         doReturn(null).when(checkedFuture).get();
123         doReturn(true).when(checkedFuture).isDone();
124         doReturn("checkedFuture").when(checkedFuture).toString();
125         doReturn(YangInstanceIdentifier.of(Rib.QNAME)).when(this.rib).getYangRibId();
126         doReturn(this.dataTreeChangeService).when(this.rib).getService();
127         doReturn(this.listener).when(this.dataTreeChangeService).registerDataTreeChangeListener(any(), any());
128         doReturn(new BgpId("127.0.0.1")).when(this.rib).getBgpIdentifier();
129         doReturn(true).when(this.future).cancel(true);
130         doReturn(this.future).when(this.dispatcher).createReconnectingClient(any(InetSocketAddress.class),
131                 any(InetSocketAddress.class), anyInt(), any(KeyMapping.class));
132         doReturn(this.dispatcher).when(this.rib).getDispatcher();
133
134         doReturn(java.util.Optional.of(new BgpTableTypeImpl(Ipv4AddressFamily.class,
135                 UnicastSubsequentAddressFamily.class)))
136                 .when(this.tableTypeRegistry).getTableType(any());
137         doReturn(java.util.Optional.of(TABLES_KEY)).when(this.tableTypeRegistry).getTableKey(any());
138         doReturn(Collections.singleton(new BgpTableTypeImpl(Ipv4AddressFamily.class,
139                 UnicastSubsequentAddressFamily.class)))
140                 .when(this.rib).getLocalTables();
141
142         doNothing().when(this.bgpPeerRegistry).addPeer(any(IpAddress.class),
143                 any(BGPSessionListener.class), any(BGPSessionPreferences.class));
144         doNothing().when(this.bgpPeerRegistry).removePeer(any(IpAddress.class));
145         doReturn("registry").when(this.bgpPeerRegistry).toString();
146         doNothing().when(this.listener).close();
147         doReturn(this.bgpPeerRegistry).when(this.dispatcher).getBGPPeerRegistry();
148         doReturn(this.peerTracker).when(this.rib).getPeerTracker();
149         doReturn(this.policies).when(this.rib).getRibPolicies();
150         doReturn(null).when(this.peerGroupLoader)
151                 .getPeerGroup(any(InstanceIdentifier.class), any(String.class));
152     }
153 }