Skip to content
Snippets Groups Projects
Link.py 1.93 KiB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from __future__ import annotations
from typing import TYPE_CHECKING, Dict
from ..entity._Entity import _Entity
from ..entity.EntityCollection import EntityCollection
from .LinkEndpoint import LinkEndpoint
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from .Keys import KEY_LINK, KEY_LINK_ENDPOINTS
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

if TYPE_CHECKING:
    from .Context import Context
    from .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

    def endpoint(self, link_endpoint_uuid : str) -> LinkEndpoint: return LinkEndpoint(link_endpoint_uuid, self)

    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
        }