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.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
31 import org.opendaylight.mdsal.eos.binding.api.Entity;
32 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
33 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
34 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
35 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
36 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
37 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
38 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
39 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
40 import org.opendaylight.ovsdb.lib.OvsdbConnection;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 public class SouthboundProviderTest extends AbstractDataBrokerTest {
49 private EntityOwnershipService entityOwnershipService;
52 public void setUp() throws CandidateAlreadyRegisteredException {
53 entityOwnershipService = mock(EntityOwnershipService.class);
54 when(entityOwnershipService.registerListener(anyString(), any(EntityOwnershipListener.class))).thenReturn(
55 mock(EntityOwnershipListenerRegistration.class));
56 when(entityOwnershipService.registerCandidate(any(Entity.class))).thenReturn(mock(
57 EntityOwnershipCandidateRegistration.class));
61 public void testInit() throws CandidateAlreadyRegisteredException {
62 // Indicate that this is the owner
63 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
64 Optional.of(EntityOwnershipState.from(true, true)));
66 try (SouthboundProvider southboundProvider = new SouthboundProvider(
68 entityOwnershipService,
69 Mockito.mock(OvsdbConnection.class),
70 Mockito.mock(DOMSchemaService.class),
71 Mockito.mock(BindingNormalizedNodeSerializer.class),
72 Mockito.mock(DiagStatusService.class))) {
74 // Initiate the session
75 southboundProvider.init();
77 // Verify that at least one listener was registered
78 verify(entityOwnershipService, atLeastOnce()).registerListener(
79 anyString(), any(EntityOwnershipListener.class));
81 // Verify that a candidate was registered
82 verify(entityOwnershipService).registerCandidate(any(Entity.class));
87 public void testInitWithClose() throws CandidateAlreadyRegisteredException {
88 // Indicate that this is the owner
89 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
90 Optional.of(EntityOwnershipState.from(true, true)));
92 try (SouthboundProvider southboundProvider = new SouthboundProvider(
94 entityOwnershipService,
95 Mockito.mock(OvsdbConnection.class),
96 Mockito.mock(DOMSchemaService.class),
97 Mockito.mock(BindingNormalizedNodeSerializer.class),
98 Mockito.mock(DiagStatusService.class))) {
100 // Initiate the session
101 southboundProvider.init();
103 // Verify that at least one listener was registered
104 verify(entityOwnershipService, atLeastOnce()).registerListener(
105 anyString(), any(EntityOwnershipListener.class));
107 // Verify that a candidate was registered
108 verify(entityOwnershipService).registerCandidate(any(Entity.class));
111 southboundProvider.close();
116 public void testGetDb() {
117 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
118 Optional.of(EntityOwnershipState.from(true, true)));
120 try (SouthboundProvider southboundProvider = new SouthboundProvider(
122 entityOwnershipService,
123 Mockito.mock(OvsdbConnection.class),
124 Mockito.mock(DOMSchemaService.class),
125 Mockito.mock(BindingNormalizedNodeSerializer.class),
126 Mockito.mock(DiagStatusService.class))) {
128 southboundProvider.init();
130 assertEquals(getDataBroker(), SouthboundProvider.getDb());
135 public void testHandleOwnershipChange() throws ReadFailedException {
136 when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
137 Optional.of(EntityOwnershipState.from(false, true)));
138 Entity entity = new Entity("ovsdb-southbound-provider", "ovsdb-southbound-provider");
139 KeyedInstanceIdentifier<Topology, TopologyKey> topologyIid = InstanceIdentifier
140 .create(NetworkTopology.class)
141 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
143 try (SouthboundProvider southboundProvider = new SouthboundProvider(
145 entityOwnershipService,
146 Mockito.mock(OvsdbConnection.class),
147 Mockito.mock(DOMSchemaService.class),
148 Mockito.mock(BindingNormalizedNodeSerializer.class),
149 Mockito.mock(DiagStatusService.class))) {
151 southboundProvider.init();
153 // At this point the OVSDB topology must not be present in either tree
154 assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
155 topologyIid).checkedGet().isPresent());
156 assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
157 topologyIid).checkedGet().isPresent());
160 southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
161 EntityOwnershipChangeState.from(false, true, true)));
163 // Now the OVSDB topology must be present in both trees
164 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
165 topologyIid).checkedGet().isPresent());
166 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
167 topologyIid).checkedGet().isPresent());
169 // Verify idempotency
170 southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
171 EntityOwnershipChangeState.from(false, true, true)));
173 // The OVSDB topology must be present in both trees
174 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
175 topologyIid).checkedGet().isPresent());
176 assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
177 topologyIid).checkedGet().isPresent());