Migrate HwvtepHACache users to HwvtepNodeHACache
[netvirt.git] / elanmanager / impl / src / test / java / org / opendaylight / netvirt / elan / l2gw / nodehandlertest / NodeConnectedHandlerTest.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 package org.opendaylight.netvirt.elan.l2gw.nodehandlertest;
9
10 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
11 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
12
13 import com.google.common.base.Optional;
14 import java.util.UUID;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
22 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
23 import org.opendaylight.netvirt.elan.l2gw.ha.handlers.NodeConnectedHandler;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Created by eaksahu on 10/14/2016.
38  */
39 public class NodeConnectedHandlerTest extends AbstractConcurrentDataBrokerTest {
40
41     // Uncomment this to keep running this test indefinitely
42     // This is very useful to detect concurrency issues, respectively prove
43     // that the use of AbstractConcurrentDataBrokerTest instead of AbstractDataBrokerTest
44     // does NOT cause any concurrency issues and make this test flaky...
45     // public static @ClassRule RunUntilFailureClassRule classRepeater = new RunUntilFailureClassRule();
46     // public @Rule RunUntilFailureRule repeater = new RunUntilFailureRule(classRepeater);
47
48     static Logger LOG = LoggerFactory.getLogger(NodeConnectedHandlerTest.class);
49
50     NodeConnectedHandler nodeConnectedHandler;
51     NodeConnectedHandlerUtils handlerUtils = new NodeConnectedHandlerUtils();
52
53     String d1UUID;
54     String d2UUID;
55
56     Optional<Node> d1GlobalOpNode;
57     Optional<Node> d2GlobalOpNode;
58     Optional<Node> haGlobalOpNode;
59
60     Optional<Node> d1PsOpNode;
61     Optional<Node> d2PsOpNode;
62     Optional<Node> haPsOpNode;
63
64     Optional<Node> d1GlobalConfigNode;
65     Optional<Node> d2GlobalConfigNode;
66     Optional<Node> haGlobalConfigNode;
67
68     Optional<Node> d1PsConfigNode;
69     Optional<Node> d2PsConfigNode;
70     Optional<Node> haPsConfigNode;
71
72     static String managerHAId = "s3-clusterid";
73     static String switchName = "s3";
74
75     InstanceIdentifier<Node> d1NodePath;
76     InstanceIdentifier<Node> d2NodePath;
77     InstanceIdentifier<Node> haNodePath;
78
79     InstanceIdentifier<Node> d1PsNodePath;
80     InstanceIdentifier<Node> d2PsNodePath;
81     InstanceIdentifier<Node> haPsNodePath;
82
83     NodeId haNodeId;
84
85     @Before
86     public void setupForHANode() {
87         d1UUID = java.util.UUID.nameUUIDFromBytes("d1uuid".getBytes()).toString();
88         d2UUID = java.util.UUID.nameUUIDFromBytes("d2uuid".getBytes()).toString();
89         d1NodePath = getInstanceIdentifier(d1UUID);
90         d2NodePath = getInstanceIdentifier(d2UUID);
91         haNodePath = getInstanceIdentifier(managerHAId);
92
93         haNodeId = getNodeId(managerHAId);
94
95         NodeId d1NodeId = d1NodePath.firstKeyOf(Node.class).getNodeId();
96         String d1PsNodeIdVal = d1NodeId.getValue() + "/physicalswitch/" + switchName;
97         d1PsNodePath = createInstanceIdentifier(d1PsNodeIdVal);
98
99         NodeId d2NodeId = d2NodePath.firstKeyOf(Node.class).getNodeId();
100         String d2PsNodeIdVal = d2NodeId.getValue() + "/physicalswitch/" + switchName;
101         d2PsNodePath = createInstanceIdentifier(d2PsNodeIdVal);
102
103         haPsNodePath = createInstanceIdentifier(haNodeId.getValue() + "/physicalswitch/" + switchName);
104
105         nodeConnectedHandler = new NodeConnectedHandler(getDataBroker(), Mockito.mock(HwvtepNodeHACache.class));
106     }
107
108     @Test
109     public void testD1Connect() throws Exception {
110         ReadWriteTransaction tx = getDataBroker().newReadWriteTransaction();
111         handlerUtils.addPsNode(d1PsNodePath, d1NodePath, DataProvider.getPortNameListD1(), tx).checkedGet();
112
113         tx = getDataBroker().newReadWriteTransaction();
114         handlerUtils.addNode(d1NodePath, d1PsNodePath, DataProvider.getLogicalSwitchDataD1(),
115                 DataProvider.getLocalUcasMacDataD1(), DataProvider.getLocalMcastDataD1(),
116                 DataProvider.getRemoteMcastDataD1(), DataProvider.getRemoteUcasteMacDataD1(),
117                 DataProvider.getGlobalTerminationPointIpD1(), tx).checkedGet();
118
119         readNodes();
120         tx = getDataBroker().newReadWriteTransaction();
121         nodeConnectedHandler.handleNodeConnected(d1GlobalOpNode.get(), d1NodePath, haNodePath, haGlobalConfigNode,
122                 haPsConfigNode, tx);
123         tx.submit().checkedGet();
124         readNodes();
125         //verify global ha manager config should have ha_children
126         Assert.assertTrue(haGlobalConfigNode.isPresent() && d1GlobalOpNode.isPresent());
127         TestUtil.verifyHAconfigNode(haGlobalConfigNode.get(), d1GlobalOpNode.get());
128
129         Assert.assertTrue(d1GlobalOpNode.isPresent() && haGlobalOpNode.isPresent() && d1PsOpNode.isPresent()
130                 && haPsOpNode.isPresent());
131         TestUtil.verifyHAOpNode(d1GlobalOpNode.get(), haGlobalOpNode.get(),
132                 d1PsOpNode.get(), haPsOpNode.get(), haNodePath, d1PsNodePath, haPsNodePath, haNodeId, getDataBroker());
133     }
134
135     public static InstanceIdentifier<Node> createInstanceIdentifier(String nodeIdString) {
136         NodeId nodeId = new NodeId(new Uri(nodeIdString));
137         NodeKey nodeKey = new NodeKey(nodeId);
138         TopologyKey topoKey = new TopologyKey(new TopologyId(new Uri("hwvtep:1")));
139         return InstanceIdentifier.builder(NetworkTopology.class)
140                 .child(Topology.class, topoKey)
141                 .child(Node.class, nodeKey)
142                 .build();
143     }
144
145     public static InstanceIdentifier<Node> getInstanceIdentifier(String haUUidVal) {
146         String nodeString = "hwvtep://uuid/" + UUID.nameUUIDFromBytes(haUUidVal.getBytes()).toString();
147         NodeId nodeId = new NodeId(new Uri(nodeString));
148         NodeKey nodeKey = new NodeKey(nodeId);
149         TopologyKey topoKey = new TopologyKey(new TopologyId(new Uri("hwvtep:1")));
150         return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topoKey)
151                 .child(Node.class, nodeKey).build();
152     }
153
154     public static NodeId getNodeId(String haId) {
155         String nodeString = "hwvtep://uuid/" + UUID.nameUUIDFromBytes(haId.getBytes()).toString();
156         NodeId nodeId = new NodeId(new Uri(nodeString));
157         return nodeId;
158     }
159
160     public void readNodes() throws Exception {
161         ReadOnlyTransaction tx = getDataBroker().newReadOnlyTransaction();
162         d1GlobalOpNode = TestUtil.readNode(OPERATIONAL, d1NodePath, tx);
163         d2GlobalOpNode = TestUtil.readNode(OPERATIONAL, d2NodePath, tx);
164         haGlobalOpNode = TestUtil.readNode(OPERATIONAL, haNodePath, tx);
165
166         d1PsOpNode = TestUtil.readNode(OPERATIONAL, d1PsNodePath, tx);
167         d2PsOpNode = TestUtil.readNode(OPERATIONAL, d2PsNodePath, tx);
168         haPsOpNode = TestUtil.readNode(OPERATIONAL, haPsNodePath, tx);
169
170         haGlobalConfigNode = TestUtil.readNode(CONFIGURATION, haNodePath, tx);
171         d1GlobalConfigNode = TestUtil.readNode(CONFIGURATION, d1NodePath, tx);
172         d2GlobalConfigNode = TestUtil.readNode(CONFIGURATION, d2NodePath, tx);
173
174         haPsConfigNode = TestUtil.readNode(CONFIGURATION, haPsNodePath, tx);
175         d1PsConfigNode = TestUtil.readNode(CONFIGURATION, d1PsNodePath, tx);
176         d2PsConfigNode = TestUtil.readNode(CONFIGURATION, d2PsNodePath, tx);
177
178         tx.close();
179     }
180
181 }