summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.console/src/org/gluster/storage/management/console/dialogs/AddBrickPage.java
blob: 9c87a58063566b06b29fb9a089eabcba45df4602 (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
/*******************************************************************************
 * 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.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
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.Volume.VOLUME_TYPE;

import com.richclientgui.toolbox.duallists.DualListComposite.ListContentChangedListener;
import com.richclientgui.toolbox.duallists.IRemovableContentProvider;

/**
 * @author root
 * 
 */
public class AddBrickPage extends WizardPage {
	private List<Device> availableDevices = new ArrayList<Device>();
	private Volume volume = null;
	private BricksSelectionPage page = null;


	public static final String PAGE_NAME = "add.disk.volume.page";

	/**
	 * @param pageName
	 */
	protected AddBrickPage(Volume volume) {
		super(PAGE_NAME);
		this.volume = volume;
		setTitle("Add Brick");

		String description = "Add bricks to [" + volume.getName() + "] ";
		if ( volume.getVolumeType() == VOLUME_TYPE.DISTRIBUTED_REPLICATE) {
			description += "(in multiples of " + volume.getReplicaCount() + ")";
		} else if (volume.getVolumeType() == VOLUME_TYPE.DISTRIBUTED_STRIPE) {
			description += "(in multiples of " + volume.getStripeCount() + ")";
		}
		setDescription(description);

		availableDevices = getAvailableDevices(volume);
		
		setPageComplete(false);
		setErrorMessage("Please select bricks to be added to the volume [" + volume.getName()  +"]");
	}

	
	private boolean  isDeviceUsed(Volume volume, Device device){
		for (Brick volumeBrick : volume.getBricks()) {
			if ( device.getQualifiedBrickName(volume.getName()).equals(volumeBrick.getQualifiedName())) {
				return true;
			}
		}
		return false;
	}
	
	protected List<Device> getAvailableDevices(Volume volume) {
		List<Device> availableDevices = new ArrayList<Device>();
		for (Device device : GlusterDataModelManager.getInstance().getReadyDevicesOfAllServers()) {
			if ( ! isDeviceUsed(volume, device) ) {
				availableDevices.add(device);
			}
		}
		return availableDevices;
	}

	public Set<Device> getChosenDevices() {
		return new HashSet<Device>(page.getChosenDevices());
	}
	
	public Set<Brick> getChosenBricks( String volumeName ) {
		return page.getChosenBricks(volumeName);
	}
	
	private boolean isValidDiskSelection(int diskCount) {
		if ( diskCount == 0) {
			return false;
		}
		switch (volume.getVolumeType()) {
		case DISTRIBUTED_REPLICATE:
			return (diskCount % volume.getReplicaCount() == 0);
		case DISTRIBUTED_STRIPE:
			return (diskCount % volume.getStripeCount() == 0);
		}
		return true;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(Composite parent) {
		getShell().setText("Add Brick");
		List<Device> chosenDevices = new ArrayList<Device>(); // or volume.getDisks();
		
		page = new BricksSelectionPage(parent, SWT.NONE, availableDevices, chosenDevices, volume.getName());
		page.addDiskSelectionListener(new ListContentChangedListener<Device>() {
			@Override
			public void listContentChanged(IRemovableContentProvider<Device> contentProvider) {
				List<Device> newChosenDevices = page.getChosenDevices();
				
				// validate chosen disks
				if(isValidDiskSelection(newChosenDevices.size())) {
					clearError();
				} else {
					setError();
				}
			}
		});
		setControl(page);
	}

	private void setError() {
		String errorMessage = null;
		if ( volume.getVolumeType() == VOLUME_TYPE.DISTRIBUTE) {
			errorMessage = "Please select at least one brick!";
		} else if( volume.getVolumeType() == VOLUME_TYPE.DISTRIBUTED_REPLICATE) {
			errorMessage = "Please select bricks in multiples of " + volume.getReplicaCount();
		} else {
			errorMessage = "Please select bricks in multiples of " + volume.getStripeCount();
		}

		setPageComplete(false);
		setErrorMessage(errorMessage);
	}

	private void clearError() {
		setErrorMessage(null);
		setPageComplete(true);
	}

	public BricksSelectionPage getDialogPage() {
		return this.page;
	}
	
	public void setPageComplete() {
		
	}

}