summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.core/src/org/gluster/storage/management/core/model/Volume.java
blob: 41e9d291f4806d3b7922da02a38e3c94adeeb27f (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*******************************************************************************
 * Copyright (c) 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 Affero 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 Affero General Public License
 * for more details.
 *  
 * You should have received a copy of the GNU Affero 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.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

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

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


@XmlRootElement
public class Volume extends Entity {
	public enum VOLUME_STATUS {
		ONLINE, OFFLINE
	};

	public enum VOLUME_TYPE {
		DISTRIBUTE, REPLICATE, DISTRIBUTED_REPLICATE, STRIPE, DISTRIBUTED_STRIPE
	};
	
	public enum TRANSPORT_TYPE {
		ETHERNET, INFINIBAND
	};

	public enum NAS_PROTOCOL {
		GLUSTERFS, NFS, CIFS
	};

	
	public static final int DEFAULT_REPLICA_COUNT = 2;
	public static final int DEFAULT_STRIPE_COUNT = 4;

	public static final String OPTION_AUTH_ALLOW = "auth.allow";
	public static final String OPTION_NFS_DISABLE = "nfs.disable";

	private static final String[] VOLUME_TYPE_STR = new String[] { "Distribute", "Replicate", "Distributed Replicate",
			"Stripe", "Distributed Stripe" };

	private static final String[] TRANSPORT_TYPE_STR = new String[] { "Ethernet", "Infiniband" };
	private static final String[] STATUS_STR = new String[] { "Online", "Offline" };
	private static final String[] NAS_PROTOCOL_STR = new String[] { "Gluster", "NFS", "CIFS" };

	private VOLUME_TYPE volumeType;
	private TRANSPORT_TYPE transportType;
	private VOLUME_STATUS status;
	private int replicaCount;
	private int stripeCount;
	private VolumeOptions options = new VolumeOptions();
	private List<Brick> bricks = new ArrayList<Brick>();
	private List<String> cifsUsers;

	public Volume() {
	}

	// Only GlusterFS is enabled
	private Set<NAS_PROTOCOL> nasProtocols = new LinkedHashSet<NAS_PROTOCOL>(Arrays.asList(new NAS_PROTOCOL[] {
			NAS_PROTOCOL.GLUSTERFS }));

	public String getVolumeTypeStr() {
		return getVolumeTypeStr(getVolumeType());
	}
	
	public static String getVolumeTypeStr(VOLUME_TYPE volumeType) {
		return VOLUME_TYPE_STR[volumeType.ordinal()];
	}
	
	public static VOLUME_TYPE getVolumeTypeByStr(String volumeTypeStr) {
		return VOLUME_TYPE.valueOf(volumeTypeStr);
	}
	
	public static TRANSPORT_TYPE getTransportTypeByStr(String transportTypeStr) {
		return TRANSPORT_TYPE.valueOf(transportTypeStr);
	}

	public String getTransportTypeStr() {
		return TRANSPORT_TYPE_STR[getTransportType().ordinal()];
	}

	public String getStatusStr() {
		return STATUS_STR[getStatus().ordinal()];
	}

	public int getNumOfBricks() {
		return bricks.size();
	}

	public VOLUME_TYPE getVolumeType() {
		return volumeType;
	}

	public void setVolumeType(VOLUME_TYPE volumeType) {
		this.volumeType = volumeType;
		// TODO find a way to get the replica / strip count
		if (volumeType == VOLUME_TYPE.DISTRIBUTED_STRIPE) {
			setReplicaCount(0);
			setStripeCount(DEFAULT_STRIPE_COUNT);
		} else if (volumeType == VOLUME_TYPE.DISTRIBUTED_REPLICATE) {
			setReplicaCount(DEFAULT_REPLICA_COUNT);
			setStripeCount(0);
		} else {
			setReplicaCount(0);
			setStripeCount(0);
		}
	}

	public TRANSPORT_TYPE getTransportType() {
		return transportType;
	}

	public void setTransportType(TRANSPORT_TYPE transportType) {
		this.transportType = transportType;
	}

	public VOLUME_STATUS getStatus() {
		return status;
	}

	public int getReplicaCount() {
		return replicaCount;
	}

	public void setReplicaCount(int replicaCount) {
		this.replicaCount = replicaCount;
	}

	public int getStripeCount() {
		return stripeCount;
	}

	public void setStripeCount(int stripeCount) {
		this.stripeCount = stripeCount;
	}

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

	@XmlElementWrapper(name = "nasProtocols")
	@XmlElement(name = "nasProtocol", type=NAS_PROTOCOL.class)
	public Set<NAS_PROTOCOL> getNASProtocols() {
		return nasProtocols;
	}

	public void setNASProtocols(Set<NAS_PROTOCOL> nasProtocols) {
		this.nasProtocols = nasProtocols;
	}

	public String getNASProtocolsStr() {
		String protocolsStr = "";
		for (NAS_PROTOCOL protocol : nasProtocols) {
			String protocolStr = NAS_PROTOCOL_STR[protocol.ordinal()];
			protocolsStr += (protocolsStr.isEmpty() ? protocolStr : ", " + protocolStr);
		}
		return protocolsStr;
	}

	@XmlTransient
	public String getAccessControlList() {
		return options.get(OPTION_AUTH_ALLOW);
	}

	public void setAccessControlList(String accessControlList) {
		setOption(OPTION_AUTH_ALLOW, accessControlList);
	}
	
	@XmlTransient
	public boolean isNfsEnabled() {
		String nfsDisabled = options.get(OPTION_NFS_DISABLE);
		if(nfsDisabled == null || nfsDisabled.equalsIgnoreCase(GlusterConstants.OFF)) {
			return true;
		} else {
			return false;
		}
	}

	@XmlElement(name="options")
	public VolumeOptions getOptions() {
		return options;
	}

	public void setOption(String key, String value) {
		options.put(key, value);
	}

	public void setOptions(VolumeOptions options) {
		this.options = options;
	}
	
	public void setOptions(LinkedHashMap<String, String> options) {
		List<VolumeOption> volumeOptions = new ArrayList<VolumeOption>();
		for(Entry<String, String> entry : options.entrySet()) {
			volumeOptions.add(new VolumeOption(entry.getKey(), entry.getValue()));
		}
		this.options.setOptions(volumeOptions);
	}

	public void addBrick(Brick brick) {
		bricks.add(brick);
	}
	
	public void addBricks(Collection<Brick> bricks) {
		this.bricks.addAll(bricks);
	}
	
	
	public void setBricks(List<Brick> bricks) {
		this.bricks = bricks;
	}
	
	public void removeBrick(Brick brick) {
		bricks.remove(brick);
	}

	@XmlElementWrapper(name = "bricks")
	@XmlElement(name = "brick", type=Brick.class)
	public List<Brick> getBricks() {
		return bricks;
	}

	public void enableNFS() {
		nasProtocols.add(NAS_PROTOCOL.NFS);
		setOption(OPTION_NFS_DISABLE, GlusterConstants.OFF);
	}

	public void disableNFS() {
		nasProtocols.remove(NAS_PROTOCOL.NFS);
		setOption(OPTION_NFS_DISABLE, GlusterConstants.ON);
	}
	
	public void enableCifs() {
		if (!nasProtocols.contains(NAS_PROTOCOL.CIFS)) {
			nasProtocols.add(NAS_PROTOCOL.CIFS);
		}
	}
	
	public void disableCifs() {
		nasProtocols.remove(NAS_PROTOCOL.CIFS);
	}
	
	public boolean isCifsEnable() {
		return nasProtocols.contains(NAS_PROTOCOL.CIFS);
	}
	
	public void setCifsUsers(List<String> cifsUsers) {
		this.cifsUsers = cifsUsers;
	}

	public List<String> getCifsUsers() {
		return cifsUsers;
	}


	public Volume(String name, Entity parent, VOLUME_TYPE volumeType, TRANSPORT_TYPE transportType, VOLUME_STATUS status) {
		super(name, parent);
		setVolumeType(volumeType);
		setTransportType(transportType);
		setStatus(status);
	}

	/**
	 * Filter matches if any of the properties name, volume type, transport type, status and number of disks contains
	 * the filter string
	 */
	@Override
	public boolean filter(String filterString, boolean caseSensitive) {
		return StringUtil.filterString(getName() + getVolumeTypeStr() + getTransportTypeStr() + getStatusStr()
				+ getNumOfBricks(), filterString, caseSensitive);
	}
	
	public List<String> getBrickDirectories() {
		List<String> brickDirectories = new ArrayList<String>();
		for(Brick brick : getBricks()) {
			brickDirectories.add(brick.getQualifiedName());
		}
		return brickDirectories;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof Volume)) {
			return false;
		}
		
		Volume volume = (Volume)obj;

		if (!(getName().equals(volume.getName()) && getVolumeType() == volume.getVolumeType()
				&& getTransportType() == volume.getTransportType() && getStatus() == volume.getStatus()
				&& getReplicaCount() == volume.getReplicaCount() && getStripeCount() == volume.getStripeCount()
				&& getOptions().equals(volume.getOptions()))) {
			return false;
		}

		for(NAS_PROTOCOL nasProtocol : getNASProtocols()) {
			if(!(volume.getNASProtocols().contains(nasProtocol))) {
				return false;
			}
		}
		
		List<Brick> oldBricks = getBricks();
		List<Brick> newBricks = volume.getBricks();
		if(oldBricks.size() != newBricks.size()) {
			return false;
		}
		
		if(!GlusterCoreUtil.getAddedEntities(oldBricks, newBricks, false).isEmpty()) {
			return false;
		}

		if(!GlusterCoreUtil.getAddedEntities(newBricks, oldBricks, false).isEmpty()) {
			return false;
		}

		Map<Brick, Brick> modifiedBricks = GlusterCoreUtil.getModifiedEntities(oldBricks, newBricks);
		if(modifiedBricks.size() > 0) {
			return false;
		}
		
		return true;
	}

	/**
	 * Note: this method doesn't copy the bricks. Clients should write separate code to identify added/removed/modified
	 * bricks and update the volume bricks appropriately.
	 * 
	 * @param newVolume
	 */
	public void copyFrom(Volume newVolume) {
		setName(newVolume.getName());
		setVolumeType(newVolume.getVolumeType());
		setTransportType(newVolume.getTransportType());
		setStatus(newVolume.getStatus());
		setReplicaCount(newVolume.getReplicaCount());
		setStripeCount(newVolume.getStripeCount());
		setNASProtocols(newVolume.getNASProtocols());
		setCifsUsers(newVolume.getCifsUsers());
		getOptions().copyFrom(newVolume.getOptions());
	}
}