summaryrefslogtreecommitdiff
path: root/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/topology
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2017-09-28 03:27:36 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2017-09-28 03:27:36 +0200
commitd6d9d37abf17071ff050e45ea37c693e659a4e98 (patch)
tree248d11c478ee71a6bc47cf3f5c73870c73b4c210 /opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/topology
parent23a476613b000bf04194ec2962d270fa3cabfc5d (diff)
Implement JPA integration
Diffstat (limited to 'opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/topology')
-rw-r--r--opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/topology/JpaTopologyFactory.kt87
1 files changed, 87 insertions, 0 deletions
diff --git a/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/topology/JpaTopologyFactory.kt b/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/topology/JpaTopologyFactory.kt
new file mode 100644
index 00000000..8def721e
--- /dev/null
+++ b/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/topology/JpaTopologyFactory.kt
@@ -0,0 +1,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")
+ }
+ }
+ }
+}