summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/model/Disk.java
blob: fee589eb81153347885828dd94d08ecf800f0996 (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
/*******************************************************************************
 * 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 java.util.ArrayList;
import java.util.Collection;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

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


@XmlRootElement(name="disk")
public class Disk extends Device {
	private String description;
	
	// interface = pci, raid0, raid3, etc
	private String diskInterface;
	
	private Collection<Partition> partitions = new ArrayList<Partition>();
	
	// In case of a software raid, the disk will contain an array of other disks
	private Collection<Disk> raidDisks;

	public Disk() {
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	@XmlElement(name="interface")
	public String getDiskInterface() {
		return diskInterface;
	}

	public void setDiskInterface(String diskInterface) {
		this.diskInterface = diskInterface;
	}

	@XmlElementWrapper(name="raidDisks")
	@XmlElement(name="disk", type=Disk.class)
	public Collection<Disk> getRaidDisks() {
		return raidDisks;
	}

	public void setRaidDisks(Collection<Disk> raidDisks) {
		this.raidDisks = raidDisks;
	}

	public void setPartitions(Collection<Partition> partitions) {
		this.partitions = partitions;
	}

	@XmlElementWrapper(name="partitions")
	@XmlElement(name="partition", type=Partition.class)
	public Collection<Partition> getPartitions() {
		return partitions;
	}
	
	public boolean hasPartitions() {
		return (partitions != null && partitions.size() > 0);
	}

	public Disk(Server server, String name, String mountPoint, Double space, Double spaceInUse, DEVICE_STATUS status) {
		super(server, name, mountPoint, space, spaceInUse, status);
	}

	@Override
	public boolean filter(String filterString, boolean caseSensitive) {
		if (StringUtil.filterString(getServerName() + getName() + getStatusStr() + getSpace() + getFreeSpace()
				+ getType() + getDescription(), filterString, caseSensitive)) {
			return true;
		}
		
		// disk doesn't match. check if any of the partitions of this disk match the filter
		for(Partition partition : getPartitions()) {
			if(partition.filter(filterString, caseSensitive)) {
				return true;
			}
		}
		
		return false;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj) {
			return true;
		}
		
		if(!(obj instanceof Disk)) {
			return false;
		}
		
		Disk disk = (Disk)obj;
		
		if (!(super.equals(obj) && getDescription().equals(disk.getDescription()) && (getDiskInterface() == disk.getDiskInterface() || getDiskInterface().equals(
				disk.getDiskInterface()) ))) {
			return false;
		}
		
		if (raidDisks != null) {
			for (Disk raidDisk : raidDisks) {
				// check if the disk contains same raid disks
				if (!(raidDisk.equals(GlusterCoreUtil.getEntity(disk.getRaidDisks(), raidDisk.getName(), false)))) {
					return false;
				}
			}
		}
		
//		// check if the disk contains same partitions
//		if (partitions != null) {
//			for (Partition partition : partitions) {
//				if (!(partition.equals(GlusterCoreUtil.getEntity(disk.getPartitions(), partition.getName(), false)))) {
//					return false;
//				}
//			}
//		}
		return true;
	}

	public void copyFrom(Disk newDisk) {
		super.copyFrom(newDisk);
		setDescription(newDisk.getDescription());
		setDiskInterface(newDisk.getDiskInterface());
		setPartitions(newDisk.getPartitions());
		setRaidDisks(newDisk.getRaidDisks());
	}
	
	@Override
	public boolean isReady() {
		if (hasPartitions()) {
			for (Partition partition : getPartitions()) {
				if (partition.isReady()) {
					return true;
				}
			}
			return false;
		} else {
			return super.isReady();
		}
	}

	@Override
	public Double getSpace() {
		Double space = 0d;
		if (hasPartitions()) {
			for (Partition partition : getPartitions()) {
				space += partition.getSpace();
			}
			return space;
		} else {
			return super.getSpace();
		}
	}

	@Override
	public Double getSpaceInUse() {
		Double spaceInUse = 0d;
		if (hasPartitions()) {
			for (Partition partition : getPartitions()) {
				if (partition.isInitialized()) {
					spaceInUse += partition.getSpaceInUse();
				}
			}
			return spaceInUse;
		} else {
			return super.getSpaceInUse();
		}
	}
}