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