kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [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 | package org.onosproject.nodemetrics; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onosproject.cluster.NodeId; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
| 25 | /** |
| 26 | * Represents CPU usage info of Cluster controllers. |
| 27 | */ |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 28 | public final class NodeCpuUsage { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 29 | private final NodeId node; |
| 30 | private final double usage; |
| 31 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 32 | private NodeCpuUsage(final NodeId node, final Double usage) { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 33 | this.node = node; |
| 34 | this.usage = usage; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Overall usage of CPU includes combined usage of |
| 39 | * (user,sys,nice,idle,wait,irq,softIrq and stolen) etc. |
| 40 | * @return usage is overall usage of CPU for the Specific Node. |
| 41 | */ |
| 42 | public double usage() { |
| 43 | return usage; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public String toString() { |
| 48 | |
| 49 | return MoreObjects.toStringHelper(this) |
| 50 | .add("node", this.node) |
| 51 | .add("usage", String.format("%.2f%s", this.usage, "%")) |
| 52 | .toString(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public int hashCode() { |
| 57 | return Objects.hash(node, usage); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public boolean equals(Object obj) { |
| 62 | if (obj == null) { |
| 63 | return false; |
| 64 | } |
| 65 | if (getClass() != obj.getClass()) { |
| 66 | return false; |
| 67 | } |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 68 | final NodeCpuUsage other = (NodeCpuUsage) obj; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 69 | return Objects.equals(this.node, other.node) |
| 70 | && Objects.equals(this.usage, other.usage); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Builder for the DefaultNodeCpu object. |
| 75 | */ |
| 76 | public static final class Builder { |
| 77 | /** |
| 78 | * Builds the DefaultNodeCpu. |
| 79 | **/ |
| 80 | private NodeId node; |
| 81 | private Double usage; |
| 82 | |
| 83 | /** |
| 84 | * Sets the DefaultNodeCpu usage from Library. |
| 85 | * |
| 86 | * @param usage of CPU |
| 87 | * @return self for chaining |
| 88 | */ |
| 89 | public Builder usage(final Double usage) { |
| 90 | this.usage = usage; |
| 91 | return this; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Sets the new DefaultNodeCpu controller node id. |
| 96 | * |
| 97 | * @param node the nodeId |
| 98 | * @return self for chaining |
| 99 | */ |
| 100 | public Builder withNode(final NodeId node) { |
| 101 | this.node = node; |
| 102 | return this; |
| 103 | } |
| 104 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 105 | public NodeCpuUsage build() { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 106 | checkNotNull(node, "Must specify an node id"); |
| 107 | checkNotNull(usage, "Must specify a usage"); |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 108 | return new NodeCpuUsage(node, usage); |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | } |