2 * Copyright (c) 2015 Inocybe Technologies 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.ovsdb.southbound;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.anyString;
15 import static org.mockito.Mockito.atLeastOnce;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19 import static org.opendaylight.infrautils.ready.testutils.TestSystemReadyMonitor.Behaviour.IMMEDIATE;
21 import com.google.common.base.Stopwatch;
22 import com.google.common.util.concurrent.Uninterruptibles;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.TimeUnit;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
29 import org.opendaylight.infrautils.ready.testutils.TestSystemReadyMonitor;
30 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
31 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
32 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
33 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
34 import org.opendaylight.mdsal.eos.binding.api.Entity;
35 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
36 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
37 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
38 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
39 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
40 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
41 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
42 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
43 import org.opendaylight.ovsdb.lib.OvsdbConnection;
44 import org.opendaylight.serviceutils.upgrade.UpgradeState;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
51 public class SouthboundProviderTest extends AbstractConcurrentDataBrokerTest {
53 private EntityOwnershipService entityOwnershipService;
55 public SouthboundProviderTest() {
60 public void setUp() throws CandidateAlreadyRegisteredException {
61 entityOwnershipService = mock(EntityOwnershipService.class);
62 when(entityOwnershipService.registerListener(anyString(), any(EntityOwnershipListener.class))).thenReturn(
63 mock(EntityOwnershipListenerRegistration.class));
64 when(entityOwnershipService.registerCandidate(any(Entity.class))).thenReturn(mock(
65 EntityOwnershipCandidateRegistration.class));
69 public void testInit() throws CandidateAlreadyRegisteredException {
70 // Indicate that this is the owner
71 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
72 java.util.Optional.of(EntityOwnershipState.from(true, true)));
74 try (SouthboundProvider southboundProvider = new SouthboundProvider(
76 entityOwnershipService,
77 Mockito.mock(OvsdbConnection.class),
78 Mockito.mock(DOMSchemaService.class),
79 Mockito.mock(BindingNormalizedNodeSerializer.class),
80 new TestSystemReadyMonitor(IMMEDIATE),
81 Mockito.mock(DiagStatusService.class),
82 Mockito.mock(UpgradeState.class))) {
84 // Initiate the session
85 southboundProvider.init();
87 // Verify that at least one listener was registered
88 verify(entityOwnershipService, atLeastOnce()).registerListener(
89 anyString(), any(EntityOwnershipListener.class));
91 // Verify that a candidate was registered
92 verify(entityOwnershipService).registerCandidate(any(Entity.class));
97 public void testInitWithClose() throws CandidateAlreadyRegisteredException {
98 // Indicate that this is the owner
99 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
100 java.util.Optional.of(EntityOwnershipState.from(true, true)));
102 try (SouthboundProvider southboundProvider = new SouthboundProvider(
104 entityOwnershipService,
105 Mockito.mock(OvsdbConnection.class),
106 Mockito.mock(DOMSchemaService.class),
107 Mockito.mock(BindingNormalizedNodeSerializer.class),
108 new TestSystemReadyMonitor(IMMEDIATE),
109 Mockito.mock(DiagStatusService.class),
110 Mockito.mock(UpgradeState.class))) {
112 // Initiate the session
113 southboundProvider.init();
115 // Verify that at least one listener was registered
116 verify(entityOwnershipService, atLeastOnce()).registerListener(
117 anyString(), any(EntityOwnershipListener.class));
119 // Verify that a candidate was registered
120 verify(entityOwnershipService).registerCandidate(any(Entity.class));
123 southboundProvider.close();
128 public void testGetDb() {
129 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
130 java.util.Optional.of(EntityOwnershipState.from(true, true)));
132 try (SouthboundProvider southboundProvider = new SouthboundProvider(
134 entityOwnershipService,
135 Mockito.mock(OvsdbConnection.class),
136 Mockito.mock(DOMSchemaService.class),
137 Mockito.mock(BindingNormalizedNodeSerializer.class),
138 new TestSystemReadyMonitor(IMMEDIATE),
139 Mockito.mock(DiagStatusService.class),
140 Mockito.mock(UpgradeState.class))) {
142 southboundProvider.init();
144 assertEquals(getDataBroker(), SouthboundProvider.getDb());
149 public void testHandleOwnershipChange() throws ExecutionException, InterruptedException {
150 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
151 java.util.Optional.of(EntityOwnershipState.from(false, true)));
152 Entity entity = new Entity("ovsdb-southbound-provider", "ovsdb-southbound-provider");
153 KeyedInstanceIdentifier<Topology, TopologyKey> topologyIid = InstanceIdentifier
154 .create(NetworkTopology.class)
155 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
157 try (SouthboundProvider southboundProvider = new SouthboundProvider(
159 entityOwnershipService,
160 Mockito.mock(OvsdbConnection.class),
161 Mockito.mock(DOMSchemaService.class),
162 Mockito.mock(BindingNormalizedNodeSerializer.class),
163 new TestSystemReadyMonitor(IMMEDIATE),
164 Mockito.mock(DiagStatusService.class),
165 Mockito.mock(UpgradeState.class))) {
167 southboundProvider.init();
169 // At this point the OVSDB topology must not be present in either tree
170 assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
171 topologyIid).get().isPresent());
172 assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
173 topologyIid).get().isPresent());
176 southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
177 EntityOwnershipChangeState.from(false, true, true)));
179 // Up to 3 seconds for DTCL to settle
180 final Stopwatch sw = Stopwatch.createStarted();
181 while (!southboundProvider.isRegistered() && sw.elapsed(TimeUnit.SECONDS) < 3) {
182 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
184 assertTrue(southboundProvider.isRegistered());
186 // Now the OVSDB topology must be present in both trees
187 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
188 topologyIid).get().isPresent());
189 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
190 topologyIid).get().isPresent());
192 // Verify idempotency
193 southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
194 EntityOwnershipChangeState.from(false, true, true)));
196 // The OVSDB topology must be present in both trees
197 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
198 topologyIid).get().isPresent());
199 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
200 topologyIid).get().isPresent());