blob: 1ec963d7a49e36c1e68d843bf8e53c4d801401c2 [file] [log] [blame]
Jian Li7d180c52016-02-01 21:53:08 -08001/*
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 */
16package org.onosproject.cpman.impl;
17
18import com.google.common.collect.ImmutableSet;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.packet.IpAddress;
22import org.onosproject.cluster.ClusterService;
23import org.onosproject.cluster.ControllerNode;
24import org.onosproject.cluster.NodeId;
25import org.onosproject.cpman.ControlMetric;
26import org.onosproject.cpman.ControlMetricType;
27import org.onosproject.cpman.MetricValue;
28import org.onosproject.net.DeviceId;
29
30import java.util.Optional;
Jian Li85060ac2016-02-04 09:58:56 -080031import java.util.Set;
Jian Li7d180c52016-02-01 21:53:08 -080032
33import static org.easymock.EasyMock.anyObject;
34import static org.easymock.EasyMock.createMock;
35import static org.easymock.EasyMock.expect;
36import static org.easymock.EasyMock.replay;
37import static org.hamcrest.Matchers.is;
38import static org.junit.Assert.assertThat;
Jian Li85060ac2016-02-04 09:58:56 -080039
40import static org.onosproject.cpman.ControlResource.*;
Jian Li7d180c52016-02-01 21:53:08 -080041
42/**
43 * Unit test of control plane monitoring service.
44 */
45public 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 Li7d180c52016-02-01 21:53:08 -080052
Jian Li85060ac2016-02-04 09:58:56 -080053 /**
54 * Sets up the services required by control plane monitor.
55 */
Jian Li7d180c52016-02-01 21:53:08 -080056 @Before
Jian Li85060ac2016-02-04 09:58:56 -080057 public void setup() {
Jian Li7d180c52016-02-01 21:53:08 -080058 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 Li85060ac2016-02-04 09:58:56 -0800108 private void testUpdateMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) {
Jian Li7d180c52016-02-01 21:53:08 -0800109 ControlMetric cm = new ControlMetric(cmt, mv);
Jian Li85060ac2016-02-04 09:58:56 -0800110 monitor.updateMetric(cm, UPDATE_INTERVAL, resourceName);
Jian Li7d180c52016-02-01 21:53:08 -0800111 }
112
Jian Li85060ac2016-02-04 09:58:56 -0800113 private void testLoadMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) {
114 assertThat(monitor.getLoad(nodeId, cmt, resourceName).latest(), is(mv.getLoad()));
Jian Li7d180c52016-02-01 21:53:08 -0800115 }
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 Li85060ac2016-02-04 09:58:56 -0800126 /**
127 * Tests cpu metric update and load function.
128 */
Jian Li7d180c52016-02-01 21:53:08 -0800129 @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 Li85060ac2016-02-04 09:58:56 -0800137 /**
138 * Tests memory metric update and load function.
139 */
Jian Li7d180c52016-02-01 21:53:08 -0800140 @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 Li85060ac2016-02-04 09:58:56 -0800148 /**
149 * Tests disk metric update and load function.
150 */
Jian Li7d180c52016-02-01 21:53:08 -0800151 @Test
152 public void testDiskMetric() {
153 MetricValue mv = new MetricValue.Builder().load(50).add();
154
Jian Li85060ac2016-02-04 09:58:56 -0800155 Set<String> set = ImmutableSet.of("disk1", "disk2");
Jian Li7d180c52016-02-01 21:53:08 -0800156
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 Li85060ac2016-02-04 09:58:56 -0800164 /**
165 * Tests network metric update and load function.
166 */
Jian Li7d180c52016-02-01 21:53:08 -0800167 @Test
168 public void testNetworkMetric() {
169 MetricValue mv = new MetricValue.Builder().load(10).add();
170
Jian Li85060ac2016-02-04 09:58:56 -0800171 Set<String> set = ImmutableSet.of("eth0", "eth1");
Jian Li7d180c52016-02-01 21:53:08 -0800172
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 Li85060ac2016-02-04 09:58:56 -0800180 /**
181 * Tests control message update and load function.
182 */
Jian Li7d180c52016-02-01 21:53:08 -0800183 @Test
Jian Li85060ac2016-02-04 09:58:56 -0800184 public void testControlMessage() {
Jian Li7d180c52016-02-01 21:53:08 -0800185 MetricValue mv = new MetricValue.Builder().load(10).add();
Jian Li85060ac2016-02-04 09:58:56 -0800186 Set<DeviceId> set = ImmutableSet.of(DeviceId.deviceId("of:0000000000000001"),
187 DeviceId.deviceId("of:0000000000000002"));
Jian Li7d180c52016-02-01 21:53:08 -0800188
Jian Li85060ac2016-02-04 09:58:56 -0800189 set.forEach(devId -> CONTROL_MESSAGE_METRICS.forEach(cmt ->
190 testUpdateMetricWithId(cmt, mv, devId)));
Jian Li7d180c52016-02-01 21:53:08 -0800191
Jian Li85060ac2016-02-04 09:58:56 -0800192 set.forEach(devId -> CONTROL_MESSAGE_METRICS.forEach(cmt ->
193 testLoadMetricWithId(cmt, mv, devId)));
194 }
Jian Li7d180c52016-02-01 21:53:08 -0800195
Jian Li85060ac2016-02-04 09:58:56 -0800196 /**
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 Li7d180c52016-02-01 21:53:08 -0800215 }
216}