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
9 package org.opendaylight.ovsdb.southbound;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyString;
16 import static org.mockito.Mockito.atLeastOnce;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
21 import com.google.common.base.Optional;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
29 import org.opendaylight.infrautils.ready.SystemReadyListener;
30 import org.opendaylight.infrautils.ready.SystemReadyMonitor;
31 import org.opendaylight.infrautils.ready.SystemState;
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;
50 public class SouthboundProviderTest extends AbstractDataBrokerTest {
52 private EntityOwnershipService entityOwnershipService;
55 public void setUp() throws CandidateAlreadyRegisteredException {
56 entityOwnershipService = mock(EntityOwnershipService.class);
57 when(entityOwnershipService.registerListener(anyString(), any(EntityOwnershipListener.class))).thenReturn(
58 mock(EntityOwnershipListenerRegistration.class));
59 when(entityOwnershipService.registerCandidate(any(Entity.class))).thenReturn(mock(
60 EntityOwnershipCandidateRegistration.class));
64 public void testInit() throws CandidateAlreadyRegisteredException {
65 // Indicate that this is the owner
66 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
67 Optional.of(EntityOwnershipState.from(true, true)));
69 try (SouthboundProvider southboundProvider = new SouthboundProvider(
71 entityOwnershipService,
72 Mockito.mock(OvsdbConnection.class),
73 Mockito.mock(DOMSchemaService.class),
74 Mockito.mock(BindingNormalizedNodeSerializer.class),
75 new ImmediateSystemReadyMonitor(),
76 Mockito.mock(DiagStatusService.class))) {
78 // Initiate the session
79 southboundProvider.init();
81 // Verify that at least one listener was registered
82 verify(entityOwnershipService, atLeastOnce()).registerListener(
83 anyString(), any(EntityOwnershipListener.class));
85 // Verify that a candidate was registered
86 verify(entityOwnershipService).registerCandidate(any(Entity.class));
91 public void testInitWithClose() throws CandidateAlreadyRegisteredException {
92 // Indicate that this is the owner
93 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
94 Optional.of(EntityOwnershipState.from(true, true)));
96 try (SouthboundProvider southboundProvider = new SouthboundProvider(
98 entityOwnershipService,
99 Mockito.mock(OvsdbConnection.class),
100 Mockito.mock(DOMSchemaService.class),
101 Mockito.mock(BindingNormalizedNodeSerializer.class),
102 new ImmediateSystemReadyMonitor(),
103 Mockito.mock(DiagStatusService.class))) {
105 // Initiate the session
106 southboundProvider.init();
108 // Verify that at least one listener was registered
109 verify(entityOwnershipService, atLeastOnce()).registerListener(
110 anyString(), any(EntityOwnershipListener.class));
112 // Verify that a candidate was registered
113 verify(entityOwnershipService).registerCandidate(any(Entity.class));
116 southboundProvider.close();
121 public void testGetDb() {
122 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
123 Optional.of(EntityOwnershipState.from(true, true)));
125 try (SouthboundProvider southboundProvider = new SouthboundProvider(
127 entityOwnershipService,
128 Mockito.mock(OvsdbConnection.class),
129 Mockito.mock(DOMSchemaService.class),
130 Mockito.mock(BindingNormalizedNodeSerializer.class),
131 new ImmediateSystemReadyMonitor(),
132 Mockito.mock(DiagStatusService.class))) {
134 southboundProvider.init();
136 assertEquals(getDataBroker(), SouthboundProvider.getDb());
141 public void testHandleOwnershipChange() throws ReadFailedException {
142 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
143 Optional.of(EntityOwnershipState.from(false, true)));
144 Entity entity = new Entity("ovsdb-southbound-provider", "ovsdb-southbound-provider");
145 KeyedInstanceIdentifier<Topology, TopologyKey> topologyIid = InstanceIdentifier
146 .create(NetworkTopology.class)
147 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
149 try (SouthboundProvider southboundProvider = new SouthboundProvider(
151 entityOwnershipService,
152 Mockito.mock(OvsdbConnection.class),
153 Mockito.mock(DOMSchemaService.class),
154 Mockito.mock(BindingNormalizedNodeSerializer.class),
155 new ImmediateSystemReadyMonitor(),
156 Mockito.mock(DiagStatusService.class))) {
158 southboundProvider.init();
160 // At this point the OVSDB topology must not be present in either tree
161 assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
162 topologyIid).checkedGet().isPresent());
163 assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
164 topologyIid).checkedGet().isPresent());
167 southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
168 EntityOwnershipChangeState.from(false, true, true)));
170 // Now the OVSDB topology must be present in both trees
171 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
172 topologyIid).checkedGet().isPresent());
173 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
174 topologyIid).checkedGet().isPresent());
176 // Verify idempotency
177 southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
178 EntityOwnershipChangeState.from(false, true, true)));
180 // The OVSDB topology must be present in both trees
181 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
182 topologyIid).checkedGet().isPresent());
183 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
184 topologyIid).checkedGet().isPresent());
188 private static class ImmediateSystemReadyMonitor implements SystemReadyMonitor {
191 public SystemState getSystemState() {
192 return SystemState.ACTIVE;
196 public void registerListener(SystemReadyListener listener) {
197 listener.onSystemBootReady();