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 Disk usage of Cluster controllers. |
| 27 | */ |
| 28 | public final class NodeDiskUsage { |
| 29 | |
| 30 | private final NodeId node; |
| 31 | |
| 32 | private final long free; |
| 33 | |
| 34 | private final long used; |
| 35 | |
| 36 | private final long total; |
| 37 | |
| 38 | private final Units units; |
| 39 | |
| 40 | private final double usage; |
| 41 | |
| 42 | private static final double PERCENTAGE_MULTIPLIER = 100.0; |
| 43 | |
| 44 | private NodeDiskUsage(final NodeId node, final Long free, |
| 45 | final Long used, final Long total, final Units units, |
| 46 | final Double usage) { |
| 47 | this.node = node; |
| 48 | this.free = free; |
| 49 | this.used = used; |
| 50 | this.total = total; |
| 51 | this.units = units; |
| 52 | this.usage = usage; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @return free disk space available available for the specific nodeId. |
| 57 | */ |
| 58 | public long free() { |
| 59 | return free; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return used disk space used for the specific nodeId. |
| 64 | */ |
| 65 | public long used() { |
| 66 | return used; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return total disk space for the specific nodeId.. |
| 71 | */ |
| 72 | public long total() { |
| 73 | return total; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return units in Kbs /Mbs/ Gbs. |
| 78 | */ |
| 79 | public Units units() { |
| 80 | return units; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Percentage of Disk usage is calculated from Used, |
| 85 | * Available and Total Disk available in Specific Node. |
| 86 | * @return usage overall usage of Disk space for the specific nodeId. |
| 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) |
| 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 | } |
| 117 | NodeDiskUsage other = (NodeDiskUsage) obj; |
| 118 | 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 NodeDiskusage object. |
| 128 | */ |
| 129 | public static final class Builder { |
| 130 | /** |
| 131 | * Builds the NodeDiskusage. |
| 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 NodeDiskusage controller nodeid. |
| 141 | * |
| 142 | * @param node the 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 NodeDiskusage Disk size units(bytes, Kbs, Mbs, |
| 152 | * Gbs). |
| 153 | * |
| 154 | * @param unit the units |
| 155 | * @return self for chaining |
| 156 | */ |
| 157 | public Builder withUnit(final Units unit) { |
| 158 | this.unit = unit; |
| 159 | return this; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Sets the new NodeDiskusage controller free space. |
| 164 | * |
| 165 | * @param free the free space |
| 166 | * @return self for chaining |
| 167 | */ |
| 168 | public Builder free(final Long free) { |
| 169 | this.free = free; |
| 170 | return this; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Sets the new NodeDiskusage controller used space. |
| 175 | * |
| 176 | * @param used the used space |
| 177 | * @return self for chaining |
| 178 | */ |
| 179 | public Builder used(final Long used) { |
| 180 | this.used = used; |
| 181 | return this; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Sets the new NodeDiskusage controller total disk. |
| 186 | * |
| 187 | * @param total the total disk space |
| 188 | * @return self for chaining |
| 189 | */ |
| 190 | public Builder total(final Long total) { |
| 191 | this.total = total; |
| 192 | return this; |
| 193 | } |
| 194 | |
| 195 | public NodeDiskUsage build() { |
| 196 | checkNotNull(node, "Must specify an node id"); |
| 197 | checkNotNull(unit, "Must specify a unit"); |
| 198 | checkNotNull(used, "Must specify a used Diskspace"); |
| 199 | checkNotNull(free, "Must specify a free Diskspace"); |
| 200 | checkNotNull(total, "Must specify a total Diskspace"); |
| 201 | double usage = used * PERCENTAGE_MULTIPLIER / total; |
| 202 | return new NodeDiskUsage(node, free, used, total, unit, usage); |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | |
| 207 | } |