2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.rib.impl.config;
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;
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.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.mdsal.singleton.common.api.ClusterSingletonService;
33 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
34 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
35 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
36 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
37 import org.opendaylight.protocol.bgp.rib.impl.spi.AbstractImportPolicy;
38 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
41 import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.ImportPolicyPeerTracker;
43 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
44 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
45 import org.opendaylight.protocol.bgp.rib.impl.stats.rib.impl.BGPRenderStats;
46 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRib;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Rib;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
64 import org.osgi.framework.ServiceRegistration;
66 class AbstractConfig {
67 static final TablesKey TABLES_KEY = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
68 protected static final AsNumber AS = new AsNumber(72L);
69 protected ClusterSingletonService singletonService;
73 protected ClusterSingletonServiceRegistration singletonServiceRegistration;
75 protected BGPTableTypeRegistryConsumer tableTypeRegistry;
77 protected BgpDeployer.WriteConfiguration configurationWriter;
79 protected DOMTransactionChain domTx;
81 protected BGPRenderStats render;
83 protected BGPDispatcher dispatcher;
85 protected ServiceRegistration serviceRegistration;
87 protected BGPPeerRegistry bgpPeerRegistry;
89 protected ListenerRegistration listener;
91 protected Future future;
93 protected DOMDataWriteTransaction domDW;
95 private ImportPolicyPeerTracker importPolicyPeerTracker;
97 private DOMDataTreeChangeService dataTreeChangeService;
98 protected static final RibId RIB_ID = new RibId("test");
101 public void setUp() throws Exception {
102 MockitoAnnotations.initMocks(this);
103 Mockito.doAnswer(invocationOnMock->{
104 this.singletonService = (ClusterSingletonService) invocationOnMock.getArguments()[0];
105 return this.singletonServiceRegistration;
106 }).when(this.rib).registerClusterSingletonService(any(ClusterSingletonService.class));
107 Mockito.doReturn(new LongAdder()).when(this.render).getConfiguredPeerCounter();
108 Mockito.doReturn(this.render).when(this.rib).getRenderStats();
109 Mockito.doReturn(InstanceIdentifier.create(BgpRib.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight
110 .params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib.class, new RibKey(RIB_ID))).when(this.rib)
111 .getInstanceIdentifier();
112 Mockito.doReturn(this.domTx).when(this.rib).createPeerChain(any(TransactionChainListener.class));
113 Mockito.doReturn(AS).when(this.rib).getLocalAs();
114 Mockito.doReturn(this.importPolicyPeerTracker).when(this.rib).getImportPolicyPeerTracker();
115 Mockito.doNothing().when(this.importPolicyPeerTracker).peerRoleChanged(any(YangInstanceIdentifier.class), any(PeerRole.class));
116 Mockito.doReturn(mock(AbstractImportPolicy.class)).when(this.importPolicyPeerTracker).policyFor(any(PeerId.class));
117 Mockito.doReturn(mock(RIBSupportContextRegistry.class)).when(this.rib).getRibSupportContext();
118 Mockito.doReturn(Collections.emptySet()).when(this.rib).getLocalTablesKeys();
119 Mockito.doNothing().when(this.domTx).close();
120 Mockito.doReturn(this.domDW).when(this.domTx).newWriteOnlyTransaction();
121 Mockito.doNothing().when(this.domDW).put(eq(LogicalDatastoreType.OPERATIONAL), any(YangInstanceIdentifier.class), any(MapEntryNode.class));
122 Mockito.doNothing().when(this.domDW).delete(eq(LogicalDatastoreType.OPERATIONAL), any(YangInstanceIdentifier.class));
123 Mockito.doNothing().when(this.domDW).merge(eq(LogicalDatastoreType.OPERATIONAL), any(YangInstanceIdentifier.class), any(NormalizedNode.class));
124 final CheckedFuture checkedFuture = mock(CheckedFuture.class);
125 Mockito.doAnswer(invocation -> {
126 final Runnable callback = (Runnable) invocation.getArguments()[0];
129 }).when(checkedFuture).addListener(Mockito.any(Runnable.class), Mockito.any(Executor.class));
130 Mockito.doReturn(checkedFuture).when(this.domDW).submit();
131 Mockito.doReturn(null).when(checkedFuture).checkedGet();
132 Mockito.doReturn(null).when(checkedFuture).get();
133 Mockito.doReturn("checkedFuture").when(checkedFuture).toString();
134 Mockito.doAnswer(invocationOnMock->{
135 this.singletonService.closeServiceInstance();
137 }).when(this.singletonServiceRegistration).close();
138 Mockito.doReturn(YangInstanceIdentifier.of(Rib.QNAME)).when(this.rib).getYangRibId();
139 Mockito.doReturn(this.dataTreeChangeService).when(this.rib).getService();
140 Mockito.doReturn(this.listener).when(this.dataTreeChangeService).registerDataTreeChangeListener(any(), any());
141 Mockito.doReturn(mock(ServiceGroupIdentifier.class)).when(this.rib).getRibIServiceGroupIdentifier();
142 Mockito.doReturn(new BgpId("127.0.0.1")).when(this.rib).getBgpIdentifier();
143 Mockito.doReturn(true).when(this.future).cancel(true);
144 Mockito.doReturn(this.future).when(this.dispatcher)
145 .createReconnectingClient(any(InetSocketAddress.class), anyInt(), any(Optional.class));
146 Mockito.doReturn(this.dispatcher).when(this.rib).getDispatcher();
148 Mockito.doReturn(java.util.Optional.of(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class)))
149 .when(this.tableTypeRegistry).getTableType(any());
150 Mockito.doReturn(java.util.Optional.of(TABLES_KEY)).when(this.tableTypeRegistry).getTableKey(any());
151 Mockito.doReturn(Collections.singleton(new BgpTableTypeImpl(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class)))
152 .when(this.rib).getLocalTables();
153 Mockito.doNothing().when(this.configurationWriter).apply();
155 Mockito.doNothing().when(this.bgpPeerRegistry).addPeer(any(IpAddress.class), any(BGPSessionListener.class), any(BGPSessionPreferences.class));
156 Mockito.doNothing().when(this.bgpPeerRegistry).removePeer(any(IpAddress.class));
157 Mockito.doReturn("registry").when(this.bgpPeerRegistry).toString();
158 Mockito.doNothing().when(this.listener).close();
159 Mockito.doReturn(this.bgpPeerRegistry).when(this.dispatcher).getBGPPeerRegistry();