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