defer OVSDB port 6640 opening until system is ready
[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
9 package org.opendaylight.ovsdb.southbound;
10
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;
20
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;
49
50 public class SouthboundProviderTest extends AbstractDataBrokerTest {
51
52     private EntityOwnershipService entityOwnershipService;
53
54     @Before
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));
61     }
62
63     @Test
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)));
68
69         try (SouthboundProvider southboundProvider = new SouthboundProvider(
70                 getDataBroker(),
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))) {
77
78             // Initiate the session
79             southboundProvider.init();
80
81             // Verify that at least one listener was registered
82             verify(entityOwnershipService, atLeastOnce()).registerListener(
83                     anyString(), any(EntityOwnershipListener.class));
84
85             // Verify that a candidate was registered
86             verify(entityOwnershipService).registerCandidate(any(Entity.class));
87         }
88     }
89
90     @Test
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)));
95
96         try (SouthboundProvider southboundProvider = new SouthboundProvider(
97                 getDataBroker(),
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))) {
104
105             // Initiate the session
106             southboundProvider.init();
107
108             // Verify that at least one listener was registered
109             verify(entityOwnershipService, atLeastOnce()).registerListener(
110                     anyString(), any(EntityOwnershipListener.class));
111
112             // Verify that a candidate was registered
113             verify(entityOwnershipService).registerCandidate(any(Entity.class));
114
115             //Close the session
116             southboundProvider.close();
117         }
118     }
119
120     @Test
121     public void testGetDb() {
122         when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
123                 Optional.of(EntityOwnershipState.from(true, true)));
124
125         try (SouthboundProvider southboundProvider = new SouthboundProvider(
126                 getDataBroker(),
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))) {
133
134             southboundProvider.init();
135
136             assertEquals(getDataBroker(), SouthboundProvider.getDb());
137         }
138     }
139
140     @Test
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));
148
149         try (SouthboundProvider southboundProvider = new SouthboundProvider(
150                 getDataBroker(),
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))) {
157
158             southboundProvider.init();
159
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());
165
166             // Become owner
167             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
168                     EntityOwnershipChangeState.from(false, true, true)));
169
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());
175
176             // Verify idempotency
177             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
178                     EntityOwnershipChangeState.from(false, true, true)));
179
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());
185         }
186     }
187
188     private static class ImmediateSystemReadyMonitor implements SystemReadyMonitor {
189
190         @Override
191         public SystemState getSystemState() {
192             return SystemState.ACTIVE;
193         }
194
195         @Override
196         public void registerListener(SystemReadyListener listener) {
197             listener.onSystemBootReady();
198         }
199
200     }
201 }