ba5dce98292137610992047bcd135efe4309322e
[ovsdb.git] / southbound / southbound-impl / src / test / java / org / opendaylight / ovsdb / southbound / SouthboundProviderTest.java
1 /*
2  * Copyright (c) 2015 Inocybe Technologies and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.southbound;
9
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;
20
21 import com.google.common.base.Stopwatch;
22 import com.google.common.util.concurrent.Uninterruptibles;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
30 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
31 import org.opendaylight.infrautils.ready.testutils.TestSystemReadyMonitor;
32 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
49
50 public class SouthboundProviderTest extends AbstractConcurrentDataBrokerTest {
51
52     private EntityOwnershipService entityOwnershipService;
53
54     public SouthboundProviderTest() {
55         super(true);
56     }
57
58     @Before
59     public void setUp() throws CandidateAlreadyRegisteredException {
60         entityOwnershipService = mock(EntityOwnershipService.class);
61         when(entityOwnershipService.registerListener(anyString(), any(EntityOwnershipListener.class))).thenReturn(
62                 mock(EntityOwnershipListenerRegistration.class));
63         when(entityOwnershipService.registerCandidate(any(Entity.class))).thenReturn(mock(
64                 EntityOwnershipCandidateRegistration.class));
65     }
66
67     @Test
68     public void testInit() throws CandidateAlreadyRegisteredException {
69         // Indicate that this is the owner
70         when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
71                 java.util.Optional.of(EntityOwnershipState.from(true, true)));
72
73         try (SouthboundProvider southboundProvider = new SouthboundProvider(
74                 getDataBroker(),
75                 entityOwnershipService,
76                 Mockito.mock(OvsdbConnection.class),
77                 Mockito.mock(DOMSchemaService.class),
78                 Mockito.mock(BindingNormalizedNodeSerializer.class),
79                 new TestSystemReadyMonitor(IMMEDIATE),
80                 Mockito.mock(DiagStatusService.class))) {
81
82             // Initiate the session
83             southboundProvider.init();
84
85             // Verify that at least one listener was registered
86             verify(entityOwnershipService, atLeastOnce()).registerListener(
87                     anyString(), any(EntityOwnershipListener.class));
88
89             // Verify that a candidate was registered
90             verify(entityOwnershipService).registerCandidate(any(Entity.class));
91         }
92     }
93
94     @Test
95     public void testInitWithClose() throws CandidateAlreadyRegisteredException {
96         // Indicate that this is the owner
97         when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
98                 java.util.Optional.of(EntityOwnershipState.from(true, true)));
99
100         try (SouthboundProvider southboundProvider = new SouthboundProvider(
101                 getDataBroker(),
102                 entityOwnershipService,
103                 Mockito.mock(OvsdbConnection.class),
104                 Mockito.mock(DOMSchemaService.class),
105                 Mockito.mock(BindingNormalizedNodeSerializer.class),
106                 new TestSystemReadyMonitor(IMMEDIATE),
107                 Mockito.mock(DiagStatusService.class))) {
108
109             // Initiate the session
110             southboundProvider.init();
111
112             // Verify that at least one listener was registered
113             verify(entityOwnershipService, atLeastOnce()).registerListener(
114                     anyString(), any(EntityOwnershipListener.class));
115
116             // Verify that a candidate was registered
117             verify(entityOwnershipService).registerCandidate(any(Entity.class));
118
119             //Close the session
120             southboundProvider.close();
121         }
122     }
123
124     @Test
125     public void testGetDb() {
126         when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
127             java.util.Optional.of(EntityOwnershipState.from(true, true)));
128
129         try (SouthboundProvider southboundProvider = new SouthboundProvider(
130                 getDataBroker(),
131                 entityOwnershipService,
132                 Mockito.mock(OvsdbConnection.class),
133                 Mockito.mock(DOMSchemaService.class),
134                 Mockito.mock(BindingNormalizedNodeSerializer.class),
135                 new TestSystemReadyMonitor(IMMEDIATE),
136                 Mockito.mock(DiagStatusService.class))) {
137
138             southboundProvider.init();
139
140             assertEquals(getDataBroker(), SouthboundProvider.getDb());
141         }
142     }
143
144     @Test
145     public void testHandleOwnershipChange() throws ReadFailedException {
146         when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
147             java.util.Optional.of(EntityOwnershipState.from(false, true)));
148         Entity entity = new Entity("ovsdb-southbound-provider", "ovsdb-southbound-provider");
149         KeyedInstanceIdentifier<Topology, TopologyKey> topologyIid = InstanceIdentifier
150                 .create(NetworkTopology.class)
151                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
152
153         try (SouthboundProvider southboundProvider = new SouthboundProvider(
154                 getDataBroker(),
155                 entityOwnershipService,
156                 Mockito.mock(OvsdbConnection.class),
157                 Mockito.mock(DOMSchemaService.class),
158                 Mockito.mock(BindingNormalizedNodeSerializer.class),
159                 new TestSystemReadyMonitor(IMMEDIATE),
160                 Mockito.mock(DiagStatusService.class))) {
161
162             southboundProvider.init();
163
164             // At this point the OVSDB topology must not be present in either tree
165             assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
166                     topologyIid).checkedGet().isPresent());
167             assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
168                     topologyIid).checkedGet().isPresent());
169
170             // Become owner
171             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
172                     EntityOwnershipChangeState.from(false, true, true)));
173
174             // Up to 3 seconds for DTCL to settle
175             final Stopwatch sw = Stopwatch.createStarted();
176             while (!southboundProvider.isRegistered() && sw.elapsed(TimeUnit.SECONDS) < 3) {
177                 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
178             }
179             assertTrue(southboundProvider.isRegistered());
180
181             // Now the OVSDB topology must be present in both trees
182             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
183                     topologyIid).checkedGet().isPresent());
184             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
185                     topologyIid).checkedGet().isPresent());
186
187             // Verify idempotency
188             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
189                     EntityOwnershipChangeState.from(false, true, true)));
190
191             // The OVSDB topology must be present in both trees
192             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
193                     topologyIid).checkedGet().isPresent());
194             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
195                     topologyIid).checkedGet().isPresent());
196         }
197     }
198 }