bug 8029 handle expired in transit entries
[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 org.junit.Test;
12 import org.junit.runner.RunWith;
13 import org.mockito.Mockito;
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.api.mockito.PowerMockito;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.junit4.PowerMockRunner;
34 import org.powermock.reflect.Whitebox;
35
36 import java.util.List;
37 import java.util.Map;
38 import java.util.concurrent.CountDownLatch;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
42
43 @RunWith(PowerMockRunner.class)
44 @PrepareForTest({HwvtepConnectionInstance.class, HwvtepConnectionManager.class, Operations.class, DependencyQueue.class})
45 public class DependencyQueueTest extends DataChangeListenerTestBase {
46
47     String[][] terminationPoints = new String[][]{
48             {"192.168.122.20"},
49             {"192.168.122.30"}
50     };
51
52     UnMetDependencyGetter<RemoteMcastMacs> MCAST_MAC_DATA_VALIDATOR;
53     HwvtepOperationalState opState;
54     RemoteMcastMacs mac;
55     InstanceIdentifier<RemoteMcastMacs> macIid;
56     InstanceIdentifier<LogicalSwitches> lsIid;
57     Map<Class<? extends Identifiable>, List<InstanceIdentifier>> unMetDependencies;
58
59     void setupForTest() throws Exception {
60         MCAST_MAC_DATA_VALIDATOR = Whitebox.getInternalState(McastMacsRemoteUpdateCommand.class, UnMetDependencyGetter.class);
61         opState = new HwvtepOperationalState(connectionInstance);
62         mac = TestBuilders.buildRemoteMcastMacs(nodeIid,"FF:FF:FF:FF:FF:FF", "ls0",
63                 new String[]{"192.168.122.20", "192.168.122.30"});
64         lsIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
65                 child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName("ls0")));
66         macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
67                 child(RemoteMcastMacs.class, new RemoteMcastMacsKey(mac.getKey()));
68         setFinalStatic(DependencyQueue.class, "executorService", PowerMockito.mock(SameThreadScheduledExecutor.class, Mockito.CALLS_REAL_METHODS));
69     }
70
71     @Test
72     public void testLogicalSwitchConfigDependency() throws Exception {
73         setupForTest();
74         unMetDependencies = MCAST_MAC_DATA_VALIDATOR.getUnMetConfigDependencies(opState, mac);
75         unMetDependencies.remove(TerminationPoint.class);
76
77         final CountDownLatch latch = new CountDownLatch(1);
78         opState.getDeviceInfo().addJobToQueue(new DependentJob.ConfigWaitingJob(macIid, mac, unMetDependencies) {
79             @Override
80             protected void onDependencyResolved(HwvtepOperationalState operationalState, TransactionBuilder transactionBuilder) {
81                 latch.countDown();
82             }
83         });
84         assertEquals(1, latch.getCount());
85         addData(CONFIGURATION, LogicalSwitches.class, new String[]{"ls0", "100"});
86         addData(CONFIGURATION, TerminationPoint.class, terminationPoints);
87         assertEquals(0, latch.getCount());
88     }
89
90     @Test
91     public void testLogicalSwitchInTransitDependency() throws Exception {
92         setupForTest();
93         opState.getDeviceInfo().markKeyAsInTransit(LogicalSwitches.class, lsIid);
94         unMetDependencies = MCAST_MAC_DATA_VALIDATOR.getInTransitDependencies(opState, mac);
95
96         final CountDownLatch latch = new CountDownLatch(1);
97         opState.getDeviceInfo().addJobToQueue(new DependentJob.OpWaitingJob<RemoteMcastMacs>(macIid, mac, unMetDependencies) {
98             @Override
99             protected void onDependencyResolved(HwvtepOperationalState operationalState, 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         assertEquals(0, latch.getCount());
109
110     }
111 }