forked from cisco-en-programmability/catalystcenter-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_trace_workflow_manager.py
More file actions
1738 lines (1587 loc) · 70.3 KB
/
Copy pathpath_trace_workflow_manager.py
File metadata and controls
1738 lines (1587 loc) · 70.3 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2025, Cisco Systems
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
"""Ansible module to perform operations on create and delete path trace details between
two different IP addresses and network in Cisco Catalyst Center."""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
__author__ = ["A Mohamed Rafeek, Madhan Sankaranarayanan"]
DOCUMENTATION = r"""
---
module: path_trace_workflow_manager
short_description: Resource module for managing PathTrace
settings in Cisco Catalyst Center
description: |
This module allows the management of PathTrace settings in Cisco Catalyst Center.
- It supports creating and deleting PathTrace configurations.
- This module configures PathTrace settings in Cisco Catalyst Center, including
source/destination IPs, ports, and protocols.
version_added: '6.31.0'
extends_documentation_fragment:
- cisco.catalystcenter.workflow_manager_params
author:
- A Mohamed Rafeek (@mabdulk2)
- Madhan Sankaranarayanan (@madhansansel)
options:
config_verify:
description: |
Set to `true` to enable configuration verification on Cisco Catalyst Center after applying
the playbook configuration. This ensures that the system validates the configuration
state after the change is applied.
type: bool
default: true
state:
description: |
Specifies the desired state for the configuration. If `merged`, the module will create
or update the configuration, adding new settings or modifying existing ones. If `deleted`,
it will remove the specified settings.
type: str
choices: ["merged", "deleted"]
default: merged
config:
description: A list containing the details for Path
Trace configuration.
type: list
elements: dict
required: true
suboptions:
source_ip:
description: |
The source IP address for the path trace. Either flow_analysis_id or
both source_ip and dest_ip are required.
type: str
required: false
dest_ip:
description: |
The destination IP address for the path trace. Either flow_analysis_id or
both source_ip and dest_ip are required.
type: str
required: false
source_port:
description: The source port for the path trace
(optional).
type: int
required: false
dest_port:
description: The destination port for the path
trace (optional).
type: int
required: false
protocol:
description: The protocol to use for the path
trace, e.g., TCP, UDP (optional).
type: str
choices: ["TCP", "UDP"]
required: false
include_stats:
description: |
A list of optional statistics (multiple choice) to include in the path trace,
such as QOS statistics or additional details. Examples: "DEVICE_STATS",
"INTERFACE_STATS", "QOS_STATS", "PERFORMANCE_STATS", "ACL_TRACE".
- DEVICE_STATS - Collects hardware-related statistics of network devices
along the path, including CPU usage, memory, uptime, and interface status.
- INTERFACE_STATS - Gathers details about interfaces used in the path,
such as interface type, bandwidth usage, errors, and drops.
- QOS_STATS - Displays Quality of Service (QoS) settings on interfaces,
including traffic classification, priority settings, and congestion management.
- PERFORMANCE_STATS: Provides network performance metrics like latency,
jitter, and packet loss.
- ACL_TRACE: Analyzes Access Control List (ACL) rules applied along
the path to identify blocked traffic or policy mismatches.
type: list
elements: str
required: false
periodic_refresh:
description: |
Boolean value to enable periodic refresh for the path trace.
type: bool
required: false
default: true
get_last_pathtrace_result:
description: |
Boolean value to display the last result again for the path trace.
type: bool
required: false
default: true
delete_on_completion:
description: |
Boolean value indicating whether to delete the path trace after generation.
This applies only when periodic_refresh is set to false..
type: bool
required: false
default: true
flow_analysis_id:
description: |
The Flow Analysis ID uniquely identifies a specific path trace operation in
Cisco Catalyst Center. This UUID-format identifier serves multiple purposes
across different operational states.
**Creation Context:**
When creating a new path trace, the API returns a flow_analysis_id in the
response's "request.id" field. This identifier should be captured using
Ansible's register functionality for subsequent operations.
**Retrieval Operations:**
- If provided, retrieves the specific path trace associated with this ID
- If omitted, the module searches based on source_ip and dest_ip parameters
- Provides precise identification when multiple traces exist between the same endpoints
**Deletion Operations:**
- When state is 'deleted', this ID enables targeted removal of specific traces
- If not provided, the module searches for matching traces using source_ip/dest_ip
- Essential for scenarios where multiple path traces exist with identical endpoints
**Best Practices:**
- Always capture flow_analysis_id when creating path traces using register
- Use flow_analysis_id for precise trace management in automation workflows
- Preferred over source_ip/dest_ip combination for unique trace identification
**Format:** UUID string (For example, "99e067de-8776-40d2-9f6a-1e6ab2ef083c")
type: str
required: false
requirements:
- catalystcentersdk >= 3.1.6.0.2
- python >= 3.12
notes:
- SDK Method used are
path_trace.PathTraceWorkflow.retrieves_all_previous_pathtraces_summary,
path_trace.PathTraceWorkflow.retrieves_previous_pathtraces_summary,
path_trace.PathTraceWorkflow.initiate_a_new_pathtrace,
path_trace.PathTraceWorkflow.delete_pathtrace_by_id,
- API paths used are GET/dna/intent/api/v1/flow-analysis
POST/dna/intent/api/v1/flow-analysis GET/dna/intent/api/v1/flow-analysis/{flowAnalysisId}
DELETE/dna/intent/api/v1/flow-analysis/{flowAnalysisId}
"""
EXAMPLES = r"""
---
- hosts: catalystcenter_servers
vars_files:
- credentials.yml
gather_facts: false
connection: local
tasks:
- name: Create and auto-delete path trace on Cisco
Catalyst Center
cisco.catalystcenter.path_trace_workflow_manager:
catalystcenter_host: "{{ catalystcenter_host }}"
catalystcenter_port: "{{ catalystcenter_port }}"
catalystcenter_username: "{{ catalystcenter_username }}"
catalystcenter_password: "{{ catalystcenter_password }}"
catalystcenter_verify: "{{ catalystcenter_verify }}"
catalystcenter_debug: "{{ catalystcenter_debug }}"
catalystcenter_version: "{{ catalystcenter_version }}"
catalystcenter_log: true
catalystcenter_log_level: DEBUG
catalystcenter_log_append: true
state: merged
config_verify: true
config:
- source_ip: "204.1.2.3" # required field
dest_ip: "204.1.2.4" # required field
source_port: 4020 # optional field
dest_port: 4021 # optional field
protocol: "TCP" # optional field
include_stats: # optional field
- DEVICE_STATS
- INTERFACE_STATS
- QOS_STATS
- PERFORMANCE_STATS
- ACL_TRACE
periodic_refresh: false # optional field
delete_on_completion: true # optional field
- name: Delete path trace based on source and destination IP
cisco.catalystcenter.path_trace_workflow_manager:
catalystcenter_host: "{{ catalystcenter_host }}"
catalystcenter_port: "{{ catalystcenter_port }}"
catalystcenter_username: "{{ catalystcenter_username }}"
catalystcenter_password: "{{ catalystcenter_password }}"
catalystcenter_verify: "{{ catalystcenter_verify }}"
catalystcenter_debug: "{{ catalystcenter_debug }}"
catalystcenter_version: "{{ catalystcenter_version }}"
catalystcenter_log_level: DEBUG
catalystcenter_log: true
state: deleted
config_verify: true
config:
- source_ip: "204.1.2.3" # required field
dest_ip: "204.1.2.4" # required field
- name: Retrieve last path trace
cisco.catalystcenter.path_trace_workflow_manager:
catalystcenter_host: "{{ catalystcenter_host }}"
catalystcenter_port: "{{ catalystcenter_port }}"
catalystcenter_username: "{{ catalystcenter_username }}"
catalystcenter_password: "{{ catalystcenter_password }}"
catalystcenter_verify: "{{ catalystcenter_verify }}"
catalystcenter_debug: "{{ catalystcenter_debug }}"
catalystcenter_version: "{{ catalystcenter_version }}"
catalystcenter_log_level: DEBUG
catalystcenter_log: true
state: merged
config_verify: true
config:
- source_ip: "204.1.2.3" # required field
dest_ip: "204.1.2.4" # required field
get_last_pathtrace_result: true
- name: Retrieve path trace based on the flow analysis
id
cisco.catalystcenter.path_trace_workflow_manager:
catalystcenter_host: "{{ catalystcenter_host }}"
catalystcenter_port: "{{ catalystcenter_port }}"
catalystcenter_username: "{{ catalystcenter_username }}"
catalystcenter_password: "{{ catalystcenter_password }}"
catalystcenter_verify: "{{ catalystcenter_verify }}"
catalystcenter_debug: "{{ catalystcenter_debug }}"
catalystcenter_version: "{{ catalystcenter_version }}"
catalystcenter_log_level: DEBUG
catalystcenter_log: true
state: merged
config_verify: true
config:
# When create a path trace, it returns a flow_analysis_id
# (the "id" from the "request" section), which should be
# shown in a register.
- flow_analysis_id: 99e067de-8776-40d2-9f6a-1e6ab2ef083c
delete_on_completion: false # optional field
register: output_list
- name: Retrieve and Delete path trace based on
the required field
cisco.catalystcenter.path_trace_workflow_manager:
catalystcenter_host: "{{ catalystcenter_host }}"
catalystcenter_port: "{{ catalystcenter_port }}"
catalystcenter_username: "{{ catalystcenter_username }}"
catalystcenter_password: "{{ catalystcenter_password }}"
catalystcenter_verify: "{{ catalystcenter_verify }}"
catalystcenter_debug: "{{ catalystcenter_debug }}"
catalystcenter_version: "{{ catalystcenter_version }}"
catalystcenter_log_level: DEBUG
catalystcenter_log: true
state: merged
config_verify: true
config:
- source_ip: "204.1.2.3" # required field
dest_ip: "204.1.2.4" # required field
register: output_list
- name: Delete path trace based on registered flow
analysis id
cisco.catalystcenter.path_trace_workflow_manager:
catalystcenter_host: "{{ catalystcenter_host }}"
catalystcenter_port: "{{ catalystcenter_port }}"
catalystcenter_username: "{{ catalystcenter_username }}"
catalystcenter_password: "{{ catalystcenter_password }}"
catalystcenter_verify: "{{ catalystcenter_verify }}"
catalystcenter_debug: "{{ catalystcenter_debug }}"
catalystcenter_version: "{{ catalystcenter_version }}"
catalystcenter_log_level: DEBUG
catalystcenter_log: true
state: deleted
config_verify: true
config:
- flow_analysis_id: output_list.request.id
- name: delete path trace based on the flow analysis
id
cisco.catalystcenter.path_trace_workflow_manager:
catalystcenter_host: "{{ catalystcenter_host }}"
catalystcenter_port: "{{ catalystcenter_port }}"
catalystcenter_username: "{{ catalystcenter_username }}"
catalystcenter_password: "{{ catalystcenter_password }}"
catalystcenter_verify: "{{ catalystcenter_verify }}"
catalystcenter_debug: "{{ catalystcenter_debug }}"
catalystcenter_version: "{{ catalystcenter_version }}"
catalystcenter_log_level: DEBUG
catalystcenter_log: true
state: deleted
config_verify: true
config:
# When create a path trace, it returns a flow_analysis_id
# (the "id" from the "request" section), which should be
# shown in a register.
- flow_analysis_id: 99e067de-8776-40d2-9f6a-1e6ab2ef083c
- name: Create/Retrieve Path trace for the config
list.
cisco.catalystcenter.path_trace_workflow_manager:
catalystcenter_host: "{{ catalystcenter_host }}"
catalystcenter_port: "{{ catalystcenter_port }}"
catalystcenter_username: "{{ catalystcenter_username }}"
catalystcenter_password: "{{ catalystcenter_password }}"
catalystcenter_verify: "{{ catalystcenter_verify }}"
catalystcenter_debug: "{{ catalystcenter_debug }}"
catalystcenter_version: "{{ catalystcenter_version }}"
catalystcenter_log_level: DEBUG
catalystcenter_log: true
state: merged
config_verify: true
config:
- source_ip: "204.1.2.3" # required field
dest_ip: "204.1.2.4" # required field
source_port: 4020 # optional field
dest_port: 4021 # optional field
protocol: "TCP" # optional field
include_stats: # optional field
- DEVICE_STATS
- INTERFACE_STATS
- QOS_STATS
- PERFORMANCE_STATS
- ACL_TRACE
periodic_refresh: false # optional field
delete_on_completion: true # optional field
- source_ip: "204.1.1.2" # required field
dest_ip: "204.1.2.4" # required field
get_last_pathtrace_result: true # optional field
delete_on_completion: true # optional field
- flow_analysis_id: 99e067de-8776-40d2-9f6a-1e6ab2ef083c
"""
RETURN = r"""
#Case 1: Successful creation of trace path based on multiple fields
response_1:
description: A dictionary with the response returned by the Cisco Catalyst Center Python SDK
returned: always
type: dict
sample: >
{
"msg": "Path trace created and verified successfully for '[{'source_ip': '204.1.2.3',
'dest_ip': '204.1.2.4', 'source_port': 4020, 'dest_port': 4021, 'protocol': 'TCP',
'periodic_refresh': False, 'include_stats': ['DEVICE-STATS',
'INTERFACE-STATS', 'QOS-STATS', 'PERFORMANCE-STATS', 'ACL-TRACE'],
'flow_analysis_id': 'f30d648d-adb7-42ba-88f9-9a9e4c4fca4e'}]'.",
"response": [
{
"lastUpdate": "Fri Feb 21 19:16:46 GMT 2025",
"networkElementsInfo": [
{
"egressInterface": {
"physicalInterface": {
"id": "b65f159e-b67d-49d4-92d0-801a0eda6426",
"name": "TenGigabitEthernet1/1/7",
"usedVlan": "NA",
"vrfName": "global"
}
},
"id": "e62e6405-13e4-4f1b-ae1c-580a28a96a88",
"ip": "204.1.2.3",
"linkInformationSource": "ISIS",
"name": "SJ-BN-9300",
"role": "DISTRIBUTION",
"type": "Switches and Hubs"
},
{
"egressInterface": {
"physicalInterface": {
"id": "2897a064-9079-4c9c-adf2-3e0b5cf22724",
"name": "TenGigabitEthernet1/1/7",
"usedVlan": "NA",
"vrfName": "global"
}
},
"id": "820bd13a-f565-4778-a320-9ec9f23b4725",
"ingressInterface": {
"physicalInterface": {
"id": "c98d09f3-b57e-468f-a9a1-65e75249e94f",
"name": "TenGigabitEthernet1/1/8",
"usedVlan": "NA",
"vrfName": "global"
}
},
"ip": "204.1.1.22",
"linkInformationSource": "ISIS",
"name": "DC-T-9300",
"role": "ACCESS",
"type": "Switches and Hubs"
},
{
"id": "0be10e21-34c7-4c76-b217-56327ed1f418",
"ingressInterface": {
"physicalInterface": {
"id": "f24b433c-8388-453e-a034-fcaf516bc749",
"name": "TenGigabitEthernet2/1/8",
"usedVlan": "NA",
"vrfName": "global"
}
},
"ip": "204.1.2.4",
"name": "NY-BN-9300",
"role": "DISTRIBUTION",
"type": "Switches and Hubs"
}
],
"request": {
"controlPath": false,
"createTime": 1740165404872,
"destIP": "204.1.2.4",
"destPort": "4021",
"id": "81d8b994-fb62-48dc-aa45-cb3a62d4e4b4",
"lastUpdateTime": 1740165406115,
"periodicRefresh": false,
"protocol": "TCP",
"sourceIP": "204.1.2.3",
"sourcePort": "4020",
"status": "COMPLETED"
}
}
],
"status": "success"
}
#Case 2: Retrieve the path trace based on flow analysis id
response_2:
description: A dictionary or list with the response returned by the Cisco Catalyst Center Python SDK
returned: always
type: dict
sample: >
{
"msg": "Path trace created and verified successfully for '[{'flow_analysis_id':
'99e067de-8776-40d2-9f6a-1e6ab2ef083c'}]'.",
"response": [
{
"lastUpdate": "Fri Feb 21 19:21:16 GMT 2025",
"networkElementsInfo": [
{
"egressInterface": {
"physicalInterface": {
"id": "b65f159e-b67d-49d4-92d0-801a0eda6426",
"name": "TenGigabitEthernet1/1/7",
"usedVlan": "NA",
"vrfName": "global"
}
},
"id": "e62e6405-13e4-4f1b-ae1c-580a28a96a88",
"ip": "204.1.2.3",
"linkInformationSource": "ISIS",
"name": "SJ-BN-9300",
"role": "DISTRIBUTION",
"type": "Switches and Hubs"
},
{
"egressInterface": {
"physicalInterface": {
"id": "2897a064-9079-4c9c-adf2-3e0b5cf22724",
"name": "TenGigabitEthernet1/1/7",
"usedVlan": "NA",
"vrfName": "global"
}
},
"id": "820bd13a-f565-4778-a320-9ec9f23b4725",
"ingressInterface": {
"physicalInterface": {
"id": "c98d09f3-b57e-468f-a9a1-65e75249e94f",
"name": "TenGigabitEthernet1/1/8",
"usedVlan": "NA",
"vrfName": "global"
}
},
"ip": "204.1.1.22",
"linkInformationSource": "ISIS",
"name": "DC-T-9300",
"role": "ACCESS",
"type": "Switches and Hubs"
},
{
"id": "0be10e21-34c7-4c76-b217-56327ed1f418",
"ingressInterface": {
"physicalInterface": {
"id": "f24b433c-8388-453e-a034-fcaf516bc749",
"name": "TenGigabitEthernet2/1/8",
"usedVlan": "NA",
"vrfName": "global"
}
},
"ip": "204.1.2.4",
"name": "NY-BN-9300",
"role": "DISTRIBUTION",
"type": "Switches and Hubs"
}
],
"request": {
"controlPath": false,
"createTime": 1740156374801,
"destIP": "204.1.2.4",
"destPort": "80",
"id": "99e067de-8776-40d2-9f6a-1e6ab2ef083c",
"lastUpdateTime": 1740156376055,
"periodicRefresh": false,
"protocol": "TCP",
"sourceIP": "204.1.2.3",
"sourcePort": "80",
"status": "COMPLETED"
}
}
],
"status": "success"
}
#Case 3: Retrieve the last created path trace based on source and dest IP
response_3:
description: A dictionary or list with the response returned by the Cisco Catalyst Center Python SDK
returned: always
type: dict
sample: >
{
"msg": "Path trace created and verified successfully for '[{'source_ip': '204.1.1.2',
'dest_ip': '204.1.2.4', 'get_last_pathtrace_result': True,
'flow_analysis_id': 'f30d648d-adb7-42ba-88f9-9a9e4c4fca4e'}]'.",
"response": [
{
"lastUpdate": "Fri Feb 21 19:25:52 GMT 2025",
"networkElementsInfo": [
{
"egressInterface": {
"physicalInterface": {
"id": "44aafd2d-5822-4ce5-95c5-11909e9425f6",
"name": "TenGigabitEthernet1/1/1",
"usedVlan": "NA",
"vrfName": "global"
}
},
"id": "99b62ead-51d6-4bfc-9b0c-dab087f184e9",
"ip": "204.1.1.2",
"linkInformationSource": "ISIS",
"name": "SJ-EN-9300",
"role": "ACCESS",
"type": "Switches and Hubs"
},
{
"egressInterface": {
"physicalInterface": {
"id": "b65f159e-b67d-49d4-92d0-801a0eda6426",
"name": "TenGigabitEthernet1/1/7",
"usedVlan": "NA",
"vrfName": "global"
}
},
"id": "e62e6405-13e4-4f1b-ae1c-580a28a96a88",
"ingressInterface": {
"physicalInterface": {
"id": "0610f80e-09fc-4083-8aaa-7cf318b211de",
"name": "TenGigabitEthernet1/1/2",
"usedVlan": "NA",
"vrfName": "global"
}
},
"ip": "204.1.2.3",
"linkInformationSource": "ISIS",
"name": "SJ-BN-9300",
"role": "DISTRIBUTION",
"type": "Switches and Hubs"
},
{
"egressInterface": {
"physicalInterface": {
"id": "2897a064-9079-4c9c-adf2-3e0b5cf22724",
"name": "TenGigabitEthernet1/1/7",
"usedVlan": "NA",
"vrfName": "global"
}
},
"id": "820bd13a-f565-4778-a320-9ec9f23b4725",
"ingressInterface": {
"physicalInterface": {
"id": "c98d09f3-b57e-468f-a9a1-65e75249e94f",
"name": "TenGigabitEthernet1/1/8",
"usedVlan": "NA",
"vrfName": "global"
}
},
"ip": "204.1.1.22",
"linkInformationSource": "ISIS",
"name": "DC-T-9300",
"role": "ACCESS",
"type": "Switches and Hubs"
},
{
"id": "0be10e21-34c7-4c76-b217-56327ed1f418",
"ingressInterface": {
"physicalInterface": {
"id": "f24b433c-8388-453e-a034-fcaf516bc749",
"name": "TenGigabitEthernet2/1/8",
"usedVlan": "NA",
"vrfName": "global"
}
},
"ip": "204.1.2.4",
"name": "NY-BN-9300",
"role": "DISTRIBUTION",
"type": "Switches and Hubs"
}
],
"request": {
"controlPath": false,
"createTime": 1740162201882,
"destIP": "204.1.2.4",
"id": "3cb51b94-2a50-4a92-b204-13ffdde22ef9",
"lastUpdateTime": 1740162203167,
"periodicRefresh": false,
"sourceIP": "204.1.1.2",
"status": "COMPLETED"
}
}
],
"status": "success"
}
#Case 4: Delete path trace based on flow analysis id
response_4:
description: A dictionary or list with the response returned by the Cisco Catalyst Center Python SDK
returned: always
type: dict
sample: >
{
"msg": "Path trace deleted and verified successfully for '[{'source_ip': '204.1.1.2',
'dest_ip': '204.1.2.4', 'get_last_pathtrace_result': True}]'.",
"response":"Path trace deleted and verified successfully for '[{'source_ip': '204.1.1.2',
'dest_ip': '204.1.2.4', 'get_last_pathtrace_result': True}]'.",
"status": "success"
}
#Case 5: Delete path trace based on Source and Destination IP
response_5:
description: A dictionary or list with the response returned by the Cisco Catalyst Center Python SDK
returned: always
type: dict
sample: >
{
"msg": "Path trace deleted and verified successfully for '[{'flow_analysis_id':
'99e067de-8776-40d2-9f6a-1e6ab2ef083c'}]'.",
"response": "Path trace deleted and verified successfully for '[{'flow_analysis_id':
'99e067de-8776-40d2-9f6a-1e6ab2ef083c'}]'.",
"status": "success"
}
"""
import re
import time
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.cisco.catalystcenter.plugins.module_utils.catalystcenter import (
CatalystCenterBase,
validate_list_of_dicts,
)
class PathTraceWorkflow(CatalystCenterBase):
"""Class containing member attributes for Assurance setting workflow manager module"""
def __init__(self, module):
super().__init__(module)
self.supported_states = ["merged", "deleted"]
self.create_path, self.delete_path, self.not_processed = [], [], []
self.success_path = []
self.keymap = dict(
flow_analysis_id="id",
source_ip="sourceIP",
dest_ip="destIP",
dest_port="destPort",
source_port="sourcePort",
periodic_refresh="periodicRefresh",
INTERFACE_STATS="INTERFACE-STATS",
QOS_STATS="QOS-STATS",
DEVICE_STATS="DEVICE-STATS",
PERFORMANCE_STATS="PERFORMANCE-STATS",
ACL_TRACE="ACL-TRACE",
)
def validate_input(self):
"""
Validate the fields provided in the playbook.
Checks the configuration provided in the playbook against a predefined specification
to ensure it adheres to the expected structure and data types.
Parameters:
self: The instance of the class containing the 'config' attribute to be validated.
Returns:
The method updates these attributes of the instance:
- self.msg: A message describing the validation result.
- self.status: The status of the validation ('success' or 'failed').
- self.validated_config: If successful, a validated version of the 'config' parameter.
"""
temp_spec = {
"source_ip": {"type": "str", "required": False},
"dest_ip": {"type": "str", "required": False},
"source_port": {
"type": "int",
"range_min": 1,
"range_max": 65535,
"required": False,
},
"dest_port": {
"type": "int",
"range_min": 1,
"range_max": 65535,
"required": False,
},
"protocol": {"type": "str", "choices": ["TCP", "UDP"], "required": False},
"periodic_refresh": {"type": "bool", "required": False},
"include_stats": {"type": "list", "elements": "str", "required": False},
"get_last_pathtrace_result": {"type": "bool", "required": False},
"flow_analysis_id": {"type": "str", "required": False},
"delete_on_completion": {"type": "bool", "required": False}
}
if not self.config:
self.msg = "The playbook configuration is empty or missing."
self.set_operation_result("failed", False, self.msg, "ERROR")
return self
# Validate configuration against the specification
valid_temp, invalid_params = validate_list_of_dicts(self.config, temp_spec)
if invalid_params:
self.msg = "The playbook contains invalid parameters: {0}".format(
invalid_params
)
self.set_operation_result("failed", False, self.msg, "ERROR")
return self
valid_temp = [
{key: value for key, value in data.items() if value is not None}
for data in valid_temp
]
self.validated_config = valid_temp
self.msg = "Successfully validated playbook configuration parameters using 'validate_input': {0}".format(
str(valid_temp)
)
self.log(self.msg, "INFO")
return self
def input_data_validation(self, config):
"""
Additional validation to check if the provided input path trace data is correct
and as per the UI Cisco Catalyst Center.
Parameters:
self (object): An instance of a class for interacting with Cisco Catalyst Center.
config (dict): Dictionary containing the input path trace details.
Returns:
self: Current object with path trace input data.
Description:
Iterates through available path trace data and Returns the list of invalid
data for further action or validation.
"""
self.log("Starting path trace input validation.", "INFO")
errormsg = []
valid_inclusions = (
"DEVICE_STATS",
"INTERFACE_STATS",
"QOS_STATS",
"PERFORMANCE_STATS",
"ACL_TRACE",
)
for each_path in config:
self.log("Validating path trace entry: {0}".format(str(each_path)), "DEBUG")
delete_on_completion = each_path.get("delete_on_completion")
if delete_on_completion is not None and delete_on_completion not in (
True,
False,
):
errormsg.append(
"delete_on_completion: Invalid value {0} in playbook. Must be either true or false.".format(
delete_on_completion
)
)
flow_analysis_id = each_path.get("flow_analysis_id")
if flow_analysis_id:
if not self.is_valid_uuid_regex(flow_analysis_id):
errormsg.append(
"flow_analysis_id: Invalid value '{0}'. Must be a valid UUID.".format(
flow_analysis_id
)
)
break
source_ip = each_path.get("source_ip")
if source_ip is None:
errormsg.append("source_ip: Source IP Address is missing in playbook.")
elif not (self.is_valid_ipv4(source_ip) or self.is_valid_ipv6(source_ip)):
errormsg.append(
"source_ip: Invalid Source IP Address '{0}' in playbook. Must be a valid IPv4 or IPv6 address".format(
source_ip
)
)
dest_ip = each_path.get("dest_ip")
if dest_ip is None:
errormsg.append(
"dest_ip: Destination IP Address is missing in playbook."
)
elif not (self.is_valid_ipv4(dest_ip) or self.is_valid_ipv6(dest_ip)):
errormsg.append(
"dest_ip: Invalid Destination IP Address '{0}' in playbook. Must be a valid IPv4 or IPv6 address".format(
dest_ip
)
)
source_port = each_path.get("source_port")
if source_port and source_port not in range(1, 65536):
errormsg.append(
"source_port: Invalid Source Port number '{0}' in playbook. Must be between 1 and 65535.".format(
source_port
)
)
dest_port = each_path.get("dest_port")
if dest_port and dest_port not in range(1, 65536):
errormsg.append(
"dest_port: Invalid Destination Port number '{0}' in playbook. Must be between 1 and 65535.".format(
dest_port
)
)
protocol = each_path.get("protocol")
if protocol and protocol not in ("TCP", "UDP"):
errormsg.append(
"protocol: Invalid protocol '{0}'. Must be 'TCP' or 'UDP'.".format(
protocol
)
)
periodic_refresh = each_path.get("periodic_refresh")
if periodic_refresh is not None and periodic_refresh not in (True, False):
errormsg.append(
"periodic_refresh: Invalid periodic refresh "
+ "'{0}' in playbook. Must be either true or false.".format(
periodic_refresh
)
)
get_last_pathtrace_result = each_path.get("get_last_pathtrace_result")
if (
get_last_pathtrace_result is not None
and get_last_pathtrace_result not in (True, False)
):
errormsg.append(
"get_last_pathtrace_result: Invalid get last pathtrace result "
+ "'{0}' in playbook. Must be either true or false.".format(
get_last_pathtrace_result
)
)
include_stats = each_path.get("include_stats")
if include_stats:
collect_invalid_stats = []
for each_include in include_stats:
if each_include not in valid_inclusions:
collect_invalid_stats.append(each_include)
if collect_invalid_stats:
errormsg.append(
"include_stats: Invalid value(s) '{0}'. Must be one or more of: {1}.".format(
str(collect_invalid_stats), ", ".join(valid_inclusions)
)
)
if len(errormsg) > 0:
self.msg = "Invalid parameters in playbook config: '{0}' ".format(errormsg)
self.log(self.msg, "ERROR")
self.set_operation_result(
"failed", False, self.msg, "ERROR"
).check_return_status()
self.msg = "Successfully validated config params: {0}".format(str(config))
self.log(self.msg, "INFO")
return self
def is_valid_uuid_regex(self, uuid_string):
"""
Validates if the given string is a valid UUID (version 1 to 5).
Parameters:
self (object): An instance of a class used for interacting with Cisco Catalyst Center.
uuid_string (str): String contains uuid to check valid uuid.
Returns:
bool: Return response as True or False if UUID matched.
"""
uuid_pattern = re.compile(
r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
)
return bool(uuid_pattern.match(str(uuid_string)))
def get_want(self, config):
"""
Retrieve path trace or delete path trace data from playbook configuration.
Parameters:
self (object): An instance of a class used for interacting with Cisco Catalyst Center.
config (dict): The configuration dictionary containing path trace details.
Returns:
self: The current instance of the class with updated 'want' attributes.
Description:
This function parses the playbook configuration to extract information related to path
trace. It stores these details in the 'want' dictionary
for later use in the Ansible module.
"""
want = {}
if config:
self.log("Validating configuration: {0}".format(str(config)), "DEBUG")
self.input_data_validation(config).check_return_status()
want["assurance_pathtrace"] = config
self.log(
"Path trace data extracted and stored in 'want': {0}".format(str(want)),
"INFO",
)
else:
self.log("No configuration provided for path trace data.", "WARNING")
self.want = want
self.log("Desired State (want): {0}".format(str(self.want)), "INFO")
return self
def get_have(self, config):
"""
Get the current path trace details for the given config from Cisco Catalyst Center
Parameters:
config (dict) - Playbook details containing Path Trace
Returns:
self - The current object with path trace flow analysis id and details response.
"""
self.log("Starting to retrieve path trace details.", "DEBUG")
self.have["assurance_pathtrace"] = []
for each_path in config:
if not each_path.get("flow_analysis_id"):
self.log(
"Missing 'flow_analysis_id' for path: {0}".format(each_path),
"WARNING",
)
get_trace = self.get_path_trace(each_path)
if not get_trace:
self.msg = (
"Unable to get path trace for the flow analysis id: {0}".format(
each_path
)
)
self.log(self.msg, "DEBUG")
else:
self.have["assurance_pathtrace"].extend(get_trace)
else:
self.log(
"Found 'flow_analysis_id' for path: {0}".format(each_path), "DEBUG"
)
self.log("Current State (have): {0}".format(self.have), "INFO")
self.msg = "Successfully retrieved the details from the system"
self.status = "success"
return self
def get_path_trace(self, config_data):
"""
Get the path trace for the given playbook data and response with
flow analysis id.
Parameters:
self (object): An instance of a class used for interacting with Cisco Catalyst Center.
config (dict): A dict containing input data to get id for path trace.
Returns:
list: return the list of flow analysis IDs or None.
Description:
This function used to get the flow analysis id from the input config.
"""
offset_limit = 500
offset = 1
payload_data = dict(
limit=offset_limit, offset=offset, order="DESC", sort_by="createTime"
)