summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/model/Device.java
blob: 014826c36965d54a2e251f7f8a74c1f28b38993c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Console.
 *
 * Gluster Management Console is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Gluster Management Console is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.core.model;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

import org.gluster.storage.management.core.utils.StringUtil;



/**
 *
 */
public class Device extends Entity {
	public enum DEVICE_STATUS {
		//TODO: Status "READY" to be removed after python script is changed accordingly
		INITIALIZED, UNINITIALIZED, INITIALIZING, IO_ERROR, UNKNOWN
	};
	
	public enum DEVICE_TYPE {
		DATA, BOOT, SWAP, UNKNOWN
	};

	private static final String[] DEVICE_STATUS_STR = { "Initialized", "Uninitialized", "Initializing", "I/O Error", "Unknown" };
	private static final String[] DEVICE_TYPE_STR = { "Data", "Boot", "Swap", "Unknown" };

	// type = data, boot, other
	private DEVICE_TYPE type;
	
	private String fsType;
	private String fsVersion;
	
	private String serverName;
	private String mountPoint;
	
	private Double space = 0.0;
	private Double spaceInUse = 0.0;
	private DEVICE_STATUS status;

	public Device() {
	}
	
	public Device(Server server, String name, String mountPoint, Double space, Double spaceInUse, DEVICE_STATUS status) {
		super(name, server);
		setServerName(server != null ? server.getName() : "");
		setMountPoint(mountPoint);
		setSpace(space);
		setSpaceInUse(spaceInUse);
		setStatus(status);
	}
	
	@XmlElement(name="size")
	public Double getSpace() {
		return space;
	}
	
	public Double getFreeSpace() {
		return (getSpace() - getSpaceInUse());
	}

	public void setSpace(Double space) {
		this.space = space;
	}
	
	public boolean isUninitialized() {
		return getStatus() == DEVICE_STATUS.UNINITIALIZED;
	}
	
	public boolean hasErrors() {
		return getStatus() == DEVICE_STATUS.IO_ERROR;
	}
	
	public boolean isInitialized() {
		return getStatus() == DEVICE_STATUS.INITIALIZED;
	}
	
	public boolean isReady() {
		return (getStatus() == DEVICE_STATUS.INITIALIZED && getType() == DEVICE_TYPE.DATA);
	}
	
	public DEVICE_STATUS getStatus() {
		return status;
	}

	public String getStatusStr() {
		if (getStatus() == null) {
			// Return as Unknown
			return DEVICE_STATUS_STR[DEVICE_STATUS.UNKNOWN.ordinal()]; 
		}

		if(isReady()) {
			return "Available";
		}
		return DEVICE_STATUS_STR[getStatus().ordinal()];
	}

	public void setStatus(DEVICE_STATUS status) {
		this.status = status;
	}

	public Double getSpaceInUse() {
		return spaceInUse;
	}

	public void setSpaceInUse(Double spaceInUse) {
		this.spaceInUse = spaceInUse;
	}

	@XmlTransient
	public String getServerName() {
		return serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public void setMountPoint(String mountPoint) {
		this.mountPoint = mountPoint;
	}

	public String getMountPoint() {
		return mountPoint;
	}
	
	public DEVICE_TYPE getType() {
		return type;
	}
	
	public String getTypeStr() {
		return DEVICE_TYPE_STR[type.ordinal()];
	}

	public void setType(DEVICE_TYPE diskType) {
		this.type = diskType;
	}
	
	public String getFsType() {
		return fsType;
	}

	public void setFsType(String fsType) {
		this.fsType = fsType;
	}

	public String getFsVersion() {
		return fsVersion;
	}

	public void setFsVersion(String fsVersion) {
		this.fsVersion = fsVersion;
	}
	
	@Override
	public boolean filter(String filterString, boolean caseSensitive) {
		return StringUtil.filterString(getServerName() + getName() + getStatusStr() + getSpace() + getFreeSpace()
				+ getType(), filterString, caseSensitive);
	}
	
	public String getQualifiedName() {
		return getServerName() + ":" + getName();
	}
	
	public String getQualifiedBrickName(String volumeName) {
		return getServerName() + ":" + getMountPoint() + "/" + volumeName;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}
		
		if(!(obj instanceof Device)) {
			return false;
		}
		
		Device device = (Device)obj;
		
		String oldMountPoint = (getMountPoint() == null ? "" : getMountPoint());
		String oldFsType = (getFsType() == null ? "" : getFsType());
		String oldFsVersion = (getFsVersion() == null ? "" : getFsVersion()); 
		
		String newMountPoint = (device.getMountPoint() == null ? "" : getMountPoint());
		String newFsType = (device.getFsType() == null ? "" : getFsType());
		String newFsVersion = (device.getFsVersion() == null ? "" : getFsVersion()); 

		if (getName().equals(device.getName()) && getServerName().equals(device.getServerName())
				&& oldMountPoint.equals(newMountPoint) && getStatus() == device.getStatus()
				&& getSpace().equals(device.getSpace()) && getSpaceInUse().equals(device.getSpaceInUse())
				&& oldFsType.equals(newFsType) && oldFsVersion.equals(newFsVersion)
				&& getType() == device.getType()) {
			return true;
		}
		
		return false;
	}

	public void copyFrom(Device newDevice) {
		setName(newDevice.getName());
		setParent(newDevice.getParent());
		setMountPoint(newDevice.getMountPoint());
		setServerName(newDevice.getServerName());
		setStatus(newDevice.getStatus());
		setFsType(newDevice.getFsType());
		setType(newDevice.getType());
		setFsVersion(newDevice.getFsVersion());
		setSpace(newDevice.getSpace());
		setSpaceInUse(newDevice.getSpaceInUse());
	}
}