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.clustering.CandidateAlreadyRegisteredException;
27 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
28 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
29 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
30 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
31 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
32 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
33 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
34 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
35 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
36 import org.opendaylight.controller.sal.core.api.model.SchemaService;
37 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
38 import org.opendaylight.ovsdb.lib.OvsdbConnection;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
45 public class SouthboundProviderTest extends AbstractDataBrokerTest {
47 private EntityOwnershipService entityOwnershipService;
50 public void setUp() throws CandidateAlreadyRegisteredException {
51 entityOwnershipService = mock(EntityOwnershipService.class);
52 when(entityOwnershipService.registerListener(anyString(), any(EntityOwnershipListener.class))).thenReturn(
53 mock(EntityOwnershipListenerRegistration.class));
54 when(entityOwnershipService.registerCandidate(any(Entity.class))).thenReturn(mock(
55 EntityOwnershipCandidateRegistration.class));
59 public void testInit() throws CandidateAlreadyRegisteredException {
60 // Indicate that this is the owner
61 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
62 Optional.of(new EntityOwnershipState(true, true)));
64 try (SouthboundProvider southboundProvider = new SouthboundProvider(
66 entityOwnershipService,
67 Mockito.mock(OvsdbConnection.class),
68 Mockito.mock(SchemaService.class),
69 Mockito.mock(BindingNormalizedNodeSerializer.class))) {
71 // Initiate the session
72 southboundProvider.init();
74 // Verify that at least one listener was registered
75 verify(entityOwnershipService, atLeastOnce()).registerListener(
76 anyString(), any(EntityOwnershipListener.class));
78 // Verify that a candidate was registered
79 verify(entityOwnershipService).registerCandidate(any(Entity.class));
84 public void testInitWithClose() throws CandidateAlreadyRegisteredException {
85 // Indicate that this is the owner
86 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
87 Optional.of(new EntityOwnershipState(true, true)));
89 try (SouthboundProvider southboundProvider = new SouthboundProvider(
91 entityOwnershipService,
92 Mockito.mock(OvsdbConnection.class),
93 Mockito.mock(SchemaService.class),
94 Mockito.mock(BindingNormalizedNodeSerializer.class))) {
96 // Initiate the session
97 southboundProvider.init();
99 // Verify that at least one listener was registered
100 verify(entityOwnershipService, atLeastOnce()).registerListener(
101 anyString(), any(EntityOwnershipListener.class));
103 // Verify that a candidate was registered
104 verify(entityOwnershipService).registerCandidate(any(Entity.class));
107 southboundProvider.close();
112 public void testGetDb() {
113 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
114 Optional.of(new EntityOwnershipState(true, true)));
116 try (SouthboundProvider southboundProvider = new SouthboundProvider(
118 entityOwnershipService,
119 Mockito.mock(OvsdbConnection.class),
120 Mockito.mock(SchemaService.class),
121 Mockito.mock(BindingNormalizedNodeSerializer.class))) {
123 southboundProvider.init();
125 assertEquals(getDataBroker(), SouthboundProvider.getDb());
130 public void testHandleOwnershipChange() throws ReadFailedException {
131 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
132 Optional.of(new EntityOwnershipState(false, true)));
133 Entity entity = new Entity("ovsdb-southbound-provider", "ovsdb-southbound-provider");
134 KeyedInstanceIdentifier<Topology, TopologyKey> topologyIid = InstanceIdentifier
135 .create(NetworkTopology.class)
136 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
138 try (SouthboundProvider southboundProvider = new SouthboundProvider(
140 entityOwnershipService,
141 Mockito.mock(OvsdbConnection.class),
142 Mockito.mock(SchemaService.class),
143 Mockito.mock(BindingNormalizedNodeSerializer.class))) {
145 southboundProvider.init();
147 // At this point the OVSDB topology must not be present in either tree
148 assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
149 topologyIid).checkedGet().isPresent());
150 assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
151 topologyIid).checkedGet().isPresent());
154 southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity, false, true, true));
156 // Now the OVSDB topology must be present in both trees
157 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
158 topologyIid).checkedGet().isPresent());
159 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
160 topologyIid).checkedGet().isPresent());
162 // Verify idempotency
163 southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity, false, true, true));
165 // The OVSDB topology must be present in both trees
166 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
167 topologyIid).checkedGet().isPresent());
168 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
169 topologyIid).checkedGet().isPresent());