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; |
| 31 | |
| 32 | import static org.easymock.EasyMock.anyObject; |
| 33 | import static org.easymock.EasyMock.createMock; |
| 34 | import static org.easymock.EasyMock.expect; |
| 35 | import static org.easymock.EasyMock.replay; |
| 36 | import static org.hamcrest.Matchers.is; |
| 37 | import static org.junit.Assert.assertThat; |
| 38 | import static org.onosproject.cpman.ControlMetricType.CPU_IDLE_TIME; |
| 39 | import static org.onosproject.cpman.ControlMetricType.CPU_LOAD; |
| 40 | import static org.onosproject.cpman.ControlMetricType.DISK_READ_BYTES; |
| 41 | import static org.onosproject.cpman.ControlMetricType.DISK_WRITE_BYTES; |
| 42 | import static org.onosproject.cpman.ControlMetricType.FLOW_MOD_PACKET; |
| 43 | import static org.onosproject.cpman.ControlMetricType.FLOW_REMOVED_PACKET; |
| 44 | import static org.onosproject.cpman.ControlMetricType.INBOUND_PACKET; |
| 45 | import static org.onosproject.cpman.ControlMetricType.MEMORY_FREE; |
| 46 | import static org.onosproject.cpman.ControlMetricType.MEMORY_FREE_RATIO; |
| 47 | import static org.onosproject.cpman.ControlMetricType.MEMORY_USED; |
| 48 | import static org.onosproject.cpman.ControlMetricType.MEMORY_USED_RATIO; |
| 49 | import static org.onosproject.cpman.ControlMetricType.NW_INCOMING_BYTES; |
| 50 | import static org.onosproject.cpman.ControlMetricType.NW_INCOMING_PACKETS; |
| 51 | import static org.onosproject.cpman.ControlMetricType.NW_OUTGOING_BYTES; |
| 52 | import static org.onosproject.cpman.ControlMetricType.NW_OUTGOING_PACKETS; |
| 53 | import static org.onosproject.cpman.ControlMetricType.OUTBOUND_PACKET; |
| 54 | import static org.onosproject.cpman.ControlMetricType.REPLY_PACKET; |
| 55 | import static org.onosproject.cpman.ControlMetricType.REQUEST_PACKET; |
| 56 | import static org.onosproject.cpman.ControlMetricType.SYS_CPU_TIME; |
| 57 | import static org.onosproject.cpman.ControlMetricType.TOTAL_CPU_TIME; |
| 58 | import static org.onosproject.cpman.ControlMetricType.USER_CPU_TIME; |
| 59 | |
| 60 | /** |
| 61 | * Unit test of control plane monitoring service. |
| 62 | */ |
| 63 | public class ControlPlaneMonitorTest { |
| 64 | |
| 65 | private ControlPlaneMonitor monitor; |
| 66 | private static final Integer UPDATE_INTERVAL = 1; |
| 67 | private ClusterService mockClusterService; |
| 68 | private ControllerNode mockControllerNode; |
| 69 | private NodeId nodeId; |
| 70 | private static final ImmutableSet<ControlMetricType> CPU_METRICS = |
| 71 | ImmutableSet.of(CPU_IDLE_TIME, CPU_LOAD, SYS_CPU_TIME, |
| 72 | USER_CPU_TIME, TOTAL_CPU_TIME); |
| 73 | private static final ImmutableSet<ControlMetricType> MEMORY_METRICS = |
| 74 | ImmutableSet.of(MEMORY_FREE, MEMORY_FREE_RATIO, MEMORY_USED, |
| 75 | MEMORY_USED_RATIO); |
| 76 | private static final ImmutableSet<ControlMetricType> DISK_METRICS = |
| 77 | ImmutableSet.of(DISK_READ_BYTES, DISK_WRITE_BYTES); |
| 78 | private static final ImmutableSet<ControlMetricType> NETWORK_METRICS = |
| 79 | ImmutableSet.of(NW_INCOMING_BYTES, NW_OUTGOING_BYTES, |
| 80 | NW_INCOMING_PACKETS, NW_OUTGOING_PACKETS); |
| 81 | private static final ImmutableSet<ControlMetricType> CTRL_MSGS = |
| 82 | ImmutableSet.of(INBOUND_PACKET, OUTBOUND_PACKET, FLOW_MOD_PACKET, |
| 83 | FLOW_REMOVED_PACKET, REQUEST_PACKET, REPLY_PACKET); |
| 84 | |
| 85 | @Before |
| 86 | public void setup() throws Exception { |
| 87 | monitor = new ControlPlaneMonitor(); |
| 88 | monitor.activate(); |
| 89 | |
| 90 | nodeId = new NodeId("1"); |
| 91 | mockControllerNode = new MockControllerNode(nodeId); |
| 92 | mockClusterService = createMock(ClusterService.class); |
| 93 | monitor.clusterService = mockClusterService; |
| 94 | |
| 95 | expect(mockClusterService.getNode(anyObject())) |
| 96 | .andReturn(mockControllerNode).anyTimes(); |
| 97 | expect(mockClusterService.getLocalNode()) |
| 98 | .andReturn(mockControllerNode).anyTimes(); |
| 99 | replay(mockClusterService); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Mock class for a controller node. |
| 104 | */ |
| 105 | private static class MockControllerNode implements ControllerNode { |
| 106 | final NodeId id; |
| 107 | |
| 108 | public MockControllerNode(NodeId id) { |
| 109 | this.id = id; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public NodeId id() { |
| 114 | return this.id; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public IpAddress ip() { |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public int tcpPort() { |
| 124 | return 0; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private void testUpdateMetricWithoutId(ControlMetricType cmt, MetricValue mv) { |
| 129 | ControlMetric cm = new ControlMetric(cmt, mv); |
| 130 | monitor.updateMetric(cm, UPDATE_INTERVAL, Optional.ofNullable(null)); |
| 131 | } |
| 132 | |
| 133 | private void testLoadMetricWithoutId(ControlMetricType cmt, MetricValue mv) { |
| 134 | assertThat(monitor.getLoad(nodeId, cmt, Optional.ofNullable(null)).latest(), is(mv.getLoad())); |
| 135 | } |
| 136 | |
| 137 | private void testUpdateMetricWithResource(ControlMetricType cmt, MetricValue mv, String resoureName) { |
| 138 | ControlMetric cm = new ControlMetric(cmt, mv); |
| 139 | monitor.updateMetric(cm, UPDATE_INTERVAL, resoureName); |
| 140 | } |
| 141 | |
| 142 | private void testLoadMetricWithResource(ControlMetricType cmt, MetricValue mv, String resoureName) { |
| 143 | assertThat(monitor.getLoad(nodeId, cmt, resoureName).latest(), is(mv.getLoad())); |
| 144 | } |
| 145 | |
| 146 | private void testUpdateMetricWithId(ControlMetricType cmt, MetricValue mv, DeviceId did) { |
| 147 | ControlMetric cm = new ControlMetric(cmt, mv); |
| 148 | monitor.updateMetric(cm, UPDATE_INTERVAL, Optional.of(did)); |
| 149 | } |
| 150 | |
| 151 | private void testLoadMetricWithId(ControlMetricType cmt, MetricValue mv, DeviceId did) { |
| 152 | assertThat(monitor.getLoad(nodeId, cmt, Optional.of(did)).latest(), is(mv.getLoad())); |
| 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testCpuMetric() { |
| 157 | MetricValue mv = new MetricValue.Builder().load(30).add(); |
| 158 | |
| 159 | CPU_METRICS.forEach(cmt -> testUpdateMetricWithoutId(cmt, mv)); |
| 160 | CPU_METRICS.forEach(cmt -> testLoadMetricWithoutId(cmt, mv)); |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void testMemoryMetric() { |
| 165 | MetricValue mv = new MetricValue.Builder().load(40).add(); |
| 166 | |
| 167 | MEMORY_METRICS.forEach(cmt -> testUpdateMetricWithoutId(cmt, mv)); |
| 168 | MEMORY_METRICS.forEach(cmt -> testLoadMetricWithoutId(cmt, mv)); |
| 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void testDiskMetric() { |
| 173 | MetricValue mv = new MetricValue.Builder().load(50).add(); |
| 174 | |
| 175 | ImmutableSet<String> set = ImmutableSet.of("disk1", "disk2"); |
| 176 | |
| 177 | set.forEach(disk -> DISK_METRICS.forEach(cmt -> |
| 178 | testUpdateMetricWithResource(cmt, mv, disk))); |
| 179 | |
| 180 | set.forEach(disk -> DISK_METRICS.forEach(cmt -> |
| 181 | testLoadMetricWithResource(cmt, mv, disk))); |
| 182 | } |
| 183 | |
| 184 | @Test |
| 185 | public void testNetworkMetric() { |
| 186 | MetricValue mv = new MetricValue.Builder().load(10).add(); |
| 187 | |
| 188 | ImmutableSet<String> set = ImmutableSet.of("eth0", "eth1"); |
| 189 | |
| 190 | set.forEach(network -> NETWORK_METRICS.forEach(cmt -> |
| 191 | testUpdateMetricWithResource(cmt, mv, network))); |
| 192 | |
| 193 | set.forEach(network -> NETWORK_METRICS.forEach(cmt -> |
| 194 | testLoadMetricWithResource(cmt, mv, network))); |
| 195 | } |
| 196 | |
| 197 | @Test |
| 198 | public void testUpdateControlMessage() { |
| 199 | MetricValue mv = new MetricValue.Builder().load(10).add(); |
| 200 | |
| 201 | ImmutableSet<String> set = ImmutableSet.of("of:0000000000000001", |
| 202 | "of:0000000000000002"); |
| 203 | |
| 204 | set.forEach(ctrlMsg -> CTRL_MSGS.forEach(cmt -> |
| 205 | testUpdateMetricWithId(cmt, mv, DeviceId.deviceId(ctrlMsg)))); |
| 206 | |
| 207 | set.forEach(ctrlMsg -> CTRL_MSGS.forEach(cmt -> |
| 208 | testLoadMetricWithId(cmt, mv, DeviceId.deviceId(ctrlMsg)))); |
| 209 | } |
| 210 | } |