Remove redundant names in paths
[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.opendaylight.controller.md.sal.binding.api.DataBroker;
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.netvirt.elan.l2gw.ha.handlers.NodeConnectedHandler;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Created by eaksahu on 10/14/2016.
37  */
38 public class NodeConnectedHandlerTest extends AbstractConcurrentDataBrokerTest {
39
40     // Uncomment this to keep running this test indefinitely
41     // This is very useful to detect concurrency issues, respectively prove
42     // that the use of AbstractConcurrentDataBrokerTest instead of AbstractDataBrokerTest
43     // does NOT cause any concurrency issues and make this test flaky...
44     // public static @ClassRule RunUntilFailureClassRule classRepeater = new RunUntilFailureClassRule();
45     // public @Rule RunUntilFailureRule repeater = new RunUntilFailureRule(classRepeater);
46
47     static Logger LOG = LoggerFactory.getLogger(NodeConnectedHandlerTest.class);
48     DataBroker dataBroker;
49
50     NodeConnectedHandler nodeConnectedHandler = new NodeConnectedHandler(this.dataBroker);
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         dataBroker = getDataBroker();
88         d1UUID = java.util.UUID.nameUUIDFromBytes("d1uuid".getBytes()).toString();
89         d2UUID = java.util.UUID.nameUUIDFromBytes("d2uuid".getBytes()).toString();
90         d1NodePath = getInstanceIdentifier(d1UUID);
91         d2NodePath = getInstanceIdentifier(d2UUID);
92         haNodePath = getInstanceIdentifier(managerHAId);
93
94         haNodeId = getNodeId(managerHAId);
95
96         NodeId d1NodeId = d1NodePath.firstKeyOf(Node.class).getNodeId();
97         String d1PsNodeIdVal = d1NodeId.getValue() + "/physicalswitch/" + switchName;
98         d1PsNodePath = createInstanceIdentifier(d1PsNodeIdVal);
99
100         NodeId d2NodeId = d2NodePath.firstKeyOf(Node.class).getNodeId();
101         String d2PsNodeIdVal = d2NodeId.getValue() + "/physicalswitch/" + switchName;
102         d2PsNodePath = createInstanceIdentifier(d2PsNodeIdVal);
103
104         haPsNodePath = createInstanceIdentifier(haNodeId.getValue() + "/physicalswitch/" + switchName);
105     }
106
107     @Test
108     public void testD1Connect() throws Exception {
109         ReadWriteTransaction tx = this.dataBroker.newReadWriteTransaction();
110         handlerUtils.addPsNode(d1PsNodePath, d1NodePath, DataProvider.getPortNameListD1(), tx).checkedGet();
111
112         tx = this.dataBroker.newReadWriteTransaction();
113         handlerUtils.addNode(d1NodePath, d1PsNodePath, DataProvider.getLogicalSwitchDataD1(),
114                 DataProvider.getLocalUcasMacDataD1(), DataProvider.getLocalMcastDataD1(),
115                 DataProvider.getRemoteMcastDataD1(), DataProvider.getRemoteUcasteMacDataD1(),
116                 DataProvider.getGlobalTerminationPointIpD1(), tx).checkedGet();
117
118         readNodes();
119         tx = this.dataBroker.newReadWriteTransaction();
120         nodeConnectedHandler.handleNodeConnected(d1GlobalOpNode.get(), d1NodePath, haNodePath, haGlobalConfigNode,
121                 haPsConfigNode, tx);
122         tx.submit().checkedGet();
123         readNodes();
124         //verify global ha manager config should have ha_children
125         Assert.assertTrue(haGlobalConfigNode.isPresent() && d1GlobalOpNode.isPresent());
126         TestUtil.verifyHAconfigNode(haGlobalConfigNode.get(), d1GlobalOpNode.get());
127
128         Assert.assertTrue(d1GlobalOpNode.isPresent() && haGlobalOpNode.isPresent() && d1PsOpNode.isPresent()
129                 && haPsOpNode.isPresent());
130         TestUtil.verifyHAOpNode(d1GlobalOpNode.get(), haGlobalOpNode.get(),
131                 d1PsOpNode.get(), haPsOpNode.get(), haNodePath, d1PsNodePath, haPsNodePath, haNodeId, this.dataBroker);
132     }
133
134     public static InstanceIdentifier<Node> createInstanceIdentifier(String nodeIdString) {
135         NodeId nodeId = new NodeId(new Uri(nodeIdString));
136         NodeKey nodeKey = new NodeKey(nodeId);
137         TopologyKey topoKey = new TopologyKey(new TopologyId(new Uri("hwvtep:1")));
138         return InstanceIdentifier.builder(NetworkTopology.class)
139                 .child(Topology.class, topoKey)
140                 .child(Node.class, nodeKey)
141                 .build();
142     }
143
144     public static InstanceIdentifier<Node> getInstanceIdentifier(String haUUidVal) {
145         String nodeString = "hwvtep://uuid/" + UUID.nameUUIDFromBytes(haUUidVal.getBytes()).toString();
146         NodeId nodeId = new NodeId(new Uri(nodeString));
147         NodeKey nodeKey = new NodeKey(nodeId);
148         TopologyKey topoKey = new TopologyKey(new TopologyId(new Uri("hwvtep:1")));
149         return InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, topoKey)
150                 .child(Node.class, nodeKey).build();
151     }
152
153     public static NodeId getNodeId(String haId) {
154         String nodeString = "hwvtep://uuid/" + UUID.nameUUIDFromBytes(haId.getBytes()).toString();
155         NodeId nodeId = new NodeId(new Uri(nodeString));
156         return nodeId;
157     }
158
159     public void readNodes() throws Exception {
160         ReadOnlyTransaction tx = this.dataBroker.newReadOnlyTransaction();
161         d1GlobalOpNode = TestUtil.readNode(OPERATIONAL, d1NodePath, tx);
162         d2GlobalOpNode = TestUtil.readNode(OPERATIONAL, d2NodePath, tx);
163         haGlobalOpNode = TestUtil.readNode(OPERATIONAL, haNodePath, tx);
164
165         d1PsOpNode = TestUtil.readNode(OPERATIONAL, d1PsNodePath, tx);
166         d2PsOpNode = TestUtil.readNode(OPERATIONAL, d2PsNodePath, tx);
167         haPsOpNode = TestUtil.readNode(OPERATIONAL, haPsNodePath, tx);
168
169         haGlobalConfigNode = TestUtil.readNode(CONFIGURATION, haNodePath, tx);
170         d1GlobalConfigNode = TestUtil.readNode(CONFIGURATION, d1NodePath, tx);
171         d2GlobalConfigNode = TestUtil.readNode(CONFIGURATION, d2NodePath, tx);
172
173         haPsConfigNode = TestUtil.readNode(CONFIGURATION, haPsNodePath, tx);
174         d1PsConfigNode = TestUtil.readNode(CONFIGURATION, d1PsNodePath, tx);
175         d2PsConfigNode = TestUtil.readNode(CONFIGURATION, d2PsNodePath, tx);
176
177         tx.close();
178     }
179
180 }