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