blob: 75a248273dda898844edc66d555a9059507f548e [file] [log] [blame]
Jian Li46148902016-01-29 13:33:50 -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
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;
21import org.junit.Test;
Jian Lidaf55ea2016-04-04 20:38:30 -070022import org.onosproject.cpman.ControlMetricType;
23import org.onosproject.cpman.ControlResource;
Jian Li46148902016-01-29 13:33:50 -080024import org.onosproject.cpman.MetricsDatabase;
Jian Lidaf55ea2016-04-04 20:38:30 -070025import org.onosproject.net.DeviceId;
Jian Li46148902016-01-29 13:33:50 -080026
27import java.util.HashMap;
28import java.util.Map;
Jian Lidaf55ea2016-04-04 20:38:30 -070029import java.util.Set;
Jian Li46148902016-01-29 13:33:50 -080030import java.util.concurrent.TimeUnit;
31
32import static org.hamcrest.Matchers.is;
33import static org.junit.Assert.assertThat;
34
35/**
36 * Unit test for control plane metrics back-end database.
37 */
38public class MetricsDatabaseTest {
39
40 private MetricsDatabase mdb;
41 private static final String CPU_METRIC = "cpu";
42 private static final String CPU_LOAD = "load";
Jian Lidaf55ea2016-04-04 20:38:30 -070043 private static final String DEFAULT_RES = "resource";
Jian Li46148902016-01-29 13:33:50 -080044 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 Lidaf55ea2016-04-04 20:38:30 -070047 private Map<DeviceId, MetricsDatabase> devMetricsMap;
Jian Li46148902016-01-29 13:33:50 -080048
49 /**
Jian Li85060ac2016-02-04 09:58:56 -080050 * Initializes metrics database instance.
Jian Li46148902016-01-29 13:33:50 -080051 */
52 @Before
53 public void setUp() {
54 mdb = new DefaultMetricsDatabase.Builder()
55 .withMetricName(CPU_METRIC)
Jian Lidaf55ea2016-04-04 20:38:30 -070056 .withResourceName(DEFAULT_RES)
Jian Li46148902016-01-29 13:33:50 -080057 .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 Lidaf55ea2016-04-04 20:38:30 -0700126 .withResourceName(DEFAULT_RES)
Jian Li46148902016-01-29 13:33:50 -0800127 .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 Lidaf55ea2016-04-04 20:38:30 -0700139
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 Li46148902016-01-29 13:33:50 -0800186}