summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbOsVirt.java
blob: c505f703e661848287f46abc522fed6390233f88 (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
package org.openslx.bwlp.sat.database.mappers;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openslx.bwlp.sat.database.Database;
import org.openslx.bwlp.sat.database.MysqlConnection;
import org.openslx.bwlp.sat.database.MysqlStatement;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Virtualizer;

public class DbOsVirt {

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

	public static void storeOsList(List<OperatingSystem> list) throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			{
				MysqlStatement osGeneralStmt = connection.prepareStatement("INSERT INTO operatingsystem"
						+ " (osid, displayname, architecture, maxmem, maxcpu) VALUES"
						+ " (:osid, :displayname, :architecture, :maxmem, :maxcpu)"
						+ " ON DUPLICATE KEY UPDATE displayname = VALUES(displayname), architecture = VALUES(architecture),"
						+ "  maxmem = VALUES(maxmem), maxcpu = VALUES(maxcpu)");
				for (OperatingSystem os : list) {
					osGeneralStmt.setInt("osid", os.osId);
					osGeneralStmt.setString("displayname", os.osName);
					osGeneralStmt.setString("architecture", os.architecture);
					osGeneralStmt.setInt("maxmem", os.maxMemMb);
					osGeneralStmt.setInt("maxcpu", os.maxCores);
					osGeneralStmt.executeUpdate();
				}
				osGeneralStmt.close();
			}
			connection.commit();
			MysqlStatement virtStmt = connection.prepareStatement("INSERT IGNORE INTO os_x_virt"
					+ " (osid, virtid, virtoskeyword)              VALUES"
					+ " (:osid, :virtid, :virtoskeyword)"
					+ " ON DUPLICATE KEY UPDATE virtoskeyword = VALUES(virtoskeyword)");
			for (OperatingSystem os : list) {
				if (os.virtualizerOsId == null) {
					LOGGER.warn("OS " + os.osName + " (" + os.osId + ") has no virtualizerkeys");
				} else {
					virtStmt.setInt("osid", os.osId);
					for (Entry<String, String> virtkey : os.virtualizerOsId.entrySet()) {
						virtStmt.setString("virtid", virtkey.getKey());
						virtStmt.setString("virtoskeyword", virtkey.getValue());
						virtStmt.executeUpdate();
					}
				}
			}
			connection.commit();
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbOsVirt.storeOsList()", e);
			throw e;
		}
	}

	public static List<OperatingSystem> getOsList() throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			// Query OSs
			MysqlStatement stmt = connection.prepareStatement("SELECT"
					+ " osid, displayname, architecture, maxmem, maxcpu FROM operatingsystem");
			ResultSet rs = stmt.executeQuery();
			List<OperatingSystem> list = new ArrayList<>();
			Map<Integer, Map<String, String>> osVirtMappings = getOsVirtMappings(connection);
			while (rs.next()) {
				int osId = rs.getInt("osid");
				list.add(new OperatingSystem(osId, rs.getString("displayname"), osVirtMappings.get(osId),
						rs.getString("architecture"), rs.getInt("maxmem"), rs.getInt("maxcpu")));
			}
			return list;
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbOsVirt.getOsList()", e);
			throw e;
		}
	}

	private static Map<Integer, Map<String, String>> getOsVirtMappings(MysqlConnection connection)
			throws SQLException {
		MysqlStatement stmt = connection.prepareStatement("SELECT osid, virtid, virtoskeyword FROM os_x_virt");
		ResultSet rs = stmt.executeQuery();
		Map<Integer, Map<String, String>> map = new HashMap<>();
		while (rs.next()) {
			Integer osId = rs.getInt("osid");
			Map<String, String> osMap = map.get(osId);
			if (osMap == null) {
				osMap = new HashMap<>();
				map.put(osId, osMap);
			}
			osMap.put(rs.getString("virtid"), rs.getString("virtoskeyword"));
		}
		return map;
	}

	public static void storeVirtualizerList(List<Virtualizer> list) throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			MysqlStatement stmt = connection.prepareStatement("INSERT INTO virtualizer"
					+ " (virtid, virtname) VALUES" + " (:virtid, :virtname)"
					+ " ON DUPLICATE KEY UPDATE virtname = VALUES(virtname)");
			for (Virtualizer virt : list) {
				stmt.setString("virtid", virt.virtId);
				stmt.setString("virtname", virt.virtName);
				stmt.executeUpdate();
			}
			connection.commit();
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbOsVirt.storeVirtualizerList()", e);
			throw e;
		}
	}

	public static List<Virtualizer> getVirtualizerList() throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			MysqlStatement stmt = connection.prepareStatement("SELECT virtid, virtname" + " FROM virtualizer");
			ResultSet rs = stmt.executeQuery();
			List<Virtualizer> list = new ArrayList<>();
			while (rs.next()) {
				list.add(new Virtualizer(rs.getString("virtid"), rs.getString("virtname")));
			}
			return list;
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbOsVirt.getVirtualizerList()", e);
			throw e;
		}
	}

}