22c8d543ecb9a7540712b42fc86dfbda1a821bea
[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.mock;
15
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import io.netty.util.concurrent.Future;
19 import java.net.InetSocketAddress;
20 import java.util.Collections;
21 import java.util.concurrent.Executor;
22 import java.util.concurrent.atomic.LongAdder;
23 import org.junit.Before;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
31 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
32 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
33 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
34 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
35 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
36 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
37 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
38 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.AbstractImportPolicy;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
43 import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer;
44 import org.opendaylight.protocol.bgp.rib.impl.spi.ImportPolicyPeerTracker;
45 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
46 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
47 import org.opendaylight.protocol.bgp.rib.impl.stats.rib.impl.BGPRenderStats;
48 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRib;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Rib;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
61 import org.opendaylight.yangtools.concepts.ListenerRegistration;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
64 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
66 import org.osgi.framework.ServiceRegistration;
67
68 class AbstractConfig {
69     static final TablesKey TABLES_KEY = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
70     protected static final AsNumber AS = new AsNumber(72L);
71     protected ClusterSingletonService singletonService;
72     @Mock
73     protected RIB rib;
74     @Mock
75     protected ClusterSingletonServiceRegistration singletonServiceRegistration;
76     @Mock
77     protected BGPTableTypeRegistryConsumer tableTypeRegistry;
78     @Mock
79     protected BgpDeployer.WriteConfiguration configurationWriter;
80     @Mock
81     protected DOMTransactionChain domTx;
82     @Mock
83     protected BGPRenderStats render;
84     @Mock
85     protected BGPDispatcher dispatcher;
86     @Mock
87     protected ServiceRegistration serviceRegistration;
88     @Mock
89     protected BGPPeerRegistry bgpPeerRegistry;
90     @Mock
91     protected ListenerRegistration listener;
92     @Mock
93     protected Future future;
94     @Mock
95     protected DOMDataWriteTransaction domDW;
96     @Mock
97     private ImportPolicyPeerTracker importPolicyPeerTracker;
98     @Mock
99     private DOMDataTreeChangeService dataTreeChangeService;
100     protected static final RibId RIB_ID = new RibId("test");
101
102     @Before
103     public void setUp() throws Exception {
104         MockitoAnnotations.initMocks(this);
105         Mockito.doAnswer(invocationOnMock->{
106             this.singletonService = (ClusterSingletonService) invocationOnMock.getArguments()[0];
107             return this.singletonServiceRegistration;
108         }).when(this.rib).registerClusterSingletonService(any(ClusterSingletonService.class));
109         Mockito.doReturn(new LongAdder()).when(this.render).getConfiguredPeerCounter();
110         Mockito.doReturn(this.render).when(this.rib).getRenderStats();
111         Mockito.doReturn(InstanceIdentifier.create(BgpRib.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight
112             .params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib.class, new RibKey(RIB_ID))).when(this.rib)
113             .getInstanceIdentifier();
114         Mockito.doReturn(this.domTx).when(this.rib).createPeerChain(any(TransactionChainListener.class));
115         Mockito.doReturn(AS).when(this.rib).getLocalAs();
116         Mockito.doReturn(this.importPolicyPeerTracker).when(this.rib).getImportPolicyPeerTracker();
117         Mockito.doNothing().when(this.importPolicyPeerTracker).peerRoleChanged(any(YangInstanceIdentifier.class), any(PeerRole.class));
118         Mockito.doReturn(mock(AbstractImportPolicy.class)).when(this.importPolicyPeerTracker).policyFor(any(PeerId.class));
119         Mockito.doReturn(mock(RIBSupportContextRegistry.class)).when(this.rib).getRibSupportContext();
120         Mockito.doReturn(Collections.emptySet()).when(this.rib).getLocalTablesKeys();
121         Mockito.doNothing().when(this.domTx).close();
122         Mockito.doReturn(this.domDW).when(this.domTx).newWriteOnlyTransaction();
123         Mockito.doNothing().when(this.domDW).put(eq(LogicalDatastoreType.OPERATIONAL), any(YangInstanceIdentifier.class), any(MapEntryNode.class));
124         Mockito.doNothing().when(this.domDW).delete(eq(LogicalDatastoreType.OPERATIONAL), any(YangInstanceIdentifier.class));
125         Mockito.doNothing().when(this.domDW).merge(eq(LogicalDatastoreType.OPERATIONAL), any(YangInstanceIdentifier.class), any(NormalizedNode.class));
126         final CheckedFuture checkedFuture = mock(CheckedFuture.class);
127         Mockito.doAnswer(invocation -> {
128             final Runnable callback = (Runnable) invocation.getArguments()[0];
129             callback.run();
130             return null;
131         }).when(checkedFuture).addListener(Mockito.any(Runnable.class), Mockito.any(Executor.class));
132         Mockito.doReturn(checkedFuture).when(this.domDW).submit();
133         Mockito.doReturn(null).when(checkedFuture).checkedGet();
134         Mockito.doReturn(null).when(checkedFuture).get();
135         Mockito.doReturn("checkedFuture").when(checkedFuture).toString();
136         Mockito.doAnswer(invocationOnMock->{
137             this.singletonService.closeServiceInstance();
138             return null;
139         }).when(this.singletonServiceRegistration).close();
140         Mockito.doReturn(YangInstanceIdentifier.of(Rib.QNAME)).when(this.rib).getYangRibId();
141         Mockito.doReturn(this.dataTreeChangeService).when(this.rib).getService();
142         Mockito.doReturn(this.listener).when(this.dataTreeChangeService).registerDataTreeChangeListener(any(), any());
143         Mockito.doReturn(mock(ServiceGroupIdentifier.class)).when(this.rib).getRibIServiceGroupIdentifier();
144         Mockito.doReturn(new BgpId("127.0.0.1")).when(this.rib).getBgpIdentifier();
145         Mockito.doReturn(true).when(this.future).cancel(true);
146         Mockito.doReturn(this.future).when(this.dispatcher)
147             .createReconnectingClient(any(InetSocketAddress.class), anyInt(), any(Optional.class));
148         Mockito.doReturn(this.dispatcher).when(this.rib).getDispatcher();
149
150         Mockito.doReturn(java.util.Optional.of(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class)))
151             .when(this.tableTypeRegistry).getTableType(any());
152         Mockito.doReturn(java.util.Optional.of(TABLES_KEY)).when(this.tableTypeRegistry).getTableKey(any());
153         Mockito.doReturn(Collections.singleton(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class)))
154             .when(this.rib).getLocalTables();
155         Mockito.doNothing().when(this.configurationWriter).apply();
156
157         Mockito.doNothing().when(this.bgpPeerRegistry).addPeer(any(IpAddress.class), any(BGPSessionListener.class), any(BGPSessionPreferences.class));
158         Mockito.doNothing().when(this.bgpPeerRegistry).removePeer(any(IpAddress.class));
159         Mockito.doReturn("registry").when(this.bgpPeerRegistry).toString();
160         Mockito.doNothing().when(this.listener).close();
161         Mockito.doReturn(this.bgpPeerRegistry).when(this.dispatcher).getBGPPeerRegistry();
162     }
163 }