blob: 7a88a2a4f290dd8e19b3cdc9693c009f7e2b2fbb [file] [log] [blame]
kdarapu97843dc2018-05-10 12:46:32 +05301/*
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 */
16package org.onosproject.nodemetrics;
17
18import com.google.common.base.MoreObjects;
19import org.onosproject.cluster.NodeId;
20
21import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * Represents CPU usage info of Cluster controllers.
27 */
nitinanand920fcb42018-05-18 17:30:36 +053028public final class NodeCpuUsage {
kdarapu97843dc2018-05-10 12:46:32 +053029 private final NodeId node;
30 private final double usage;
31
nitinanand920fcb42018-05-18 17:30:36 +053032 private NodeCpuUsage(final NodeId node, final Double usage) {
kdarapu97843dc2018-05-10 12:46:32 +053033 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 }
nitinanand920fcb42018-05-18 17:30:36 +053068 final NodeCpuUsage other = (NodeCpuUsage) obj;
kdarapu97843dc2018-05-10 12:46:32 +053069 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
nitinanand920fcb42018-05-18 17:30:36 +0530105 public NodeCpuUsage build() {
kdarapu97843dc2018-05-10 12:46:32 +0530106 checkNotNull(node, "Must specify an node id");
107 checkNotNull(usage, "Must specify a usage");
nitinanand920fcb42018-05-18 17:30:36 +0530108 return new NodeCpuUsage(node, usage);
kdarapu97843dc2018-05-10 12:46:32 +0530109 }
110 }
111
112}