2 * Copyright (c) 2016 Red Hat and others. All rights reserved.
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
8 package org.opendaylight.genius.mdsalutil.hwvtep;
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;
26 import java.util.ArrayList;
27 import java.util.List;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertTrue;
33 public class HwvtepHACacheTest {
35 HwvtepHACache hwvtepHACache = HwvtepHACache.getInstance();
38 public void testAddHAChild() {
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();
46 hwvtepHACache.addChild(parent, child1);
48 assertTrue(hwvtepHACache.isHAEnabledDevice(child1));
49 assertTrue(hwvtepHACache.isHAParentNode(parent));
51 hwvtepHACache.addChild(parent, child2);
52 assertTrue(hwvtepHACache.isHAEnabledDevice(child1));
53 assertTrue(hwvtepHACache.isHAEnabledDevice(child2));
54 assertTrue(hwvtepHACache.isHAParentNode(parent));
56 assertTrue(hwvtepHACache.getHAChildNodes().contains(child1));
57 assertTrue(hwvtepHACache.getHAChildNodes().contains(child2));
58 assertTrue(hwvtepHACache.getHAParentNodes().contains(parent));
60 assertTrue(hwvtepHACache.getChildrenForHANode(parent).contains(child1));
61 assertTrue(hwvtepHACache.getChildrenForHANode(parent).contains(child2));
63 List<DebugEvent> events = hwvtepHACache.getNodeEvents();
64 assertTrue(events.contains(new NodeEvent.ChildAddedEvent(child1NodeId)));
65 assertTrue(events.contains(new NodeEvent.ChildAddedEvent(child2NodeId)));
67 hwvtepHACache.updateConnectedNodeStatus(child1);
68 assertTrue(hwvtepHACache.getNodeEvents().contains(new NodeEvent.NodeConnectedEvent(child1NodeId)));
70 hwvtepHACache.updateDisconnectedNodeStatus(child1);
71 assertTrue(hwvtepHACache.getNodeEvents().contains(new NodeEvent.NodeDisconnectedEvent(child1NodeId)));
73 hwvtepHACache.cleanupParent(parent);
74 assertFalse(hwvtepHACache.isHAEnabledDevice(child1));
75 assertFalse(hwvtepHACache.isHAEnabledDevice(child2));
76 assertFalse(hwvtepHACache.isHAParentNode(parent));
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)