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