BGPCEP-754: Rework EffectiveRibInWriter
[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.rev171207.BgpRib;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Rib;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.RibId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.bgp.rib.RibKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.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 RibId RIB_ID = new RibId("test");
64     static final TablesKey TABLES_KEY = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
65     @Mock
66     protected RIB rib;
67     @Mock
68     protected BGPTableTypeRegistryConsumer tableTypeRegistry;
69     @Mock
70     protected DOMTransactionChain domTx;
71     @Mock
72     protected BGPDispatcher dispatcher;
73     @Mock
74     protected ServiceRegistration<?> serviceRegistration;
75     @Mock
76     protected BGPPeerRegistry bgpPeerRegistry;
77     @Mock
78     protected ListenerRegistration<?> listener;
79     @Mock
80     protected Future<?> future;
81     @Mock
82     protected DOMDataWriteTransaction domDW;
83     @Mock
84     private DOMDataTreeChangeService dataTreeChangeService;
85     private BGPPeerTracker peerTracker = new BGPPeerTrackerImpl();
86
87     @Before
88     public void setUp() throws Exception {
89         super.setUp();
90         doReturn(InstanceIdentifier.create(BgpRib.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight
91                 .params.xml.ns.yang.bgp.rib.rev171207.bgp.rib.Rib.class, new RibKey(RIB_ID))).when(this.rib)
92                 .getInstanceIdentifier();
93         doReturn(this.domTx).when(this.rib).createPeerDOMChain(any(TransactionChainListener.class));
94         doAnswer(invocation -> {
95             final Object[] args = invocation.getArguments();
96             return getDataBroker().createTransactionChain((TransactionChainListener) args[0]);
97         }).when(this.rib).createPeerChain(any(TransactionChainListener.class));
98
99         doReturn(getDataBroker()).when(this.rib).getDataBroker();
100         doReturn(AS).when(this.rib).getLocalAs();
101         doReturn(mock(RIBSupportContextRegistry.class)).when(this.rib).getRibSupportContext();
102         doReturn(Collections.emptySet()).when(this.rib).getLocalTablesKeys();
103         doNothing().when(this.domTx).close();
104         doReturn(this.domDW).when(this.domTx).newWriteOnlyTransaction();
105         doNothing().when(this.domDW).put(eq(LogicalDatastoreType.OPERATIONAL),
106                 any(YangInstanceIdentifier.class), any(MapEntryNode.class));
107         doNothing().when(this.domDW).delete(eq(LogicalDatastoreType.OPERATIONAL),
108                 any(YangInstanceIdentifier.class));
109         doNothing().when(this.domDW).merge(eq(LogicalDatastoreType.OPERATIONAL),
110                 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
111         final CheckedFuture<?, ?> checkedFuture = mock(CheckedFuture.class);
112         doAnswer(invocation -> {
113             final Runnable callback = (Runnable) invocation.getArguments()[0];
114             callback.run();
115             return null;
116         }).when(checkedFuture).addListener(Mockito.any(Runnable.class), Mockito.any(Executor.class));
117         doReturn(checkedFuture).when(this.domDW).submit();
118         doReturn(null).when(checkedFuture).checkedGet();
119         doReturn(null).when(checkedFuture).get();
120         doReturn(true).when(checkedFuture).isDone();
121         doReturn("checkedFuture").when(checkedFuture).toString();
122         doReturn(YangInstanceIdentifier.of(Rib.QNAME)).when(this.rib).getYangRibId();
123         doReturn(this.dataTreeChangeService).when(this.rib).getService();
124         doReturn(this.listener).when(this.dataTreeChangeService).registerDataTreeChangeListener(any(), any());
125         doReturn(new BgpId("127.0.0.1")).when(this.rib).getBgpIdentifier();
126         doReturn(true).when(this.future).cancel(true);
127         doReturn(this.future).when(this.dispatcher)
128                 .createReconnectingClient(any(InetSocketAddress.class), anyInt(), any(KeyMapping.class));
129         doReturn(this.dispatcher).when(this.rib).getDispatcher();
130
131         doReturn(java.util.Optional.of(new BgpTableTypeImpl(Ipv4AddressFamily.class,
132                 UnicastSubsequentAddressFamily.class)))
133                 .when(this.tableTypeRegistry).getTableType(any());
134         doReturn(java.util.Optional.of(TABLES_KEY)).when(this.tableTypeRegistry).getTableKey(any());
135         doReturn(Collections.singleton(new BgpTableTypeImpl(Ipv4AddressFamily.class,
136                 UnicastSubsequentAddressFamily.class)))
137                 .when(this.rib).getLocalTables();
138
139         doNothing().when(this.bgpPeerRegistry).addPeer(any(IpAddress.class),
140                 any(BGPSessionListener.class), any(BGPSessionPreferences.class));
141         doNothing().when(this.bgpPeerRegistry).removePeer(any(IpAddress.class));
142         doReturn("registry").when(this.bgpPeerRegistry).toString();
143         doNothing().when(this.listener).close();
144         doReturn(this.bgpPeerRegistry).when(this.dispatcher).getBGPPeerRegistry();
145         doReturn(this.peerTracker).when(this.rib).getPeerTracker();
146         doReturn(this.policies).when(this.rib).getRibPolicies();
147     }
148 }