Use key tunnel remove events on tunnel name
[genius.git] / mdsalutil / mdsalutil-impl / src / test / java / org / opendaylight / genius / utils / hwvtep / internal / HwvtepNodeHACacheImplTest.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies 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 package org.opendaylight.genius.utils.hwvtep.internal;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableSet;
16 import org.junit.Test;
17 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25
26 public class HwvtepNodeHACacheImplTest {
27     private final HwvtepNodeHACacheImpl hwvtepNodeHACacheImpl = new HwvtepNodeHACacheImpl();
28
29     @Test
30     public void testAddChild() {
31         InstanceIdentifier<Node> parent = newNodeInstanceIdentifier("ha");
32         InstanceIdentifier<Node> child1 = newNodeInstanceIdentifier("d1");
33
34         hwvtepNodeHACacheImpl.addChild(parent, child1);
35
36         assertTrue(hwvtepNodeHACacheImpl.isHAEnabledDevice(child1));
37         assertTrue(hwvtepNodeHACacheImpl.isHAParentNode(parent));
38
39         InstanceIdentifier<Node> child2 = newNodeInstanceIdentifier("d1");
40         hwvtepNodeHACacheImpl.addChild(parent, child2);
41         assertTrue(hwvtepNodeHACacheImpl.isHAEnabledDevice(child1));
42         assertTrue(hwvtepNodeHACacheImpl.isHAEnabledDevice(child2));
43         assertTrue(hwvtepNodeHACacheImpl.isHAParentNode(parent));
44
45         assertEquals(ImmutableSet.of(child1, child2), hwvtepNodeHACacheImpl.getHAChildNodes());
46         assertEquals(ImmutableSet.of(parent), hwvtepNodeHACacheImpl.getHAParentNodes());
47
48         assertEquals(ImmutableSet.of(child1, child2), hwvtepNodeHACacheImpl.getChildrenForHANode(parent));
49
50         hwvtepNodeHACacheImpl.removeParent(parent);
51         assertFalse(hwvtepNodeHACacheImpl.isHAEnabledDevice(child1));
52         assertFalse(hwvtepNodeHACacheImpl.isHAEnabledDevice(child2));
53         assertFalse(hwvtepNodeHACacheImpl.isHAParentNode(parent));
54     }
55
56     @Test
57     public void testNodeConnectionStatus() {
58         InstanceIdentifier<Node> node1 = newNodeInstanceIdentifier("node1");
59         InstanceIdentifier<Node> node2 = newNodeInstanceIdentifier("node2");
60
61         hwvtepNodeHACacheImpl.updateConnectedNodeStatus(node1);
62         assertEquals(ImmutableMap.of("node1", Boolean.TRUE), hwvtepNodeHACacheImpl.getNodeConnectionStatuses());
63
64         hwvtepNodeHACacheImpl.updateConnectedNodeStatus(node2);
65         assertEquals(ImmutableMap.of("node1", Boolean.TRUE, "node2", Boolean.TRUE),
66                 hwvtepNodeHACacheImpl.getNodeConnectionStatuses());
67
68         hwvtepNodeHACacheImpl.updateDisconnectedNodeStatus(node1);
69         assertEquals(ImmutableMap.of("node1", Boolean.FALSE, "node2", Boolean.TRUE),
70                 hwvtepNodeHACacheImpl.getNodeConnectionStatuses());
71     }
72
73     private static InstanceIdentifier<Node> newNodeInstanceIdentifier(String id) {
74         NodeId nodeId = new NodeId(id);
75         NodeKey nodeKey = new NodeKey(nodeId);
76         TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
77         return InstanceIdentifier.builder(NetworkTopology.class)
78                 .child(Topology.class, topoKey)
79                 .child(Node.class, nodeKey)
80                 .build();
81     }
82 }