OVSDB-458 DiagStatus support for OVSDB
[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.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;
46
47 public class SouthboundProviderTest extends AbstractDataBrokerTest {
48
49     private EntityOwnershipService entityOwnershipService;
50
51     @Before
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));
58     }
59
60     @Test
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)));
65
66         try (SouthboundProvider southboundProvider = new SouthboundProvider(
67                 getDataBroker(),
68                 entityOwnershipService,
69                 Mockito.mock(OvsdbConnection.class),
70                 Mockito.mock(DOMSchemaService.class),
71                 Mockito.mock(BindingNormalizedNodeSerializer.class),
72                 Mockito.mock(DiagStatusService.class))) {
73
74             // Initiate the session
75             southboundProvider.init();
76
77             // Verify that at least one listener was registered
78             verify(entityOwnershipService, atLeastOnce()).registerListener(
79                     anyString(), any(EntityOwnershipListener.class));
80
81             // Verify that a candidate was registered
82             verify(entityOwnershipService).registerCandidate(any(Entity.class));
83         }
84     }
85
86     @Test
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)));
91
92         try (SouthboundProvider southboundProvider = new SouthboundProvider(
93                 getDataBroker(),
94                 entityOwnershipService,
95                 Mockito.mock(OvsdbConnection.class),
96                 Mockito.mock(DOMSchemaService.class),
97                 Mockito.mock(BindingNormalizedNodeSerializer.class),
98                 Mockito.mock(DiagStatusService.class))) {
99
100             // Initiate the session
101             southboundProvider.init();
102
103             // Verify that at least one listener was registered
104             verify(entityOwnershipService, atLeastOnce()).registerListener(
105                     anyString(), any(EntityOwnershipListener.class));
106
107             // Verify that a candidate was registered
108             verify(entityOwnershipService).registerCandidate(any(Entity.class));
109
110             //Close the session
111             southboundProvider.close();
112         }
113     }
114
115     @Test
116     public void testGetDb() {
117         when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
118                 Optional.of(EntityOwnershipState.from(true, true)));
119
120         try (SouthboundProvider southboundProvider = new SouthboundProvider(
121                 getDataBroker(),
122                 entityOwnershipService,
123                 Mockito.mock(OvsdbConnection.class),
124                 Mockito.mock(DOMSchemaService.class),
125                 Mockito.mock(BindingNormalizedNodeSerializer.class),
126                 Mockito.mock(DiagStatusService.class))) {
127
128             southboundProvider.init();
129
130             assertEquals(getDataBroker(), SouthboundProvider.getDb());
131         }
132     }
133
134     @Test
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));
142
143         try (SouthboundProvider southboundProvider = new SouthboundProvider(
144                 getDataBroker(),
145                 entityOwnershipService,
146                 Mockito.mock(OvsdbConnection.class),
147                 Mockito.mock(DOMSchemaService.class),
148                 Mockito.mock(BindingNormalizedNodeSerializer.class),
149                 Mockito.mock(DiagStatusService.class))) {
150
151             southboundProvider.init();
152
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());
158
159             // Become owner
160             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
161                     EntityOwnershipChangeState.from(false, true, true)));
162
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());
168
169             // Verify idempotency
170             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity,
171                     EntityOwnershipChangeState.from(false, true, true)));
172
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());
178         }
179     }
180 }