blob: 62502f98c566aebfd9e3347d092ae5b546bf6562 [file] [log] [blame]
Jian Li7d180c52016-02-01 21:53:08 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li7d180c52016-02-01 21:53:08 -08003 *
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;
Jian Li23906cc2016-03-31 11:16:44 -070029import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
30import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
Jian Li7d180c52016-02-01 21:53:08 -080031
32import java.util.Optional;
Jian Li85060ac2016-02-04 09:58:56 -080033import java.util.Set;
Jian Li7d180c52016-02-01 21:53:08 -080034
35import static org.easymock.EasyMock.anyObject;
36import static org.easymock.EasyMock.createMock;
37import static org.easymock.EasyMock.expect;
38import static org.easymock.EasyMock.replay;
39import static org.hamcrest.Matchers.is;
40import static org.junit.Assert.assertThat;
Jian Li23906cc2016-03-31 11:16:44 -070041import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
42import static org.onosproject.cpman.ControlResource.CPU_METRICS;
43import static org.onosproject.cpman.ControlResource.DISK_METRICS;
44import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
45import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
46import static org.onosproject.cpman.ControlResource.Type;
Jian Li7d180c52016-02-01 21:53:08 -080047
48/**
49 * Unit test of control plane monitoring service.
50 */
51public class ControlPlaneMonitorTest {
52
53 private ControlPlaneMonitor monitor;
54 private static final Integer UPDATE_INTERVAL = 1;
55 private ClusterService mockClusterService;
56 private ControllerNode mockControllerNode;
Jian Li23906cc2016-03-31 11:16:44 -070057 private ClusterCommunicationService mockCommunicationService;
Jian Li7d180c52016-02-01 21:53:08 -080058 private NodeId nodeId;
Jian Li7d180c52016-02-01 21:53:08 -080059
Jian Li85060ac2016-02-04 09:58:56 -080060 /**
61 * Sets up the services required by control plane monitor.
62 */
Jian Li7d180c52016-02-01 21:53:08 -080063 @Before
Jian Li85060ac2016-02-04 09:58:56 -080064 public void setup() {
Jian Li7d180c52016-02-01 21:53:08 -080065 monitor = new ControlPlaneMonitor();
Jian Li23906cc2016-03-31 11:16:44 -070066
67 mockCommunicationService = new ClusterCommunicationServiceAdapter();
68 monitor.communicationService = mockCommunicationService;
Jian Li7d180c52016-02-01 21:53:08 -080069
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 Li23906cc2016-03-31 11:16:44 -070080
81 monitor.activate();
Jian Li7d180c52016-02-01 21:53:08 -080082 }
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 Li23906cc2016-03-31 11:16:44 -0700116 assertThat(monitor.getLocalLoad(cmt, Optional.ofNullable(null)).latest(), is(mv.getLoad()));
Jian Li7d180c52016-02-01 21:53:08 -0800117 }
118
Jian Li85060ac2016-02-04 09:58:56 -0800119 private void testUpdateMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) {
Jian Li7d180c52016-02-01 21:53:08 -0800120 ControlMetric cm = new ControlMetric(cmt, mv);
Jian Li85060ac2016-02-04 09:58:56 -0800121 monitor.updateMetric(cm, UPDATE_INTERVAL, resourceName);
Jian Li7d180c52016-02-01 21:53:08 -0800122 }
123
Jian Li85060ac2016-02-04 09:58:56 -0800124 private void testLoadMetricWithResource(ControlMetricType cmt, MetricValue mv, String resourceName) {
Jian Li23906cc2016-03-31 11:16:44 -0700125 assertThat(monitor.getLocalLoad(cmt, resourceName).latest(), is(mv.getLoad()));
Jian Li7d180c52016-02-01 21:53:08 -0800126 }
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 Li23906cc2016-03-31 11:16:44 -0700134 assertThat(monitor.getLocalLoad(cmt, Optional.of(did)).latest(), is(mv.getLoad()));
Jian Li7d180c52016-02-01 21:53:08 -0800135 }
136
Jian Li85060ac2016-02-04 09:58:56 -0800137 /**
138 * Tests cpu metric update and load function.
139 */
Jian Li7d180c52016-02-01 21:53:08 -0800140 @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 Li85060ac2016-02-04 09:58:56 -0800148 /**
149 * Tests memory metric update and load function.
150 */
Jian Li7d180c52016-02-01 21:53:08 -0800151 @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 Li85060ac2016-02-04 09:58:56 -0800159 /**
160 * Tests disk metric update and load function.
161 */
Jian Li7d180c52016-02-01 21:53:08 -0800162 @Test
163 public void testDiskMetric() {
164 MetricValue mv = new MetricValue.Builder().load(50).add();
165
Jian Li85060ac2016-02-04 09:58:56 -0800166 Set<String> set = ImmutableSet.of("disk1", "disk2");
Jian Li7d180c52016-02-01 21:53:08 -0800167
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 Li85060ac2016-02-04 09:58:56 -0800175 /**
176 * Tests network metric update and load function.
177 */
Jian Li7d180c52016-02-01 21:53:08 -0800178 @Test
179 public void testNetworkMetric() {
180 MetricValue mv = new MetricValue.Builder().load(10).add();
181
Jian Li85060ac2016-02-04 09:58:56 -0800182 Set<String> set = ImmutableSet.of("eth0", "eth1");
Jian Li7d180c52016-02-01 21:53:08 -0800183
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 Li85060ac2016-02-04 09:58:56 -0800191 /**
192 * Tests control message update and load function.
193 */
Jian Li7d180c52016-02-01 21:53:08 -0800194 @Test
Jian Li85060ac2016-02-04 09:58:56 -0800195 public void testControlMessage() {
Jian Li7d180c52016-02-01 21:53:08 -0800196 MetricValue mv = new MetricValue.Builder().load(10).add();
Jian Li85060ac2016-02-04 09:58:56 -0800197 Set<DeviceId> set = ImmutableSet.of(DeviceId.deviceId("of:0000000000000001"),
198 DeviceId.deviceId("of:0000000000000002"));
Jian Li7d180c52016-02-01 21:53:08 -0800199
Jian Li85060ac2016-02-04 09:58:56 -0800200 set.forEach(devId -> CONTROL_MESSAGE_METRICS.forEach(cmt ->
201 testUpdateMetricWithId(cmt, mv, devId)));
Jian Li7d180c52016-02-01 21:53:08 -0800202
Jian Li85060ac2016-02-04 09:58:56 -0800203 set.forEach(devId -> CONTROL_MESSAGE_METRICS.forEach(cmt ->
204 testLoadMetricWithId(cmt, mv, devId)));
205 }
Jian Li7d180c52016-02-01 21:53:08 -0800206
Jian Li85060ac2016-02-04 09:58:56 -0800207 /**
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 Li7d180c52016-02-01 21:53:08 -0800226 }
227}