summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/ContainerUtils.java
blob: ca57a2656dc52f42279a84802763314ec6ceddf1 (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
package org.openslx.dozmod.util;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.bwlp.thrift.iface.ImageSummaryRead;
import org.openslx.bwlp.thrift.iface.LectureSummary;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.I18n;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.cache.ImageCache;
import org.openslx.dozmod.thrift.cache.LectureCache;
import org.openslx.thrifthelper.TConst;
import org.openslx.thrifthelper.ThriftManager;
import org.openslx.util.ThriftUtil;
import org.openslx.virtualization.configuration.container.ContainerDefinition;
import org.openslx.virtualization.configuration.container.ContainerMeta;

import java.awt.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

/**
 * Class with some temporally Helper Functions.
 */
public class ContainerUtils {

	/**
	 * Checks ImageBaseId of image if already linked with existing Lectures in LectureCache.
	 *
	 * @param image The image to check.
	 * @return true if the images imageBaseId is already linked with a lecture.
	 */
	public static boolean isContainerImageLinked(ImageSummaryRead image) {
		if (image != null && image.getVirtId().equals(TConst.VIRT_DOCKER)) {
			List<LectureSummary> lectureSummaries = LectureCache.get(true);
			for (LectureSummary lecture : lectureSummaries) {
				// lecture.imageBaseId is null when no image linked to it -> NullPointer on equals
				if (image.imageBaseId.equals(lecture.imageBaseId)) {
					return true;
				}
			}
		}
		return false;
	}

	public static List<ImageSummaryRead> getDataContainerImages() {
		String satelliteToken = Session.getSatelliteToken();
		List<ImageSummaryRead> images = ImageCache.get(true);
		List<ImageSummaryRead> dataContainerImages = new ArrayList<>();
		for (ImageSummaryRead image: images) {
			if (image.getVirtId().equals(TConst.VIRT_DOCKER))
			{
				try {
					byte[] rawVirtConfig;
					ByteBuffer byteBuffer = ThriftManager.getSatClient()
							.getImageVersionVirtConfig(satelliteToken, image.getLatestVersionId());
					rawVirtConfig = ThriftUtil.unwrapByteBuffer(byteBuffer);
					ContainerDefinition containerDefinition = ContainerDefinition.fromByteArray(rawVirtConfig);
					if (containerDefinition.getContainerMeta().getImageType() == ContainerMeta.ContainerImageType.DATA)
						dataContainerImages.add(image);

				} catch (TException e) {
//					LOGGER.error("Failed to retrieve virtualizer config for image version " + "'"
//							+ image.getLatestVersionId() + ", see trace: ", e);
				}
			}
		}
		return dataContainerImages;
	}

	public static void showWarning(Component c, Logger logger) {
		Gui.showMessageBox(c, I18n.WINDOW.getString("LectureDetails.Message.error.containerLinkedWithLecture"),
				MessageType.WARNING, logger, null);
	}

	public static String getImageNameByBaseId(String imageBaseId) {
		List<ImageSummaryRead> images = ImageCache.get(true);
		for (ImageSummaryRead image: images) {
			if (image.imageBaseId.equals(imageBaseId))
				return image.getImageName();
		}
		return imageBaseId;
	}
}