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