Commit 72a67e71 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Multiple changes:

Common:
- reorganized Database API to enable extension of Context API with entities (e.g. Service)
- solved minor bugs in Database API

Context and Device services:
- rearranged services and unit tests according to mentioned Context API changes

Service service:
- initial release (under development, this is just a backup of code)
parent 9cc44b30
Loading
Loading
Loading
Loading
+53 −0
Original line number Diff line number Diff line
apiVersion: apps/v1
kind: Deployment
metadata:
  name: serviceservice
spec:
  selector:
    matchLabels:
      app: serviceservice
  template:
    metadata:
      labels:
        app: serviceservice
    spec:
      terminationGracePeriodSeconds: 5
      containers:
      - name: server
        image: registry.gitlab.com/teraflow-h2020/controller/device:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 3030
        env:
        - name: DB_ENGINE
          value: "redis"
        - name: REDIS_DATABASE_ID
          value: "0"
        - name: LOG_LEVEL
          value: "DEBUG"
        readinessProbe:
          exec:
            command: ["/bin/grpc_health_probe", "-addr=:3030"]
        livenessProbe:
          exec:
            command: ["/bin/grpc_health_probe", "-addr=:3030"]
        resources:
          requests:
            cpu: 250m
            memory: 512Mi
          limits:
            cpu: 700m
            memory: 1024Mi
---
apiVersion: v1
kind: Service
metadata:
  name: serviceservice
spec:
  type: ClusterIP
  selector:
    app: serviceservice
  ports:
  - name: grpc
    port: 3030
    targetPort: 3030
+12 −3
Original line number Diff line number Diff line
import logging
from typing import List
from ..engines._DatabaseEngine import _DatabaseEngine
from .context.Context import Context
from .Exceptions import WrongDatabaseEngine, MutexException
from common.database.api.Exceptions import WrongDatabaseEngine, MutexException
from common.database.api.context.Context import Context
from common.database.api.context.Keys import KEY_CONTEXTS
from common.database.api.entity.EntityCollection import EntityCollection
from common.database.engines._DatabaseEngine import _DatabaseEngine

LOGGER = logging.getLogger(__name__)

@@ -13,6 +15,10 @@ class Database:
        self._database_engine = database_engine
        self._acquired = False
        self._owner_key = None
        self._contexts = EntityCollection(self, KEY_CONTEXTS)

    @property
    def database_engine(self) -> _DatabaseEngine: return self._database_engine

    def __enter__(self) -> '_DatabaseEngine':
        self._acquired, self._owner_key = self._database_engine.lock()
@@ -36,4 +42,7 @@ class Database:
        entries.sort()
        return ['[{:>4s}] {:100s} :: {}'.format(k_type, k_name, k_value) for k_name,k_type,k_value in entries]

    @property
    def contexts(self) -> EntityCollection: return self._contexts

    def context(self, context_uuid : str) -> Context: return Context(context_uuid, self._database_engine)
+29 −7
Original line number Diff line number Diff line
from typing import Dict
from ...engines._DatabaseEngine import _DatabaseEngine
from ..entity._RootEntity import _RootEntity
from ..entity.EntityCollection import EntityCollection
from .Keys import KEY_CONTEXT, KEY_TOPOLOGIES
from .Topology import Topology
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

VALIDATORS = {}  # no attributes accepted
TRANSCODERS = {} # no transcoding applied to attributes
@@ -12,6 +13,7 @@ class Context(_RootEntity):
    def __init__(self, context_uuid : str, database_engine : _DatabaseEngine):
        super().__init__(database_engine, context_uuid, KEY_CONTEXT, VALIDATORS, TRANSCODERS)
        self._topologies = EntityCollection(self, KEY_TOPOLOGIES)
        self._services = EntityCollection(self, KEY_SERVICES)

    @property
    def parent(self) -> 'Context': return self
@@ -25,11 +27,17 @@ class Context(_RootEntity):
    @property
    def topologies(self) -> EntityCollection: return self._topologies

    @property
    def services(self) -> EntityCollection: return self._services

    def topology(self, topology_uuid : str) -> Topology: return Topology(topology_uuid, self)

    def service(self, service_uuid : str) -> Service: return Service(service_uuid, self)

    def create(self) -> 'Context': return self

    def delete(self):
        for service_uuid in self.services.get(): self.service(service_uuid).delete()
        for topology_uuid in self.topologies.get(): self.topology(topology_uuid).delete()
        self.attributes.delete()

@@ -38,5 +46,19 @@ class Context(_RootEntity):
            'contextUuid': {'uuid': self.context_uuid},
        }

    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()
        ]

    def dump(self) -> Dict:
        return {topology_uuid : self.topology(topology_uuid).dump() for topology_uuid in self.topologies.get()}
        return {
            'contextId': self.dump_id(),
            'topologies': self.dump_topologies(),
            'services': self.dump_services(),
        }
+37 −11
Original line number Diff line number Diff line
# Database keys
KEY_CONTEXTS            =                'contexts{container_name}'

# Context keys
KEY_CONTEXT             =                'context[{context_uuid}]'
KEY_TOPOLOGIES          = KEY_CONTEXT  + '/topologies{container_name}'
KEY_SERVICES            = KEY_CONTEXT  + '/services{container_name}'

# Context.Topology keys
KEY_TOPOLOGY            = KEY_CONTEXT  + '/topology[{topology_uuid}]'
KEY_DEVICES             = KEY_TOPOLOGY + '/devices{container_name}'
KEY_LINKS               = KEY_TOPOLOGY + '/links{container_name}'

# Context.Topology.Device keys
KEY_DEVICE              = KEY_TOPOLOGY + '/device[{device_uuid}]'
KEY_DEVICE_ENDPOINTS    = KEY_DEVICE   + '/endpoints{container_name}'
KEY_ENDPOINT         = KEY_DEVICE   + '/endpoint[{endpoint_uuid}]' 

# Context.Topology.Device.Endpoint keys
KEY_DEVICE_ENDPOINT     = KEY_DEVICE   + '/endpoint[{endpoint_uuid}]'

# Context.Topology.Link keys
KEY_LINK                = KEY_TOPOLOGY + '/link[{link_uuid}]'
KEY_LINK_ENDPOINTS      = KEY_LINK     + '/endpoints{container_name}'
KEY_LINK_ENDPOINT    = KEY_LINK     + '/endpoint[{link_endpoint_uuid}]'

# Context.Topology.Link.Endpoint Keys
KEY_LINK_ENDPOINT       = KEY_LINK     + '/endpoint[{endpoint_uuid}]'

# Service keys
KEY_SERVICE             = KEY_CONTEXT  + '/service[{service_uuid}]'
KEY_SERVICE_ENDPOINTS   = KEY_SERVICE  + '/endpoints{container_name}'
KEY_SERVICE_CONSTRAINTS = KEY_SERVICE  + '/constraints{container_name}'

# Context.Service.Endpoint Keys
KEY_SERVICE_ENDPOINT    = KEY_SERVICE  + '/endpoint[{endpoint_uuid}]'

# Context.Service.Constraint Keys
KEY_SERVICE_CONSTRAINT  = KEY_SERVICE  + '/constraint[{constraint_uuid}]'
Loading