Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.drivers.server.impl.stats; |
| 18 | |
| 19 | import org.onosproject.drivers.server.stats.CpuStatistics; |
| 20 | |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | import static com.google.common.base.Preconditions.checkArgument; |
| 26 | |
| 27 | /** |
| 28 | * Default implementation for CPU statistics. |
| 29 | */ |
| 30 | public final class DefaultCpuStatistics implements CpuStatistics { |
| 31 | |
| 32 | private static final float MIN_CPU_LOAD = (float) 0.0; |
| 33 | private static final float MAX_CPU_LOAD = (float) 1.0; |
| 34 | |
| 35 | // Upper limit of CPU cores in one machine |
| 36 | public static final int MAX_CPU_NB = 512; |
| 37 | |
| 38 | private final DeviceId deviceId; |
| 39 | |
| 40 | private final int id; |
| 41 | private final float load; |
| 42 | private final boolean isBusy; |
| 43 | |
| 44 | private DefaultCpuStatistics( |
| 45 | DeviceId deviceId, |
| 46 | int id, |
| 47 | float load, |
| 48 | boolean isBusy) { |
| 49 | checkNotNull(deviceId, "Device ID is NULL"); |
| 50 | checkArgument( |
| 51 | (id >= 0) && (id < MAX_CPU_NB), |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 52 | "Invalid CPU core ID " + String.valueOf(id) + ", not in [0, " + String.valueOf(MAX_CPU_NB - 1) + "]" |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 53 | ); |
| 54 | checkArgument( |
| 55 | (load >= MIN_CPU_LOAD) && (load <= MAX_CPU_LOAD), |
Georgios Katsikas | 80e0b9f | 2018-07-21 20:29:18 +0200 | [diff] [blame^] | 56 | "Invalid CPU load " + Float.toString(load) + ", not in [" + MIN_CPU_LOAD + ", " + MAX_CPU_LOAD + "]" |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 57 | ); |
| 58 | |
| 59 | this.deviceId = deviceId; |
| 60 | this.id = id; |
| 61 | this.load = load; |
| 62 | this.isBusy = isBusy; |
| 63 | } |
| 64 | |
| 65 | // Constructor for serializer |
| 66 | private DefaultCpuStatistics() { |
| 67 | this.deviceId = null; |
| 68 | this.id = 0; |
| 69 | this.load = 0; |
| 70 | this.isBusy = false; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Creates a builder for DefaultCpuStatistics object. |
| 75 | * |
| 76 | * @return builder object for DefaultCpuStatistics object |
| 77 | */ |
| 78 | public static DefaultCpuStatistics.Builder builder() { |
| 79 | return new Builder(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int id() { |
| 84 | return this.id; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public float load() { |
| 89 | return this.load; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean busy() { |
| 94 | return this.isBusy; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
| 99 | return MoreObjects.toStringHelper(this) |
| 100 | .omitNullValues() |
| 101 | .add("device", deviceId) |
| 102 | .add("id", id()) |
| 103 | .add("load", load()) |
| 104 | .add("isBusy", busy()) |
| 105 | .toString(); |
| 106 | } |
| 107 | |
| 108 | public static final class Builder { |
| 109 | |
| 110 | DeviceId deviceId; |
| 111 | int id; |
| 112 | float load; |
| 113 | boolean isBusy; |
| 114 | |
| 115 | private Builder() { |
| 116 | |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Sets the device identifier. |
| 121 | * |
| 122 | * @param deviceId device identifier |
| 123 | * @return builder object |
| 124 | */ |
| 125 | public Builder setDeviceId(DeviceId deviceId) { |
| 126 | this.deviceId = deviceId; |
| 127 | |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Sets the CPU ID. |
| 133 | * |
| 134 | * @param id the CPU ID |
| 135 | * @return builder object |
| 136 | */ |
| 137 | public Builder setId(int id) { |
| 138 | this.id = id; |
| 139 | |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Sets the CPU load. |
| 145 | * |
| 146 | * @param load CPU load |
| 147 | * @return builder object |
| 148 | */ |
| 149 | public Builder setLoad(float load) { |
| 150 | this.load = load; |
| 151 | |
| 152 | return this; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Sets the CPU status (free or busy). |
| 157 | * |
| 158 | * @param isBusy CPU status |
| 159 | * @return builder object |
| 160 | */ |
| 161 | public Builder setIsBusy(boolean isBusy) { |
| 162 | this.isBusy = isBusy; |
| 163 | |
| 164 | return this; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Creates a DefaultCpuStatistics object. |
| 169 | * |
| 170 | * @return DefaultCpuStatistics object |
| 171 | */ |
| 172 | public DefaultCpuStatistics build() { |
| 173 | return new DefaultCpuStatistics( |
| 174 | deviceId, |
| 175 | id, |
| 176 | load, |
| 177 | isBusy |
| 178 | ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | } |