summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageWizard.java
blob: 33cdea748c811686d21043173c3879fb79551354 (plain) (blame)
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
package org.openslx.dozmod.gui.wizard;

import java.awt.Window;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.bwlp.thrift.iface.ImageBaseWrite;
import org.openslx.bwlp.thrift.iface.ImageVersionWrite;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.wizard.page.ImageCustomPermissionPage;
import org.openslx.dozmod.gui.wizard.page.ImageMetaDataPage;
import org.openslx.dozmod.gui.wizard.page.ImageUploadPage;
import org.openslx.dozmod.state.UploadWizardState;
import org.openslx.dozmod.thrift.Session;
import org.openslx.thrifthelper.ThriftManager;

@SuppressWarnings("serial")
public class ImageWizard extends Wizard {

	private final static Logger LOGGER = Logger.getLogger(ImageWizard.class);

	private final UploadWizardState uploadWizardState = new UploadWizardState();;
	protected ImageUploadPage imageUploadPage;
	protected ImageMetaDataPage imageMetaDataPage;
	protected ImageCustomPermissionPage imageCustomPermissionPage;

	/**
	 * Wizard for creating or editing an image
	 * 
	 * @param editExistingImage whether to create new or edit existing image
	 */
	public ImageWizard(Window parent) {
		super(parent);
		imageUploadPage = new ImageUploadPage(this, uploadWizardState);
		imageMetaDataPage = new ImageMetaDataPage(this, uploadWizardState);
		imageCustomPermissionPage = new ImageCustomPermissionPage(this, uploadWizardState);
		addPage(imageUploadPage);
		addPage(imageMetaDataPage);
		addPage(imageCustomPermissionPage);
	}

	@Override
	public String getWindowTitle() {
		return "Neues Image erzeugen";
	}

	@Override
	public boolean performFinish() {
		// TODO evaluate table state and create the map of permissions
		// 
		// since we only started the download and created a "blank" image entry
		// we can here do all the sanity checks on the fields of UploadWizardState
		// and react accordingly.
		// check state
		if (!isStateValid()) {
			LOGGER.error("Invalid state!");
			return false;
		} else {
			// TODO run the actually request over external threaded class
			try {
				// push to sat
				ThriftManager.getSatClient().updateImageBase(Session.getSatelliteToken(),
						uploadWizardState.uuid.toString(), imageBaseWriteFromState());
			} catch (TException e) {
				LOGGER.error("Fail to push image metadata to satellite: ", e);
				return false;
			}
		}

		// push permissions to satellite server
		if (uploadWizardState.permissionList != null) {
			// user skipped permission list stuff
			try {
				ThriftManager.getSatClient().writeImagePermissions(Session.getSatelliteToken(),
						uploadWizardState.uuid, uploadWizardState.permissionList);
			} catch (TException e) {
				// TODO Auto-generated catch block
				LOGGER.error("Could not write permissions list to satellite: ", e);
				return false;
			}
		}
		try {
			// push to sat
			ThriftManager.getSatClient().updateImageVersion(Session.getSatelliteToken(),
					uploadWizardState.transferInformation.getToken(),
					new ImageVersionWrite(uploadWizardState.isEnabled, uploadWizardState.isRestricted));
		} catch (TException e) {
			LOGGER.error("Could not set active/restricted flags to satellite: ", e);
			return false;
		}

		Gui.showMessageBox(this, "Creation of image worked :)", MessageType.INFO, LOGGER, null);
		return true;
	}

	private boolean isStateValid() {
		// debug purposes
		if (uploadWizardState.name == null || uploadWizardState.name.isEmpty()) {
			LOGGER.error("No name set in state!");
			return false;
		}
		if (uploadWizardState.description == null || uploadWizardState.description.isEmpty()) {
			LOGGER.error("No description set in state!");
			return false;
		}
		if (uploadWizardState.descriptionFile == null) {
			LOGGER.error("No description file set in state!");
			return false;
		} else {
			if (!uploadWizardState.descriptionFile.canRead()) {
				LOGGER.error(uploadWizardState.descriptionFile.getAbsolutePath() + " cannot be read!");
				return false;
			}
		}
		if (uploadWizardState.diskFile == null) {
			LOGGER.error("No disk file set in state!");
			return false;
		} else {
			if (!uploadWizardState.diskFile.canRead()) {
				LOGGER.error(uploadWizardState.diskFile.getAbsolutePath() + " cannot be read!");
				return false;
			}
		}
		if (uploadWizardState.selectedOs == null) {
			LOGGER.error("No OS set in state!");
			return false;
		} else {
			if (!(uploadWizardState.selectedOs.isSetArchitecture()
					&& uploadWizardState.selectedOs.isSetOsId() && uploadWizardState.selectedOs.isSetOsName() && uploadWizardState.selectedOs.isSetVirtualizerOsId())) {
				LOGGER.error("OS has missing fields: " + uploadWizardState.selectedOs.toString());
				return false;
			}
		}
		if (uploadWizardState.meta == null) {
			LOGGER.error("No vm meta data set in state!");
			return false;
		}
		if (uploadWizardState.permissions == null) {
			LOGGER.error("No permissions set in state!");
			return false;
		}
		if (uploadWizardState.shareMode == null) {
			LOGGER.error("No share mode set in state!");
			return false;
		}
		if (uploadWizardState.uuid == null) {
			LOGGER.error("No uuid set in state!");
			return false;
		}
		return true;
	}

	private ImageBaseWrite imageBaseWriteFromState() {
		// build imageBaseWrite
		return new ImageBaseWrite(uploadWizardState.name, uploadWizardState.description,
				uploadWizardState.selectedOs.getOsId(), uploadWizardState.meta.getVirtualizer().getVirtId(),
				uploadWizardState.isTemplate, uploadWizardState.permissions, uploadWizardState.shareMode);
	}
}