summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.console/src/org/gluster/storage/management/console/dialogs/CreateVolumePage1.java
blob: 022ae6dcb573e94f2067dd766eed9944ba04623d (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*******************************************************************************
 * 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.console.dialogs;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.gluster.storage.management.console.GlusterDataModelManager;
import org.gluster.storage.management.core.model.Brick;
import org.gluster.storage.management.core.model.Device;
import org.gluster.storage.management.core.model.Volume;
import org.gluster.storage.management.core.model.Brick.BRICK_STATUS;
import org.gluster.storage.management.core.model.Volume.NAS_PROTOCOL;
import org.gluster.storage.management.core.model.Volume.TRANSPORT_TYPE;
import org.gluster.storage.management.core.model.Volume.VOLUME_TYPE;
import org.gluster.storage.management.core.utils.ValidationUtil;


public class CreateVolumePage1 extends WizardPage {
	public static final String PAGE_NAME = "create.volume.page.1";
	private Text txtName;
	private ComboViewer typeComboViewer;
	private Text txtAccessControl;
	private Text txtCifsUsers;
	private Volume volume = new Volume();
	private Button btnNfs;
	private Button btnCIFS;
	private Button btnStartVolume;
	private Link linkCustomize;
	private List<Device> allDevices;
	private List<Device> selectedDevices;

	/**
	 * Create the wizard.
	 */
	public CreateVolumePage1() {
		super(PAGE_NAME);
		setTitle("Create Volume");
		setDescription("Create a new Volume by choosing bricks from the cluster servers and configuring the volume properties.");
		
		// by default, we create volume with all available disks
		allDevices = GlusterDataModelManager.getInstance().getReadyDevicesOfAllServers();
		selectedDevices = allDevices; // volume.setDisks(allDisks);
	}
	
	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	public void createControl(Composite parent) {
		setPageComplete(false);
		Composite container = createContainer(parent);
		
		createEmptyRow(container);
		
		createNameLabel(container);
		createNameText(container);		
		
		createTypeLabel(container);
		createTypeCombo(container);
		
//		createTransportTypeLabel(container);
//		createTransportTypeValueLabel(container);
		
		createDisksLabel(container);
		createDisksCustomizeLink(container);
		
		createNasProtocolLabel(container);
		createNasProtocolCheckboxes(container);
		
		createCifsUserLabel(container);
		createCifsUserText(container);
		
		createEmptyLabel(container);
		createCifsUserInfoLabel(container);
		
		createAccessControlLabel(container);
		createAccessControlText(container);

		createEmptyLabel(container);
		createAccessControlInfoLabel(container);
		
		createStartVolumeLabel(container);
		createStartVolumeCheckbox(container);
	}

	private void createStartVolumeCheckbox(Composite container) {
		btnStartVolume = new Button(container, SWT.CHECK);
		btnStartVolume.setSelection(true);
	}

	private void createStartVolumeLabel(Composite container) {
		Label lblStartVolume = new Label(container, SWT.NONE);
		lblStartVolume.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblStartVolume.setText("Start Volume: ");
	}
	
	private void createCifsUserInfoLabel(Composite container) {
		Label lblCifsUserInfo = new Label(container, SWT.TOP);
		lblCifsUserInfo.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		lblCifsUserInfo.setText("(Comma separated list user names)");
	}

	private void createAccessControlInfoLabel(Composite container) {
		Label lblAccessControlInfo = new Label(container, SWT.TOP);
		lblAccessControlInfo.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		lblAccessControlInfo.setText("(Comma separated list of IP addresses/hostnames)");
	}

	private void createEmptyLabel(Composite container) {
		new Label(container, SWT.NONE);
	}

	private void createAccessControlText(Composite container) {
		txtAccessControl = new Text(container, SWT.BORDER);
		txtAccessControl.setText("*");
		GridData accessControlData = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		accessControlData.widthHint = 300;
		txtAccessControl.setLayoutData(accessControlData);		
		txtAccessControl.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				validateForm();
			}
		});
	}

	private void createAccessControlLabel(Composite container) {
		Label lblAccessControl = new Label(container, SWT.NONE);
		lblAccessControl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblAccessControl.setText("Allow Access From: ");
	}
	
	private void createCifsUserLabel(Composite container) {
		Label lblAccessControl = new Label(container, SWT.NONE);
		lblAccessControl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblAccessControl.setText("CIFS Users: ");
	}
	
	private void createCifsUserText(Composite container) {
		txtCifsUsers = new Text(container, SWT.BORDER);
//		txtCifsUsers.setText("testuser1,testuser2,testuser3");
		GridData cifsControlData = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		cifsControlData.widthHint = 300;
		txtCifsUsers.setLayoutData(cifsControlData);	
		txtCifsUsers.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				validateForm();				
			}
		});
	}

	private void createNasProtocolCheckboxes(Composite container) {
		Button btnGluster = new Button(container, SWT.CHECK);
		btnGluster.setEnabled(false);
		btnGluster.setSelection(true);
		btnGluster.setText("Gluster");
		createEmptyLabel(container);
		
		btnNfs = new Button(container, SWT.CHECK);
		btnNfs.setEnabled(true);
		btnNfs.setSelection(true);
		btnNfs.setText("NFS");
		createEmptyLabel(container);
		
		btnCIFS = new Button(container, SWT.CHECK);
		btnCIFS.setEnabled(true);
		btnCIFS.setSelection(false);
		btnCIFS.setText("CIFS");
		btnCIFS.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				validateForm();
			}
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				validateForm();				
			}
		});
	}

	private void createNasProtocolLabel(Composite container) {
		Label lblNasProtocol = new Label(container, SWT.RIGHT);
		lblNasProtocol.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNasProtocol.setText("Access Protocol: ");
	}

	private void createDisksCustomizeLink(Composite container) {
		linkCustomize = new Link(container, SWT.UNDERLINE_LINK);
		linkCustomize.setText("All Brick(s) (<a>customize</a>)" );
		linkCustomize.setEnabled(false);
		linkCustomize.addListener (SWT.Selection, new Listener () {
			public void handleEvent(Event event) {
				Display.getDefault().asyncExec(new Runnable() {
					
					@Override
					public void run() {
						SelectDisksDialog dialog = new SelectDisksDialog(getShell(), allDevices, selectedDevices, txtName.getText().trim());

						dialog.create();
				        if(dialog.open() == Window.OK) {
				        	// user has customized disks. get them from the dialog box.
				        	selectedDevices = dialog.getSelectedDevices();
				        	linkCustomize.setText("" + selectedDevices.size() + " Brick(s) (<a>customize</a>)");
				        	validateForm();
				        }
					}
				});
			}
		});
	}

	private void createDisksLabel(Composite container) {
		Label lblDisks = new Label(container, SWT.RIGHT);
		lblDisks.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblDisks.setText("Bricks: ");
	}

	private void createTypeCombo(Composite container) {
		typeComboViewer = new ComboViewer(container, SWT.READ_ONLY);
		Combo typeCombo = typeComboViewer.getCombo();
		GridData typeComboData = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		typeCombo.setLayoutData(typeComboData);
		typeComboViewer.setContentProvider(new ArrayContentProvider());
		
		VOLUME_TYPE[] volumeTypes = new VOLUME_TYPE[3];
		volumeTypes[0] = VOLUME_TYPE.DISTRIBUTE;
		volumeTypes[1] = VOLUME_TYPE.REPLICATE;
		volumeTypes[2] = VOLUME_TYPE.STRIPE;
		
		typeComboViewer.setInput(volumeTypes);
		typeCombo.select(0); // default type = Plain Distribute
		typeComboViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				return Volume.getVolumeTypeStr((VOLUME_TYPE)element);
			}
		});
		typeComboViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				validateForm();
			}
		});
	}
	
	private void createTypeLabel(Composite container) {
		Label lblType = new Label(container, SWT.NONE);
		lblType.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblType.setText("Type: ");
	}

	private void createNameText(Composite container) {
		txtName = new Text(container, SWT.BORDER);
		GridData txtNameData = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1);
		txtNameData.widthHint = 300;
		txtName.setTextLimit(32);
		txtName.setLayoutData(txtNameData);
		txtName.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				validateForm();
			}
		});
	}

	private void createNameLabel(Composite container) {
		Label lblName = new Label(container, SWT.NONE);
		lblName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblName.setText("Name: ");
	}

	private void createEmptyRow(Composite container) {
		createEmptyLabel(container);
		createEmptyLabel(container);
	}

	private Composite createContainer(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		setControl(container);

		GridLayout gl_container = new GridLayout(2, false);
		gl_container.verticalSpacing = 10;
		gl_container.marginHeight = 10;
		gl_container.marginLeft = 20;
		gl_container.horizontalSpacing = 10;
		container.setLayout(gl_container);
		return container;
	}

	public Volume getVolume() {
		volume.setName(txtName.getText());
		
		IStructuredSelection selection = (IStructuredSelection)typeComboViewer.getSelection();
		volume.setVolumeType((VOLUME_TYPE)selection.getFirstElement());
		volume.setReplicaCount(Volume.DEFAULT_REPLICA_COUNT);
		volume.setStripeCount(Volume.DEFAULT_STRIPE_COUNT);

		volume.setTransportType(TRANSPORT_TYPE.ETHERNET); // Support only for Ethernet
		Set<NAS_PROTOCOL> nasProtocols = new HashSet<Volume.NAS_PROTOCOL>();
		nasProtocols.add(NAS_PROTOCOL.GLUSTERFS);
		nasProtocols.add(NAS_PROTOCOL.NFS);
		
		volume.setAccessControlList(txtAccessControl.getText());

		if (btnNfs.getSelection()) {
			volume.enableNFS();
		} else {
			volume.disableNFS();
		}
		
		if (btnCIFS.getSelection()) {
			volume.enableCifs();
			volume.setCifsUsers(Arrays.asList(txtCifsUsers.getText().split(",")));
		} else {
			volume.disableCifs();
		}
		
		addVolumeBricks();
		return volume;
	}

	private void addVolumeBricks() {
		// first clear existing bricks, if any
		volume.getBricks().clear();
		
		for (Device device : selectedDevices) {
			Brick brick = new Brick(device.getServerName(), BRICK_STATUS.ONLINE, device.getMountPoint() + "/"
					+ volume.getName());
			volume.addBrick(brick);
		}
	}
	
	public Boolean startVolumeAfterCreation() {
		return btnStartVolume.getSelection();
	}
	
	public Boolean volumeExists(String volumeName) {
		List<Volume> volumes = GlusterDataModelManager.getInstance().getModel().getCluster().getVolumes();
		for (Volume volume : volumes) {
			if (volume.getName().equals(volumeName)) {
				setErrorMessage("Volume name already exists.");
				return false;
			}
		}
		return true;
	}
	
	private void validateForm() {
		clearErrors();
		validateVolumeName();
		validateCifsUsers();
		validateAccessControl();
		validateDisks();
	}

	private void validateDisks() {
		int diskCount = selectedDevices.size();
		
		if(diskCount  < 1) {
			setError("At least one brick must be selected!");
		}

		VOLUME_TYPE volumeType = (VOLUME_TYPE) ((IStructuredSelection) typeComboViewer
				.getSelection()).getFirstElement();
		if ((volumeType == VOLUME_TYPE.DISTRIBUTED_REPLICATE || volumeType == VOLUME_TYPE.REPLICATE ) && diskCount % 2 != 0) {
			setError("Mirror type volume requires bricks in multiples of two");
		} else if ((volumeType == VOLUME_TYPE.DISTRIBUTED_STRIPE || volumeType == VOLUME_TYPE.STRIPE) && diskCount % 4 != 0) {
			setError("Stripe type volume requires bricks in multiples of four");
		}
	}
	
	private void validateAccessControl() {
		String accessControl = txtAccessControl.getText().trim();
		if (accessControl.length() == 0) {
			setError("Please enter Access Control");
			return;
		}
		
		if (!ValidationUtil.isValidAccessControl(accessControl)) {
			setError("Invalid IP address/Host name [" + ValidationUtil.getInvalidIpOrHostname(accessControl)
					+ "]. Please enter a valid value!");
		}
	}
	
	
	private void validateCifsUsers() {
		if (btnCIFS.getSelection()) {
			String cifsUserList = txtCifsUsers.getText().trim();
			if (cifsUserList.length() == 0) {
				setError("Please enter cifs user name");
				return;
			}
		}
	}

	private void validateVolumeName() {
		String volumeName = txtName.getText().trim();
		String volumeNameToken = "^[a-zA-Z][a-zA-Z0-9\\-]*";
		
		if (volumeName.length() > 0) {
			linkCustomize.setEnabled(true);
		}
		
		if(volumeName.length() == 0) {
			setError("Please enter Volume Name");
			linkCustomize.setEnabled(false);
		}
		
		if (!volumeName.matches(volumeNameToken)) {
			setError("Please enter valid Volume Name");
		}
		
		if(!volumeExists(volumeName)) {
			setError("Volume [" + volumeName + "] already exists!"); 
		}
	}

	private void clearErrors() {
		setErrorMessage(null);
		setPageComplete(true);
	}
	
	private void setError(String errorMsg) {
		setPageComplete(false);
		setErrorMessage(errorMsg);
	}
}