def test_callback_send_n_get(self): """Check callback for sending DIMSE N-GET messages.""" # N-GET-RQ primitive = N_GET() primitive.MessageID = 1 primitive.RequestedSOPClassUID = '1.1.1' primitive.RequestedSOPInstanceUID = '1.1.1.1' self.dimse.send_msg(primitive, 1) # Plus user defined primitive.AttributeIdentifierList = [(0x0000, 0x0000), (0xffff, 0xffff)] self.dimse.send_msg(primitive, 1) # N-GET-RSP # Mandatory elements primitive = N_GET() primitive.MessageIDBeingRespondedTo = 1 primitive.Status = 0x0000 self.dimse.send_msg(primitive, 1) # User defined primitive.AffectedSOPClassUID = '1.1.1' primitive.AffectedSOPInstanceUID = '1.1.1.1' self.dimse.send_msg(primitive, 1) # Conditional primitive.AttributeList = BytesIO(n_get_rsp_ds) self.dimse.send_msg(primitive, 1)
def test_conversion_rsp(self): """ Check conversion to a -RSP PDU produces the correct output """ primitive = N_GET() primitive.MessageIDBeingRespondedTo = 5 primitive.AffectedSOPClassUID = '1.2.4.10' primitive.AffectedSOPInstanceUID = '1.2.4.5.7.8' primitive.Status = 0x0000 ds = Dataset() ds.PatientID = 'Test1101' ds.PatientName = "Tube HeNe" primitive.AttributeList = BytesIO(encode(ds, True, True)) dimse_msg = N_GET_RSP() dimse_msg.primitive_to_message(primitive) pdvs = [] for fragment in dimse_msg.encode_msg(1, 16382): pdvs.append(fragment) assert len(pdvs) == 2 cs_pdv = pdvs[0].presentation_data_value_list[0][1] ds_pdv = pdvs[1].presentation_data_value_list[0][1] assert cs_pdv == n_get_rsp_cmd assert ds_pdv == n_get_rsp_ds
def test_callback_receive_n_get(self): """Check callback for receiving DIMSE N-GET messages.""" # N-GET-RQ primitive = N_GET() primitive.MessageID = 1 primitive.RequestedSOPClassUID = '1.1.1' primitive.RequestedSOPInstanceUID = '1.1.1.1' msg = N_GET_RQ() msg.primitive_to_message(primitive) msg.context_id = 1 self.dimse.debug_receive_n_get_rq(msg) # Plus user defined primitive.AttributeIdentifierList = [(0x0000, 0x0000), (0xffff, 0xffff)] msg = N_GET_RQ() msg.primitive_to_message(primitive) msg.context_id = 1 self.dimse.debug_receive_n_get_rq(msg) # N-GET-RSP # Mandatory elements primitive = N_GET() primitive.MessageIDBeingRespondedTo = 1 primitive.Status = 0x0000 msg = N_GET_RSP() msg.primitive_to_message(primitive) msg.context_id = 1 self.dimse.debug_receive_n_get_rsp(msg) # User defined primitive.AffectedSOPClassUID = '1.1.1' primitive.AffectedSOPInstanceUID = '1.1.1.1' msg = N_GET_RSP() msg.primitive_to_message(primitive) msg.context_id = 1 self.dimse.debug_receive_n_get_rsp(msg) # Conditional primitive.AttributeList = BytesIO(n_get_rsp_ds) msg = N_GET_RSP() msg.primitive_to_message(primitive) msg.context_id = 1 self.dimse.debug_receive_n_get_rsp(msg)
def SCP(self, req, context, info): """The implementation for the DIMSE N-GET service. Parameters ---------- req : dimse_primitives.C_ECHO The N-GET request primitive sent by the peer. context : presentation.PresentationContext The presentation context that the service is operating under. info : dict A dict containing details about the association. See Also -------- ae.ApplicationEntity.on_n_get association.Association.send_n_get Notes ----- **N-GET Request** *Parameters* | (M) Message ID | (M) Requested SOP Class UID | (M) Requested SOP Instance UID | (U) Attribute Identifier List *Attribute Identifier List* An element with VR AT, VM 1-n, containing an attribute tag for each of the attributes applicable to the N-GET operation. **N-GET Response** *Parameters* | (M) Message ID Being Responded To | (U) Affected SOP Class UID | (U) Affected SOP Instance UID | (C) Attribute List | (M) Status *Attribute List* A dataset containing the values of the requested attributes. References ---------- * DICOM Standard, Part 4, `Annex EE <http://dicom.nema.org/medical/dicom/current/output/html/part04.html#chapter_EE>`_ * DICOM Standard, Part 7, Sections `10.1.2 <http://dicom.nema.org/medical/dicom/current/output/html/part07.html#sect_10.1.2>`_, `10.3.2 <http://dicom.nema.org/medical/dicom/current/output/html/part07.html#sect_10.3.2>`_ and `Annex C <http://dicom.nema.org/medical/dicom/current/output/html/part07.html#chapter_C>`_ """ # Build N-GET response primitive rsp = N_GET() rsp.MessageIDBeingRespondedTo = req.MessageID rsp.AffectedSOPClassUID = req.RequestedSOPClassUID rsp.AffectedSOPInstanceUID = req.RequestedSOPInstanceUID default_handler = evt.get_default_handler(evt.EVT_N_GET) if self.assoc.get_handlers(evt.EVT_N_GET) != default_handler: try: (rsp_status, ds) = evt.trigger(self.assoc, evt.EVT_N_GET, { 'request': req, 'context': context.as_tuple, }) except Exception as exc: LOGGER.error( "Exception in the handler bound to 'evt.EVT_N_GET'") LOGGER.exception(exc) # Processing failure - Error in on_n_get callback rsp_status = 0x0110 else: info['parameters'] = { 'message_id': req.MessageID, 'requested_sop_class_uid': req.RequestedSOPClassUID, 'requested_sop_instance_uid': req.RequestedSOPInstanceUID, } # Attempt to run the ApplicationEntity's on_n_get callback # pylint: disable=broad-except try: # Send the value rather than the element (rsp_status, ds) = self.ae.on_n_get(req.AttributeIdentifierList, context.as_tuple, info) except Exception as exc: LOGGER.error( "Exception in the ApplicationEntity.on_n_get() callback") LOGGER.exception(exc) # Processing failure - Error in on_n_get callback rsp_status = 0x0110 # Validate rsp_status and set rsp.Status accordingly rsp = self.validate_status(rsp_status, rsp) # Success or Warning, must return AttributeList dataset if code_to_category(rsp.Status) in [STATUS_SUCCESS, STATUS_WARNING]: # Encode the `dataset` using the agreed transfer syntax # Will return None if failed to encode transfer_syntax = context.transfer_syntax[0] bytestream = encode(ds, transfer_syntax.is_implicit_VR, transfer_syntax.is_little_endian) if bytestream is not None: rsp.AttributeList = BytesIO(bytestream) else: LOGGER.error("Failed to encode the supplied Dataset") # Processing failure - Failed to encode dataset rsp.Status = 0x0110 self.dimse.send_msg(rsp, context.context_id)
def _get_scp(self, req, context): """ Parameters ---------- req : dimse_primitives.N_GET The N-GET request primitive sent by the peer. context : presentation.PresentationContext The presentation context that the service is operating under. See Also -------- association.Association.send_n_get Notes ----- **N-GET Request** *Parameters* | (M) Message ID | (M) Requested SOP Class UID | (M) Requested SOP Instance UID | (U) Attribute Identifier List *Attribute Identifier List* An element with VR AT, VM 1-n, containing an attribute tag for each of the attributes applicable to the N-GET operation. **N-GET Response** *Parameters* | (M) Message ID Being Responded To | (U) Affected SOP Class UID | (U) Affected SOP Instance UID | (C) Attribute List | (M) Status *Attribute List* A dataset containing the values of the requested attributes. *Status* Success | ``0x0000`` - Success Failure | ``0x0107`` - SOP Class not supported | ``0x0110`` - Processing failure | ``0x0112`` - No such SOP Instance | ``0x0117`` - Invalid object instance | ``0x0118`` - No such SOP Class | ``0x0119`` - Class-Instance conflict | ``0x0122`` - SOP Class not supported | ``0x0124`` - Refused: not authorised | ``0x0210`` - Duplicate invocation | ``0x0211`` - Unrecognised operation | ``0x0212`` - Mistyped argument | ``0x0213`` - Resource limitation References ---------- * DICOM Standard, Part 4, `Annex EE <http://dicom.nema.org/medical/dicom/current/output/html/part04.html#chapter_EE>`_ * DICOM Standard, Part 7, Sections `10.1.2 <http://dicom.nema.org/medical/dicom/current/output/html/part07.html#sect_10.1.2>`_, `10.3.2 <http://dicom.nema.org/medical/dicom/current/output/html/part07.html#sect_10.3.2>`_ and `Annex C <http://dicom.nema.org/medical/dicom/current/output/html/part07.html#chapter_C>`_ """ # Build N-GET response primitive rsp = N_GET() rsp.MessageIDBeingRespondedTo = req.MessageID rsp.AffectedSOPClassUID = req.RequestedSOPClassUID rsp.AffectedSOPInstanceUID = req.RequestedSOPInstanceUID try: status, ds = evt.trigger( self.assoc, evt.EVT_N_GET, { 'request' : req, 'context' : context.as_tuple, } ) except Exception as exc: LOGGER.error( "Exception in the handler bound to 'evt.EVT_N_GET'" ) LOGGER.exception(exc) # Processing failure - Error in handler rsp.Status = 0x0110 self.dimse.send_msg(rsp, context.context_id) return # Validate rsp_status and set rsp.Status accordingly rsp = self.validate_status(status, rsp) # Success or Warning, must return AttributeList dataset if code_to_category(rsp.Status) in [STATUS_SUCCESS, STATUS_WARNING]: # Encode the `dataset` using the agreed transfer syntax # Will return None if failed to encode transfer_syntax = context.transfer_syntax[0] bytestream = encode(ds, transfer_syntax.is_implicit_VR, transfer_syntax.is_little_endian) if bytestream is not None: rsp.AttributeList = BytesIO(bytestream) else: LOGGER.error("Failed to encode the supplied Dataset") # Processing failure - Failed to encode dataset rsp.Status = 0x0110 self.dimse.send_msg(rsp, context.context_id)
def test_exceptions(self): """ Check incorrect types/values for properties raise exceptions """ primitive = N_GET() # MessageID with pytest.raises(TypeError): primitive.MessageID = 'halp' with pytest.raises(TypeError): primitive.MessageID = 1.111 with pytest.raises(ValueError): primitive.MessageID = 65536 with pytest.raises(ValueError): primitive.MessageID = -1 # MessageIDBeingRespondedTo with pytest.raises(TypeError): primitive.MessageIDBeingRespondedTo = 'halp' with pytest.raises(TypeError): primitive.MessageIDBeingRespondedTo = 1.111 with pytest.raises(ValueError): primitive.MessageIDBeingRespondedTo = 65536 with pytest.raises(ValueError): primitive.MessageIDBeingRespondedTo = -1 # AffectedSOPClassUID with pytest.raises(TypeError): primitive.AffectedSOPClassUID = 45.2 with pytest.raises(TypeError): primitive.AffectedSOPClassUID = 100 with pytest.raises(ValueError): primitive.AffectedSOPClassUID = 'abc' # AffectedSOPInstanceUID with pytest.raises(TypeError): primitive.AffectedSOPInstanceUID = 45.2 with pytest.raises(TypeError): primitive.AffectedSOPInstanceUID = 100 with pytest.raises(ValueError): primitive.AffectedSOPInstanceUID = 'abc' # RequestedSOPClassUID with pytest.raises(TypeError): primitive.RequestedSOPClassUID = 45.2 with pytest.raises(TypeError): primitive.RequestedSOPClassUID = 100 with pytest.raises(ValueError): primitive.RequestedSOPClassUID = 'abc' # RequestedSOPInstanceUID with pytest.raises(TypeError): primitive.RequestedSOPInstanceUID = 45.2 with pytest.raises(TypeError): primitive.RequestedSOPInstanceUID = 100 with pytest.raises(ValueError): primitive.RequestedSOPInstanceUID = 'abc' # AttributeIdentifierList with pytest.raises(ValueError): primitive.AttributeIdentifierList = Tag(0x0000, 0x0000) with pytest.raises(ValueError, match="Attribute Identifier List must"): primitive.AttributeIdentifierList = ['ijk', 'abc'] with pytest.raises(ValueError, match="Attribute Identifier List must"): primitive.AttributeIdentifierList = [] # AttributeList msg = r"'AttributeList' parameter must be a BytesIO object" with pytest.raises(TypeError, match=msg): primitive.AttributeList = 'halp' with pytest.raises(TypeError): primitive.AttributeList = 1.111 with pytest.raises(TypeError): primitive.AttributeList = 50 with pytest.raises(TypeError): primitive.AttributeList = [30, 10] # Status with pytest.raises(TypeError): primitive.Status = 19.4
def test_assignment(self): """Check assignment works correctly""" primitive = N_GET() # AffectedSOPClassUID primitive.AffectedSOPClassUID = '1.1.1' assert primitive.AffectedSOPClassUID == UID('1.1.1') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.AffectedSOPClassUID = UID('1.1.2') assert primitive.AffectedSOPClassUID == UID('1.1.2') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.AffectedSOPClassUID = b'1.1.3' assert primitive.AffectedSOPClassUID == UID('1.1.3') assert isinstance(primitive.AffectedSOPClassUID, UID) # AffectedSOPInstanceUID primitive.AffectedSOPInstanceUID = b'1.2.1' assert primitive.AffectedSOPInstanceUID == UID('1.2.1') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.AffectedSOPInstanceUID = UID('1.2.2') assert primitive.AffectedSOPInstanceUID == UID('1.2.2') assert isinstance(primitive.AffectedSOPClassUID, UID) primitive.AffectedSOPInstanceUID = '1.2.3' assert primitive.AffectedSOPInstanceUID == UID('1.2.3') assert isinstance(primitive.AffectedSOPClassUID, UID) # AttributeList ref_ds = Dataset() ref_ds.PatientID = '1234567' primitive.AttributeList = BytesIO(encode(ref_ds, True, True)) ds = decode(primitive.AttributeList, True, True) assert ds.PatientID == '1234567' # AttributeIdentifierList primitive.AttributeIdentifierList = [ 0x00001000, (0x0000, 0x1000), Tag(0x7fe0, 0x0010) ] assert [Tag(0x0000, 0x1000), Tag(0x0000, 0x1000), Tag(0x7fe0, 0x0010)] == primitive.AttributeIdentifierList primitive.AttributeIdentifierList = [(0x7fe0, 0x0010)] assert [Tag(0x7fe0, 0x0010)] == primitive.AttributeIdentifierList primitive.AttributeIdentifierList = (0x7fe0, 0x0010) assert [Tag(0x7fe0, 0x0010)] == primitive.AttributeIdentifierList elem = DataElement((0x0000, 0x0005), 'AT', [Tag(0x0000, 0x1000)]) assert isinstance(elem.value, MutableSequence) primitive.AttributeIdentifierList = elem.value assert [Tag(0x0000, 0x1000)] == primitive.AttributeIdentifierList # MessageID primitive.MessageID = 11 assert 11 == primitive.MessageID # MessageIDBeingRespondedTo primitive.MessageIDBeingRespondedTo = 13 assert 13 == primitive.MessageIDBeingRespondedTo # RequestedSOPClassUID primitive.RequestedSOPClassUID = '1.1.1' assert primitive.RequestedSOPClassUID == UID('1.1.1') assert isinstance(primitive.RequestedSOPClassUID, UID) primitive.RequestedSOPClassUID = UID('1.1.2') assert primitive.RequestedSOPClassUID == UID('1.1.2') assert isinstance(primitive.RequestedSOPClassUID, UID) primitive.RequestedSOPClassUID = b'1.1.3' assert primitive.RequestedSOPClassUID == UID('1.1.3') assert isinstance(primitive.RequestedSOPClassUID, UID) # RequestedSOPInstanceUID primitive.RequestedSOPInstanceUID = b'1.2.1' assert primitive.RequestedSOPInstanceUID == UID('1.2.1') assert isinstance(primitive.RequestedSOPInstanceUID, UID) primitive.RequestedSOPInstanceUID = UID('1.2.2') assert primitive.RequestedSOPInstanceUID == UID('1.2.2') assert isinstance(primitive.RequestedSOPInstanceUID, UID) primitive.RequestedSOPInstanceUID = '1.2.3' assert primitive.RequestedSOPInstanceUID == UID('1.2.3') assert isinstance(primitive.RequestedSOPInstanceUID, UID) # Status primitive.Status = 0x0000 assert primitive.Status == 0x0000