blob: 1b152e1e0683b5479a858c9f234a780c9fd781e2 [file] [log] [blame]
Jian Li46148902016-01-29 13:33:50 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Li46148902016-01-29 13:33:50 -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
Jian Lidaf55ea2016-04-04 20:38:30 -070018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Maps;
Jian Li46148902016-01-29 13:33:50 -080020import org.junit.Before;
Jian Li951e2bb2016-05-05 14:00:54 -070021import org.junit.Ignore;
Jian Li46148902016-01-29 13:33:50 -080022import org.junit.Test;
Jian Lidaf55ea2016-04-04 20:38:30 -070023import org.onosproject.cpman.ControlMetricType;
24import org.onosproject.cpman.ControlResource;
Jian Li46148902016-01-29 13:33:50 -080025import org.onosproject.cpman.MetricsDatabase;
Jian Lidaf55ea2016-04-04 20:38:30 -070026import org.onosproject.net.DeviceId;
Jian Li46148902016-01-29 13:33:50 -080027
28import java.util.HashMap;
29import java.util.Map;
Jian Lidaf55ea2016-04-04 20:38:30 -070030import java.util.Set;
Jian Li46148902016-01-29 13:33:50 -080031import java.util.concurrent.TimeUnit;
32
33import static org.hamcrest.Matchers.is;
34import static org.junit.Assert.assertThat;
35
36/**
37 * Unit test for control plane metrics back-end database.
38 */
39public class MetricsDatabaseTest {
40
41 private MetricsDatabase mdb;
42 private static final String CPU_METRIC = "cpu";
43 private static final String CPU_LOAD = "load";
Jian Lidaf55ea2016-04-04 20:38:30 -070044 private static final String DEFAULT_RES = "resource";
Jian Li46148902016-01-29 13:33:50 -080045 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 Lidaf55ea2016-04-04 20:38:30 -070048 private Map<DeviceId, MetricsDatabase> devMetricsMap;
Jian Li46148902016-01-29 13:33:50 -080049
50 /**
Jian Li85060ac2016-02-04 09:58:56 -080051 * Initializes metrics database instance.
Jian Li46148902016-01-29 13:33:50 -080052 */
53 @Before
54 public void setUp() {
55 mdb = new DefaultMetricsDatabase.Builder()
56 .withMetricName(CPU_METRIC)
Jian Lidaf55ea2016-04-04 20:38:30 -070057 .withResourceName(DEFAULT_RES)
Jian Li46148902016-01-29 13:33:50 -080058 .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 Li951e2bb2016-05-05 14:00:54 -070083 @Ignore("FIXME: in some cases it returns incorrect range result, known as RRD4J bug")
Jian Li46148902016-01-29 13:33:50 -080084 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 Lidaf55ea2016-04-04 20:38:30 -0700128 .withResourceName(DEFAULT_RES)
Jian Li46148902016-01-29 13:33:50 -0800129 .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 Lidaf55ea2016-04-04 20:38:30 -0700141
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 Li1aa07822016-04-19 17:58:02 -0700154 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 Lidaf55ea2016-04-04 20:38:30 -0700161
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 Li46148902016-01-29 13:33:50 -0800190}