BUG 9072 - Fix OVSDB TransactionChain memory leak
[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.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;
44
45 public class SouthboundProviderTest extends AbstractDataBrokerTest {
46
47     private EntityOwnershipService entityOwnershipService;
48
49     @Before
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));
56     }
57
58     @Test
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)));
63
64         try (SouthboundProvider southboundProvider = new SouthboundProvider(
65                 getDataBroker(),
66                 entityOwnershipService,
67                 Mockito.mock(OvsdbConnection.class),
68                 Mockito.mock(SchemaService.class),
69                 Mockito.mock(BindingNormalizedNodeSerializer.class))) {
70
71             // Initiate the session
72             southboundProvider.init();
73
74             // Verify that at least one listener was registered
75             verify(entityOwnershipService, atLeastOnce()).registerListener(
76                     anyString(), any(EntityOwnershipListener.class));
77
78             // Verify that a candidate was registered
79             verify(entityOwnershipService).registerCandidate(any(Entity.class));
80         }
81     }
82
83     @Test
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)));
88
89         try (SouthboundProvider southboundProvider = new SouthboundProvider(
90                 getDataBroker(),
91                 entityOwnershipService,
92                 Mockito.mock(OvsdbConnection.class),
93                 Mockito.mock(SchemaService.class),
94                 Mockito.mock(BindingNormalizedNodeSerializer.class))) {
95
96             // Initiate the session
97             southboundProvider.init();
98
99             // Verify that at least one listener was registered
100             verify(entityOwnershipService, atLeastOnce()).registerListener(
101                     anyString(), any(EntityOwnershipListener.class));
102
103             // Verify that a candidate was registered
104             verify(entityOwnershipService).registerCandidate(any(Entity.class));
105
106             //Close the session
107             southboundProvider.close();
108         }
109     }
110
111     @Test
112     public void testGetDb() {
113         when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
114                 Optional.of(new EntityOwnershipState(true, true)));
115
116         try (SouthboundProvider southboundProvider = new SouthboundProvider(
117                 getDataBroker(),
118                 entityOwnershipService,
119                 Mockito.mock(OvsdbConnection.class),
120                 Mockito.mock(SchemaService.class),
121                 Mockito.mock(BindingNormalizedNodeSerializer.class))) {
122
123             southboundProvider.init();
124
125             assertEquals(getDataBroker(), SouthboundProvider.getDb());
126         }
127     }
128
129     @Test
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));
137
138         try (SouthboundProvider southboundProvider = new SouthboundProvider(
139                 getDataBroker(),
140                 entityOwnershipService,
141                 Mockito.mock(OvsdbConnection.class),
142                 Mockito.mock(SchemaService.class),
143                 Mockito.mock(BindingNormalizedNodeSerializer.class))) {
144
145             southboundProvider.init();
146
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());
152
153             // Become owner
154             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity, false, true, true));
155
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());
161
162             // Verify idempotency
163             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity, false, true, true));
164
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());
170         }
171     }
172 }