-
Notifications
You must be signed in to change notification settings - Fork 174
/
pdu_primitives.py
2125 lines (1750 loc) · 71.1 KB
/
pdu_primitives.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Implementation of the service parameter primitives.
"""
import logging
from typing import Optional, List, Any, Union, Tuple, cast, Type
from pydicom.uid import UID
from pynetdicom._globals import OptionalUIDType
from pynetdicom.pdu_items import (
MaximumLengthSubItem,
ImplementationClassUIDSubItem,
ImplementationVersionNameSubItem,
AsynchronousOperationsWindowSubItem,
SCP_SCU_RoleSelectionSubItem,
SOPClassExtendedNegotiationSubItem,
SOPClassCommonExtendedNegotiationSubItem,
UserIdentitySubItemRQ,
UserIdentitySubItemAC,
)
from pynetdicom.presentation import PresentationContext
from pynetdicom.utils import validate_uid, decode_bytes, set_ae, set_uid
from pynetdicom._globals import DEFAULT_MAX_LENGTH
LOGGER = logging.getLogger(__name__)
_PDUPrimitiveType = Union["A_ASSOCIATE", "A_RELEASE", "A_ABORT", "A_P_ABORT", "P_DATA"]
_UserInformationPrimitiveType = List[
Union[
"MaximumLengthNotification",
"ImplementationClassUIDNotification",
"ImplementationVersionNameNotification",
"AsynchronousOperationsWindowNegotiation",
"SCP_SCU_RoleSelectionNegotiation",
"SOPClassExtendedNegotiation",
"UserIdentityNegotiation",
"SOPClassCommonExtendedNegotiation",
]
]
_UI = Union[
"MaximumLengthNotification",
"ImplementationClassUIDNotification",
"ImplementationVersionNameNotification",
"AsynchronousOperationsWindowNegotiation",
"SCP_SCU_RoleSelectionNegotiation",
"SOPClassExtendedNegotiation",
"SOPClassCommonExtendedNegotiation",
"UserIdentityNegotiation",
]
_UITypes = Union[
Type["MaximumLengthNotification"],
Type["ImplementationClassUIDNotification"],
Type["ImplementationVersionNameNotification"],
Type["AsynchronousOperationsWindowNegotiation"],
Type["SCP_SCU_RoleSelectionNegotiation"],
Type["SOPClassExtendedNegotiation"],
Type["SOPClassCommonExtendedNegotiation"],
Type["UserIdentityNegotiation"],
]
# TODO: Rename to UserInformation
class ServiceParameter:
"""Base class for Service Parameters"""
def __eq__(self, other: Any) -> bool:
"""Equality of two ServiceParameters"""
if isinstance(other, self.__class__):
return other.__dict__ == self.__dict__
return False
def __ne__(self, other: Any) -> bool:
"""Inequality of two ServiceParameters"""
return not self == other
# Association Service primitives
class A_ASSOCIATE:
"""An A-ASSOCIATE primitive.
The establishment of an association between two AEs shall be performed
through ACSE A-ASSOCIATE request, indication, response and confirmation
primitives.
The initiator of the service is called the Requestor and the user that
receives the request is the Acceptor.
The A-ASSOCIATE primitive is used by the DUL provider to send/receive
information about the association. It gets converted to
A-ASSOCIATE-RQ, -AC, -RJ PDUs that are sent to the peer DUL provider and
gets deconverted from -RQ, -AC, -RJ PDUs received from the peer.
+------------------+---------+------------+----------+--------------+
| Parameter | Request | Indication | Response | Confirmation |
+------------------+---------+------------+----------+--------------+
| app context name | M | M(=) | M | M(=) |
+------------------+---------+------------+----------+--------------+
| calling ae title | M | M(=) | M | M(=) |
+------------------+---------+------------+----------+--------------+
| called ae title | M | M(=) | M | M(=) |
+------------------+---------+------------+----------+--------------+
| user info | M | M(=) | M | M(=) |
+------------------+---------+------------+----------+--------------+
| result | | | M | M(=) |
+------------------+---------+------------+----------+--------------+
| source | | | | M |
+------------------+---------+------------+----------+--------------+
| diagnostic | | | U | C(=) |
+------------------+---------+------------+----------+--------------+
| calling pres add | M | M(=) | | |
+------------------+---------+------------+----------+--------------+
| called pres add | M | M(=) | | |
+------------------+---------+------------+----------+--------------+
| pres contxt list | M | M(=) | | |
+------------------+---------+------------+----------+--------------+
| pres list result | | | M | M(=) |
+------------------+---------+------------+----------+--------------+
| mode | UF | MF(=) | | |
+------------------+---------+------------+----------+--------------+
| resp ae title | | | MF | MF(=) |
+------------------+---------+------------+----------+--------------+
| resp pres add | | | MF | MF(=) |
+------------------+---------+------------+----------+--------------+
| pres/sess req | UF | UF(=) | UF | UF(=) |
+------------------+---------+------------+----------+--------------+
| U - User option
| UF - User option, fixed value
| C - Conditional (on user option)
| M - Mandatory
| MF - Mandatory, fixed value
| NU - Not used
| (=) - shall have same value as request or response
References
----------
* DICOM Standard, Part 8,
:dcm:`Section 7.1.1<part08/chapter_7.html#sect_7.1.1>`
"""
# pylint: disable=too-many-instance-attributes
def __init__(self) -> None:
self._application_context_name: Optional[UID] = None
self._calling_ae_title: str = "DEFAULT"
self._called_ae_title: str = "DEFAULT"
self._user_information: _UserInformationPrimitiveType = []
self._result: Optional[int] = None
self._result_source: Optional[int] = None
self._diagnostic: Optional[int] = None
self._calling_presentation_address: Optional[Tuple[str, int]] = None
self._called_presentation_address: Optional[Tuple[str, int]] = None
self._presentation_context_definition_list: List[PresentationContext] = []
self._presentation_context_definition_results_list: List[
PresentationContext
] = []
@property
def application_context_name(self) -> Optional[UID]:
"""Get or set the *Application Context Name* parameter.
Parameters
----------
value : pydicom.uid.UID, bytes or str
The application context name proposed by the *Requestor*.
*Acceptor* returns either the same or a different name. Returned
name specifies the application context used for the association.
See the DICOM Standard, Part 8, :dcm:`Annex A
<part08/chapter_A.html>`. The application context name shall
be a valid UID or UID string and for version 3 of the DICOM
Standard should be ``'1.2.840.10008.3.1.1.1'``
Returns
-------
pydicom.uid.UID
"""
return self._application_context_name
@application_context_name.setter
def application_context_name(self, value: OptionalUIDType) -> None:
"""Set the Application Context Name parameter."""
self._application_context_name = set_uid(value, "Application Context Name")
@property
def called_ae_title(self) -> str:
"""Get or set the *Called AE Title* parameter.
Parameters
----------
value : str or bytes
The Called AE Title as a string or bytes object. Cannot be an empty
string and will be truncated to 16 characters long
Returns
-------
bytes
"""
return self._called_ae_title
@called_ae_title.setter
def called_ae_title(self, value: str) -> None:
"""Set the Called AE Title parameter."""
self._called_ae_title = cast(
str, set_ae(value, "Called AE Title", False, False)
)
@property
def called_presentation_address(self) -> Optional[Tuple[str, int]]:
"""Get or set the *Called Presentation Address* parameter.
Parameters
----------
value : (str, int) tuple
A tuple containing a valid TCP/IP address string and the port
number as an int
"""
return self._called_presentation_address
@called_presentation_address.setter
def called_presentation_address(self, value: Optional[Tuple[str, int]]) -> None:
"""Set the Called Presentation Address parameter."""
# pylint: disable=attribute-defined-outside-init
if isinstance(value, tuple):
if (
len(value) == 2
and isinstance(value[0], str)
and isinstance(value[1], int)
):
self._called_presentation_address = value
else:
LOGGER.error(
"A_ASSOCIATE.called_presentation_address must "
"be (str, int) tuple"
)
raise TypeError(
"A_ASSOCIATE.called_presentation_address "
"must be (str, int) tuple"
)
elif value is None:
self._called_presentation_address = value
else:
LOGGER.error(
"A_ASSOCIATE.called_presentation_address must be (str, int) tuple"
)
raise TypeError(
"A_ASSOCIATE.called_presentation_address must be (str, int) tuple"
)
@property
def calling_ae_title(self) -> str:
"""Get or set the *Calling AE Title* parameter.
Parameters
----------
value : str or bytes
The Calling AE Title as a string or bytes object. Cannot be an
empty string and will be truncated to 16 characters long.
Returns
-------
bytes
"""
return self._calling_ae_title
@calling_ae_title.setter
def calling_ae_title(self, value: str) -> None:
"""Set the Calling AE Title parameter."""
self._calling_ae_title = cast(
str, set_ae(value, "Calling AE Title", False, False)
)
@property
def calling_presentation_address(self) -> Optional[Tuple[str, int]]:
"""Get or set the *Calling Presentation Address* parameter.
Parameters
----------
value : (str, int) tuple
A tuple containing a valid TCP/IP address string and the port
number as an int
"""
return self._calling_presentation_address
@calling_presentation_address.setter
def calling_presentation_address(self, value: Optional[Tuple[str, int]]) -> None:
"""Set the A-ASSOCIATE Service primitive's Calling Presentation
Address parameter.
"""
# pylint: disable=attribute-defined-outside-init
if isinstance(value, tuple):
if (
len(value) == 2
and isinstance(value[0], str)
and isinstance(value[1], int)
):
self._calling_presentation_address = value
else:
LOGGER.error(
"A_ASSOCIATE.calling_presentation_address must "
"be (str, int) tuple"
)
raise TypeError(
"A_ASSOCIATE.calling_presentation_address "
"must be (str, int) tuple"
)
elif value is None:
self._calling_presentation_address = value
else:
LOGGER.error(
"A_ASSOCIATE.calling_presentation_address must be (str, int) tuple"
)
raise TypeError(
"A_ASSOCIATE.calling_presentation_address must be (str, int) tuple"
)
@property
def diagnostic(self) -> Optional[int]:
"""Get or set the *Diagnostic* parameter.
Parameters
----------
value : int
If `result_source` is "UL service-user" then allowed values are:
* 1: no reason given
* 2: application context name not supported
* 3: calling AE title not recognised
* 7: called AE title not recognised
If `result_source` is "UL service-provider (ACSE related function)"
then allowed values are:
* 1: no reason given
* 2: protocol version not supported"
If `result_source` is "UL service-provider (Presentation related
function)" then allowed values are:
* 1: temporary congestion
* 2: local limit exceeded
"""
return self._diagnostic
@diagnostic.setter
def diagnostic(self, value: Optional[int]) -> None:
"""
Set the A-ASSOCIATE Service primitive's Diagnostic parameter."""
# pylint: disable=attribute-defined-outside-init
if value is None:
pass
elif value not in [1, 2, 3, 7]:
LOGGER.error("A_ASSOCIATE.diagnostic set to an unknown value")
raise ValueError("Unknown A_ASSOCIATE.diagnostic value")
self._diagnostic = value
@property
def implementation_class_uid(self) -> Optional[UID]:
"""Get or set the *Implementation Class UID* as
:class:`~pydicom.uid.UID`.
If the A_ASSOCIATE.user_information list contains an
ImplementationClassUIDNotification item then set its
implementation_class_uid value. If not then add a
ImplementationClassUIDNotification item and set its
implementation_class_uid value.
Parameters
----------
value : pydicom.uid.UID, bytes or str
The value for the Implementation Class UID
"""
for item in self.user_information:
if isinstance(item, ImplementationClassUIDNotification):
if item.implementation_class_uid is None:
LOGGER.error("Implementation Class UID has not been set")
raise ValueError("Implementation Class UID has not been set")
return item.implementation_class_uid
LOGGER.error("Implementation Class UID has not been set")
raise ValueError("Implementation Class UID has not been set")
@implementation_class_uid.setter
def implementation_class_uid(self, value: OptionalUIDType) -> None:
"""Set the Implementation Class UID."""
# Type and value checking for the implementation_class_uid parameter is
# done by the ImplementationClassUIDNotification class
# Check for a ImplementationClassUIDNotification item
found_item = False
for item in self.user_information:
if isinstance(item, ImplementationClassUIDNotification):
found_item = True
item.implementation_class_uid = value # type: ignore
# No ImplementationClassUIDNotification item found
if not found_item:
imp_uid = ImplementationClassUIDNotification()
imp_uid.implementation_class_uid = value # type: ignore
self.user_information.append(imp_uid)
@property
def maximum_length_received(self) -> Optional[int]:
"""Get or set the *Maximum Length Received* as :class:`int`.
If the A_ASSOCIATE.user_information list contains a
MaximumLengthNotification item then set its maximum_length_received
value. If not then add a MaximumLengthNotification item and set its
maximum_length_received value.
Parameters
----------
value : int
The maximum length of each P-DATA in bytes
"""
for item in self.user_information:
if isinstance(item, MaximumLengthNotification):
return item.maximum_length_received
return None
@maximum_length_received.setter
def maximum_length_received(self, value: int) -> None:
"""Set the Maximum Length Received."""
# Type and value checking for the maximum_length_received parameter is
# done by the MaximumLengthNotification class
# Check for a MaximumLengthNotification item
found_item = False
for item in self.user_information:
if isinstance(item, MaximumLengthNotification):
found_item = True
item.maximum_length_received = value
# No MaximumLengthNotification item found
if not found_item:
max_length = MaximumLengthNotification()
max_length.maximum_length_received = value
self.user_information.append(max_length)
@property
def mode(self) -> str:
"""Return the *Mode* parameter as :class:`str`."""
return "normal"
@property
def presentation_context_definition_list(self) -> List[PresentationContext]:
"""Get or set the *Presentation Context Definition List*.
Parameters
----------
value_list : list of presentation.PresentationContext
The Presentation Contexts proposed by the Association Requestor
"""
return self._presentation_context_definition_list
@presentation_context_definition_list.setter
def presentation_context_definition_list(
self, value_list: List[PresentationContext]
) -> None:
"""Set the A-ASSOCIATE Service primitive's Presentation Context
Definition List parameter.
"""
# pylint: disable=attribute-defined-outside-init
if isinstance(value_list, list):
valid_items = []
for item in value_list:
if isinstance(item, PresentationContext):
valid_items.append(item)
else:
LOGGER.warning(
"Attempted to set "
"A_ASSOCIATE.presentation_context_definition_list to "
"a list which includes an invalid items"
)
self._presentation_context_definition_list = valid_items
else:
LOGGER.error(
"A_ASSOCIATE.presentation_context_definition_list must be a list"
)
raise TypeError(
"A_ASSOCIATE.presentation_context_definition_list must be a list"
)
@property
def presentation_context_definition_results_list(self) -> List[PresentationContext]:
"""Get or set the *Presentation Context Definition Results List*.
Parameters
----------
value_list : list of presentation.PresentationContext
The results of the Presentation Contexts proposal by the
Association Requestor
"""
return self._presentation_context_definition_results_list
@presentation_context_definition_results_list.setter
def presentation_context_definition_results_list(
self, value_list: List[PresentationContext]
) -> None:
"""Set the Presentation Context Definition Results List parameter."""
# pylint: disable=attribute-defined-outside-init
if isinstance(value_list, list):
valid_items = []
for item in value_list:
if isinstance(item, PresentationContext):
valid_items.append(item)
else:
LOGGER.warning(
"Attempted to set A_ASSOCIATE.presentation"
"_context_definition_results_list to a "
"list which includes one or more invalid "
"items"
)
self._presentation_context_definition_results_list = valid_items
else:
LOGGER.error(
"A_ASSOCIATE.presentation_context_definition_"
"results_list must be a list"
)
raise TypeError(
"A_ASSOCIATE.presentation_context_definition_"
"results_list must be a list"
)
@property
def presentation_requirements(self) -> str:
"""Return the *Presentation Kernel* as :class:`str`."""
return "Presentation Kernel"
@property
def reason_str(self) -> str:
"""Return the rejection reason as str."""
reasons = {
1: {
1: "No reason given",
2: "Application context name not supported",
3: "Calling AE title not recognised",
4: "Reserved",
5: "Reserved",
6: "Reserved",
7: "Called AE title not recognised",
8: "Reserved",
9: "Reserved",
10: "Reserved",
},
2: {1: "No reason given", 2: "Protocol version not supported"},
3: {
0: "Reserved",
1: "Temporary congestion",
2: "Local limit exceeded",
3: "Reserved",
4: "Reserved",
5: "Reserved",
6: "Reserved",
7: "Reserved",
},
}
result = cast(int, self.result_source)
diagnostic = cast(int, self.diagnostic)
try:
return reasons[result][diagnostic]
except KeyError:
LOGGER.warning(
f"Invalid A-ASSOCIATE 'Result Source' {result} and/or "
f"'Diagnostic' {diagnostic} values"
)
return "(no value available)"
@property
def responding_ae_title(self) -> Optional[str]:
"""Return the *Responding AE Title* parameter."""
return self.called_ae_title
@property
def responding_presentation_address(self) -> Optional[Tuple[str, int]]:
"""Get the Responding Presentation Address parameter."""
return self.called_presentation_address
@property
def result(self) -> Optional[int]:
"""Get or set the *Result* parameter.
Parameters
----------
value : int
One of the following:
* 0: accepted
* 1: rejected (permanent)
* 2: rejected (transient)
Returns
-------
int
"""
return self._result
@result.setter
def result(self, value: Optional[int]) -> None:
"""Set the A-ASSOCIATE Service primitive's Result parameter."""
# pylint: disable=attribute-defined-outside-init
if value is None:
pass
elif value not in [0, 1, 2]:
LOGGER.error("A_ASSOCIATE.result set to an unknown value")
raise ValueError("Unknown A_ASSOCIATE.result value")
self._result = value
@property
def result_source(self) -> Optional[int]:
"""Get or set the *Result Source* parameter.
Parameters
----------
value : int
One of the following:
* 1: UL service-user
* 2: UL service-provider (ACSE related function)
* 3: UL service-provider (presentation related function)
Returns
-------
int
"""
return self._result_source
@result_source.setter
def result_source(self, value: Optional[int]) -> None:
"""Set the A-ASSOCIATE Service primitive's Result Source parameter."""
# pylint: disable=attribute-defined-outside-init
if value is None:
pass
elif value not in [1, 2, 3]:
LOGGER.error("A_ASSOCIATE.result_source set to an unknown value")
raise ValueError("Unknown A_ASSOCIATE.result_source value")
self._result_source = value
@property
def result_str(self) -> str:
"""Return the result as str."""
results = {0: "Accepted", 1: "Rejected Permanent", 2: "Rejected Transient"}
if self.result not in results:
LOGGER.warning(f"Invalid A-ASSOCIATE 'Result' {self.result}")
return "(no value available)"
return results[self.result]
@property
def session_requirements(self) -> str:
"""Return the *Session Requirements* as :class:`str`."""
return ""
@property
def source_str(self) -> str:
"""Return the reject source as str."""
sources = {
1: "Service User",
2: "Service Provider (ACSE)",
3: "Service Provider (Presentation)",
}
if self.result_source not in sources:
LOGGER.warning(f"Invalid A-ASSOCIATE 'Result Source' {self.result_source}")
return "(no value available)"
return sources[self.result_source]
@property
def user_information(self) -> _UserInformationPrimitiveType:
"""Get or set the *User Information* parameter.
Parameters
----------
value_list : list of user information class objects
A list of user information objects, must contain at least
MaximumLengthNotification and ImplementationClassUIDNotification
"""
return self._user_information
@user_information.setter
def user_information(self, value_list: _UserInformationPrimitiveType) -> None:
"""Set the A-ASSOCIATE primitive's User Information parameter."""
# pylint: disable=attribute-defined-outside-init
valid_usr_info_items = []
if isinstance(value_list, list):
# Iterate through the items and check they're an acceptable class
for item in value_list:
if item.__class__.__name__ in [
"MaximumLengthNotification",
"ImplementationClassUIDNotification",
"ImplementationVersionNameNotification",
"AsynchronousOperationsWindowNegotiation",
"SCP_SCU_RoleSelectionNegotiation",
"SOPClassExtendedNegotiation",
"SOPClassCommonExtendedNegotiation",
"UserIdentityNegotiation",
]:
valid_usr_info_items.append(item)
else:
LOGGER.info(
"Attempted to set "
"A_ASSOCIATE.user_information to a list "
"which includes an unsupported item"
)
else:
LOGGER.error("A_ASSOCIATE.user_information must be a list")
raise TypeError("A_ASSOCIATE.user_information must be a list")
self._user_information = valid_usr_info_items
class A_RELEASE:
"""An A-RELEASE primitive.
+------------------+---------+------------+----------+--------------+
| Parameter | Request | Indication | Response | Confirmation |
+------------------+---------+------------+----------+--------------+
| reason | UF | UF(=) | UF | UF(=) |
+------------------+---------+------------+----------+--------------+
| user info |NU | NU(=) | NU | NU(=) |
+------------------+---------+------------+----------+--------------+
| result | | | MF | MF(=) |
+------------------+---------+------------+----------+--------------+
| U - User option
| UF - User option, fixed value
| C - Conditional (on user option)
| M - Mandatory
| MF - Mandatory, fixed value
| NU - Not used
| (=) - shall have same value as request or response
References
----------
* DICOM Standard, Part 8, :dcm:`Section 7.2<part08/sect_7.2.html>`
"""
def __init__(self) -> None:
self._result: Optional[str] = None
@property
def reason(self) -> str:
"""Return the *Reason* parameter."""
return "normal"
@property
def result(self) -> Optional[str]:
"""Get or set the *Result* parameter.
Parameters
----------
value : str
Must be ``None`` for request and indication, ``'affirmative'``
for response and confirmation.
"""
return self._result
@result.setter
def result(self, value: Optional[str]) -> None:
"""Set the Result parameter."""
# pylint: disable=attribute-defined-outside-init
if value is not None and value != "affirmative":
LOGGER.error("A_RELEASE.result must be None or 'affirmative'")
raise ValueError("A_RELEASE.result must be None or 'affirmative'")
self._result = value
class A_ABORT:
"""An A-ABORT primitive.
+------------------+---------+------------+
| Parameter | Request | Indication |
+------------------+---------+------------+
| abort source | | M |
+------------------+---------+------------+
| user info |NU | NU(=) |
+------------------+---------+------------+
| U - User option
| UF - User option, fixed value
| C - Conditional (on user option)
| M - Mandatory
| MF - Mandatory, fixed value
| NU - Not used
| (=) - shall have same value as request or response
References
----------
* DICOM Standard, Part 8, :dcm:`Section 7.3<part08/sect_7.3.html>`
"""
def __init__(self) -> None:
self._abort_source: Optional[int] = None
@property
def abort_source(self) -> Optional[int]:
"""Get or set the *Abort Source*.
Parameters
----------
value : int or None
The abort source, one of:
* ``0``: Upper layer service user
* ``1``: Reserved
* ``2``: Upper layer service provider
"""
if self._abort_source is None:
LOGGER.error("A_ABORT.abort_source value not set")
raise ValueError("A_ABORT.abort_source value not set")
return self._abort_source
@abort_source.setter
def abort_source(self, value: Optional[int]) -> None:
"""Set the Abort Source."""
if value in [0, 1, 2, None]:
self._abort_source = value
else:
msg = f"Invalid A-ABORT 'Source' value '{value}'"
LOGGER.error(msg)
raise ValueError(msg)
class A_P_ABORT:
"""
An A-P-ABORT primitive.
+------------------+------------+
| Parameter | Indication |
+------------------+------------+
| provider reason | P |
+------------------+------------+
| U - User option
| UF - User option, fixed value
| C - Conditional (on user option)
| M - Mandatory
| MF - Mandatory, fixed value
| NU - Not used
| P - Provider initiated
| (=) - shall have same value as request or response
References
----------
* DICOM Standard, Part 8, :dcm:`Section 7.4<part08/sect_7.4.html>`
"""
def __init__(self) -> None:
self._provider_reason: Optional[int] = None
@property
def provider_reason(self) -> Optional[int]:
"""Return the *Provider Reason*.
Parameters
----------
value : int
Indicates the reason for the abort. Allowed values are:
* ``0``: reason not specified
* ``1``: unrecognised PDU
* ``2``: unexpected PDU
* ``4``: unrecognised PDU parameter
* ``5``: unexpected PDU parameter
* ``6``: invalid PDU parameter value
"""
if self._provider_reason is None:
LOGGER.error("A_ABORT.provider_reason parameter not set")
raise ValueError("A_ABORT.provider_reason value not set")
return self._provider_reason
@provider_reason.setter
def provider_reason(self, value: Optional[int]) -> None:
"""Set the Provider Reason."""
if value in [0, 1, 2, 4, 5, 6, None]:
self._provider_reason = value
else:
msg = "Attempted to set A_P_ABORT.provider_reason to an invalid value"
LOGGER.error(msg)
raise ValueError(msg)
class P_DATA:
"""
A P-DATA primitive.
+------------------------------+---------+------------+
| Parameter | Request | Indication |
+------------------------------+---------+------------+
| presentation data value list | M | M(=) |
+------------------------------+---------+------------+
| U - User option
| UF - User option, fixed value
| C - Conditional (on user option)
| M - Mandatory
| MF - Mandatory, fixed value
| NU - Not used
| (=) - shall have same value as request or response
References
----------
* DICOM Standard, Part 8, :dcm:`Section 7.6<part08/sect_7.6.html>`
"""
def __init__(self) -> None:
self._presentation_data_value_list: List[Tuple[int, bytes]] = []
@property
def presentation_data_value_list(self) -> List[Tuple[int, bytes]]:
"""Get or set the *Presentation Data Value List*.
Parameters
----------
presentation_data_value_list : list of [int, bytes]
Contains one or more Presentation Data Values (PDV), each
consisting of a Presentation Context ID and User Data values.
The User Data values are taken from the Abstract Syntax and
encoded in the Transfer Syntax identified by the Presentation
Context ID. Each item in the list is ``[Context ID, PDV Data]``
"""
return self._presentation_data_value_list
@presentation_data_value_list.setter
def presentation_data_value_list(self, value_list: List[Tuple[int, bytes]]) -> None:
"""Set the Presentation Data Value List."""
# pylint: disable=attribute-defined-outside-init
if isinstance(value_list, list):
for pdv in value_list:
if isinstance(pdv, list):
if isinstance(pdv[0], int) and isinstance(pdv[1], bytes):
pass
else:
raise TypeError(
"P_DATA.presentation_data_value_list "
"should be a list of [int, bytes]"
)
else:
raise TypeError(
"P_DATA.presentation_data_value_list "
"should be a list of [ID, PDV]"
)
else:
raise TypeError(
"P_DATA.presentation_data_value_list "
"should be a list of [int, bytes]"
)
self._presentation_data_value_list = value_list
def __str__(self) -> str:
"""String representation of the class."""
s = "P-DATA\n"
for pdv in self.presentation_data_value_list:
header_byte = pdv[1][0]
s += f" Context ID: {pdv[0]}\n"
s += f" Value Length: {len(pdv[1])} bytes\n"
s += f" Message Control Header Byte: {header_byte:08b}\n"
# xxxxxx01 and xxxxxx011
if header_byte & 1:
# xxxxxx11
if header_byte & 2:
s += (
" Command information, last fragment of the "
"DIMSE message\n"
)
# xxxxxx01
else:
s += (
" Command information, not the last fragment of "
"the DIMSE message\n"
)
# xxxxxx00, xxxxxxx10
else:
# xxxxxx10
if header_byte & 2 != 0:
s += (
" Dataset information, last fragment of the "
"DIMSE message\n"