blob: 7c4879120521ba18f4499125e7c0fc3516a42a88 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
2 * Copyright 2016-present 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 */
sangyun-hanf98df542016-03-24 20:28:03 +090016package org.onosproject.store.cluster.impl;
17
18import org.junit.After;
19import org.junit.Before;
Sbhat353548bb22017-07-13 10:27:10 -070020import org.junit.Test;
21import org.onlab.packet.IpAddress;
22import org.onosproject.cfg.ComponentConfigAdapter;
23import org.onosproject.cluster.ClusterEvent;
24import org.onosproject.cluster.ClusterMetadataServiceAdapter;
25import org.onosproject.cluster.ClusterStore;
26import org.onosproject.cluster.ClusterStoreDelegate;
27import org.onosproject.cluster.ControllerNode;
28import org.onosproject.cluster.DefaultControllerNode;
29import org.onosproject.cluster.NodeId;
Jordan Haltermanf70bf462017-07-29 13:12:00 -070030import org.onosproject.core.Version;
31import org.onosproject.core.VersionServiceAdapter;
Sbhat353548bb22017-07-13 10:27:10 -070032import org.onosproject.store.cluster.messaging.impl.NettyMessagingManager;
33
34import java.util.Set;
35
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.*;
38import static org.junit.Assert.assertFalse;
sangyun-hanf98df542016-03-24 20:28:03 +090039
sangyun-hanf98df542016-03-24 20:28:03 +090040/**
41 * Unit test for DistributedClusterStore.
42 */
43public class DistributedClusterStoreTest {
44 DistributedClusterStore distributedClusterStore;
Sbhat353548bb22017-07-13 10:27:10 -070045 ClusterStore clusterStore;
46 NodeId nodeId;
47 ControllerNode local;
48 private static final NodeId NID1 = new NodeId("foo");
49 private static final NodeId NID2 = new NodeId("bar");
50 private static final NodeId NID3 = new NodeId("buz");
51
52 private static final IpAddress IP1 = IpAddress.valueOf("127.0.0.1");
53 private static final IpAddress IP2 = IpAddress.valueOf("127.0.0.2");
54 private static final IpAddress IP3 = IpAddress.valueOf("127.0.0.3");
55
56 private static final int PORT1 = 1;
57 private static final int PORT2 = 2;
58 private static Set<ControllerNode> nodes;
59
Sbhat353548bb22017-07-13 10:27:10 -070060 private TestDelegate delegate = new TestDelegate();
61 private class TestDelegate implements ClusterStoreDelegate {
62 private ClusterEvent event;
63 @Override
64 public void notify(ClusterEvent event) {
65 this.event = event;
66 }
67 }
68
sangyun-hanf98df542016-03-24 20:28:03 +090069 @Before
70 public void setUp() throws Exception {
71 distributedClusterStore = new DistributedClusterStore();
Sbhat353548bb22017-07-13 10:27:10 -070072 distributedClusterStore.clusterMetadataService = new ClusterMetadataServiceAdapter() {
73 @Override
74 public ControllerNode getLocalNode() {
75 return new DefaultControllerNode(NID1, IP1);
76 }
77 };
78 distributedClusterStore.messagingService = new NettyMessagingManager();
79 distributedClusterStore.cfgService = new ComponentConfigAdapter();
Jordan Haltermanf70bf462017-07-29 13:12:00 -070080 distributedClusterStore.versionService = new VersionServiceAdapter() {
81 @Override
82 public Version version() {
83 return Version.version("1.1.1");
84 }
85 };
Thomas Vachuska1ca7e9f2016-08-05 10:21:41 -070086 distributedClusterStore.activate();
Sbhat353548bb22017-07-13 10:27:10 -070087 clusterStore = distributedClusterStore;
sangyun-hanf98df542016-03-24 20:28:03 +090088 }
89
90 @After
91 public void tearDown() throws Exception {
92 distributedClusterStore.deactivate();
93 }
Sbhat353548bb22017-07-13 10:27:10 -070094
95 @Test
96 public void testEmpty() {
97 nodeId = new NodeId("newNode");
98 assertThat(clusterStore.getNode((nodeId)), is(nullValue()));
99 assertFalse(clusterStore.hasDelegate());
100 assertThat(clusterStore.getState(nodeId), is(ControllerNode.State.INACTIVE));
Jordan Haltermanf70bf462017-07-29 13:12:00 -0700101 assertThat(clusterStore.getVersion(nodeId), is(nullValue()));
Sbhat353548bb22017-07-13 10:27:10 -0700102 }
103
104 @Test
105 public void addNodes() {
Sbhat353548bb22017-07-13 10:27:10 -0700106 clusterStore.setDelegate(delegate);
107 assertThat(clusterStore.hasDelegate(), is(true));
108 clusterStore.addNode(NID1, IP1, PORT1);
109 clusterStore.addNode(NID2, IP2, PORT2);
110 clusterStore.removeNode(NID1);
111
112 assertThat(clusterStore.getNode(NID1), is(nullValue()));
113 clusterStore.addNode(NID3, IP3, PORT2);
114
115 clusterStore.markFullyStarted(true);
116 assertThat(clusterStore.getState(clusterStore.getLocalNode().id()),
117 is(ControllerNode.State.READY));
118 clusterStore.markFullyStarted(false);
119 assertThat(clusterStore.getState(clusterStore.getLocalNode().id()),
120 is(ControllerNode.State.ACTIVE));
121 nodes = clusterStore.getNodes();
122 assertThat(nodes.size(), is(2));
123 clusterStore.markFullyStarted(true);
124
125 clusterStore.unsetDelegate(delegate);
126 assertThat(clusterStore.hasDelegate(), is(false));
127 }
Sho SHIMIZUe0b7d162016-08-12 15:46:48 -0700128}