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