MD-SAL API integration
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / test / java / org / opendaylight / ovsdb / hwvtepsouthbound / DependencyQueueTest.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.hwvtepsouthbound;
10
11 import static org.junit.Assert.assertEquals;
12
13 import java.util.List;
14 import java.util.Map;
15 import java.util.concurrent.CountDownLatch;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mockito;
19 import org.mockito.junit.MockitoJUnitRunner;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.DependencyQueue;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.DependentJob;
23 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.HwvtepOperationalState;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.McastMacsRemoteUpdateCommand;
25 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.UnMetDependencyGetter;
26 import org.opendaylight.ovsdb.lib.notation.UUID;
27 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
35 import org.opendaylight.yangtools.yang.binding.Identifiable;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.powermock.reflect.Whitebox;
38
39 @RunWith(MockitoJUnitRunner.Silent.class)
40 public class DependencyQueueTest extends DataChangeListenerTestBase {
41
42     String[][] terminationPoints = new String[][]{
43             {"192.168.122.20"},
44             {"192.168.122.30"}
45     };
46
47     UnMetDependencyGetter<RemoteMcastMacs> mcastMacDataValidator;
48     HwvtepOperationalState opState;
49     RemoteMcastMacs mac;
50     InstanceIdentifier<RemoteMcastMacs> macIid;
51     InstanceIdentifier<LogicalSwitches> lsIid;
52     Map<Class<? extends Identifiable>, List<InstanceIdentifier>> unMetDependencies;
53
54     void setupForTest() throws Exception {
55         mcastMacDataValidator = Whitebox.getInternalState(McastMacsRemoteUpdateCommand.class,
56                 UnMetDependencyGetter.class);
57         opState = new HwvtepOperationalState(connectionInstance);
58         mac = TestBuilders.buildRemoteMcastMacs(nodeIid,"FF:FF:FF:FF:FF:FF", "ls0",
59                 new String[]{"192.168.122.20", "192.168.122.30"});
60         lsIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
61                 .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName("ls0")));
62         macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
63                 .child(RemoteMcastMacs.class, new RemoteMcastMacsKey(mac.key()));
64         setFinalStatic(DependencyQueue.class, "EXECUTOR_SERVICE", Mockito.mock(SameThreadScheduledExecutor.class,
65                 Mockito.CALLS_REAL_METHODS));
66     }
67
68     @Test
69     public void testLogicalSwitchConfigDependency() throws Exception {
70         setupForTest();
71         unMetDependencies = mcastMacDataValidator.getUnMetConfigDependencies(opState, mac);
72         unMetDependencies.remove(TerminationPoint.class);
73
74         final CountDownLatch latch = new CountDownLatch(1);
75         opState.getDeviceInfo().addJobToQueue(new DependentJob.ConfigWaitingJob(macIid, mac, unMetDependencies) {
76             @Override
77             protected void onDependencyResolved(HwvtepOperationalState operationalState,
78                     TransactionBuilder transactionBuilder) {
79                 latch.countDown();
80             }
81         });
82         assertEquals(1, latch.getCount());
83         addData(LogicalDatastoreType.CONFIGURATION, LogicalSwitches.class, new String[]{"ls0", "100"});
84         addData(LogicalDatastoreType.CONFIGURATION, TerminationPoint.class, terminationPoints);
85         assertEquals(0, latch.getCount());
86     }
87
88     @Test
89     public void testLogicalSwitchInTransitDependency() throws Exception {
90         setupForTest();
91         opState.getDeviceInfo().markKeyAsInTransit(LogicalSwitches.class, lsIid);
92         unMetDependencies = mcastMacDataValidator.getInTransitDependencies(opState, mac);
93
94         final CountDownLatch latch = new CountDownLatch(1);
95         opState.getDeviceInfo().addJobToQueue(new DependentJob.OpWaitingJob<RemoteMcastMacs>(
96                 macIid, mac, (Map)unMetDependencies, 0) {
97             @Override
98             protected void onDependencyResolved(HwvtepOperationalState operationalState,
99                     TransactionBuilder transactionBuilder) {
100                 latch.countDown();
101             }
102         });
103         opState.getDeviceInfo().onOperDataAvailable();
104         assertEquals(1, latch.getCount());
105
106         opState.getDeviceInfo().updateDeviceOperData(LogicalSwitches.class, lsIid, new UUID("ls0"), "ls0");
107         opState.getDeviceInfo().onOperDataAvailable();
108         //wait for sometime so that the onDependencyResolved is triggered
109         Thread.sleep(500);
110         assertEquals(0, latch.getCount());
111
112     }
113 }