Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 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.cpman.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.cluster.ClusterService; |
| 23 | import org.onosproject.cluster.ControllerNode; |
| 24 | import org.onosproject.cluster.NodeId; |
| 25 | import org.onosproject.cpman.ControlMetric; |
| 26 | import org.onosproject.cpman.ControlMetricType; |
| 27 | import org.onosproject.cpman.MetricValue; |
| 28 | import org.onosproject.net.DeviceId; |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 29 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 30 | import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 31 | |
| 32 | import java.util.Optional; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 33 | import java.util.Set; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 34 | import java.util.concurrent.ExecutionException; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 35 | |
| 36 | import static org.easymock.EasyMock.anyObject; |
| 37 | import static org.easymock.EasyMock.createMock; |
| 38 | import static org.easymock.EasyMock.expect; |
| 39 | import static org.easymock.EasyMock.replay; |
| 40 | import static org.hamcrest.Matchers.is; |
| 41 | import static org.junit.Assert.assertThat; |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 42 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 43 | import static org.onosproject.cpman.ControlResource.CPU_METRICS; |
| 44 | import static org.onosproject.cpman.ControlResource.DISK_METRICS; |
| 45 | import static org.onosproject.cpman.ControlResource.MEMORY_METRICS; |
| 46 | import static org.onosproject.cpman.ControlResource.NETWORK_METRICS; |
| 47 | import static org.onosproject.cpman.ControlResource.Type; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Unit test of control plane monitoring service. |
| 51 | */ |
| 52 | public class ControlPlaneMonitorTest { |
| 53 | |
| 54 | private ControlPlaneMonitor monitor; |
| 55 | private static final Integer UPDATE_INTERVAL = 1; |
| 56 | private ClusterService mockClusterService; |
| 57 | private ControllerNode mockControllerNode; |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 58 | private ClusterCommunicationService mockCommunicationService; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 59 | private NodeId nodeId; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 60 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Sets up the services required by control plane monitor. |
| 63 | */ |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 64 | @Before |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 65 | public void setup() { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 66 | monitor = new ControlPlaneMonitor(); |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 67 | |
| 68 | mockCommunicationService = new ClusterCommunicationServiceAdapter(); |
| 69 | monitor.communicationService = mockCommunicationService; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 70 | |
| 71 | nodeId = new NodeId("1"); |
| 72 | mockControllerNode = new MockControllerNode(nodeId); |
| 73 | mockClusterService = createMock(ClusterService.class); |
| 74 | monitor.clusterService = mockClusterService; |
| 75 | |
| 76 | expect(mockClusterService.getNode(anyObject())) |
| 77 | .andReturn(mockControllerNode).anyTimes(); |
| 78 | expect(mockClusterService.getLocalNode()) |
| 79 | .andReturn(mockControllerNode).anyTimes(); |
| 80 | replay(mockClusterService); |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 81 | |
| 82 | monitor.activate(); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Mock class for a controller node. |
| 87 | */ |
| 88 | private static class MockControllerNode implements ControllerNode { |
| 89 | final NodeId id; |
| 90 | |
| 91 | public MockControllerNode(NodeId id) { |
| 92 | this.id = id; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public NodeId id() { |
| 97 | return this.id; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public IpAddress ip() { |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public int tcpPort() { |
| 107 | return 0; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private void testUpdateMetricWithoutId(ControlMetricType cmt, MetricValue mv) { |
| 112 | ControlMetric cm = new ControlMetric(cmt, mv); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 113 | monitor.updateMetric(cm, UPDATE_INTERVAL, Optional.empty()); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 116 | private void testLoadMetric(NodeId nodeId, ControlMetricType cmt, MetricValue mv) { |
| 117 | try { |
| 118 | assertThat(monitor.getLoad(nodeId, cmt, Optional.empty()).get().latest(), is(mv.getLoad())); |
| 119 | } catch (InterruptedException e) { |
| 120 | e.printStackTrace(); |
| 121 | } catch (ExecutionException e) { |
| 122 | e.printStackTrace(); |
| 123 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 126 | private void testUpdateMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 127 | ControlMetric cm = new ControlMetric(cmt, mv); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 128 | monitor.updateMetric(cm, UPDATE_INTERVAL, resourceName); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 131 | private void testLoadMetricWithResource(NodeId nodeId, ControlMetricType cmt, MetricValue mv, String resourceName) { |
| 132 | try { |
| 133 | assertThat(monitor.getLoad(nodeId, cmt, resourceName).get().latest(), is(mv.getLoad())); |
| 134 | } catch (InterruptedException e) { |
| 135 | e.printStackTrace(); |
| 136 | } catch (ExecutionException e) { |
| 137 | e.printStackTrace(); |
| 138 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private void testUpdateMetricWithId(ControlMetricType cmt, MetricValue mv, DeviceId did) { |
| 142 | ControlMetric cm = new ControlMetric(cmt, mv); |
| 143 | monitor.updateMetric(cm, UPDATE_INTERVAL, Optional.of(did)); |
| 144 | } |
| 145 | |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 146 | private void testLoadMetricWithId(NodeId nodeId, ControlMetricType cmt, MetricValue mv, DeviceId did) { |
| 147 | try { |
| 148 | assertThat(monitor.getLoad(nodeId, cmt, Optional.of(did)).get().latest(), is(mv.getLoad())); |
| 149 | } catch (InterruptedException e) { |
| 150 | e.printStackTrace(); |
| 151 | } catch (ExecutionException e) { |
| 152 | e.printStackTrace(); |
| 153 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 156 | /** |
| 157 | * Tests cpu metric update and load function. |
| 158 | */ |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 159 | @Test |
| 160 | public void testCpuMetric() { |
| 161 | MetricValue mv = new MetricValue.Builder().load(30).add(); |
| 162 | |
| 163 | CPU_METRICS.forEach(cmt -> testUpdateMetricWithoutId(cmt, mv)); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 164 | CPU_METRICS.forEach(cmt -> testLoadMetric(nodeId, cmt, mv)); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 167 | /** |
| 168 | * Tests memory metric update and load function. |
| 169 | */ |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 170 | @Test |
| 171 | public void testMemoryMetric() { |
| 172 | MetricValue mv = new MetricValue.Builder().load(40).add(); |
| 173 | |
| 174 | MEMORY_METRICS.forEach(cmt -> testUpdateMetricWithoutId(cmt, mv)); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 175 | MEMORY_METRICS.forEach(cmt -> testLoadMetric(nodeId, cmt, mv)); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 178 | /** |
| 179 | * Tests disk metric update and load function. |
| 180 | */ |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 181 | @Test |
| 182 | public void testDiskMetric() { |
| 183 | MetricValue mv = new MetricValue.Builder().load(50).add(); |
| 184 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 185 | Set<String> set = ImmutableSet.of("disk1", "disk2"); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 186 | |
| 187 | set.forEach(disk -> DISK_METRICS.forEach(cmt -> |
| 188 | testUpdateMetricWithResource(cmt, mv, disk))); |
| 189 | |
| 190 | set.forEach(disk -> DISK_METRICS.forEach(cmt -> |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 191 | testLoadMetricWithResource(nodeId, cmt, mv, disk))); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 194 | /** |
| 195 | * Tests network metric update and load function. |
| 196 | */ |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 197 | @Test |
| 198 | public void testNetworkMetric() { |
| 199 | MetricValue mv = new MetricValue.Builder().load(10).add(); |
| 200 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 201 | Set<String> set = ImmutableSet.of("eth0", "eth1"); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 202 | |
| 203 | set.forEach(network -> NETWORK_METRICS.forEach(cmt -> |
| 204 | testUpdateMetricWithResource(cmt, mv, network))); |
| 205 | |
| 206 | set.forEach(network -> NETWORK_METRICS.forEach(cmt -> |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 207 | testLoadMetricWithResource(nodeId, cmt, mv, network))); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 210 | /** |
| 211 | * Tests control message update and load function. |
| 212 | */ |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 213 | @Test |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 214 | public void testControlMessage() { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 215 | MetricValue mv = new MetricValue.Builder().load(10).add(); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 216 | Set<DeviceId> set = ImmutableSet.of(DeviceId.deviceId("of:0000000000000001"), |
| 217 | DeviceId.deviceId("of:0000000000000002")); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 218 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 219 | set.forEach(devId -> CONTROL_MESSAGE_METRICS.forEach(cmt -> |
| 220 | testUpdateMetricWithId(cmt, mv, devId))); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 221 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 222 | set.forEach(devId -> CONTROL_MESSAGE_METRICS.forEach(cmt -> |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame] | 223 | testLoadMetricWithId(nodeId, cmt, mv, devId))); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 224 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 225 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 226 | /** |
| 227 | * Tests available resource update and load function. |
| 228 | */ |
| 229 | @Test |
| 230 | public void testAvailableResources() { |
| 231 | MetricValue mv = new MetricValue.Builder().load(50).add(); |
| 232 | |
| 233 | Set<String> diskSet = ImmutableSet.of("disk1", "disk2"); |
| 234 | |
| 235 | diskSet.forEach(disk -> DISK_METRICS.forEach(cmt -> |
| 236 | testUpdateMetricWithResource(cmt, mv, disk))); |
| 237 | |
| 238 | Set<String> networkSet = ImmutableSet.of("eth0", "eth1"); |
| 239 | |
| 240 | networkSet.forEach(network -> NETWORK_METRICS.forEach(cmt -> |
| 241 | testUpdateMetricWithResource(cmt, mv, network))); |
| 242 | |
Jian Li | 89eeccd | 2016-05-06 02:10:33 -0700 | [diff] [blame^] | 243 | assertThat(monitor.availableResourcesSync(nodeId, Type.DISK), is(diskSet)); |
| 244 | assertThat(monitor.availableResourcesSync(nodeId, Type.NETWORK), is(networkSet)); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 245 | } |
| 246 | } |