0dcc6a1076d3d4a5ea632af6812c3a2d3bedb18b
[onosfw.git] /
1 /*
2  * Copyright 2014 Open Networking Laboratory
3  *
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  */
16 package org.onosproject.store.cluster.messaging.impl;
17
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.onosproject.cluster.DefaultControllerNode;
23 import org.onosproject.cluster.NodeId;
24 import org.onosproject.store.cluster.impl.ClusterNodesDelegate;
25 import org.onlab.packet.IpAddress;
26
27 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.TimeUnit;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertTrue;
32
33 /**
34  * Tests of the cluster communication manager.
35  */
36 public 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
44     private static final IpAddress IP = IpAddress.valueOf("127.0.0.1");
45
46     private ClusterCommunicationManager ccm1;
47     private ClusterCommunicationManager ccm2;
48
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
56     public void setUp() throws Exception {
57
58         NettyMessagingManager messagingService = new NettyMessagingManager();
59         messagingService.activate();
60
61         ccm1 = new ClusterCommunicationManager();
62         ccm1.activate();
63
64         ccm2 = new ClusterCommunicationManager();
65         ccm2.activate();
66
67 //        ccm1.initialize(node1, cnd1);
68 //        ccm2.initialize(node2, cnd2);
69     }
70
71     @After
72     public void tearDown() {
73         ccm1.deactivate();
74         ccm2.deactivate();
75     }
76
77     @Ignore("FIXME: failing randomly?")
78     @Test
79     public void connect() throws Exception {
80         cnd1.latch = new CountDownLatch(1);
81         cnd2.latch = new CountDownLatch(1);
82
83 //        ccm1.addNode(node2);
84         validateDelegateEvent(cnd1, Op.DETECTED, node2.id());
85         validateDelegateEvent(cnd2, Op.DETECTED, node1.id());
86     }
87
88     @Test
89     @Ignore
90     public void disconnect() throws Exception {
91         cnd1.latch = new CountDownLatch(1);
92         cnd2.latch = new CountDownLatch(1);
93
94 //        ccm1.addNode(node2);
95         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
112     enum Op { DETECTED, VANISHED, REMOVED };
113
114     private class TestDelegate implements ClusterNodesDelegate {
115
116         Op op;
117         CountDownLatch latch;
118         NodeId nodeId;
119
120         @Override
121         public DefaultControllerNode nodeDetected(NodeId nodeId, IpAddress ip, int tcpPort) {
122             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     }
142 }