blob: f7ced1252770106387fdfc8af415bdc92c3f61a5 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.cluster.messaging.impl;
tom5346a952014-09-30 09:02:51 -070017
18import org.junit.After;
19import org.junit.Before;
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070020import org.junit.Ignore;
tom5346a952014-09-30 09:02:51 -070021import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.cluster.DefaultControllerNode;
23import org.onosproject.cluster.NodeId;
24import org.onosproject.store.cluster.impl.ClusterNodesDelegate;
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070025import org.onlab.packet.IpAddress;
tom5346a952014-09-30 09:02:51 -070026
27import java.util.concurrent.CountDownLatch;
28import java.util.concurrent.TimeUnit;
29
tome440a632014-09-30 09:04:21 -070030import static org.junit.Assert.assertEquals;
31import static org.junit.Assert.assertTrue;
tom5346a952014-09-30 09:02:51 -070032
33/**
34 * Tests of the cluster communication manager.
35 */
36public class ClusterCommunicationManagerTest {
37
38 private static final NodeId N1 = new NodeId("n1");
39 private static final NodeId N2 = new NodeId("n2");
40
41 private static final int P1 = 9881;
42 private static final int P2 = 9882;
43
Pavlin Radoslavov444b5192014-10-28 10:45:19 -070044 private static final IpAddress IP = IpAddress.valueOf("127.0.0.1");
tom5346a952014-09-30 09:02:51 -070045
Madan Jampani3b0dfd52014-10-02 16:48:13 -070046 private ClusterCommunicationManager ccm1;
47 private ClusterCommunicationManager ccm2;
tom5346a952014-09-30 09:02:51 -070048
49 private TestDelegate cnd1 = new TestDelegate();
50 private TestDelegate cnd2 = new TestDelegate();
51
52 private DefaultControllerNode node1 = new DefaultControllerNode(N1, IP, P1);
53 private DefaultControllerNode node2 = new DefaultControllerNode(N2, IP, P2);
54
55 @Before
Madan Jampani890bc352014-10-01 22:35:29 -070056 public void setUp() throws Exception {
tom5346a952014-09-30 09:02:51 -070057
Thomas Vachuskab093c912015-04-20 10:28:26 -070058 NettyMessagingManager messagingService = new NettyMessagingManager();
Madan Jampani890bc352014-10-01 22:35:29 -070059 messagingService.activate();
60
Madan Jampani3b0dfd52014-10-02 16:48:13 -070061 ccm1 = new ClusterCommunicationManager();
tom5346a952014-09-30 09:02:51 -070062 ccm1.activate();
63
Madan Jampani3b0dfd52014-10-02 16:48:13 -070064 ccm2 = new ClusterCommunicationManager();
tom5346a952014-09-30 09:02:51 -070065 ccm2.activate();
66
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070067// ccm1.initialize(node1, cnd1);
68// ccm2.initialize(node2, cnd2);
tom5346a952014-09-30 09:02:51 -070069 }
70
71 @After
72 public void tearDown() {
73 ccm1.deactivate();
74 ccm2.deactivate();
75 }
76
Yuta HIGUCHI8e493792014-10-01 19:36:32 -070077 @Ignore("FIXME: failing randomly?")
tom5346a952014-09-30 09:02:51 -070078 @Test
79 public void connect() throws Exception {
80 cnd1.latch = new CountDownLatch(1);
81 cnd2.latch = new CountDownLatch(1);
82
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070083// ccm1.addNode(node2);
tom5346a952014-09-30 09:02:51 -070084 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
85 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
86 }
87
88 @Test
Madan Jampani890bc352014-10-01 22:35:29 -070089 @Ignore
tom5346a952014-09-30 09:02:51 -070090 public void disconnect() throws Exception {
91 cnd1.latch = new CountDownLatch(1);
92 cnd2.latch = new CountDownLatch(1);
93
Yuta HIGUCHIbbfc96a2014-10-13 18:05:44 -070094// ccm1.addNode(node2);
tom5346a952014-09-30 09:02:51 -070095 validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
96 validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
97
98 cnd1.latch = new CountDownLatch(1);
99 cnd2.latch = new CountDownLatch(1);
100 ccm1.deactivate();
101//
102// validateDelegateEvent(cnd2, Op.VANISHED, node1.id());
103 }
104
105 private void validateDelegateEvent(TestDelegate delegate, Op op, NodeId nodeId)
106 throws InterruptedException {
107 assertTrue("did not connect in time", delegate.latch.await(2500, TimeUnit.MILLISECONDS));
108 assertEquals("incorrect event", op, delegate.op);
109 assertEquals("incorrect event node", nodeId, delegate.nodeId);
110 }
111
Sho SHIMIZU9f614a42015-09-11 15:32:46 -0700112 enum Op { DETECTED, VANISHED, REMOVED }
tom5346a952014-09-30 09:02:51 -0700113
114 private class TestDelegate implements ClusterNodesDelegate {
115
116 Op op;
117 CountDownLatch latch;
118 NodeId nodeId;
119
120 @Override
Pavlin Radoslavov444b5192014-10-28 10:45:19 -0700121 public DefaultControllerNode nodeDetected(NodeId nodeId, IpAddress ip, int tcpPort) {
tom5346a952014-09-30 09:02:51 -0700122 latch(nodeId, Op.DETECTED);
123 return new DefaultControllerNode(nodeId, ip, tcpPort);
124 }
125
126 @Override
127 public void nodeVanished(NodeId nodeId) {
128 latch(nodeId, Op.VANISHED);
129 }
130
131 @Override
132 public void nodeRemoved(NodeId nodeId) {
133 latch(nodeId, Op.REMOVED);
134 }
135
136 private void latch(NodeId nodeId, Op op) {
137 this.op = op;
138 this.nodeId = nodeId;
139 latch.countDown();
140 }
141 }
Madan Jampani890bc352014-10-01 22:35:29 -0700142}