-
Notifications
You must be signed in to change notification settings - Fork 3
/
avalanche.py
1470 lines (1198 loc) · 62.1 KB
/
avalanche.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
###############################################################################
#
# Avalanche Python API
# by Spirent Communications
#
# Date: June 6, 2016
# Author: Matthew Jefferson - [email protected]
#
# Description: This is a Python wrapper for the Avalanche Tcl API. This software
# translates the Python commands into the corresponding Tcl commands.
# It also returns the response as a Python data structure, if
# possible.
#
###############################################################################
#
# Modification History
# Version Modified
# 1.0.0 6/6/2016 by Matthew Jefferson
# -Initial release.
#
# 1.1.0 10/28/2016 by Matthew Jefferson
# -Added the methods reserveAll and releaseAll.
# -Fixed a small issue with Python2 support.
#
# 1.1.1 11/11/2016 by Matthew Jefferson
# -Fixed an issue with av.config. Most values are now encased in
# curly brackets (just like the GUI export). This fixes an issue
# with some rare parameters.
# -Fixed an issue with av.perform and the "export" command.
#
# 1.1.2 11/30/2016 by Matthew Jefferson
# -Changed the init slightly so that the apipath and libpath are placed
# at the head of the Tcl auto_path. This will impact the usefulness
# of the TCLLIBPATH environment variable.
#
# 1.1.3 01/10/2017 by Matthew Jefferson
# -Uploaded the library to PyPI.
#
# 1.1.4 01/10/2017 by Matthew Jefferson
# -Messed up 1.1.3. Uploading the updated code.
#
# 1.1.5 01/10/2017 by Matthew Jefferson
# -Fixed a small issue with integers.
#
# 1.1.6 01/10/2017 by Matthew Jefferson
# -Fixed a small issue the getEvents() method. Window path delimiters
# need to be changed from "\"" to "/".
#
# 2.0.0 10/02/2020 by Matthew Jefferson
# -Replaced tkinter with the subprocess module. This was done because
# tkinter's version of Tcl is fixed to the version of Python used.
# This made it very challenging to find the necessary Tcl packages
# required by the Avalanche API. Also, moving to subprocess should
# allow 64-bit Python to use 32-bit Tcl.
#
###############################################################################
###############################################################################
# Copyright (c) 2016 SPIRENT COMMUNICATIONS OF CALABASAS, INC.
# All Rights Reserved
#
# SPIRENT COMMUNICATIONS OF CALABASAS, INC.
# LICENSE AGREEMENT
#
# By accessing or executing this software, you agree to be bound by the terms
# of this agreement.
#
# Redistribution and use of this software in source and binary forms, with or
# without modification, are permitted provided that the following conditions
# are met:
# 1. Redistribution of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistribution's in binary form must reproduce the above copyright notice.
# This list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name SPIRENT, SPIRENT COMMUNICATIONS, SMARTBITS, Spirent
# TestCenter, Avalanche, nor the names of its contributors may be used to
# endorse or promote products derived from this software without specific
# prior written permission.
#
# This software is provided by the copyright holders and contributors [as is]
# and any express or implied warranties, including, but not limited to, the
# implied warranties of merchantability and fitness for a particular purpose
# are disclaimed. In no event shall the Spirent Communications of Calabasas,
# Inc. Or its contributors be liable for any direct, indirect, incidental,
# special, exemplary, or consequential damages (including, but not limited to,
# procurement of substitute goods or services; loss of use, data, or profits;
# or business interruption) however caused and on any theory of liability,
# whether in contract, strict liability, or tort (including negligence or
# otherwise) arising in any way out of the use of this software, even if
# advised of the possibility of such damage.
#
###############################################################################
from __future__ import print_function
import sys
import getpass # Used to retrieve the username.
import ast # Used to convert strings to dict.
import os
import atexit
import re
from shutil import copyfile # Used for copying files.
# The following are required for logging.
import logging
import datetime
import inspect
#from inspect import getargvalues, stack
# if sys.hexversion >= 0x03000000:
# from tkinter import *
# else:
# from Tkinter import *
from subprocess import Popen, PIPE
class AVA:
###############################################################################
####
#### Public Methods
####
###############################################################################
def login(self, userName="", password="", mode="", workspace="", tempworkspace=False):
"""
Description:
Starts a new Avalanche Automation session or connects to an already existent session.
Uses the default workspace, named Default, if "workspace=<workspaceName>
option - value pair has not been defined. Use the "workspace=<workspaceName>"
option - value pair to log in to an existing custom workspace, or to create a new
custom workspace, and log in to it.
"""
self.LogCommand()
tclcode = "av::login"
if userName != "" or password != "" or mode != "":
if userName == "":
userName = getpass.getuser()
tclcode += " " + userName
if password != "" or mode != "":
if password == "":
password = "default"
# The password is currently ignored.
tclcode += " " + password
if mode != "":
tclcode += " " + mode
if tempworkspace:
tclcode += " -temp-workspace"
else:
if workspace != "":
tclcode += " -workspace " + workspace
result = self.Exec(tclcode)
logging.info("ABL Log Location: " + self.Exec("av::get system1 -ablLogLocation"))
logging.info("Username: " + self.Exec("av::get system1 -user"))
logging.info("Workspace: " + self.Exec("av::get system1 -workspace"))
logging.info("Project Path: " + self.Exec("av::get system1.metainfo -defaultDirectoryPath"))
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def logout(self):
"""
Description:
Closes the session, stops any running test, saves any non-saved data
on the disk, and stops the Avalanche Automation (java) process by
default, when either no argument is provided or when the shutdown
argument is provided (see examples). The only difference between the
shutdown and no-shutdown arguments is stopping or not stopping the
Avalanche Automation (java) process. The av.logout command with the
no-shutdown argument will leave the Avalanche Automation (java)
process running after the user logs out.
If the temporary workspace is used (see av.login command description),
the av.logout command deletes all the tests and test results that were
created during the session.
"""
self.LogCommand()
result = self.Exec("av::logout")
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def config(self, objecthandle, **kwargs):
"""
Description
Updates the attribute of an object with a new value, if it meets the validation rules.
Syntax
av.config objectHandle attrName=value [, [attrName=value] ...]
av.config objectHandle DANPath=value [, [DANPath=value] ...]
av.config DDNPath attrName=value [, [attrName=value] ...]
av.config DDNPath DANPath=value [, [DANPath=value] ...]
Comments
The av::config command modifies the value of one or more object attributes, if it meets
the validation rules. Note: If you attempt to modify an attribute for a read-only object,
or a specified value does not meet the validation rules, the av::config command raises
an exception.
-When you modify object attributes, use attrName/value pairs.
For example: av::config project1 -name Project1
-You can use Direct Descendant Notation (DDN) to identify the object and Descendant
Attribute Notation (DAN) to identify the attribute.
For example:
A DAN path is a dotted path name beginning with a sequence of one or more object types,
and ending with an attribute name. Avalanche Automation combines the handle (or the
DDNPath) with the DANPath to resolve the attribute reference. The path must identify
a valid sequence of objects in the Avalanche Automation data model hierarchy.
av::config $project.test -name Test1
av::config $project -userprofile.name SSLv3
In both DDN and DAN paths, an object type name may have an index suffix (an integer in
parentheses) to reference one of multiple children of the same type.
For more information about these notations, see Referencing Objects: Object Paths.
Return Value
None. Errors are raised as exceptions, encoded as string values that describe the error condition.
Example
av.config("userprofile1", dnsRetries=10)
av.config("userprofile1", cifsng.cifsngDataRandomization=true)
av.config(project + ".test.userprofile", sipng.firstRTPPort=1026)
"""
self.LogCommand()
tclcode = 'av::config ' + objecthandle + ' '
for key in kwargs:
#tclcode = tclcode + ' ' + '-' + key + ' "' + str(kwargs[key]) + '"'
reg = re.compile("\[")
if reg.match(str(kwargs[key])):
# This is a Tcl command (eg: [NULL]).
tclcode = tclcode + ' ' + '-' + key + " " + str(kwargs[key])
else:
tclcode = tclcode + ' ' + '-' + key + ' {' + str(kwargs[key]) + '}'
result = self.Exec(tclcode)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def get(self, objecthandle, *args):
"""
Description:
Returns the value(s) of one or more object attributes, or a set of
object handles.
Returns a single value if a single attribute is specified, otherwise,
a dictionary is returned.
Syntax:
av.get handle [attributeName]
av.get handle [DANPath]
av.get DDNPath [attributeName]
av.get DDNPath [DANPath]
av.get handle | DDNPath [relationName]
Comments
The av.get command returns the value of one or more object attributes, or,
in the case of relation references, one or more object handles.
-The handle identifies the object from which data will be retrieved.
If you do not specify any attributes, Avalanche Automation returns the
values for all attributes and all relations defined for the object.
-attributeName identifies an attribute for the specified object.
-The DANPath (Descendant Attribute Notation path) is a dotted path name
beginning with a sequence of one or more relation names, and ending with
an attribute name. A relation name may have an index suffix (an integer
in parenthesis) to reference one of multiple children of the same type.
Avalanche Automation combines the handle (or the DDNPath) with the DANPath
to resolve the attribute reference. The path must identify a valid
sequence of objects in the test hierarchy. For example:
av.get(project, "test(1).name")
-Avalanche Automation combines the object and attribute specifications to
retrieve the value of the attribute for the first Test object child of the
project.
-The DDNPath (Direct Descendant Notation path) is a dotted path name sequence.
The sequence begins with an object handle, followed by one or more relation
names. The path must identify a valid sequence of objects in the data model
hierarchy. Avalanche Automation returns data for the object identified by
the last name in the sequence. For example:
av.get("project1.test", "name")
In this case, Avalanche Automation returns the value of the name attribute
for the first Test child of the specified Project object.
-If there is more than one instance of a particular object type, as children
of the specified object, use an index notation. (In the example above, the
index value 1 is implied.) Avalanche Automation assigns index values in the
order of object creation. For example:
av.get(project + ".test(2)")
Avalanche Automation returns the attributes and all relations for the second
Test object child of the specified Project object.
When you use a relation reference with the get function, it provides access
to one or more objects connected to the object identified by a handle (or DDNPath).
Specify a name for the relation reference, using relationName.
For example:
av.get(project, "Tests")
This function call returns the handle(s) for the Test child object(s) of the
Project object.
Return Value
When you retrieve one or more attributes, av.get returns the single attribute
value or a dictionary. If you do not specify any attributes, the get function
can return either a single value or a dictionary.
Errors are raised as exceptions, encoded as string values that describe the
error condition.
Example
av.get(project, "path")
av.get(test, "netrworkprofile.tcpoptions.tcptimeout")
av.get(project + "userprofile(2)", "nfs.dataRandomization")
"""
self.LogCommand()
tclcode = "av::get " + objecthandle
for key in args:
tclcode += " -" + key
result = self.Exec(tclcode)
# Determine if we need to return a dictionary or just the result of the command.
if len(args) == 0:
result = self.List2Dict(result)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def perform(self, command, objecthandle, **kwargs):
"""
Description
Executes a custom command or subcommand for the specified object.
Syntax
av.perform(<sub-command>, <handle>, [[<argument>], [...])
Comments
The av.perform command executes a sub-command. See the Avalanche Automation
Object Reference document (Avalanche_Auto_Obj_Ref.pdf) for a complete list
of sub-commands.
Return Value
The return value depends on the sub-command type, and is absent in most cases.
Errors are raised as exceptions, encoded as string values that describe the
error condition.
Example
av.perform("save", "system1")
av.perform("export", "system1", projectsTestsHandles="test1")
"""
self.LogCommand()
tclcode = "av::perform " + command + " " + objecthandle
if command == "SetInterfaceAttributes":
# WARNING: This AVA API command IGNORES the argument names, and instead makes the arguments position dependent.
# av::perform SetInterfaceAttributes $tests_handle.topology.interface(1) -port 10.140.99.40/1/1 -physIf 0 -interfaceDisplayString 0,0 -interfaceLocationString 0,0
port = kwargs["port"]
physIf = kwargs["physIf"]
display = kwargs["interfaceDisplayString"]
locationstring = kwargs["interfaceLocationString"]
tclcode += " -ignored1 " + str(port) + " -ignored2 " + str(physIf) + " -ignored3 " + str(display) + " -ignored4 " + str(locationstring)
elif command == "Export":
# For this command, the arguments MUST be in a specific order (don't ask me why).
test = kwargs["projectstestshandles"]
tclcode += " -projectstestshandles " + str(test)
if "options" in kwargs:
tclcode += ' -options "' + str(kwargs["options"]) + '"'
if "newpath" in kwargs:
newpath = kwargs["newpath"]
tclcode += ' -newpath "' + str(kwargs["newpath"]) + '"'
else:
for key in kwargs:
tclcode = tclcode + " " + "-" + str(key) + ' "' + str(kwargs[key]) + '"'
result = self.Exec(tclcode)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def apply(self, testHandle, trial=0, continueIfAlreadyRunning=0, removeOldTest=0, rerun=0):
"""
Description
Starts a specified test on the devices.
Syntax
av.apply(<testHandle>, [trial], [continueIfAlreadyRunning], [removeOldTest], [rerun])
Comments
The apply command saves the configuration, performs validation, uploads
test configuration to devices, and runs (or reruns) the test. This call
is asynchronous, so the client will get control right after the call.
The standard async_method_completed event will be sent after the test is
started; the specific test state events will also be sent. For more
information, please refer to Running the Test.
Return Value
The request id. Errors are raised as exceptions, encoded as string values
that describe the error condition.
Example
av.apply(testHandle)
av.apply(testHandle, 1)
"""
self.LogCommand()
tclcode = "av::apply " + testHandle + " " + str(trial) + " " + str(continueIfAlreadyRunning) + " " + str(removeOldTest) + " " + str(rerun)
result = self.Exec(tclcode)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def connect(self, ipAddress, type="", executesynchronous=""):
"""
Description
Connects to the specified device.
Syntax
av.connect(<ipAddress>, [<type>="STC|Appliance"], [<executesynchronous>=false|true])
Comments
After this command call is completed, the device will be added to the device
list under the PhysicalChassisManager object. If Avalanche Automation is already
connected to this device, then it gets the latest state from the device and returns
the existent handle. The av::connect command runs in synchronous mode by default.
The mode can be changed by using the -executesyschronous Boolean option.
If a -type is not specified, the Appliance platform will be assumed and tried. If
Avalanche TclAPI fails to connect, then the Spirent TestCenter chassis is assumed
to be the hardware platform, and the connection will be retried.
Return Value
The request id, if run in asynchronous mode; the chassis/appliance handle, if run
in synchronous mode (default). Otherwise, errors will be displayed on the screen.
Example
av.connect("10.72.55.80")
"""
self.LogCommand()
tclcode = "av::connect " + ipAddress
if type != "":
tclcode += " -type " + type
if executesynchronous != "":
tclcode += " -executesynchronous " + executesynchronous
requestid = self.Exec(tclcode)
logging.debug(" - Python result - " + str(requestid))
return requestid
#==============================================================================
def create(self, objecttype, under, **kwargs):
"""
Description
Creates a new object of the specified type, under the specified parent.
Syntax
av.create(<objecttype>, [attr=<value>] ...])
Comments
The av.create command creates one or more Avalanche Automation objects under the
specified parent object. When you call the create function, you specify the type(s)
of one or more objects to be created. You can specify:
-An object type name (such as the Project object or the Test object). For example:
av.create("project", under="system1", name="Project1")
-When you create an object, you must specify the handle of the parent object
under which the new object is to be created.
-When you create an object, you can also set the object attributes at the same
time. To set attributes, specify one or more attribute name/value pairs.
-If you specify attribute name/value pairs, together with an object type path,
Avalanche Automation applies the attribute values to the object associated
with the last name specified in the object type path. In the following example,
Avalanche Automation creates a Project object. When Avalanche Automation creates
the Project object, it sets the name attribute to Project1 and the path attribute
to C:\Project\Project1.
av.create("project", under="system1", name="Project1", path="C:/Projects/Project1")
-You can specify a Descendant Attribute Notation (DAN) path as part of the attribute
reference. Avalanche Automation uses the specified object type to create the primary
object, and the DAN path to create any additional objects. For information about path
name specification, see Object, Attribute, and Relation References.
Return Value
The av.create command returns a unique string value that is the object handle
for the object specified in the function call. (The av.create function returns only
the handle for the primaryobject that is created. To obtain the handles for any
descendent objects that are created, use the get function to retrieve the child objects.)
Example
project = av.create("project", under="system1", name="Project1")
test = av.create("tests", under=project, name="Test1", testType="deviceComplex")
sp = av.create("ServerProfiles", under=project, name="ServerProfile", applicationProtocol="HTTP", http.keepAlive="on")
"""
self.LogCommand()
tclcode = "av::create " + objecttype + " -under " + under
for key in kwargs:
tclcode = tclcode + " " + "-" + key + " " + str(kwargs[key])
objecthandle = self.Exec(tclcode)
logging.debug(" - Python result - " + str(objecthandle))
return objecthandle
#==============================================================================
def delete(self, handle):
"""
Description
Deletes a node from the data model.
Syntax
av.delete(<handle>)
av.delete(<DDNPath>)
Comments
Deletes the object identified by the objectHandle or DDNPath from the
data model. Avalanche Automation also deletes all descendants of the
specified object (if any).
Return Value
None.
Errors are raised as exceptions, encoded as string values that
describe the error condition.
Example
av.delete(projectHandle)
av.delete(projectHandle + ".userp")
"""
self.LogCommand()
tclcode = "av::delete " + handle
result = self.Exec(tclcode)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def disconnect(self, ipAddress):
"""
Description
Disconnects from the specified device and removes it from the device
list under the PhysicalDevicesManager object.
Syntax
av.disconnect(<ipAddress>)
Comments
Does nothing, if Avalanche Automation was not connected to specified device.
Return Value
None. Errors are raised as exceptions, encoded as string values that
describe the error condition.
Example
av.disconnect("10.50.20.77")
"""
self.LogCommand()
tclcode = "av::disconnect " + ipAddress
result = self.Exec(tclcode)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def getEvents(self):
"""
Description
Gets the events that were generated by Avalanche Automation for this session,
from the latest av.getEvents call.
Syntax
av.getEvents
Comments
None.
Return Value
A list of generated event dictionaries. Refer to Events Handling for event object details.
Errors are raised as exceptions, encoded as string values that describe the error condition.
Example
av.getEvents
"""
self.LogCommand()
tclcode = "av::getEvents"
tclresult = self.Exec(tclcode)
# Convert any backslashes to forward slashes. This may happen on Windows, which uses the backslash
# for file path delimiters.
tclresult = re.sub(r"\\", r"/", tclresult)
# The goal is to create a Python list of dictionaries, where element of the list
# is a Avalanche Event dictionary with the keys "message", "additional" and "name".
# The "additional" key is also a dictionary.
eventlist = []
# First step is to break the Tcl list into a Python list (using Tcl).
tclcode = "set pythonlist {};"
tclcode += "foreach event [list " + tclresult + "] {"
tclcode += " regsub -all {\\n} $event {} event;"
tclcode += " append pythonlist \\\"$event\\\" , "
tclcode += "};"
tclcode += "puts $pythonlist"
#tclresult = self.Exec(tclcode)
# This step is what converts the Tcl string to a list.
#listofstrings = ast.literal_eval(tclresult)
listofstrings = self.Exec(tclcode)
# Now convert each list element to a dictionary.
for event in listofstrings:
# Construct a Python dictionary using Tcl.
# This is done because it is MUCH easier for Tcl to deal with Tcl lists.
tclcode = "set pythondict \{;"
tclcode += "foreach element [list " + event + "] {"
tclcode += " append pythondict \"\\\"[lindex $element 0]\\\": \\\"[lindex $element 1]\\\", \""
tclcode += "}; append pythondict \}; puts $pythondict"
#tclresult = self.tcl.eval(tclcode)
#eventdict = ast.literal_eval(tclresult)
eventdict = self.Exec(tclcode)
# The "addtional" field may contain an additional list of information.
# This is also converted into a dictionary.
if eventdict["additional"] != "":
tclcode = "set pythondict \{;"
tclcode += "foreach element [list " + eventdict["additional"] + "] {"
tclcode += " append pythondict \"\\\"[lindex $element 0]\\\": \\\"[lindex $element 1]\\\", \""
tclcode += "}; append pythondict \}; puts $pythondict"
#tclresult = self.tcl.eval(tclcode)
#eventdict["additional"] = ast.literal_eval(tclresult)
eventdict["additional"] = self.Exec(tclcode)
eventlist.append(eventdict)
logging.debug(" - Python result - " + str(eventlist))
return eventlist
#==============================================================================
def getSessions(self):
"""
Description
Retrieves the list of registered sessions on Avalanche Automation.
Syntax
av.getSessions
Comments
You can use this function without being logged in to an Avalanche Automation session.
Return Value
A list of registered sessions.
Example
av.getSessions
"""
self.LogCommand()
tclcode = "av::getSessions"
tclresult = self.Exec(tclcode)
logging.debug(" - Python result - " + str(tclresult))
return tclresult
#==============================================================================
def release(self, portAddress):
"""
Description
Releases the specified port.
Syntax
av.release(<portAddress>)
Comments
You can only release ports that you have reserved. For information about
port reservations, and the syntax for identifying ports, see the description
of the reserve function.
Return Value
None. Errors are raised as exceptions, encoded as string values that
describe the error condition.
Example
av.release("10.50.70.82/1/1")
"""
self.LogCommand()
tclcode = "av::release " + portAddress
result = self.Exec(tclcode)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def reserve(self, portAddress):
"""
Description
Reserves the specified port.
Syntax
av.reserve(<portAddress>)
Comments
Reserves the specified port for the username that was specified/determined
upon the av.login command. You must be connected to the chassis (or appliance)
before you can reserve ports. The port can only be reserved if it is available
(that is, if not disabled or reserved by another user). To force reserve a
port, use the av.perform("reservePort") command.
Return Value
Handle to the PhysicalPort object.
Errors are raised as exceptions, encoded as string values that describe
the error condition.
Example
av.reserve("10.50.70.82/2/1")
"""
self.LogCommand()
tclcode = "av::reserve " + portAddress
porthandle = self.Exec(tclcode)
logging.debug(" - Python result - " + str(porthandle))
return porthandle
#==============================================================================
def reserveAll(self, test, force=False, chassistype=""):
# Reserve all of the interfaces defined in the test.
# Do this by determining the existing interfaces, and then reserving them.
# The user only need set the "port" attribute for each interface object
# before calling this method.
#
# This method replaces the following native calls:
# perform("SetInterfaceAttributes")
# connect(<chassisip>)
# perform("ReservePort") or reserve()
# We need to find the information for the "SetInterfaceAttributes" command.
# It is found in the physical port information when you connect to the hardware/virtual.
for config in self.get(test, "configuration").split():
for topology in self.get(config, "topology").split():
for interface in self.get(topology, "interface").split():
location = self.get(interface, "port")
# We need to connect to the chassis to pull the physical port information.
# We could do this more efficiently (only connect once per unquie chassis).
chassisip = self.get(interface, "adminIPAddress")
self.connect(chassisip, type=chassistype)
# Locate the physical port referenced by the "location".
for chassis in self.get("system1.physicalchassismanager", "physicalchassis").split():
for module in self.get(chassis, "physicaltestmodules").split():
for port in self.get(module, "ports").split():
if self.get(port, "location") == location:
# We found the port. Now map and reserve it.
physif = self.get(port, "physIf")
ids = self.get(port, "locationDisplayString")
ils = self.get(port, "locationString")
self.perform("SetInterfaceAttributes", interface, port=location, physIf=physif, interfaceDisplayString=ids, interfaceLocationString=ils)
if force:
self.perform("ReservePort", "system1", portaddress=location, force="force")
else:
self.perform("ReservePort", "system1", portaddress=location)
return
#==============================================================================
def releaseAll(self):
# Release all ports that are currently reserved by this process.
for chassis in self.get("system1.physicalchassismanager", "physicalchassis").split():
for module in self.get(chassis, "physicaltestmodules").split():
for port in self.get(module, "ports").split():
if self.get(port, "reservationState") == "Reserved by User":
self.release(self.get(port, "location"))
return
#==============================================================================
def setABLLogAutoCleanup(self, bEnabled):
"""
Description
Automatically removes the current test's log files at the end of the test run.
Syntax
av.setABLLogAutoCleanup(<bEnabled>)
Comments
Automatically removes the current test's log files at the end of the test run.
Return Value
None.
Example
av.setABLLogAutoCleanup(1)
"""
self.LogCommand()
tclcode = "av::setABLLogAutoCleanup " + str(bEnabled)
result = self.Exec(tclcode)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def downloadABLlogs(self, path=""):
"""
Description
Don't know...it's not a documented command.
Syntax
av.downloadABLlogs(<path>)
Comments
I'm assuming this downloads the ABL logs to the specified path.
Return Value
None.
Example
av.downloadABLlogs()
"""
self.LogCommand()
if path == "":
path = os.path.abspath(os.getcwd())
tclcode = "av::downloadABLlogs " + path
result = self.Exec(tclcode)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def subscribe(self, side, viewAttributesList):
"""
Description
Subscribes to view the list of runtime statistics that users specify.
Syntax
av.subscribe(<side>, <viewAttributesList>)
Comments
Subscribes to view runtime statistics for those that user specifies as
viewAttributesList. The attribute names should be one of the supported
statistics names, for example, http,successfulConns or http,attemptedConns.
Wildcards are also supported, such as http*. Please refer to Appendix A.
List of Runtime Statistics for full list of runtime statistics.
Return Value
Returns the handle to the ResultDataSet object, which consists of the
ResultDataObject with statistics values. By default, the returned
ResultDataSet will only contain the latest actual values. In order to
obtain the values from a specific point in time during the test run, user
must add the 'all' keyword to the list of viewAttributesList. See Runtime
statistics for more information.
Example
av.subscribe("client", ["http,successfulConns", "http,attemptedConns"])
av.subscribe("server", "http*")
"""
self.LogCommand()
tclcode = "av::subscribe " + side + " [list " + " ".join(viewAttributesList) + "]"
resultdataset = self.Exec(tclcode)
logging.debug(" - Python result - " + str(resultdataset))
return resultdataset
#==============================================================================
def unsubscribe(self, handle):
"""
Description
Removes a subscription for the specified ResultDataSet.
Syntax
av.unsubscribe(<handle>)
Comments
The av.unsubscribe command removes a subscription for the specified handle
of the ResultDataSet object that was returned by the subscribe function.
Return Value
None. Errors are raised as exceptions, encoded as string values that
describe the error condition.
Example
av.unsubscribe(rdsHandle)
"""
self.LogCommand()
tclcode = "av::unsubscribe " + handle
result = self.Exec(tclcode)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================
def waitUntilCommandIsDone(self, requestId=""):
"""
Description
Waits until the command specified by the request id is complete.
Syntax
av.waitUntilCommandIsDone(<requestId>)
Comments
This function waits until the command, specified by request id, is out
of the PENDING state and completes its job. This is useful for asynchronous
commands. If the requestId is not specified, then it waits until any
command is completed.
Return Value
Result of an asynchronous command.
Errors are raised as exceptions, encoded as string values that describe the error condition.
Example
av.waitUntilCommandIsDone(av.connect("10.50.70.82", executesynchronous=False))
CAUTION: "av.waitUntilCommandIsDone" will time out if the command it waits for does
not complete in a certain time.
"""
self.LogCommand()
tclcode = "av::waitUntilCommandIsDone"
# NOTE: Not yet tested!
if requestId != "":
tclcode += " " + requestId
tclresult = self.Exec(tclcode)
logging.debug(" - Python result - " + str(tclresult))
return tclresult
#==============================================================================
def handleOf(self, parentHandle, relationName, objectName):
"""
Description
Retrieves the handle of an object by its type name, parent handle, and
name of the object.
Syntax
av.handleOf(<parentHandle>, <relationName>, <objectName>)
Comments
Searches children that are identified by the relation name of the parent
object, identified by a handle, and retrieves the handle of the object
named objectName. The object that is searched should have a name attribute.
Return Value
Handle of the object.
Errors are raised as exceptions, encoded as string values that describe the
error condition.
Example
av.handleOf("project1", "serverprofiles", "IPv6")
av.handleOf("system1", "projects", "Project1")
"""
self.LogCommand()
tclcode = "av::handleOf " + parentHandle + " " + relationName + " " + objectName
objecthandle = self.Exec(tclcode)
logging.debug(" - Python result - " + str(objecthandle))
return objecthandle
#==============================================================================
def nodeExists(self, handle):
"""
Description
Checks whether the object specified by the handle exists in the data model.
Syntax
av.nodeExists(<handle>)
Comments
Checks whether the object specified by the handle exists in the data model
(i.e. the get function will return information about the object).
Return Value
1, if the object exists in the data model, otherwise 0.
Example
av.nodeExists("project1")
av.nodeExists(testHandle)
"""
self.LogCommand()
tclcode = "av::nodeExists " + handle
#tclresult = self.Exec(tclcode)
#result = ast.literal_eval(tclresult)
result = self.Exec(tclcode)
logging.debug(" - Python result - " + str(result))
return result
#==============================================================================