Newer
Older
#
#
# # ----- Connection -------------------------------------------------------------------------------------------------
#
# @safe_and_metered_rpc_method(METRICS, LOGGER)
# def ListConnectionIds(self, request : ServiceId, context : grpc.ServicerContext) -> ConnectionIdList:
# 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)
# def ListConnections(self, request : ContextId, context : grpc.ServicerContext) -> ServiceList:
# 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)
# def GetConnection(self, request : ConnectionId, context : grpc.ServicerContext) -> Connection:
# 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)
# def SetConnection(self, request : Connection, context : grpc.ServicerContext) -> ConnectionId:
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
# 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)
# def RemoveConnection(self, request : ConnectionId, context : grpc.ServicerContext) -> Empty:
# 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)
## def GetConnectionEvents(self, request : Empty, context : grpc.ServicerContext) -> Iterator[ConnectionEvent]:
## 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)
# def ListPolicyRuleIds(self, request : Empty, context: grpc.ServicerContext) -> PolicyRuleIdList:
# 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)
# def ListPolicyRules(self, request : Empty, context: grpc.ServicerContext) -> PolicyRuleList:
# 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)
# def GetPolicyRule(self, request : PolicyRuleId, context: grpc.ServicerContext) -> PolicyRule:
# 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)
# def SetPolicyRule(self, request : PolicyRule, context: grpc.ServicerContext) -> PolicyRuleId:
# 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)
# def RemovePolicyRule(self, request : PolicyRuleId, context: grpc.ServicerContext) -> Empty:
# 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()
#