Upgrade support of ovsdb reconciliation
[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.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;
50
51 public class SouthboundProviderTest extends AbstractConcurrentDataBrokerTest {
52
53     private EntityOwnershipService entityOwnershipService;
54
55     public SouthboundProviderTest() {
56         super(true);
57     }
58
59     @Before
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));
66     }
67
68     @Test
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)));
73
74         try (SouthboundProvider southboundProvider = new SouthboundProvider(
75                 getDataBroker(),
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))) {
83
84             // Initiate the session
85             southboundProvider.init();
86
87             // Verify that at least one listener was registered
88             verify(entityOwnershipService, atLeastOnce()).registerListener(
89                     anyString(), any(EntityOwnershipListener.class));
90
91             // Verify that a candidate was registered
92             verify(entityOwnershipService).registerCandidate(any(Entity.class));
93         }
94     }
95
96     @Test
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)));
101
102         try (SouthboundProvider southboundProvider = new SouthboundProvider(
103                 getDataBroker(),
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))) {
111
112             // Initiate the session
113             southboundProvider.init();
114
115             // Verify that at least one listener was registered
116             verify(entityOwnershipService, atLeastOnce()).registerListener(
117                     anyString(), any(EntityOwnershipListener.class));
118
119             // Verify that a candidate was registered
120             verify(entityOwnershipService).registerCandidate(any(Entity.class));
121
122             //Close the session
123             southboundProvider.close();
124         }
125     }
126
127     @Test
128     public void testGetDb() {
129         when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
130             java.util.Optional.of(EntityOwnershipState.from(true, true)));
131
132         try (SouthboundProvider southboundProvider = new SouthboundProvider(
133                 getDataBroker(),
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))) {
141
142             southboundProvider.init();
143
144             assertEquals(getDataBroker(), SouthboundProvider.getDb());
145         }
146     }
147
148     @Test
149     public void testHandleOwnershipChange() throws ReadFailedException {
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));
156
157         try (SouthboundProvider southboundProvider = new SouthboundProvider(
158                 getDataBroker(),
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))) {
166
167             southboundProvider.init();
168
169             // At this point the OVSDB topology must not be present in either tree
170             assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
171                     topologyIid).checkedGet().isPresent());
172             assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
173                     topologyIid).checkedGet().isPresent());
174
175             // Become owner
176             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
177                     EntityOwnershipChangeState.from(false, true, true)));
178
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);
183             }
184             assertTrue(southboundProvider.isRegistered());
185
186             // Now the OVSDB topology must be present in both trees
187             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
188                     topologyIid).checkedGet().isPresent());
189             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
190                     topologyIid).checkedGet().isPresent());
191
192             // Verify idempotency
193             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
194                     EntityOwnershipChangeState.from(false, true, true)));
195
196             // The OVSDB topology must be present in both trees
197             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
198                     topologyIid).checkedGet().isPresent());
199             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
200                     topologyIid).checkedGet().isPresent());
201         }
202     }
203 }