Bug 5306: Enable the SSL connection for ovs manager
[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 testGetDb() {
85         when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
86                 Optional.of(new EntityOwnershipState(true, true)));
87
88         try (SouthboundProvider southboundProvider = new SouthboundProvider(
89                 getDataBroker(),
90                 entityOwnershipService,
91                 Mockito.mock(OvsdbConnection.class),
92                 Mockito.mock(SchemaService.class),
93                 Mockito.mock(BindingNormalizedNodeSerializer.class))) {
94
95             southboundProvider.init();
96
97             assertEquals(getDataBroker(), SouthboundProvider.getDb());
98         }
99     }
100
101     @Test
102     public void testHandleOwnershipChange() throws ReadFailedException {
103         when(entityOwnershipService.getOwnershipState(any(Entity.class))).thenReturn(
104                 Optional.of(new EntityOwnershipState(false, true)));
105         Entity entity = new Entity("ovsdb-southbound-provider", "ovsdb-southbound-provider");
106         KeyedInstanceIdentifier<Topology, TopologyKey> topologyIid = InstanceIdentifier
107                 .create(NetworkTopology.class)
108                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID));
109
110         try (SouthboundProvider southboundProvider = new SouthboundProvider(
111                 getDataBroker(),
112                 entityOwnershipService,
113                 Mockito.mock(OvsdbConnection.class),
114                 Mockito.mock(SchemaService.class),
115                 Mockito.mock(BindingNormalizedNodeSerializer.class))) {
116
117             southboundProvider.init();
118
119             // At this point the OVSDB topology must not be present in either tree
120             assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
121                     topologyIid).checkedGet().isPresent());
122             assertFalse(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
123                     topologyIid).checkedGet().isPresent());
124
125             // Become owner
126             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity, false, true, true));
127
128             // Now the OVSDB topology must be present in both trees
129             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
130                     topologyIid).checkedGet().isPresent());
131             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
132                     topologyIid).checkedGet().isPresent());
133
134             // Verify idempotency
135             southboundProvider.handleOwnershipChange(new EntityOwnershipChange(entity, false, true, true));
136
137             // The OVSDB topology must be present in both trees
138             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION,
139                     topologyIid).checkedGet().isPresent());
140             assertTrue(getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
141                     topologyIid).checkedGet().isPresent());
142         }
143     }
144 }