package org.openslx.dozmod.thrift.cache;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.PresetNetRule;
import org.openslx.bwlp.thrift.iface.PresetRunScript;
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 = LogManager.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.netShares == null)
return new ArrayList<NetShare>(0);
return pd.netShares;
}
public static List<PresetRunScript> getPredefinedRunScripts() {
PredefinedData pd = predefinedData.get();
if (pd == null || pd.runScripts == null)
return new ArrayList<PresetRunScript>(0);
return pd.runScripts;
}
public static List<PresetNetRule> getPredefinedNetRules() {
PredefinedData pd = predefinedData.get();
if (pd == null || pd.networkExceptions == null)
return new ArrayList<PresetNetRule>(0);
return pd.networkExceptions;
}
}