Jian Li | 4614890 | 2016-01-29 13:33:50 -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 | |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Maps; |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 22 | import org.onosproject.cpman.ControlMetricType; |
| 23 | import org.onosproject.cpman.ControlResource; |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 24 | import org.onosproject.cpman.MetricsDatabase; |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 26 | |
| 27 | import java.util.HashMap; |
| 28 | import java.util.Map; |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 29 | import java.util.Set; |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 30 | import java.util.concurrent.TimeUnit; |
| 31 | |
| 32 | import static org.hamcrest.Matchers.is; |
| 33 | import static org.junit.Assert.assertThat; |
| 34 | |
| 35 | /** |
| 36 | * Unit test for control plane metrics back-end database. |
| 37 | */ |
| 38 | public class MetricsDatabaseTest { |
| 39 | |
| 40 | private MetricsDatabase mdb; |
| 41 | private static final String CPU_METRIC = "cpu"; |
| 42 | private static final String CPU_LOAD = "load"; |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 43 | private static final String DEFAULT_RES = "resource"; |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 44 | private static final String MEMORY_METRIC = "memory"; |
| 45 | private static final String MEMORY_FREE_PERC = "freePerc"; |
| 46 | private static final String MEMORY_USED_PERC = "usedPerc"; |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 47 | private Map<DeviceId, MetricsDatabase> devMetricsMap; |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 50 | * Initializes metrics database instance. |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 51 | */ |
| 52 | @Before |
| 53 | public void setUp() { |
| 54 | mdb = new DefaultMetricsDatabase.Builder() |
| 55 | .withMetricName(CPU_METRIC) |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 56 | .withResourceName(DEFAULT_RES) |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 57 | .addMetricType(CPU_LOAD) |
| 58 | .build(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Tests the metric update function. |
| 63 | */ |
| 64 | @Test |
| 65 | public void testMetricUpdate() { |
| 66 | long currentTime = System.currentTimeMillis() / 1000L; |
| 67 | |
| 68 | mdb.updateMetric(CPU_LOAD, 30, currentTime); |
| 69 | assertThat(30D, is(mdb.recentMetric(CPU_LOAD))); |
| 70 | |
| 71 | mdb.updateMetric(CPU_LOAD, 40, currentTime + 60); |
| 72 | assertThat(40D, is(mdb.recentMetric(CPU_LOAD))); |
| 73 | |
| 74 | mdb.updateMetric(CPU_LOAD, 50, currentTime + 120); |
| 75 | assertThat(50D, is(mdb.recentMetric(CPU_LOAD))); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests the metric range fetch function. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testMetricRangeFetch() { |
| 83 | // full range fetch |
| 84 | assertThat(mdb.metrics(CPU_LOAD).length, is(60 * 24)); |
| 85 | |
| 86 | // query one minute time range |
| 87 | assertThat(mdb.recentMetrics(CPU_LOAD, 1, TimeUnit.MINUTES).length, is(1)); |
| 88 | |
| 89 | // query one hour time range |
| 90 | assertThat(mdb.recentMetrics(CPU_LOAD, 1, TimeUnit.HOURS).length, is(60)); |
| 91 | |
| 92 | // query one day time range |
| 93 | assertThat(mdb.recentMetrics(CPU_LOAD, 1, TimeUnit.DAYS).length, is(60 * 24)); |
| 94 | |
| 95 | // query a specific time range |
| 96 | long endTime = System.currentTimeMillis() / 1000L; |
| 97 | long startTime = endTime - 60 * 5; |
| 98 | assertThat(mdb.metrics(CPU_LOAD, startTime, endTime).length, is(5)); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Test the projected time range. |
| 103 | */ |
| 104 | @Test(expected = IllegalArgumentException.class) |
| 105 | public void testExceededTimeRange() { |
| 106 | // query 25 hours time range |
| 107 | assertThat(mdb.recentMetrics(CPU_LOAD, 25, TimeUnit.HOURS).length, is(60 * 24)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Test the projected time range. |
| 112 | */ |
| 113 | @Test(expected = IllegalArgumentException.class) |
| 114 | public void testInsufficientTimeRange() { |
| 115 | // query 50 seconds time range |
| 116 | assertThat(mdb.recentMetrics(CPU_LOAD, 50, TimeUnit.SECONDS).length, is(1)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Test multiple metrics update and query. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testMultipleMetrics() { |
| 124 | MetricsDatabase multiMdb = new DefaultMetricsDatabase.Builder() |
| 125 | .withMetricName(MEMORY_METRIC) |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 126 | .withResourceName(DEFAULT_RES) |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 127 | .addMetricType(MEMORY_FREE_PERC) |
| 128 | .addMetricType(MEMORY_USED_PERC) |
| 129 | .build(); |
| 130 | |
| 131 | Map<String, Double> metrics = new HashMap<>(); |
| 132 | metrics.putIfAbsent(MEMORY_FREE_PERC, 30D); |
| 133 | metrics.putIfAbsent(MEMORY_USED_PERC, 70D); |
| 134 | multiMdb.updateMetrics(metrics); |
| 135 | |
| 136 | assertThat(30D, is(multiMdb.recentMetric(MEMORY_FREE_PERC))); |
| 137 | assertThat(70D, is(multiMdb.recentMetric(MEMORY_USED_PERC))); |
| 138 | } |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Tests device metrics map update and query. |
| 142 | */ |
| 143 | @Test |
| 144 | public void testDeviceMetricsMap() { |
| 145 | ControlResource.Type type = ControlResource.Type.CONTROL_MESSAGE; |
| 146 | DeviceId devId1 = DeviceId.deviceId("of:0000000000000101"); |
| 147 | DeviceId devId2 = DeviceId.deviceId("of:0000000000000102"); |
| 148 | |
| 149 | devMetricsMap = Maps.newHashMap(); |
| 150 | |
| 151 | Set<DeviceId> devices = ImmutableSet.of(devId1, devId2); |
| 152 | devices.forEach(dev -> |
| 153 | devMetricsMap.putIfAbsent(dev, |
| 154 | genMDbBuilder(type, ControlResource.CONTROL_MESSAGE_METRICS) |
| 155 | .withResourceName(dev.toString()) |
| 156 | .build())); |
| 157 | |
| 158 | Map<String, Double> metrics1 = new HashMap<>(); |
| 159 | ControlResource.CONTROL_MESSAGE_METRICS.forEach(msgType -> |
| 160 | metrics1.putIfAbsent(msgType.toString(), 10D)); |
| 161 | |
| 162 | Map<String, Double> metrics2 = new HashMap<>(); |
| 163 | ControlResource.CONTROL_MESSAGE_METRICS.forEach(msgType -> |
| 164 | metrics2.putIfAbsent(msgType.toString(), 20D)); |
| 165 | |
| 166 | |
| 167 | devMetricsMap.get(devId1).updateMetrics(metrics1); |
| 168 | devMetricsMap.get(devId2).updateMetrics(metrics2); |
| 169 | |
| 170 | ControlResource.CONTROL_MESSAGE_METRICS.forEach(msgType -> |
| 171 | assertThat(10D, is(devMetricsMap.get(devId1).recentMetric(msgType.toString()))) |
| 172 | ); |
| 173 | |
| 174 | ControlResource.CONTROL_MESSAGE_METRICS.forEach(msgType -> |
| 175 | assertThat(20D, is(devMetricsMap.get(devId2).recentMetric(msgType.toString()))) |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | private MetricsDatabase.Builder genMDbBuilder(ControlResource.Type resourceType, |
| 180 | Set<ControlMetricType> metricTypes) { |
| 181 | MetricsDatabase.Builder builder = new DefaultMetricsDatabase.Builder(); |
| 182 | builder.withMetricName(resourceType.toString()); |
| 183 | metricTypes.forEach(type -> builder.addMetricType(type.toString())); |
| 184 | return builder; |
| 185 | } |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 186 | } |