Merge " changes for REST API to make TZ specific parameters (transport zone name...
[genius.git] / mdsalutil / mdsalutil-api / src / test / java / org / opendaylight / genius / mdsalutil / hwvtep / HwvtepHACacheTest.java
1 /*
2  * Copyright (c) 2016 Red Hat 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.mdsalutil.hwvtep;
9
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.genius.utils.hwvtep.DebugEvent;
14 import org.opendaylight.genius.utils.hwvtep.HwvtepHACache;
15 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
16 import org.opendaylight.genius.utils.hwvtep.NodeEvent;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
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 import java.util.ArrayList;
27 import java.util.List;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertTrue;
32
33 public class HwvtepHACacheTest {
34
35     HwvtepHACache hwvtepHACache = HwvtepHACache.getInstance();
36
37     @Test
38     public void testAddHAChild() {
39
40         InstanceIdentifier<Node> parent = newNodeInstanceIdentifier("ha");
41         InstanceIdentifier<Node> child1 = newNodeInstanceIdentifier("d1");
42         InstanceIdentifier<Node> child2 = newNodeInstanceIdentifier("d1");
43         String child1NodeId = child1.firstKeyOf(Node.class).getNodeId().getValue();
44         String child2NodeId = child2.firstKeyOf(Node.class).getNodeId().getValue();
45
46         hwvtepHACache.addChild(parent, child1);
47
48         assertTrue(hwvtepHACache.isHAEnabledDevice(child1));
49         assertTrue(hwvtepHACache.isHAParentNode(parent));
50
51         hwvtepHACache.addChild(parent, child2);
52         assertTrue(hwvtepHACache.isHAEnabledDevice(child1));
53         assertTrue(hwvtepHACache.isHAEnabledDevice(child2));
54         assertTrue(hwvtepHACache.isHAParentNode(parent));
55
56         assertTrue(hwvtepHACache.getHAChildNodes().contains(child1));
57         assertTrue(hwvtepHACache.getHAChildNodes().contains(child2));
58         assertTrue(hwvtepHACache.getHAParentNodes().contains(parent));
59
60         assertTrue(hwvtepHACache.getChildrenForHANode(parent).contains(child1));
61         assertTrue(hwvtepHACache.getChildrenForHANode(parent).contains(child2));
62
63         List<DebugEvent> events = hwvtepHACache.getNodeEvents();
64         assertTrue(events.contains(new NodeEvent.ChildAddedEvent(child1NodeId)));
65         assertTrue(events.contains(new NodeEvent.ChildAddedEvent(child2NodeId)));
66
67         hwvtepHACache.updateConnectedNodeStatus(child1);
68         assertTrue(hwvtepHACache.getNodeEvents().contains(new NodeEvent.NodeConnectedEvent(child1NodeId)));
69
70         hwvtepHACache.updateDisconnectedNodeStatus(child1);
71         assertTrue(hwvtepHACache.getNodeEvents().contains(new NodeEvent.NodeDisconnectedEvent(child1NodeId)));
72
73         hwvtepHACache.cleanupParent(parent);
74         assertFalse(hwvtepHACache.isHAEnabledDevice(child1));
75         assertFalse(hwvtepHACache.isHAEnabledDevice(child2));
76         assertFalse(hwvtepHACache.isHAParentNode(parent));
77     }
78
79     public static InstanceIdentifier<Node> newNodeInstanceIdentifier(String id) {
80         String nodeString = "hwvtep://uuid/" + java.util.UUID.nameUUIDFromBytes(id.getBytes()).toString();
81         NodeId nodeId = new NodeId(new Uri(nodeString));
82         NodeKey nodeKey = new NodeKey(nodeId);
83         TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
84         return InstanceIdentifier.builder(NetworkTopology.class)
85                 .child(Topology.class, topoKey)
86                 .child(Node.class, nodeKey)
87                 .build();
88     }
89 }