blob: 4531fe19bde10f9087de0b07858cff65bc7673bb [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 */
16
17package org.onosproject.nodemetrics;
18
19import com.google.common.base.MoreObjects;
20import org.onosproject.cluster.NodeId;
21
22import java.util.Objects;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * Represents Memory usage of Cluster controllers.
28 */
nitinanand920fcb42018-05-18 17:30:36 +053029public final class NodeMemoryUsage {
kdarapu97843dc2018-05-10 12:46:32 +053030
31 private final NodeId node;
32
33 private final Units units;
34
35 private final long free;
36
37 private final long used;
38
39 private final long total;
40
41 private final double usage;
42
43 private static final double PERCENTAGE_MULTIPLIER = 100.0;
44
nitinanand920fcb42018-05-18 17:30:36 +053045 private NodeMemoryUsage(final NodeId node, final Units units,
kdarapu97843dc2018-05-10 12:46:32 +053046 final Long free, final Long used, final Long total,
47 final Double usage) {
48 this.node = node;
49 this.units = units;
50 this.free = free;
51 this.used = used;
52 this.total = total;
53 this.usage = usage;
54 }
55
56 /**
57 * @return free memory available for the specific nodeId.
58 */
59 public long free() {
60 return free;
61 }
62
63 /**
64 * @return used memory for the specific nodeId..
65 */
66 public long used() {
67 return used;
68 }
69
70 /**
71 * @return total memory for the specific nodeId..
72 */
73 public long total() {
74 return total;
75 }
76
77 /**
78 * @return memory in Kbs / Mbs etc.
79 */
80 public Units units() {
81 return units;
82 }
83
84 /**
85 * Percentage of Memory usage is calculated from Used, Available and Total Memory.
86 * @return usage overall usage of memory in percentage for the specific node.
87 */
88 public double usage() {
89 return usage;
90 }
91
92 @Override
93 public int hashCode() {
94 return Objects.hash(node, free, used, total, units, usage);
95 }
96
97 @Override
98 public String toString() {
99 return MoreObjects.toStringHelper(this)
100 .add("node", this.node)
101 .add("free", this.free)
102 .add("used", this.used)
103 .add("total", this.total)
104 .add("units", this.units.toString())
105 .add("usage", this.usage + "%")
106 .toString();
107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (obj == null) {
112 return false;
113 }
114 if (getClass() != obj.getClass()) {
115 return false;
116 }
nitinanand920fcb42018-05-18 17:30:36 +0530117 NodeMemoryUsage other = (NodeMemoryUsage) obj;
kdarapu97843dc2018-05-10 12:46:32 +0530118 return Objects.equals(this.node, other.node)
119 && Objects.equals(this.free, other.free)
120 && Objects.equals(this.used, other.used)
121 && Objects.equals(this.total, other.total)
122 && Objects.equals(this.units, other.units)
123 && Objects.equals(this.usage, other.usage);
124 }
125
126 /**
127 * Builder for the DefaultNodeMemory object.
128 */
129 public static final class Builder {
130 /**
131 * Builds the DefaultNodeMemory.
132 **/
133 private NodeId node;
134 private Units unit;
135 private Long free;
136 private Long used;
137 private Long total;
138
139 /**
140 * Sets the new DefaultNodeMemory controller nodeId.
141 *
142 * @param node nodeId
143 * @return self for chaining
144 */
145 public Builder withNode(final NodeId node) {
146 this.node = node;
147 return this;
148 }
149
150 /**
151 * Sets the new DefaultNodeMemory Disk size units(bytes, Kbs, Mbs, Gbs).
152 *
153 * @param unit units
154 * @return self for chaining
155 */
156 public Builder withUnit(final Units unit) {
157 this.unit = unit;
158 return this;
159 }
160
161 /**
162 * Sets the new DefaultNodeMemory controller free space.
163 *
164 * @param free free space
165 * @return self for chaining
166 */
167 public Builder free(final Long free) {
168 this.free = free;
169 return this;
170 }
171
172 /**
173 * Sets the new DefaultNodeMemory controller used space.
174 *
175 * @param used used space
176 * @return self for chaining
177 */
178 public Builder used(final Long used) {
179 this.used = used;
180 return this;
181 }
182
183 /**
184 * Sets the new DefaultNodeMemory controller total disk.
185 *
186 * @param total the total disk space
187 * @return self for chaining
188 */
189 public Builder total(final Long total) {
190 this.total = total;
191 return this;
192 }
193
nitinanand920fcb42018-05-18 17:30:36 +0530194 public NodeMemoryUsage build() {
kdarapu97843dc2018-05-10 12:46:32 +0530195 checkNotNull(node, "Must specify an node id");
196 checkNotNull(unit, "Must specify a unit");
197 checkNotNull(used, "Must specify a used Diskspace");
198 checkNotNull(free, "Must specify a free Diskspace");
199 checkNotNull(total, "Must specify a total Diskspace");
200 double usage = used * PERCENTAGE_MULTIPLIER / total;
nitinanand920fcb42018-05-18 17:30:36 +0530201 return new NodeMemoryUsage(node, unit, free, used, total, usage);
kdarapu97843dc2018-05-10 12:46:32 +0530202 }
203
204 }
205
206}