summaryrefslogtreecommitdiff
path: root/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/topology/JpaTopologyFactory.kt
blob: 8def721e4bb90cc3121bb82323e09f746bc02398 (plain)
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
/*
 * MIT License
 *
 * Copyright (c) 2017 atlarge-research
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package nl.atlarge.opendc.topology

import nl.atlarge.opendc.integration.jpa.schema.*

/**
 * A [TopologyFactory] that converts a [Section] of an experiment as defined by the API, into a proper [Topology].
 *
 * @property section The section to convert into a topology.
 * @property builder A builder for a topology to use.
 * @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
 */
class JpaTopologyFactory(val section: Section, val builder: TopologyBuilder = AdjacencyList.builder()) : TopologyFactory {
	/**
	 * Create a [MutableTopology] instance.
	 *
	 * @return A mutable topology.
	 */
	override fun create(): MutableTopology = builder.construct {
		val datacenter = section.datacenter
		add(datacenter)
		datacenter.rooms.forEach { room ->
			add(room)
			connect(datacenter, room, tag = "room")

			room.objects.forEach { roomObject(room, it) }
		}
	}

	/**
	 * Handle the objects in a room.
	 *
	 * @param obj The obj to handle.
	 */
	private fun MutableTopology.roomObject(parent: Room, obj: RoomObject) = when(obj) {
		is Rack -> rack(parent, obj)
		else -> Unit
	}

	/**
	 * Handle a rack in a room.
	 *
	 * @param parent The parent of the rack.
	 * @param rack The rack to handle.
	 */
	private fun MutableTopology.rack(parent: Room, rack: Rack) {
		add(rack)
		connect(parent, rack, tag = "rack")
		rack.machines.forEach { machine ->
			add(machine)
			connect(rack, machine, tag = "machine")

			machine.cpus.forEach { cpu ->
				add(cpu)
				connect(machine, cpu, tag = "cpu")
			}

			machine.gpus.forEach { gpu ->
				add(gpu)
				connect(machine, gpu, tag = "gpu")
			}
		}
	}
}