Skip to content
Snippets Groups Projects
Topology.py 2.32 KiB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from __future__ import annotations
from typing import TYPE_CHECKING, Dict
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.database.api.context.Keys import KEY_TOPOLOGY, KEY_DEVICES, KEY_LINKS
from common.database.api.context.topology.device.Device import Device
from common.database.api.context.topology.link.Link import Link
from common.database.api.entity._Entity import _Entity
from common.database.api.entity.EntityCollection import EntityCollection
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

if TYPE_CHECKING:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    from common.database.api.context.Context import Context
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
VALIDATORS = {}  # no attributes accepted
TRANSCODERS = {} # no transcoding applied to attributes
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

class Topology(_Entity):
    def __init__(self, topology_uuid : str, parent : 'Context'):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        super().__init__(parent, topology_uuid, KEY_TOPOLOGY, VALIDATORS, TRANSCODERS)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self._devices = EntityCollection(self, KEY_DEVICES)
        self._links = EntityCollection(self, KEY_LINKS)

    @property
    def parent(self) -> 'Context': return self._parent

    @property
    def context(self) -> 'Context': return self._parent

    @property
    def context_uuid(self) -> str: return self.context.context_uuid

    @property
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def topology_uuid(self) -> str: return self._entity_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    @property
    def devices(self) -> EntityCollection: return self._devices

    @property
    def links(self) -> EntityCollection: return self._links

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def device(self, device_uuid : str) -> Device: return Device(device_uuid, self)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def link(self, link_uuid : str) -> Link: return Link(link_uuid, self)

    def create(self) -> 'Topology':
        self.parent.topologies.add(self.topology_uuid)
        return self

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def delete(self) -> None:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        for device_uuid in self.devices.get(): self.device(device_uuid).delete()
        for link_uuid in self.links.get(): self.link(link_uuid).delete()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self.attributes.delete()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self.parent.topologies.delete(self.topology_uuid)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def dump_id(self) -> Dict:
        return {
            'contextId': self.context.dump_id(),
            'topoId': {'uuid': self.topology_uuid},
        }

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def dump(self) -> Dict:
        devices = [self.device(device_uuid).dump() for device_uuid in self.devices.get()]
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        links = [self.link(link_uuid).dump() for link_uuid in self.links.get()]
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return {
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            'topoId': self.dump_id(),
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            'device': devices,
            'link': links,
        }