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.devices; |
| 18 | |
| 19 | import org.onosproject.drivers.server.devices.CpuDevice; |
| 20 | import org.onosproject.drivers.server.devices.CpuVendor; |
| 21 | |
| 22 | import org.onosproject.drivers.server.impl.stats.DefaultCpuStatistics; |
| 23 | |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | |
| 29 | import java.util.Objects; |
| 30 | |
| 31 | /** |
| 32 | * Default implementation for CPU core devices. |
| 33 | */ |
| 34 | public class DefaultCpuDevice implements CpuDevice { |
| 35 | |
| 36 | private final int id; |
| 37 | private final CpuVendor vendor; |
| 38 | private final long frequency; |
| 39 | |
| 40 | // Maximum CPU core frequency in MHz |
| 41 | public static final long MAX_FREQUENCY_MHZ = 4500; |
| 42 | |
| 43 | public DefaultCpuDevice(int id, CpuVendor vendor, long frequency) { |
| 44 | checkArgument( |
| 45 | (id >= 0) && (id < DefaultCpuStatistics.MAX_CPU_NB), |
| 46 | "CPU core ID must be in [0, " + |
| 47 | String.valueOf(DefaultCpuStatistics.MAX_CPU_NB - 1) + "]" |
| 48 | ); |
| 49 | checkNotNull( |
| 50 | vendor, |
| 51 | "CPU core vendor cannot be null" |
| 52 | ); |
| 53 | checkArgument( |
| 54 | (frequency > 0) && (frequency <= MAX_FREQUENCY_MHZ), |
| 55 | "CPU core frequency (MHz) must be positive and less or equal than " + |
| 56 | MAX_FREQUENCY_MHZ + " MHz" |
| 57 | ); |
| 58 | |
| 59 | this.id = id; |
| 60 | this.vendor = vendor; |
| 61 | this.frequency = frequency; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int id() { |
| 66 | return this.id; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public CpuVendor vendor() { |
| 71 | return this.vendor; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public long frequency() { |
| 76 | return this.frequency; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public String toString() { |
| 81 | return MoreObjects.toStringHelper(this) |
| 82 | .omitNullValues() |
| 83 | .add("id", id()) |
| 84 | .add("vendor", vendor()) |
| 85 | .add("frequency", frequency()) |
| 86 | .toString(); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean equals(Object obj) { |
| 91 | if (obj == this) { |
| 92 | return true; |
| 93 | } |
| 94 | if (!(obj instanceof CpuDevice)) { |
| 95 | return false; |
| 96 | } |
| 97 | CpuDevice device = (CpuDevice) obj; |
| 98 | return this.id() == device.id() && |
| 99 | this.vendor() == device.vendor() && |
| 100 | this.frequency() == device.frequency(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int hashCode() { |
| 105 | return Objects.hash(id, vendor, frequency); |
| 106 | } |
| 107 | |
| 108 | } |