Skip to content
Snippets Groups Projects
Commit 77483ce3 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Context component:

- partial code implementation
parent a8e2c9b3
No related branches found
No related tags found
2 merge requests!54Release 2.0.0,!34Context Scalability extensions using CockroachDB + Removal of Stateful database inside Device + other
......@@ -58,7 +58,7 @@ from context.service.database.ContextModel import ContextModel
#from context.service.database.ServiceModel import (
# ServiceModel, grpc_to_enum__service_status, grpc_to_enum__service_type)
#from context.service.database.SliceModel import SliceModel, grpc_to_enum__slice_status
#from context.service.database.TopologyModel import TopologyModel
from context.service.database.TopologyModel import TopologyModel
#from .Constants import (
# CONSUME_TIMEOUT, TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, TOPIC_SERVICE, TOPIC_SLICE,
# TOPIC_TOPOLOGY)
......@@ -111,8 +111,10 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
def GetContext(self, request: ContextId, context : grpc.ServicerContext) -> Context:
context_uuid = request.context_uuid.uuid
def callback(session : Session) -> Optional[Dict]:
obj : Optional[ContextModel] = \
session.query(ContextModel).filter_by(context_uuid=context_uuid).one_or_none()
obj : Optional[ContextModel] = session\
.query(ContextModel)\
.filter_by(context_uuid=context_uuid)\
.one_or_none()
return None if obj is None else obj.dump()
obj = run_transaction(sessionmaker(bind=self.db_engine), callback)
if obj is None: raise NotFoundException(ContextModel.__name__.replace('Model', ''), context_uuid)
......@@ -202,47 +204,50 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
def ListTopologyIds(self, request: ContextId, context : grpc.ServicerContext) -> TopologyIdList:
context_uuid = request.context_uuid.uuid
with self.session() as session:
result = session.query(ContextModel).options(selectinload(ContextModel.topology)).filter_by(context_uuid=context_uuid).one_or_none()
if not result:
raise NotFoundException(ContextModel.__name__.replace('Model', ''), context_uuid)
db_topologies = result.topology
return TopologyIdList(topology_ids=[db_topology.dump_id() for db_topology in db_topologies])
return ContextIdList(context_ids=run_transaction(sessionmaker(bind=self.db_engine), callback))
def callback(session : Session) -> List[Dict]:
obj_list : List[ContextModel] = session.query(ContextModel).all()
obj_list : List[TopologyModel] = session.query(TopologyModel).filter_by(context_uuid=context_uuid).all()
#.options(selectinload(ContextModel.topology)).filter_by(context_uuid=context_uuid).one_or_none()
return [obj.dump_id() for obj in obj_list]
#with self.session() as session:
# result = session.query(ContextModel).options(selectinload(ContextModel.topology)).filter_by(context_uuid=context_uuid).one_or_none()
# if not result:
# raise NotFoundException(ContextModel.__name__.replace('Model', ''), context_uuid)
# db_topologies = result.topology
return TopologyIdList(topology_ids=run_transaction(sessionmaker(bind=self.db_engine), callback))
@safe_and_metered_rpc_method(METRICS, LOGGER)
def ListContexts(self, request: Empty, context : grpc.ServicerContext) -> ContextList:
def ListTopologies(self, request: ContextId, context : grpc.ServicerContext) -> TopologyList:
context_uuid = request.context_uuid.uuid
def callback(session : Session) -> List[Dict]:
obj_list : List[ContextModel] = session.query(ContextModel).all()
obj_list : List[TopologyModel] = session.query(TopologyModel).filter_by(context_uuid=context_uuid).all()
#.options(selectinload(ContextModel.topology)).filter_by(context_uuid=context_uuid).one_or_none()
return [obj.dump() for obj in obj_list]
return ContextList(contexts=run_transaction(sessionmaker(bind=self.db_engine), callback))
#with self.session() as session:
# result = session.query(ContextModel).options(selectinload(ContextModel.topology)).filter_by(
# context_uuid=context_uuid).one_or_none()
# if not result:
# raise NotFoundException(ContextModel.__name__.replace('Model', ''), context_uuid)
# db_topologies = result.topology
return TopologyList(topologies=run_transaction(sessionmaker(bind=self.db_engine), callback))
@safe_and_metered_rpc_method(METRICS, LOGGER)
def GetTopology(self, request: TopologyId, context : grpc.ServicerContext) -> Topology:
context_uuid = request.context_id.context_uuid.uuid
topology_uuid = request.topology_uuid.uuid
def callback(session : Session) -> Optional[Dict]:
obj : Optional[TopologyModel] = session\
.query(TopologyModel)\
.filter_by(context_uuid=context_uuid, topology_uuid=topology_uuid)\
.one_or_none()
return None if obj is None else obj.dump()
obj = run_transaction(sessionmaker(bind=self.db_engine), callback)
if obj is None: raise NotFoundException(TopologyModel.__name__.replace('Model', ''), context_uuid)
return Topology(**obj)
# @safe_and_metered_rpc_method(METRICS, LOGGER)
# def ListTopologies(self, request: ContextId, context : grpc.ServicerContext) -> TopologyList:
# context_uuid = request.context_uuid.uuid
#
# with self.session() as session:
# result = session.query(ContextModel).options(selectinload(ContextModel.topology)).filter_by(
# context_uuid=context_uuid).one_or_none()
# if not result:
# raise NotFoundException(ContextModel.__name__.replace('Model', ''), context_uuid)
#
# db_topologies = result.topology
# return TopologyList(topologies=[db_topology.dump() for db_topology in db_topologies])
#
# @safe_and_metered_rpc_method(METRICS, LOGGER)
# def GetTopology(self, request: TopologyId, context : grpc.ServicerContext) -> Topology:
# topology_uuid = request.topology_uuid.uuid
#
# result, dump = self.database.get_object(TopologyModel, topology_uuid, True)
# with self.session() as session:
# devs = None
......@@ -265,8 +270,7 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
# links.append(session.query(LinkModel).filter_by(**filt).one())
#
# return Topology(**result.dump(devs, links))
#
#
# @safe_and_metered_rpc_method(METRICS, LOGGER)
# def SetTopology(self, request: Topology, context : grpc.ServicerContext) -> TopologyId:
# context_uuid = request.topology_id.context_id.context_uuid.uuid
......@@ -300,7 +304,7 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
# dict_topology_id = db_topology.dump_id()
# notify_event(self.messagebroker, TOPIC_TOPOLOGY, event_type, {'topology_id': dict_topology_id})
# return TopologyId(**dict_topology_id)
#
# @safe_and_metered_rpc_method(METRICS, LOGGER)
# def RemoveTopology(self, request: TopologyId, context : grpc.ServicerContext) -> Empty:
# context_uuid = request.context_id.context_uuid.uuid
......@@ -317,13 +321,12 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
# event_type = EventTypeEnum.EVENTTYPE_REMOVE
# notify_event(self.messagebroker, TOPIC_TOPOLOGY, event_type, {'topology_id': dict_topology_id})
# return Empty()
#
## @safe_and_metered_rpc_method(METRICS, LOGGER)
## def GetTopologyEvents(self, request: Empty, context : grpc.ServicerContext) -> Iterator[TopologyEvent]:
## for message in self.messagebroker.consume({TOPIC_TOPOLOGY}, consume_timeout=CONSUME_TIMEOUT):
## yield TopologyEvent(**json.loads(message.content))
#
#
# @safe_and_metered_rpc_method(METRICS, LOGGER)
# def GetTopologyEvents(self, request: Empty, context : grpc.ServicerContext) -> Iterator[TopologyEvent]:
# for message in self.messagebroker.consume({TOPIC_TOPOLOGY}, consume_timeout=CONSUME_TIMEOUT):
# yield TopologyEvent(**json.loads(message.content))
# # ----- Device -----------------------------------------------------------------------------------------------------
#
# @safe_and_metered_rpc_method(METRICS, LOGGER)
......
......@@ -16,8 +16,8 @@ import logging
from typing import Dict
from sqlalchemy import Column, Float, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from ._Base import _Base
#from sqlalchemy.orm import relationship
LOGGER = logging.getLogger(__name__)
......@@ -27,7 +27,7 @@ class ContextModel(_Base):
context_name = Column(String(), nullable=False)
created_at = Column(Float)
#topology = relationship('TopologyModel', back_populates='context')
topology = relationship('TopologyModel', back_populates='context')
def dump_id(self) -> Dict:
return {'context_uuid': {'uuid': self.context_uuid}}
......@@ -48,8 +48,13 @@ class ContextModel(_Base):
return [TopologyModel(self.database, pk).dump_id() for pk,_ in db_topology_pks]
"""
def dump(self, include_services=True, include_topologies=True) -> Dict: # pylint: disable=arguments-differ
def dump(self,
include_services : bool = True, # pylint: disable=arguments-differ
include_slices : bool = True, # pylint: disable=arguments-differ
include_topologies : bool = True # pylint: disable=arguments-differ
) -> Dict:
result = {'context_id': self.dump_id(), 'name': self.context_name}
# if include_services: result['service_ids'] = self.dump_service_ids()
# if include_slices: result['slice_ids'] = self.dump_slice_ids()
# if include_topologies: result['topology_ids'] = self.dump_topology_ids()
return result
......@@ -12,21 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging, operator
from typing import Dict, List
from sqlalchemy.orm import relationship
import logging #, operator
from typing import Dict #, List
from sqlalchemy import Column, ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from context.service.database._Base import Base
from sqlalchemy.orm import relationship
from ._Base import _Base
LOGGER = logging.getLogger(__name__)
class TopologyModel(Base):
class TopologyModel(_Base):
__tablename__ = 'Topology'
context_uuid = Column(UUID(as_uuid=False), ForeignKey("Context.context_uuid"), primary_key=True)
context_uuid = Column(UUID(as_uuid=False), ForeignKey('context.context_uuid'), primary_key=True)
topology_uuid = Column(UUID(as_uuid=False), primary_key=True, unique=True)
# Relationships
context = relationship("ContextModel", back_populates="topology")
context = relationship('ContextModel', back_populates='topology')
def dump_id(self) -> Dict:
context_id = self.context.dump_id()
......@@ -35,16 +36,16 @@ class TopologyModel(Base):
'topology_uuid': {'uuid': self.topology_uuid},
}
@staticmethod
def main_pk_name() -> str:
return 'topology_uuid'
#@staticmethod
#def main_pk_name() -> str:
# return 'topology_uuid'
def dump( # pylint: disable=arguments-differ
self, devices=None, links=None
) -> Dict:
def dump(self) -> Dict:
# pylint: disable=arguments-differ
result = {'topology_id': self.dump_id()}
if devices:
result['device_ids'] = [device.dump_id() for device in devices]
if links:
result['link_ids'] = [link.dump_id() for link in links]
# params: , devices=None, links=None
#if devices:
# result['device_ids'] = [device.dump_id() for device in devices]
#if links:
# result['link_ids'] = [link.dump_id() for link in links]
return result
......@@ -14,3 +14,4 @@
from ._Base import _Base, rebuild_database
from .ContextModel import ContextModel
from .TopologyModel import TopologyModel
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment