25951074cf4c2e4aeae81c2c1cb51b9b571b391d
[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.AbstractConcurrentDataBrokerTest;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
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 AbstractConcurrentDataBrokerTest {
37
38     private final OvsdbConnection ovsdbConnection = mock(OvsdbConnection.class);
39     private DataBroker dataBroker;
40     private OvsdbDataTreeChangeListener listener;
41
42     public OvsdbDataTreeChangeListenerTest() {
43         super(true);
44     }
45
46     @Before
47     public void setupListener() {
48         dataBroker = getDataBroker();
49         EntityOwnershipService entityOwnershipService = mock(EntityOwnershipService.class);
50         InstanceIdentifierCodec instanceIdentifierCodec = mock(InstanceIdentifierCodec.class);
51         listener = new OvsdbDataTreeChangeListener(dataBroker,
52                 new OvsdbConnectionManager(dataBroker, new TransactionInvokerImpl(dataBroker), entityOwnershipService,
53                         ovsdbConnection, instanceIdentifierCodec), instanceIdentifierCodec);
54     }
55
56     @Test
57     public void testConnect() throws UnknownHostException, InterruptedException, ExecutionException {
58         // Given ...
59
60         // When we request a connection ...
61         InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
62         int port = 6640;
63         IpAddress ipAddress = SouthboundMapper.createIpAddress(inetAddress);
64         PortNumber portNumber = new PortNumber(port);
65
66         final ConnectionInfo connectionInfo = new ConnectionInfoBuilder()
67                 .setRemoteIp(ipAddress)
68                 .setRemotePort(portNumber)
69                 .build();
70         final InstanceIdentifier<Node> iid = SouthboundUtils.createInstanceIdentifier(connectionInfo);
71         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
72         transaction.put(LogicalDatastoreType.CONFIGURATION, iid, SouthboundUtils.createNode(connectionInfo),
73                 WriteTransaction.CREATE_MISSING_PARENTS);
74         transaction.submit().get();
75
76         // Then the listener tries to open a connection
77         Mockito.verify(ovsdbConnection, Mockito.timeout(5000)).connect(inetAddress, port);
78     }
79 }