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 ...@@ -58,7 +58,7 @@ from context.service.database.ContextModel import ContextModel
#from context.service.database.ServiceModel import ( #from context.service.database.ServiceModel import (
# ServiceModel, grpc_to_enum__service_status, grpc_to_enum__service_type) # 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.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 ( #from .Constants import (
# CONSUME_TIMEOUT, TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, TOPIC_SERVICE, TOPIC_SLICE, # CONSUME_TIMEOUT, TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, TOPIC_SERVICE, TOPIC_SLICE,
# TOPIC_TOPOLOGY) # TOPIC_TOPOLOGY)
...@@ -111,8 +111,10 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer ...@@ -111,8 +111,10 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
def GetContext(self, request: ContextId, context : grpc.ServicerContext) -> Context: def GetContext(self, request: ContextId, context : grpc.ServicerContext) -> Context:
context_uuid = request.context_uuid.uuid context_uuid = request.context_uuid.uuid
def callback(session : Session) -> Optional[Dict]: def callback(session : Session) -> Optional[Dict]:
obj : Optional[ContextModel] = \ obj : Optional[ContextModel] = session\
session.query(ContextModel).filter_by(context_uuid=context_uuid).one_or_none() .query(ContextModel)\
.filter_by(context_uuid=context_uuid)\
.one_or_none()
return None if obj is None else obj.dump() return None if obj is None else obj.dump()
obj = run_transaction(sessionmaker(bind=self.db_engine), callback) obj = run_transaction(sessionmaker(bind=self.db_engine), callback)
if obj is None: raise NotFoundException(ContextModel.__name__.replace('Model', ''), context_uuid) if obj is None: raise NotFoundException(ContextModel.__name__.replace('Model', ''), context_uuid)
...@@ -202,47 +204,50 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer ...@@ -202,47 +204,50 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
def ListTopologyIds(self, request: ContextId, context : grpc.ServicerContext) -> TopologyIdList: def ListTopologyIds(self, request: ContextId, context : grpc.ServicerContext) -> TopologyIdList:
context_uuid = request.context_uuid.uuid 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]: 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] 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) @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]: 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 [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) # result, dump = self.database.get_object(TopologyModel, topology_uuid, True)
# with self.session() as session: # with self.session() as session:
# devs = None # devs = None
...@@ -265,8 +270,7 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer ...@@ -265,8 +270,7 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
# links.append(session.query(LinkModel).filter_by(**filt).one()) # links.append(session.query(LinkModel).filter_by(**filt).one())
# #
# return Topology(**result.dump(devs, links)) # return Topology(**result.dump(devs, links))
#
#
# @safe_and_metered_rpc_method(METRICS, LOGGER) # @safe_and_metered_rpc_method(METRICS, LOGGER)
# def SetTopology(self, request: Topology, context : grpc.ServicerContext) -> TopologyId: # def SetTopology(self, request: Topology, context : grpc.ServicerContext) -> TopologyId:
# context_uuid = request.topology_id.context_id.context_uuid.uuid # context_uuid = request.topology_id.context_id.context_uuid.uuid
...@@ -300,7 +304,7 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer ...@@ -300,7 +304,7 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
# dict_topology_id = db_topology.dump_id() # dict_topology_id = db_topology.dump_id()
# notify_event(self.messagebroker, TOPIC_TOPOLOGY, event_type, {'topology_id': dict_topology_id}) # notify_event(self.messagebroker, TOPIC_TOPOLOGY, event_type, {'topology_id': dict_topology_id})
# return TopologyId(**dict_topology_id) # return TopologyId(**dict_topology_id)
#
# @safe_and_metered_rpc_method(METRICS, LOGGER) # @safe_and_metered_rpc_method(METRICS, LOGGER)
# def RemoveTopology(self, request: TopologyId, context : grpc.ServicerContext) -> Empty: # def RemoveTopology(self, request: TopologyId, context : grpc.ServicerContext) -> Empty:
# context_uuid = request.context_id.context_uuid.uuid # context_uuid = request.context_id.context_uuid.uuid
...@@ -317,13 +321,12 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer ...@@ -317,13 +321,12 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer
# event_type = EventTypeEnum.EVENTTYPE_REMOVE # event_type = EventTypeEnum.EVENTTYPE_REMOVE
# notify_event(self.messagebroker, TOPIC_TOPOLOGY, event_type, {'topology_id': dict_topology_id}) # notify_event(self.messagebroker, TOPIC_TOPOLOGY, event_type, {'topology_id': dict_topology_id})
# return Empty() # return Empty()
#
## @safe_and_metered_rpc_method(METRICS, LOGGER) # @safe_and_metered_rpc_method(METRICS, LOGGER)
## def GetTopologyEvents(self, request: Empty, context : grpc.ServicerContext) -> Iterator[TopologyEvent]: # def GetTopologyEvents(self, request: Empty, context : grpc.ServicerContext) -> Iterator[TopologyEvent]:
## for message in self.messagebroker.consume({TOPIC_TOPOLOGY}, consume_timeout=CONSUME_TIMEOUT): # for message in self.messagebroker.consume({TOPIC_TOPOLOGY}, consume_timeout=CONSUME_TIMEOUT):
## yield TopologyEvent(**json.loads(message.content)) # yield TopologyEvent(**json.loads(message.content))
#
#
# # ----- Device ----------------------------------------------------------------------------------------------------- # # ----- Device -----------------------------------------------------------------------------------------------------
# #
# @safe_and_metered_rpc_method(METRICS, LOGGER) # @safe_and_metered_rpc_method(METRICS, LOGGER)
......
...@@ -16,8 +16,8 @@ import logging ...@@ -16,8 +16,8 @@ import logging
from typing import Dict from typing import Dict
from sqlalchemy import Column, Float, String from sqlalchemy import Column, Float, String
from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from ._Base import _Base from ._Base import _Base
#from sqlalchemy.orm import relationship
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
...@@ -27,7 +27,7 @@ class ContextModel(_Base): ...@@ -27,7 +27,7 @@ class ContextModel(_Base):
context_name = Column(String(), nullable=False) context_name = Column(String(), nullable=False)
created_at = Column(Float) created_at = Column(Float)
#topology = relationship('TopologyModel', back_populates='context') topology = relationship('TopologyModel', back_populates='context')
def dump_id(self) -> Dict: def dump_id(self) -> Dict:
return {'context_uuid': {'uuid': self.context_uuid}} return {'context_uuid': {'uuid': self.context_uuid}}
...@@ -48,8 +48,13 @@ class ContextModel(_Base): ...@@ -48,8 +48,13 @@ class ContextModel(_Base):
return [TopologyModel(self.database, pk).dump_id() for pk,_ in db_topology_pks] 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} result = {'context_id': self.dump_id(), 'name': self.context_name}
# if include_services: result['service_ids'] = self.dump_service_ids() # 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() # if include_topologies: result['topology_ids'] = self.dump_topology_ids()
return result return result
...@@ -12,21 +12,22 @@ ...@@ -12,21 +12,22 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging, operator import logging #, operator
from typing import Dict, List from typing import Dict #, List
from sqlalchemy.orm import relationship
from sqlalchemy import Column, ForeignKey from sqlalchemy import Column, ForeignKey
from sqlalchemy.dialects.postgresql import UUID 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__) LOGGER = logging.getLogger(__name__)
class TopologyModel(Base): class TopologyModel(_Base):
__tablename__ = 'Topology' __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) topology_uuid = Column(UUID(as_uuid=False), primary_key=True, unique=True)
# Relationships # Relationships
context = relationship("ContextModel", back_populates="topology") context = relationship('ContextModel', back_populates='topology')
def dump_id(self) -> Dict: def dump_id(self) -> Dict:
context_id = self.context.dump_id() context_id = self.context.dump_id()
...@@ -35,16 +36,16 @@ class TopologyModel(Base): ...@@ -35,16 +36,16 @@ class TopologyModel(Base):
'topology_uuid': {'uuid': self.topology_uuid}, 'topology_uuid': {'uuid': self.topology_uuid},
} }
@staticmethod #@staticmethod
def main_pk_name() -> str: #def main_pk_name() -> str:
return 'topology_uuid' # return 'topology_uuid'
def dump( # pylint: disable=arguments-differ def dump(self) -> Dict:
self, devices=None, links=None # pylint: disable=arguments-differ
) -> Dict:
result = {'topology_id': self.dump_id()} result = {'topology_id': self.dump_id()}
if devices: # params: , devices=None, links=None
result['device_ids'] = [device.dump_id() for device in devices] #if devices:
if links: # result['device_ids'] = [device.dump_id() for device in devices]
result['link_ids'] = [link.dump_id() for link in links] #if links:
# result['link_ids'] = [link.dump_id() for link in links]
return result return result
...@@ -14,3 +14,4 @@ ...@@ -14,3 +14,4 @@
from ._Base import _Base, rebuild_database from ._Base import _Base, rebuild_database
from .ContextModel import ContextModel 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