summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/cache/MetaDataCache.java
blob: 6d75f792a24b726d884ea8700c3768861f3abc70 (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
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
package org.openslx.dozmod.thrift.cache;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.thrift.TApplicationException;
import org.apache.thrift.TException;
import org.openslx.bwlp.thrift.iface.LdapFilter;
import org.openslx.bwlp.thrift.iface.Location;
import org.openslx.bwlp.thrift.iface.NetShare;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.PredefinedData;
import org.openslx.bwlp.thrift.iface.Virtualizer;
import org.openslx.dozmod.thrift.Session;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.util.GenericDataCache;
import org.openslx.util.GenericDataCache.CacheMode;

public class MetaDataCache {

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

	/**
	 * How long to cache data.
	 */
	private static final int CACHE_TIME_LONG_MS = 60 * 60 * 1000;
	
	private static final int CACHE_TIME_SHORT_MS = 10 * 60 * 1000;

	private static final GenericDataCache<List<OperatingSystem>> osCache = new GenericDataCache<List<OperatingSystem>>(
			CACHE_TIME_LONG_MS) {
		@Override
		protected List<OperatingSystem> update() throws TException {
			try {
				return ThriftManager.getSatClient().getOperatingSystems();
			} catch (Exception e) {
				LOGGER.warn("Could not get OS list from satellite, trying master for backup...", e);
			}
			return ThriftManager.getMasterClient().getOperatingSystems();
		}
	};

	private static final GenericDataCache<List<Virtualizer>> virtualizerCache = new GenericDataCache<List<Virtualizer>>(
			CACHE_TIME_LONG_MS) {
		@Override
		protected List<Virtualizer> update() throws TException {
			try {
				return ThriftManager.getSatClient().getVirtualizers();
			} catch (TException e) {
				LOGGER.warn("Could not get virtualizer list from satellite, trying master for backup...", e);
			}
			return ThriftManager.getMasterClient().getVirtualizers();
		}
	};
	
	private static int locationFails = 0;
	
	private static final GenericDataCache<List<Location>> locationCache = new GenericDataCache<List<Location>>(
			CACHE_TIME_SHORT_MS) {
		@Override
		protected List<Location> update() throws TException {
			if (locationFails > 2)
				return null;
			try {
				List<Location> ret = ThriftManager.getSatClient().getLocations();
				locationFails = 0;
				return ret;
			} catch (TApplicationException e) {
				locationFails++;
				if (locationFails == 1) {
					LOGGER.info("Satellite doesn't seem to support locations");
				}
			} catch (TException e) {
				LOGGER.warn("Could not get locations list from the satellite...", e);
			}
			// querying masterserver does not make sense for locations
			return null;
		}
	};

	/**
	 * Get all known/valid operating systems an image can be marked as.
	 * 
	 * @return
	 */
	public static List<OperatingSystem> getOperatingSystems() {
		return osCache.get();
	}

	public static OperatingSystem getOsById(int id) {
		return getOsById(id, false);
	}
	
	public static OperatingSystem getOsById(int id, boolean forceCache) {
		// First, try in "always cached" mode
		List<OperatingSystem> list = osCache.get(CacheMode.FORCE_CACHED);
		OperatingSystem os = getOsById(id, list);
		if (os != null || forceCache)
			return os;
		// Try again with a potential refresh
		List<OperatingSystem> newList = osCache.get(CacheMode.DEFAULT);
		if (list == newList) // Returned list from cache as it was still recent enough
			return null;
		return getOsById(id, newList);
	}

	private static OperatingSystem getOsById(int id, List<OperatingSystem> list) {
		if (list != null) {
			for (OperatingSystem os : list) {
				if (os.getOsId() == id)
					return os;
			}
		}
		return null;
	}

	/**
	 * Get all supported virtualizers an image can be declared to be run as.
	 * 
	 * @return
	 */
	public static List<Virtualizer> getVirtualizers() {
		return virtualizerCache.get();
	}
	
	public static Virtualizer getVirtualizerById(String virtId) {
		return getVirtualizerById(virtId, false);
	}

	public static Virtualizer getVirtualizerById(String virtId, boolean forceCache) {
		// First, try in "always cached" mode
		List<Virtualizer> list = virtualizerCache.get(CacheMode.FORCE_CACHED);
		Virtualizer virt = getVirtualizerById(virtId, list);
		if (virt != null || forceCache)
			return virt;
		// Try again with a potential refresh
		List<Virtualizer> newList = virtualizerCache.get(CacheMode.DEFAULT);
		if (list == newList) // Returned list from cache as it was still recent enough
			return null;
		return getVirtualizerById(virtId, newList);
	}

	private static Virtualizer getVirtualizerById(String virtId, List<Virtualizer> list) {
		if (list != null) {
			for (Virtualizer virt : list) {
				if (virt.getVirtId().equals(virtId))
					return virt;
			}
		}
		return null;
	}

	/**
	 * Get all known/valid locations a lecture can be assigned to.
	 * 
	 * @return
	 */
	public static List<Location> getLocations() {
		return locationCache.get();
	}

	public static Location getLocationById(int id) {
		return getLocationById(id, false);
	}
	
	public static Location getLocationById(int id, boolean forceCache) {
		// First, try in "always cached" mode
		List<Location> list = locationCache.get(CacheMode.FORCE_CACHED);
		Location location = getLocationById(id, list);
		if (location != null || forceCache)
			return location;
		// Try again with a potential refresh
		List<Location> newList = locationCache.get(CacheMode.DEFAULT);
		if (list == newList) // Returned list from cache as it was still recent enough
			return null;
		return getLocationById(id, newList);
	}

	private static Location getLocationById(int id, List<Location> list) {
		if (list != null) {
			for (Location loc : list) {
				if (loc.getLocationId() == id)
					return loc;
			}
		}
		return null;
	}
	
	/*
	 * LDAP filters and network shares
	 */

	private static final GenericDataCache<PredefinedData> predefinedData = new GenericDataCache<PredefinedData>(
			CACHE_TIME_SHORT_MS) {
		@Override
		protected PredefinedData update() throws TException {
			try {
				return ThriftManager.getSatClient().getPredefinedData(Session.getSatelliteToken());
			} catch (TException e) {
				LOGGER.warn("Could not get predefined Data from sat...", e);
			}
			return null;
		}
	};

	public static List<LdapFilter> getPredefinedLdapFilters() {
		PredefinedData pd = predefinedData.get();
		if (pd == null || pd.ldapFilter == null)
			return new ArrayList<LdapFilter>(0);
		return pd.ldapFilter;
	}

	public static List<NetShare> getPredefinedNetshares() {
		PredefinedData pd = predefinedData.get();
		if (pd == null || pd.ldapFilter == null)
			return new ArrayList<NetShare>(0);
		return pd.netShares;
	}
	
	

}