Skip to content
Snippets Groups Projects
Link.py 2.08 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.entity._Entity import _Entity
from common.database.api.entity.EntityCollection import EntityCollection
from common.database.api.context.topology.link.Endpoint import Endpoint
from common.database.api.context.Keys import KEY_LINK, KEY_LINK_ENDPOINTS
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
    from common.database.api.context.topology.Topology import Topology
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 Link(_Entity):
    def __init__(self, link_uuid : str, parent : 'Topology'):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        super().__init__(parent, link_uuid, KEY_LINK, VALIDATORS, TRANSCODERS)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self._endpoints = EntityCollection(self, KEY_LINK_ENDPOINTS)

    @property
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def parent(self) -> 'Topology': return self._parent
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    @property
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def context(self) -> 'Context': return self.parent.context
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

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

    @property
    def topology(self) -> 'Topology': return self.parent
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    @property
    def topology_uuid(self) -> str: return self.parent.topology_uuid

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

    @property
    def endpoints(self) -> EntityCollection: return self._endpoints

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def endpoint(self, link_endpoint_uuid : str) -> Endpoint: return Endpoint(link_endpoint_uuid, self)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    def create(self) -> 'Link':
        self.parent.links.add(self.link_uuid)
        return self

    def delete(self) -> None:
        for endpoint_uuid in self.endpoints.get(): self.endpoint(endpoint_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.links.delete(self.link_uuid)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def dump_id(self) -> Dict:
        return {
            'link_id': {'uuid': self.link_uuid},
        }

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def dump(self) -> Dict:
        endpoints = [self.endpoint(link_endpoint_uuid).dump() for link_endpoint_uuid in self.endpoints.get()]
        return {
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            'link_id': self.dump_id(),
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            'endpointList': endpoints
        }