Skip to content
Snippets Groups Projects
ContextServiceServicerImpl.py 64.6 KiB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#
#
#    # ----- Connection -------------------------------------------------------------------------------------------------
#
#    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#    def ListConnectionIds(self, request : ServiceId, context : grpc.ServicerContext) -> ConnectionIdList:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#        with self.session() as session:
#            result = session.query(DeviceModel).all()
#            return DeviceIdList(device_ids=[device.dump_id() for device in result])
#
#        with self.lock:
#            str_key = key_to_str([request.context_id.context_uuid.uuid, request.service_uuid.uuid])
#            db_service : ServiceModel = get_object(self.database, ServiceModel, str_key)
#            db_connections : Set[ConnectionModel] = get_related_objects(db_service, ConnectionModel)
#            db_connections = sorted(db_connections, key=operator.attrgetter('pk'))
#            return ConnectionIdList(connection_ids=[db_connection.dump_id() for db_connection in db_connections])
#
#    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#    def ListConnections(self, request : ContextId, context : grpc.ServicerContext) -> ServiceList:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#        with self.lock:
#            str_key = key_to_str([request.context_id.context_uuid.uuid, request.service_uuid.uuid])
#            db_service : ServiceModel = get_object(self.database, ServiceModel, str_key)
#            db_connections : Set[ConnectionModel] = get_related_objects(db_service, ConnectionModel)
#            db_connections = sorted(db_connections, key=operator.attrgetter('pk'))
#            return ConnectionList(connections=[db_connection.dump() for db_connection in db_connections])
#
#    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#    def GetConnection(self, request : ConnectionId, context : grpc.ServicerContext) -> Connection:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#        with self.lock:
#            db_connection : ConnectionModel = get_object(self.database, ConnectionModel, request.connection_uuid.uuid)
#            return Connection(**db_connection.dump(include_path=True, include_sub_service_ids=True))
#
#    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#    def SetConnection(self, request : Connection, context : grpc.ServicerContext) -> ConnectionId:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#        with self.lock:
#            connection_uuid = request.connection_id.connection_uuid.uuid
#
#            connection_attributes = {'connection_uuid': connection_uuid}
#
#            service_context_uuid = request.service_id.context_id.context_uuid.uuid
#            service_uuid = request.service_id.service_uuid.uuid
#            if len(service_context_uuid) > 0 and len(service_uuid) > 0:
#                str_service_key = key_to_str([service_context_uuid, service_uuid])
#                db_service : ServiceModel = get_object(self.database, ServiceModel, str_service_key)
#                connection_attributes['service_fk'] = db_service
#
#            path_hops_result = set_path(self.database, connection_uuid, request.path_hops_endpoint_ids, path_name = '')
#            db_path = path_hops_result[0]
#            connection_attributes['path_fk'] = db_path
#
#            result : Tuple[ConnectionModel, bool] = update_or_create_object(
#                self.database, ConnectionModel, connection_uuid, connection_attributes)
#            db_connection, updated = result
#
#            for sub_service_id in request.sub_service_ids:
#                sub_service_uuid         = sub_service_id.service_uuid.uuid
#                sub_service_context_uuid = sub_service_id.context_id.context_uuid.uuid
#                str_sub_service_key = key_to_str([sub_service_context_uuid, sub_service_uuid])
#                db_service : ServiceModel = get_object(self.database, ServiceModel, str_sub_service_key)
#
#                str_connection_sub_service_key = key_to_str([connection_uuid, str_sub_service_key], separator='--')
#                result : Tuple[ConnectionSubServiceModel, bool] = get_or_create_object(
#                    self.database, ConnectionSubServiceModel, str_connection_sub_service_key, {
#                        'connection_fk': db_connection, 'sub_service_fk': db_service})
#                #db_connection_sub_service, connection_sub_service_created = result
#
#            event_type = EventTypeEnum.EVENTTYPE_UPDATE if updated else EventTypeEnum.EVENTTYPE_CREATE
#            dict_connection_id = db_connection.dump_id()
#            notify_event(self.messagebroker, TOPIC_CONNECTION, event_type, {'connection_id': dict_connection_id})
#            return ConnectionId(**dict_connection_id)
#
#    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#    def RemoveConnection(self, request : ConnectionId, context : grpc.ServicerContext) -> Empty:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#        with self.lock:
#            db_connection = ConnectionModel(self.database, request.connection_uuid.uuid, auto_load=False)
#            found = db_connection.load()
#            if not found: return Empty()
#
#            dict_connection_id = db_connection.dump_id()
#            db_connection.delete()
#
#            event_type = EventTypeEnum.EVENTTYPE_REMOVE
#            notify_event(self.messagebroker, TOPIC_CONNECTION, event_type, {'connection_id': dict_connection_id})
#            return Empty()
#
##    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
##    def GetConnectionEvents(self, request : Empty, context : grpc.ServicerContext) -> Iterator[ConnectionEvent]:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
##        for message in self.messagebroker.consume({TOPIC_CONNECTION}, consume_timeout=CONSUME_TIMEOUT):
##            yield ConnectionEvent(**json.loads(message.content))
#
#
#    # ----- Policy -----------------------------------------------------------------------------------------------------
#
#    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#    def ListPolicyRuleIds(self, request : Empty, context: grpc.ServicerContext) -> PolicyRuleIdList:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#        with self.lock:
#            db_policy_rules: List[PolicyRuleModel] = get_all_objects(self.database, PolicyRuleModel)
#            db_policy_rules = sorted(db_policy_rules, key=operator.attrgetter('pk'))
#            return PolicyRuleIdList(policyRuleIdList=[db_policy_rule.dump_id() for db_policy_rule in db_policy_rules])
#
#    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#    def ListPolicyRules(self, request : Empty, context: grpc.ServicerContext) -> PolicyRuleList:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#        with self.lock:
#            db_policy_rules: List[PolicyRuleModel] = get_all_objects(self.database, PolicyRuleModel)
#            db_policy_rules = sorted(db_policy_rules, key=operator.attrgetter('pk'))
#            return PolicyRuleList(policyRules=[db_policy_rule.dump() for db_policy_rule in db_policy_rules])
#
#    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#    def GetPolicyRule(self, request : PolicyRuleId, context: grpc.ServicerContext) -> PolicyRule:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#        with self.lock:
#            policy_rule_uuid = request.uuid.uuid
#            db_policy_rule: PolicyRuleModel = get_object(self.database, PolicyRuleModel, policy_rule_uuid)
#            return PolicyRule(**db_policy_rule.dump())
#
#    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#    def SetPolicyRule(self, request : PolicyRule, context: grpc.ServicerContext) -> PolicyRuleId:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#        with self.lock:
#            policy_rule_type = request.WhichOneof('policy_rule')
#            policy_rule_json = grpc_message_to_json(request)
#            policy_rule_uuid = policy_rule_json[policy_rule_type]['policyRuleBasic']['policyRuleId']['uuid']['uuid']
#            result: Tuple[PolicyRuleModel, bool] = update_or_create_object(
#                self.database, PolicyRuleModel, policy_rule_uuid, {'value': json.dumps(policy_rule_json)})
#            db_policy, updated = result # pylint: disable=unused-variable
#
#            #event_type = EventTypeEnum.EVENTTYPE_UPDATE if updated else EventTypeEnum.EVENTTYPE_CREATE
#            dict_policy_id = db_policy.dump_id()
#            #notify_event(self.messagebroker, TOPIC_POLICY, event_type, {"policy_id": dict_policy_id})
#            return PolicyRuleId(**dict_policy_id)
#
#    @safe_and_metered_rpc_method(METRICS, LOGGER)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#    def RemovePolicyRule(self, request : PolicyRuleId, context: grpc.ServicerContext) -> Empty:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#        with self.lock:
#            policy_uuid = request.uuid.uuid
#            db_policy = PolicyRuleModel(self.database, policy_uuid, auto_load=False)
#            found = db_policy.load()
#            if not found: return Empty()
#
#            dict_policy_id = db_policy.dump_id()
#            db_policy.delete()
#            #event_type = EventTypeEnum.EVENTTYPE_REMOVE
#            #notify_event(self.messagebroker, TOPIC_POLICY, event_type, {"policy_id": dict_policy_id})
#            return Empty()
#