Skip to content
Snippets Groups Projects
Context.py 2.27 KiB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from typing import Dict, List
from common.database.api.context.service.Service import Service
from common.database.api.context.topology.Topology import Topology
from common.database.api.context.Keys import KEY_CONTEXT, KEY_SERVICES, KEY_TOPOLOGIES
from common.database.api.entity._RootEntity import _RootEntity
from common.database.api.entity.EntityCollection import EntityCollection
from common.database.engines._DatabaseEngine import _DatabaseEngine
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 Context(_RootEntity):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def __init__(self, context_uuid : str, database_engine : _DatabaseEngine):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        super().__init__(database_engine, context_uuid, KEY_CONTEXT, VALIDATORS, TRANSCODERS)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self._topologies = EntityCollection(self, KEY_TOPOLOGIES)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self._services = EntityCollection(self, KEY_SERVICES)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

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

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

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

    @property
    def topologies(self) -> EntityCollection: return self._topologies

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    @property
    def services(self) -> EntityCollection: return self._services

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def topology(self, topology_uuid : str) -> Topology: return Topology(topology_uuid, self)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def service(self, service_uuid : str) -> Service: return Service(service_uuid, self)

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def create(self) -> 'Context': return self

    def delete(self):
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        for service_uuid in self.services.get(): self.service(service_uuid).delete()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        for topology_uuid in self.topologies.get(): self.topology(topology_uuid).delete()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        self.attributes.delete()
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def dump_id(self) -> Dict:
        return {
            'contextUuid': {'uuid': self.context_uuid},
        }

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def dump_topologies(self) -> List:
        return [
            self.topology(topology_uuid).dump() for topology_uuid in self.topologies.get()
        ]

    def dump_services(self) -> List:
        return [
            self.service(service_uuid).dump() for service_uuid in self.services.get()
        ]

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    def dump(self) -> Dict:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        return {
            'contextId': self.dump_id(),
            'topologies': self.dump_topologies(),
            'services': self.dump_services(),
        }