Jian Li | ec343ff | 2016-02-02 17:11:13 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | ec343ff | 2016-02-02 17:11:13 -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 | |
| 18 | import org.onosproject.cpman.ControlLoad; |
| 19 | import org.onosproject.cpman.ControlMetricType; |
| 20 | import org.onosproject.cpman.MetricsDatabase; |
| 21 | |
| 22 | import java.util.Arrays; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | import java.util.stream.IntStream; |
| 25 | |
| 26 | /** |
| 27 | * An implementation of control plane load. |
| 28 | */ |
| 29 | public class DefaultControlLoad implements ControlLoad { |
| 30 | |
| 31 | private final MetricsDatabase mdb; |
| 32 | private final ControlMetricType type; |
| 33 | |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Constructs a control load using the given metrics database and |
| 36 | * control metric type. |
| 37 | * |
| 38 | * @param mdb metrics database |
| 39 | * @param type control metric type |
| 40 | */ |
Jian Li | ec343ff | 2016-02-02 17:11:13 -0800 | [diff] [blame] | 41 | public DefaultControlLoad(MetricsDatabase mdb, ControlMetricType type) { |
| 42 | this.mdb = mdb; |
| 43 | this.type = type; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public long average(int duration, TimeUnit unit) { |
| 48 | return (long) Arrays.stream(recent(duration, unit)).average().getAsDouble(); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public long average() { |
| 53 | return (long) Arrays.stream(all()).average().getAsDouble(); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public long rate() { |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public long latest() { |
| 63 | return (long) mdb.recentMetric(type.toString()); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public boolean isValid() { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public long time() { |
| 73 | return mdb.lastUpdate(type.toString()); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public long[] recent(int duration, TimeUnit unit) { |
| 78 | return doubleToLong(mdb.recentMetrics(type.toString(), duration, unit)); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public long[] all() { |
| 83 | return doubleToLong(mdb.metrics(type.toString())); |
| 84 | } |
| 85 | |
| 86 | private double nanToZero(double d) { |
| 87 | return Double.isNaN(d) ? 0D : d; |
| 88 | } |
| 89 | |
| 90 | private long[] doubleToLong(double[] array) { |
| 91 | final long[] longArray = new long[array.length]; |
| 92 | IntStream.range(0, array.length).forEach(i -> |
| 93 | longArray[i] = (long) nanToZero(array[i])); |
| 94 | |
| 95 | return longArray; |
| 96 | } |
| 97 | } |