bug 6579 added dependency queue
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / test / java / org / opendaylight / ovsdb / hwvtepsouthbound / DependencyQueueTest.java
1 /*
2  * Copyright (c) 2016 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 com.google.common.util.concurrent.MoreExecutors;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.DependencyQueue;
15 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.DependentJob;
16 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.HwvtepOperationalState;
17 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.McastMacsRemoteUpdateCommand;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.UnMetDependencyGetter;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.operations.Operations;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsKey;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
29 import org.opendaylight.yangtools.yang.binding.Identifiable;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33 import org.powermock.reflect.Whitebox;
34
35 import java.util.List;
36 import java.util.Map;
37 import java.util.concurrent.CountDownLatch;
38
39 import static org.junit.Assert.assertEquals;
40 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
41
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest({HwvtepConnectionInstance.class, HwvtepConnectionManager.class, Operations.class, DependencyQueue.class})
44 public class DependencyQueueTest extends DataChangeListenerTestBase {
45
46     String[][] terminationPoints = new String[][]{
47             {"192.168.122.20"},
48             {"192.168.122.30"}
49     };
50
51     UnMetDependencyGetter<RemoteMcastMacs> MCAST_MAC_DATA_VALIDATOR;
52     HwvtepOperationalState opState;
53     RemoteMcastMacs mac;
54     InstanceIdentifier<RemoteMcastMacs> macIid;
55     InstanceIdentifier<LogicalSwitches> lsIid;
56     Map<Class<? extends Identifiable>, List<InstanceIdentifier>> unMetDependencies;
57
58     void setupForTest() {
59         MCAST_MAC_DATA_VALIDATOR = Whitebox.getInternalState(McastMacsRemoteUpdateCommand.class, UnMetDependencyGetter.class);
60         opState = new HwvtepOperationalState(connectionInstance);
61         mac = TestBuilders.buildRemoteMcastMacs(nodeIid,"FF:FF:FF:FF:FF:FF", "ls0",
62                 new String[]{"192.168.122.20", "192.168.122.30"});
63         lsIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
64                 child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName("ls0")));
65         macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
66                 child(RemoteMcastMacs.class, new RemoteMcastMacsKey(mac.getKey()));
67         Whitebox.setInternalState(DependencyQueue.class, "executorService", MoreExecutors.sameThreadExecutor());
68     }
69
70     @Test
71     public void testLogicalSwitchConfigDependency() throws Exception {
72         setupForTest();
73         unMetDependencies = MCAST_MAC_DATA_VALIDATOR.getUnMetConfigDependencies(opState, mac);
74         unMetDependencies.remove(TerminationPoint.class);
75
76         final CountDownLatch latch = new CountDownLatch(1);
77         opState.getDeviceInfo().addJobToQueue(new DependentJob.ConfigWaitingJob(macIid, mac, unMetDependencies) {
78             @Override
79             protected void onDependencyResolved(HwvtepOperationalState operationalState, TransactionBuilder transactionBuilder) {
80                 latch.countDown();
81             }
82         });
83         assertEquals(1, latch.getCount());
84         addData(CONFIGURATION, LogicalSwitches.class, new String[]{"ls0", "100"});
85         addData(CONFIGURATION, TerminationPoint.class, terminationPoints);
86         assertEquals(0, latch.getCount());
87     }
88
89     @Test
90     public void testLogicalSwitchInTransitDependency() throws Exception {
91         setupForTest();
92         opState.getDeviceInfo().markKeyAsInTransit(LogicalSwitches.class, lsIid);
93         unMetDependencies = MCAST_MAC_DATA_VALIDATOR.getInTransitDependencies(opState, mac);
94
95         final CountDownLatch latch = new CountDownLatch(1);
96         opState.getDeviceInfo().addJobToQueue(new DependentJob.OpWaitingJob<RemoteMcastMacs>(macIid, mac, unMetDependencies) {
97             @Override
98             protected void onDependencyResolved(HwvtepOperationalState operationalState, TransactionBuilder transactionBuilder) {
99                 latch.countDown();
100             }
101         });
102         opState.getDeviceInfo().onOpDataAvailable();
103         assertEquals(1, latch.getCount());
104
105         opState.getDeviceInfo().updateDeviceOpData(LogicalSwitches.class, lsIid, new UUID("ls0"), "ls0");
106         opState.getDeviceInfo().onOpDataAvailable();
107         assertEquals(0, latch.getCount());
108
109     }
110 }