f66ee09e74f967e6375f9b96022a4b397a2f38b5
[ovsdb.git] / southbound / southbound-impl / src / test / java / org / opendaylight / ovsdb / southbound / OvsdbDataTreeChangeListenerTest.java
1 /*
2  * Copyright © 2016 Red Hat, Inc. and others.
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 package org.opendaylight.ovsdb.southbound;
9
10 import static org.mockito.Mockito.mock;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.concurrent.ExecutionException;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mockito;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.ovsdb.lib.OvsdbConnection;
24 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvokerImpl;
25 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32
33 /**
34  * Unit tests for the data-tree change listener.
35  */
36 public class OvsdbDataTreeChangeListenerTest extends AbstractDataBrokerTest {
37
38     private final OvsdbConnection ovsdbConnection = mock(OvsdbConnection.class);
39     private DataBroker dataBroker;
40     private OvsdbDataTreeChangeListener listener;
41
42     @Before
43     public void setupListener() {
44         dataBroker = getDataBroker();
45         EntityOwnershipService entityOwnershipService = mock(EntityOwnershipService.class);
46         InstanceIdentifierCodec instanceIdentifierCodec = mock(InstanceIdentifierCodec.class);
47         listener = new OvsdbDataTreeChangeListener(dataBroker,
48                 new OvsdbConnectionManager(dataBroker, new TransactionInvokerImpl(dataBroker), entityOwnershipService,
49                         ovsdbConnection, instanceIdentifierCodec), instanceIdentifierCodec);
50     }
51
52     @Test
53     public void testConnect() throws UnknownHostException, InterruptedException, ExecutionException {
54         // Given ...
55
56         // When we request a connection ...
57         InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
58         int port = 6640;
59         IpAddress ipAddress = SouthboundMapper.createIpAddress(inetAddress);
60         PortNumber portNumber = new PortNumber(port);
61
62         final ConnectionInfo connectionInfo = new ConnectionInfoBuilder()
63                 .setRemoteIp(ipAddress)
64                 .setRemotePort(portNumber)
65                 .build();
66         final InstanceIdentifier<Node> iid = SouthboundUtils.createInstanceIdentifier(connectionInfo);
67         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
68         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, SouthboundUtils.createNode(connectionInfo),
69                 WriteTransaction.CREATE_MISSING_PARENTS);
70         transaction.submit().get();
71
72         // Then the listener tries to open a connection
73         Mockito.verify(ovsdbConnection).connect(inetAddress, port);
74     }
75 }