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
8 package org.opendaylight.protocol.bgp.rib.impl.config;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.anyInt;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
18 import io.netty.util.concurrent.Future;
19 import java.net.InetSocketAddress;
20 import java.util.Collections;
21 import org.junit.Before;
22 import org.mockito.Mock;
23 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
28 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
29 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
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.BGPPeerTrackerImpl;
33 import org.opendaylight.protocol.bgp.rib.impl.DefaultRibPoliciesMockTest;
34 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
35 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
36 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
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.BGPPeerTracker;
40 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
41 import org.opendaylight.protocol.concepts.KeyMapping;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Rib;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.RibId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.RibKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.opendaylight.yangtools.yang.common.Uint32;
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 extends DefaultRibPoliciesMockTest {
61 protected static final AsNumber AS = new AsNumber(Uint32.valueOf(72));
62 protected static final AsNumber LOCAL_AS = new AsNumber(Uint32.valueOf(73));
63 protected static final RibId RIB_ID = new RibId("test");
64 static final TablesKey TABLES_KEY = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
68 protected BGPTableTypeRegistryConsumer tableTypeRegistry;
70 protected DOMTransactionChain domTx;
72 protected BGPDispatcher dispatcher;
74 protected ServiceRegistration<?> serviceRegistration;
76 protected BGPPeerRegistry bgpPeerRegistry;
78 protected ListenerRegistration<?> listener;
80 protected Future<?> future;
82 protected DOMDataTreeWriteTransaction domDW;
84 protected PeerGroupConfigLoader peerGroupLoader;
86 private DOMDataTreeChangeService dataTreeChangeService;
87 private final BGPPeerTracker peerTracker = new BGPPeerTrackerImpl();
91 public void setUp() throws Exception {
93 doReturn(InstanceIdentifier.create(BgpRib.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight
94 .params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib.class, new RibKey(RIB_ID))).when(this.rib)
95 .getInstanceIdentifier();
96 doReturn(this.domTx).when(this.rib).createPeerDOMChain(any(DOMTransactionChainListener.class));
97 doAnswer(invocation -> {
98 final Object[] args = invocation.getArguments();
99 return getDataBroker().createTransactionChain((TransactionChainListener) args[0]);
100 }).when(this.rib).createPeerChain(any(TransactionChainListener.class));
102 doReturn(getDataBroker()).when(this.rib).getDataBroker();
103 doReturn(AS).when(this.rib).getLocalAs();
104 doReturn(mock(RIBSupportContextRegistry.class)).when(this.rib).getRibSupportContext();
105 doReturn(Collections.emptySet()).when(this.rib).getLocalTablesKeys();
106 doNothing().when(this.domTx).close();
107 doReturn(this.domDW).when(this.domTx).newWriteOnlyTransaction();
108 doNothing().when(this.domDW).put(eq(LogicalDatastoreType.OPERATIONAL),
109 any(YangInstanceIdentifier.class), any(MapEntryNode.class));
110 doNothing().when(this.domDW).delete(eq(LogicalDatastoreType.OPERATIONAL),
111 any(YangInstanceIdentifier.class));
112 doNothing().when(this.domDW).merge(eq(LogicalDatastoreType.OPERATIONAL),
113 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
114 doReturn(CommitInfo.emptyFluentFuture()).when(this.domDW).commit();
116 doReturn(YangInstanceIdentifier.of(Rib.QNAME)).when(this.rib).getYangRibId();
117 doReturn(this.dataTreeChangeService).when(this.rib).getService();
118 doReturn(this.listener).when(this.dataTreeChangeService).registerDataTreeChangeListener(any(), any());
119 doReturn(new BgpId("127.0.0.1")).when(this.rib).getBgpIdentifier();
120 doReturn(true).when(this.future).cancel(true);
121 doReturn(this.future).when(this.dispatcher).createReconnectingClient(any(InetSocketAddress.class),
122 any(), anyInt(), any(KeyMapping.class));
123 doReturn(this.dispatcher).when(this.rib).getDispatcher();
125 doReturn(java.util.Optional.of(new BgpTableTypeImpl(Ipv4AddressFamily.class,
126 UnicastSubsequentAddressFamily.class)))
127 .when(this.tableTypeRegistry).getTableType(any());
128 doReturn(java.util.Optional.of(TABLES_KEY)).when(this.tableTypeRegistry).getTableKey(any());
129 doReturn(Collections.singleton(new BgpTableTypeImpl(Ipv4AddressFamily.class,
130 UnicastSubsequentAddressFamily.class)))
131 .when(this.rib).getLocalTables();
133 doNothing().when(this.bgpPeerRegistry).addPeer(any(IpAddressNoZone.class),
134 any(BGPSessionListener.class), any(BGPSessionPreferences.class));
135 doNothing().when(this.bgpPeerRegistry).removePeer(any(IpAddressNoZone.class));
136 doReturn("registry").when(this.bgpPeerRegistry).toString();
137 doNothing().when(this.listener).close();
138 doReturn(this.bgpPeerRegistry).when(this.dispatcher).getBGPPeerRegistry();
139 doReturn(this.peerTracker).when(this.rib).getPeerTracker();
140 doReturn(this.policies).when(this.rib).getRibPolicies();
141 doReturn(null).when(this.peerGroupLoader)
142 .getPeerGroup(any(InstanceIdentifier.class), any(String.class));