File: chassis.exp

package info (click to toggle)
opa-ff 10.10.3.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 17,372 kB
  • sloc: ansic: 189,318; sh: 25,941; perl: 15,145; exp: 8,415; makefile: 5,315; xml: 189; cpp: 93; tcl: 50; python: 44
file content (2292 lines) | stat: -rw-r--r-- 74,473 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
# BEGIN_ICS_COPYRIGHT8 ****************************************
#
# Copyright (c) 2015-2020, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions 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.
#     * Neither the name of Intel Corporation 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 COPYRIGHT OWNER OR 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.
#
# END_ICS_COPYRIGHT8   ****************************************

# [ICS VERSION STRING: unknown]

# assumptions:
#	slot numbers are alphanumeric: A-Za-z0-9 (actually are numeric in <=3.1)
#	card types are alphanumeric with dot, dash, underscore: A-Za-z0-9._-
#	versions are alphanumeric with dot, dash, underscore: A-Za-z0-9._-

# This is an expect (tcl) library of procedures to aid testing
# These functions provide support for chassis operations
# against a test target chassis

## tcl procedures to support testing:
## =============================================

global os_type

proc get_chassis_sftp_status { slot {wait_done 1 }} {
##
## get_chassis_sftp_status
## -------------------------
## return the exit status of the last sftp executed against the given slot
##
## Usage:
##	get_chassis_sftp_status slot [wait_done]
## Arguments:
##	slot - slot number to get status for
##	wait_done - if status is "In Progress", loop until changes to another value
## Returns:
##	"success"
##	"skipped" - transfer unnecessary
##	"invalid" - invalid firmware image for card type
##	"inprogress" - transfer still in progress, but wait_done not set
##	"error" - other errors or timeouts
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	while { 1 } {
		send_chassis_cmd "showLastScpRetCode $slot"
		set out [expect_list 60 { "Code: [0-9]+:" } { "usage" "Invalid" "Error" "Failed" "roblem" "roblem" "not found"} ]
		regexp {[0-9]+} $out ret
		expect_chassis_prompt 60

		if { $ret == 0 } {
			return "success"
		} elseif { $ret == 1 } {
			# unneeded
			return "skipped"
		} elseif { $ret == 2 } {
			# wrong card type
			return "invalid"
		} elseif { $ret == 3 } {
			if { ! $wait_done } {
				# transfer in progress internal to chassis
				return "inprogress"
			}
			# can be a few minutes, so lets be a little patient
			sleep 20
		} else {
			# other errors
			return "error"
		}
	}
}

proc check_chassis_capability { capability } {
##
## check_chassis_capability
## -------------------------
## return level of support for the given capability by the current chassis
##
## Usage:
##	check_chassis_capability capability
## Arguments:
##	capability - the capability name to check
## Returns:
##	0 - capability not supported
##	>0 - version of capability supported
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	send_chassis_cmd "showCapability -key $capability"
	# the [:space:] bounds the + so we get all the data on the line
	set out [expect_list 60 "{$capability: \[0-9A-Za-z\]+\[\[:space:\]\]}" { "usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
	expect_chassis_prompt 60
	#log_message "out=$out"

	# throw away the \r\n, there may be trailing spaces, TCL lists will ignore
	regexp {: ([A-Za-z0-9]+)[[:space:]]} $out line ret
	#log_message "ret=$ret"

	if { [ string equal -nocase "$ret" "unavailable" ] } {
		set ret 0
	}

	return "$ret"
}

proc get_chassis_slots { { card_type "" } } {
##
## get_chassis_type_slots
## -------------------------
## return the list of slots in the given chassis of the given card type
## if no card type is specified, a list of all slots in the chassis is returned
##
## Usage:
##	get_chassis_type_slots [card_type]
## Arguments:
##	card_type - card type, default is "" (eg. all slots)
## Returns:
##	list of slots in chassis
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	if { ! [ string equal "$card_type" "" ] } {
		send_chassis_cmd "chassisQuery -ignoreInvalidType -type $card_type"
	} else {
		send_chassis_cmd "chassisQuery"
	}
	# this could return no slots, in which case "none" is output
	# The \r\n is needed to bound the + so we get all the data on the line
	set out [expect_list 60 "{slot\[s\]*: \[0-9A-Za-z \]+\[\r\n\]}" { "usage" "Error" "Failed" "roblem" "not found"} ]
	expect_chassis_prompt 60
	#log_message "out=$out"

	# The \r\n is needed to bound the + so we get all the data on the line
	# there may be trailing spaces, TCL lists will ignore
	regexp {: ([0-9A-Za-z ]+)[\r\n]+} $out line ret
	#log_message "ret=$ret"
	if { [ regexp -nocase "none" "$ret" ] } {
		set ret ""
	}

	return "$ret"
}

proc list_esm_security_files { } {
##
## list_esm_security_files
## -------------------------
## return the list of FM security files in the given chassis
##
## Usage:
##	list_esm_security_files
## Arguments:
##	none
## Returns:
##	list of .pem files in chassis
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	send_chassis_cmd "smListSecurityFiles -showSingleLine"

	# this could return no *.pem files, in which case "none" is output
	# The \r\n is needed to bound the + so we get all the data on the line
    set out [expect_list 60 "{files: \[0-9A-Za-z._ \]+\[\r\n\]}" { "usage" "Error" "Failed" "roblem" "not found"} ]
	expect_chassis_prompt 60
	#log_message "out=$out"

	# The \r\n is needed to bound the + so we get all the data on the line
	# there may be trailing spaces, TCL lists will ignore
    regexp {: ([0-9A-Za-z._ ]+)[\r\n]+} $out line ret
	#log_message "ret=$ret"
	if { [ regexp -nocase "none" "$ret" ] } {
		set ret ""
	}

	return "$ret"
}

proc get_chassis_card_type { slot } {
##
## get_chassis_card_type
## -------------------------
## return the card type of the given slot in the given chassis
##
## Usage:
##	get_chassis_card_type slot
## Arguments:
##	slot - slot to query
## Returns:
##	card type of given slot
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	# DONE
	global spawn_id expect_out spawn_out timeout
	global expecting

	send_chassis_cmd "chassisQuery -showType $slot"
	# require trailing whitespace to anchor end of string
	set out [expect_list 60 "{type: \[a-zA-Z0-9._-\]+\[\[:space:\]\]+}" { "usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
	# just get the card type field
	regexp {: ([a-zA-Z0-9._-]+)[[:space:]]+} $out line ret

	expect_chassis_prompt 60

	return "$ret"
}

proc is_management_card_type { card_type } {
##
## is_management_card_type
## -------------------------
## is the given card_type a management card for the current chassis
##
## Usage:
##	is_management_card_type card_type
## Arguments:
##	card_type - card type to test
## Returns:
##	0-> this card type is not the management card type for this chassis
##	1-> this card type is the management card type for this chassis
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##
## Our best way to do this is to query slot 0 for its card type.  Slot 0
## is always the management card

    	set my_slot [ get_this_slot_number ]
	return [string equal [ get_chassis_card_type $my_slot] "$card_type" ]
}

proc get_this_slot_number { } {
##
## get_this_slot_number
## -------------------------
## return the slot number of the management card we are logged into
##
## Usage:
##	get_this_slot_number
## Arguments:
## Returns:
##	slot number
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	send_chassis_cmd "chassisQuery -master"
	# this could return no slots, in which case "none" is output
	# The \r\n is needed to bound the + so we get all the data on the line
	set out [expect_list 60 "{master: \[0-9A-Za-z \]+\[\r\n\]}" { "usage" "Error" "Failed" "roblem" "not found"} ]
	expect_chassis_prompt 60
	#log_message "out=$out"

	# The \r\n is needed to bound the + so we get all the data on the line
	# there may be trailing spaces, TCL lists will ignore
	regexp {: ([0-9A-Za-z ]+)[\r\n]+} $out line ret
	#log_message "ret=$ret"
	if { [ regexp -nocase "none" "$ret" ] } {
		set ret ""
	}

	return "$ret"
}

proc get_chassis_active_firmware_version { slot } {
##
## get_chassis_active_firmware_version
## -------------------------
## return the presently running firmware_version of the given slot
## in the given chassis
##
## Usage:
##	get_chassis_active_firmware_version slot
## Arguments:
##	slot - slot to query
## Returns:
##	firmware version of given slot
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	send_chassis_cmd "fwVersion $slot"
 	# echo of command
	set out [expect_list 60 { "fwVersion" "Firmware Version: [a-zA-Z0-9.]+[[:space:]]" } { "usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
	regexp {: ([a-zA-Z0-9._-]+)[[:space:]]} $out line ret
	expect_chassis_prompt 60

	return "$ret"
}

proc get_file_firmware_version { fw_file } {
##
## get_file_firmware_version
## -------------------------
## return the firmware version of the given local file
##
## Usage:
##	get_file_firmware_version fw_file
## Arguments:
##	fw_file - local pathname of file
## Returns:
##	firmware version of given file
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	log_message "opafirmware --showVersion $fw_file\n"
	set fw_version [exec /usr/sbin/opafirmware --showVersion $fw_file]
	log_message "Version: $fw_version\n"
	return "$fw_version"
}

proc get_file_firmware_type { fw_file } {
##
## get_file_firmware_type
## -------------------------
## return the firmware type of the given local file
##
## Usage:
##	get_file_firmware_type fw_file
## Arguments:
##	fw_file - local pathname of file
## Returns:
##	firmware card type of given file
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	log_message "opafirmware --showType $fw_file"
	set card_type [exec /usr/sbin/opafirmware --showType $fw_file]
	log_message "Card Type: $card_type\n"
	return "$card_type"
}

proc get_chassis_firmware_version { slot {location "act"} } {
##
## get_chassis_firmware_version
## -------------------------
## return the firmware version of the given slot in the chassis
##
## Usage:
##	get_chassis_firmware_version slot [alternate]
## Arguments:
##	slot - slot to query
##	location - location to query
##		"pri" -> primary
##		"alt" -> alternate
##		"act" -> active (running) (default)
## Returns:
##	firmware version of given slot
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	if { [string equal "$location" "act"] } {
		return [get_chassis_active_firmware_version $slot]
	}

	if { [string equal "$location" "alt"] } {
		#set cmd "bootQuery $slot -showVersion -alternate"
		set cmd "bootQuery $slot -alternate"
	} else {
		#set cmd "bootQuery $slot -showVersion"
		set cmd "bootQuery $slot"
	}
	send_chassis_cmd "$cmd"
	set out [expect_list 60 "{version: \[a-zA-Z0-9._-\]+\[\[:space:\]\]}" { "usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
	regexp {: ([a-zA-Z0-9._-]+)[[:space:]]} $out line ret
	expect_chassis_prompt 60

	return "$ret"
}

proc select_chassis_firmware_version { slot fw_version } {
##
## select_chassis_firmware_version
## -------------------------
## select the given firmware version for the given slot in the chassis
##
## Usage:
##	select_chassis_firmware_version slot fw_version
## Arguments:
##	slot - slot to act on
##	fw_version - firmware version to select
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	chassis_cmd 60 0 "bootSelect $slot -version $fw_version"

	return 0
}

proc sftp_chassis_firmware { chassis fw_file {select 0} {slot "all"} } {
##
## sftp_chassis_firmware
## -------------------------
## push the given firmware file to the specified slots in the
## given chassis.
##
## Usage:
##	sftp_chassis_firmware chassis fw_file [select [slot]]
## Arguments:
##	chassis - IP address/name of chassis to sftp to
##	fw_file - local pathname of file to push
##	select - should image be selected (0->no (default), 1->yes)
##	slot - slot to push to, default is "all" of given card type
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
	global spawn_id expect_out spawn_out timeout
	global expecting

	if { $select } {
		set nopt ""
	} else {
		set nopt "n"
	}

	# Create a basename for the firmware on the server
	set base [ exec basename $fw_file ]

	if { [ string equal "$slot" "all" ] } {
		chassis_sftp_cmd "sftp admin@\\\[$chassis\\\]:" "put $fw_file A$nopt:/firmware/$base"
	} else {
		chassis_sftp_cmd "sftp admin@\\\[$chassis\\\]:" "put $fw_file $nopt$slot:/firmware/$base"
	}
}

proc push_chassis_firmware { chassis fw_file fw_version card_type {action "push"} { slots "all" } } {
##
## push_chassis_firmware
## -------------------------
## push the given firmware file to the specified slots in the
## given chassis.
##
## Usage:
##	push_chassis_firmware chassis fw_file fw_version card_type [action [slots]]
## Arguments:
##	chassis - IP address/name of chassis to sftp and telnet to
##	fw_file - local pathname of file to push
##	fw_version - firmware version of fw_file
##	card_type - card type of fw_file
##	action - action after pushing firmware:
##			push   - do not reboot, do not change as primary
##					is already primary, no change
##			select - select as primary, but do not reboot
##			run    - select as primary and reboot to activate firmware
##	slots - list of slot numbers to push to, default is "all" of given card type
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##
##  Any slots which are not of a card type which matches that
##	of the firmware file, are quietly skipped.  This allows multi-file
##  push to work with a slot list.
##
##	If the sftp fails for some slots but succeeds for others.  The failed slots
##	are logged and an exception will be thrown, however the good slots
##	are processed to completion [except for the reboot] (if possible)
##  so that they are not left in an odd state

	global spawn_id expect_out spawn_out timeout
	global expecting

	# caller has already done this
	#set fw_version [get_file_firmware_version $fw_file]
	#set card_type [get_file_firmware_type $fw_file]

	# variables:
	#	select - do we want to select the image as primary
	#	active - do we want to select the image as primary and activate it
	if { [ string equal "$action" "push"] } {
		set select 0
		set active 0
	} elseif { [string equal "$action" "select"] } {
		set select 1
		set active 0
	} elseif { [string equal "$action" "run"] } {
		set select 1
		set active 1
	} else {
		fail_test "Invalid firmware push action: $action"
	}

	log_message "\nTest: Seeing if $chassis present running firmware supports firmware push ...\n"
	target_chassis_admin_sh $chassis
	if { [ catch { set cap [check_chassis_capability "fwPush"] } res ] != 0
	     || $cap < 1 } {
		fail_test "Chassis $chassis present running firmware does not support fwPush:\n$res"
	}
	log_message "\nTest: Getting Slot list from $chassis ...\n"

	if { [string equal "$slots" "all"] } {
		# push to all slots of matching card type
		set push_slots [get_chassis_slots $card_type]
		set num_slots [llength $push_slots]
		#set push_all 0
		set push_all 1
	} else {
		# push to a specific set of slots
		set push_slots "$slots"
		set push_all 0
	}
	set num_slots [llength $push_slots]
	if { $num_slots == 0 } {
		target_chassis_admin_sh_exit
		skip_case "No cards of type $card_type"
		return 0
	}
	set is_mgmt [ is_management_card_type $card_type]
	set use_rebootall 0
	if { $is_mgmt } {
		if { [ catch { set cap [check_chassis_capability "redundantMgmtCapable"] } res ] == 0
	     && $cap >= 1 } {
		 	set use_rebootall 1
		}
	}

	log_message "\nTest: Getting present installed firmware from $chassis ...\n"
	# first get present firmware versions for each of the slots
	set act_fw_versions ""
	set pri_fw_versions ""
	set alt_fw_versions ""
	set card_types ""
	foreach slot $push_slots {
		lappend card_types [get_chassis_card_type $slot]
		lappend act_fw_versions [get_chassis_firmware_version $slot "act"]
		lappend pri_fw_versions [get_chassis_firmware_version $slot "pri"]
		lappend alt_fw_versions [get_chassis_firmware_version $slot "alt"]
	}
	target_chassis_admin_sh_exit
	# DEBUG
	#log_message "push_slots: $push_slots"
	#log_message "num_slots: $num_slots"
	#log_message "card_types: $card_types"
	#log_message "act_fw_versions: $act_fw_versions"
	#log_message "pri_fw_versions: $pri_fw_versions"
	#log_message "alt_fw_versions: $alt_fw_versions"
	#log_message "is_mgmt: $is_mgmt"

	log_message "\nTest: Determining operations needed to upgrade to $fw_file for $chassis ..."
	# now check that card types match and determine if we need to push firmware
	set need_push ""
	set need_select ""
	set need_reboot ""
	set have_card 0
	for { set i 0 } { $i < $num_slots } { incr i } {
		set slot [lindex $push_slots $i]
		if { ! [ string equal "$card_type" [lindex $card_types $i] ] } {
			# if this happens card type is inconsistent with original
			# query for slots of given card type (hot swap during update?)
			# or user has explicitly specified slots and picked one of
			# wrong card type
			# quiety ignore this slot
			lappend need_push 0
			lappend need_select 0
			lappend need_reboot 0
			log_message "Test: Skipping slot $slot: is of type [lindex $card_types $i], $fw_file is for $card_type"
			continue
		}
		set have_card 1
		set in_pri [ string equal "$fw_version" [lindex $pri_fw_versions $i] ]
		set in_alt [ string equal "$fw_version" [lindex $alt_fw_versions $i] ]
		set booted [string equal "$fw_version" [lindex $act_fw_versions $i]]
		if { ! $in_pri && ! $in_alt } {
			# firmware is not in slot, need to push and possibly select/reboot
			lappend need_push 1
			set s $select
			log_message "Test: Will need to push firmware to slot $slot"
		} else {
			# firmware is in one of the images
			#set push_all 0
			lappend need_push 0
			# only need to select if not already the primary image
			set s [ expr $select && ! $in_pri]
			log_message "Test: Do Not need to push firmware to slot $slot"
		}
		#  could be running something not on chassis but matching pushed version
		# only need to reboot if not the active image
		set a [ expr $active && ! $booted]
		lappend need_select $s
		lappend need_reboot $a
		if { $s && $a } {
			log_message "Test: Need to select firmware and reboot slot $slot\n"
		} elseif { $a } {
			log_message "Test: Firmware already selected, Need to reboot slot $slot\n"
		} elseif { $s } {
			log_message "Test: Firmware already booted, but need to select firmware for slot $slot\n"
		} else {
			log_message "Test: No need to select firmware nor reboot slot $slot\n"
		}
	}
	# only need do a step if applicable to at least 1 slot
	set do_push [ expr [lsearch -exact $need_push 1] != -1 ]
	set do_select [ expr [lsearch -exact $need_select 1] != -1 ]
	set do_reboot [ expr [lsearch -exact $need_reboot 1] != -1 ]

	# DEBUG
	#log_message "push_all: $push_all"
	#log_message "do_push: $do_push"
	#log_message "do_select: $do_select"
	#log_message "do_reboot: $do_reboot"
	#log_message "need_push: $need_push"
	#log_message "need_select: $need_select"
	#log_message "need_reboot: $need_reboot"

	if { ! ( $do_push || $do_select || $do_reboot ) } {
		# we are done, everything is already the way we want it
		if { $have_card } {
			skip_case "No action required"
		} else {
			skip_case "No selected slots of type $card_type"
		}
		return 0
	}

	set fail 0
	set pushed 0
	set sftp_failures ""
	set sftp_statuses ""

	if { $do_push && $push_all } {
		# push to all applicable slots at once
		log_message "Test: sftp $fw_file to all applicable slots\n"
		sftp_chassis_firmware $chassis $fw_file $select "all"
		# will check status of sftp operations for slots we care about below
		log_message "\nTest: Verifying firmware was pushed to $chassis ...\n"
		target_chassis_admin_sh $chassis
	}

	for { set i 0 } { $i < $num_slots } { incr i } {
		set slot [lindex $push_slots $i]
		if { $do_push && ( $push_all || [lindex $need_push $i] ) } {
			if { ! $push_all } {
				# push one slot at a time
				log_message "\nTest: sftp $fw_file to slot $slot\n"
				sftp_chassis_firmware $chassis $fw_file $select $slot
				target_chassis_admin_sh $chassis
			}
			# check status, must do immediately after sftp
			set status [get_chassis_sftp_status $slot 1]
			if { [ string equal "$status" "success" ] || [ string equal "$status"  "skipped" ] } {
				set pushed 1
				lappend sftp_failures 0
				lappend sftp_statuses "$status"
			} else {
				log_message "\nFAILURE: Failed to sftp firmware to chassis $chassis slot $slot\n"
				set fail 1
				lappend sftp_failures 1
				lappend sftp_statuses "$status"
			}
			if { ! $push_all } {
				target_chassis_admin_sh_exit
			}
		} else {
			# we never sftp'ed to this slot
			lappend sftp_failures 0
			lappend sftp_statuses "skipped"
		}
	}
	# make sure we are logged in for operations below
	if { ! ( $do_push && $push_all ) } {
		target_chassis_admin_sh $chassis
	}

	# DEBUG
	#log_message "pushed: $pushed"
	#log_message "sftp_statuses: $sftp_statuses"
	#log_message "sftp_failures: $sftp_failures"
	#log_message "fail: $fail"

	if { $pushed } {
		# we pushed firmware to some or all of the requested slots
		# now check firmware versions

		for { set i 0 } { $i < $num_slots } { incr i } {
			set slot [lindex $push_slots $i]
			if { [lindex $sftp_failures $i] } {
				# it failed, no further checks necessary
				continue
			}
			if { ! $push_all && ! [lindex $need_push $i] } {
				# we never sftp'ed to this slot
				continue
			}
			# if we get here, status was success or skipped

			# check what is in the slot after the sftp
			set pri_fw_version [get_chassis_firmware_version $slot "pri"]
			set alt_fw_version [get_chassis_firmware_version $slot "alt"]
			set in_pri [ string equal "$fw_version" "$pri_fw_version" ]
			set in_alt [ string equal "$fw_version" "$alt_fw_version" ]
			if { $push_all && ! [lindex $need_push $i] } {
				# sftp should have skipped this slot
				if { ! [ string equal [lindex $sftp_statuses $i] "skipped" ] } {
					set sftp_failures [lreplace $sftp_failures $i $i 1]
					set fail 1
					log_message "\nFAILURE: Unexpected sftp transfer for chassis $chassis slot $slot\n"
				} elseif { ! $in_pri && ! $in_alt } {
					# the version disappeared
					set sftp_failures [lreplace $sftp_failures $i $i 1]
					set fail 1
					log_message "\nFAILURE: Unexpected removal of $fw_version for chassis $chassis slot $slot\n"
				} elseif { $select && ! $in_pri } {
					# the primary changed?
					# we can recover
					log_message "\nWARNING: Unexpected change of primary for chassis $chassis slot $slot\n"
					set need_select [lreplace $need_select $i $i 1]
					set do_select 1
				}
				continue
			}

			# if we get here, we needed to push to this slot
			# hence the slot should not have been skipped
			if { ! [ string equal [lindex $sftp_statuses $i] "success" ] } {
				# it was skipped for no reason
				set sftp_failures [lreplace $sftp_failures $i $i 1]
				set fail 1
				log_message "\nFAILURE: Unexpected skip of sftp transfer for chassis $chassis slot $slot\n"
			} elseif { ! $in_pri && ! $in_alt } {
				# the version never appeared
				set sftp_failures [lreplace $sftp_failures $i $i 1]
				set fail 1
				log_message "\nFAILURE: $fw_version did not transfer to chassis $chassis slot $slot\n"
			} elseif { $select && ! $in_pri } {
				# the image is there but was not selected
				# we can recover
				log_message "\nWARNING: sftp transfered to wrong image for chassis $chassis slot $slot\n"
				set need_select [lreplace $need_select $i $i 1]
				set do_select 1
			}
		}
	}

	# DEBUG
	#log_message "sftp_failures: $sftp_failures"
	#log_message "do_select: $do_select"
	#log_message "need_select: $need_select"

	if { $do_select } {
		# now select the firmware as primary for the necessary slots
		for { set i 0 } { $i < $num_slots } { incr i } {
			if { [lindex $sftp_failures $i] } {
				# it failed, no further ops necessary
				continue
			}
			if { ! [lindex $need_select $i] } {
				# does not need select
				continue
			}
			set slot [lindex $push_slots $i]
			select_chassis_firmware_version $slot $fw_version
		}
	}

	# if any of the above failed, do not activate or reboot it could
	# make things worse
	if { $fail } {
		fail_test "Unable to sftp firmware to requested slots in $chassis"
	}

	# DEBUG
	#log_message "do_reboot: $do_reboot"
	#log_message "need_reboot: $need_reboot"
	#log_message "is_mgmt: $is_mgmt"

	# now activate the firmware for the necessary slots
	if { $do_reboot} {
		if { $is_mgmt } {
			# we can't tell which slot is the active mgmt slot, so just reboot
			# the whole chassis
			if { $use_rebootall } {
				chassis_cmd_reboot_all
			} else {
				send_chassis_cmd "reboot now"
				expect_list 60 { "Goodbye" }
			}
			# may not be able to send logout command, so just get out of dodge
			ignore_rest

			chassis_wait_reboot_done $chassis
			return 0
		} else {
			for { set i 0 } { $i < $num_slots } { incr i } {
				if { [lindex $need_reboot $i] } {
					set slot [lindex $push_slots $i]
					chassis_cmd 60 0 "resetCard $slot now"
				}
			}
			# TBD - is there a way to poll via CLI if card has finished reboot?
			# allow 60 seconds for it to come fully on line
			sleep 60
		}
	}

	target_chassis_admin_sh_exit

	return 0
}

proc test_case_push_chassis_firmware { chassis fw_file fw_version card_type {action "push"} { slots "all" } } {
##
## test_case_push_chassis_firmware
## -------------------------
## test case to push the given firmware file to the specified slots in the
## given chassis.
##
## Usage:
##	test_case_push_chassis_firmware chassis fw_file fw_version card_type [action [slots]]
## Arguments:
##	chassis - IP address/name of chassis to sftp and telnet to
##	fw_file - local pathname of file to push
##	fw_version - firmware version of fw_file
##	card_type - card type of fw_file
##	action - action after pushing firmware:
##			push - do not reboot, do not change as primary
##					is already primary, no change
##			select - select as primary, but do not reboot
##			run - select as primary and reboot to activate firmware
##	slots - list of slot numbers to push to, default is "all" of given card type
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##
##  see push_chassis_firmware for error handling summary

	global env

	set base [exec basename $fw_file .pkg]

	test_case "$chassis.$base" "upgrade $chassis $base" "upgrade firmware on $chassis
selected slots: $slots
Firmware File: $fw_file
card type: $card_type
Firmware Version: $fw_version
Action: $action
File: TestTools/chassis.exp" noop noop {
		upvar chassis chassis
		upvar fw_file fw_file
		upvar fw_version fw_version
		upvar card_type card_type
		upvar action action
		upvar slots slots

		push_chassis_firmware "$chassis" "$fw_file" "$fw_version" "$card_type" "$action" "$slots"
	}
}

proc sftp_chassis_security_file { chassis sec_file } {
##
## sftp_chassis_security_file
## -------------------------
## push the given FM security file to the specified chassis the
# given chassis.
##
## Usage:
##	sftp_chassis_security_file chassis sec_file
## Arguments:
##	chassis - IP address/name of chassis to sftp to
##	sec_file - local pathname of file to push
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
	global spawn_id expect_out spawn_out timeout
	global expecting

	chassis_sftp_cmd "sftp admin@\\\[$chassis\\\]:" "put $sec_file /firmware"
}

proc push_chassis_security_file { chassis sec_file {action "push"} { slots "all" } } {
##
## push_chassis_security_file
## -------------------------
## push the given FM security file to the specified slots in the
## given chassis.
##
## Usage:
##	push_chassis_security_file chassis sec_file [action [slots]]
## Arguments:
##	chassis - IP address/name of chassis to sftp and telnet to
##	sec_file - local pathname of file to push
##	action - action after pushing FM security files:
##			push   - push, do not restart SM
##			run    - make sure FM running on master, not on slave
##			runall - make sure FM running on master and slave
##			restart - restart FM on master, make sure not running on slave
##			restartall - restart FM on master and slave
##			stop - stop FM on master and slave
##	slots - list of slot numbers to push to, default is "all" to all
#           management cards
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##
##  Any slots which are not of a management card, are quietly skipped.
##  This allows multi-file push to work with a slot list.
##
##	If the sftp fails for some slots but succeeds for others.  The failed slots
##	are logged and an exception will be thrown, however the good slots
##	are processed to completion.

	global spawn_id expect_out spawn_out timeout
	global expecting

	# variables:
    # runstate: 0=no change, 1=running, 2=restart, -1=stop
    if { "$action" == "push" } {
        set push 1
        set master_runstate 0
        set slave_runstate 0
    } elseif { "$action" == "stop" } {
        set push 0
        set master_runstate -1
        set slave_runstate -1
    } elseif { "$action" == "run" } {
        set push 0
        set master_runstate 1
        set slave_runstate -1
    } elseif { "$action" == "runall" } {
        set push 0
        set master_runstate 1
        set slave_runstate 1
    } elseif { "$action" == "restart" } {
        set push 0
        set master_runstate 2
        set slave_runstate -1
    } elseif { "$action" == "restartall" } {
        set push 0
        set master_runstate 2
        set slave_runstate 2
    } else {
        fail_test "Invalid FM security file push action: $action"
    }

	log_message "\nTest: Seeing if $chassis present running firmware supports FM security file push ...\n"
	target_chassis_admin_sh $chassis
	if { [ catch { set cap [check_chassis_capability "fwPush"] } res ] != 0
	     || $cap < 1 } {
		fail_test "Chassis $chassis present running firmware does not support push:\n$res"
	}

	log_message "\nTest: Getting card type of management card from $chassis ...\n"
    set my_slot [ get_this_slot_number ]
    set mgmt_card_type [get_chassis_card_type $my_slot]

    log_message "\nTest: Getting Slot list from $chassis ...\n"
	if { [string equal "$slots" "all"] } {
		# push to all slots of matching card type
		set push_slots [get_chassis_slots $mgmt_card_type]
		set num_slots [llength $push_slots]
		#set push_all 0
		set push_all 1
	} else {
		# push to a specific set of slots
		set push_slots "$slots"
		set push_all 0
	}
	set num_slots [llength $push_slots]
	if { $num_slots == 0 } {
		target_chassis_admin_sh_exit
		skip_case "No management cards of type $mgmt_card_type"
		return 0
	}

	log_message "\nTest: Getting present installed firmware from $chassis ...\n"
	# first get present card types for each of the slots
    set card_types ""
         
	foreach slot $push_slots {
		lappend card_types [get_chassis_card_type $slot]
	}
	target_chassis_admin_sh_exit

	# DEBUG
	#log_message "push_slots: $push_slots"
	#log_message "num_slots: $num_slots"
    #log_message "card_types: $card_types"
	#log_message "mgmt_card_type: $mgmt_card_type"
    #log_message "my_slot: $my_slot"

	log_message "\nTest: Determining operations needed to upgrade to $sec_file for $chassis ..."
	# now check that card types match and determine if we need to push security file
	set need_push ""
	set have_card 0
    set mgmt_slot ""
    set fm_running ""

	for { set i 0 } { $i < $num_slots } { incr i } {
		set slot [lindex $push_slots $i]
		if { ! [ string equal "$mgmt_card_type" [lindex $card_types $i] ] } {
			# if this happens card type is inconsistent with original
			# query for slots of given card type (hot swap during update?)
			# or user has explicitly specified slots and picked one of
			# wrong card type
			# quiety ignore this slot
			lappend need_push 0
			log_message "Test: Skipping slot $slot: is of type [lindex $card_types $i], $sec_file is for $mgmt_card_type"
			continue
		}

        # check whether the slot support running the ESM
        if { [ string equal "$slot" "$my_slot" ] } {
            target_chassis_admin_sh $chassis
        } else {
            target_chassis_admin_sh $chassis $slot
        }
        if { [ catch { set cap [check_chassis_capability "smConfig"] } res ] != 0
            || $cap < 1 } {
            # slot will be skipped, doesn't support smConfig
            lappend card_capabilities 0
            lappend fm_running 0
        } else {
            lappend card_capabilities 1
            lappend fm_running [ is_esm_running ]
            if { [ string equal "$mgmt_slot" ""] || [ string equal "$slot" "$my_slot" ] } {
                set mgmt_slot "$slot"
            }
        }
        if { [ string equal "$slot" "$my_slot" ] } {
            target_chassis_admin_sh_exit
        } else {
            target_chassis_admin_sh_exit 1
        }

		set have_card 1
        # valid slot so push security file to this slot
        lappend need_push $push
        if { $push } {
            log_message "Test: Will need to push FM security file to slot $slot"
        } else {
            log_message "Test: No need to push FM security files slot $slot\n"
        }
	}

	# only need do a step if applicable to at least 1 slot
	set do_push [ expr [lsearch -exact $need_push 1] != -1 ]

    # DEBUG
    #log_message "mgmt_slot: $mgmt_slot"
    #log_message "card_capabilities: $card_capabilities"
    #log_message "fm_running: $fm_running"

    if { [ string equal "$mgmt_slot" "" ] } {
        skip_case "chassis does not have an ESM license key or does not support smConfig"
        return 0
	}

	# DEBUG
	#log_message "push_all: $push_all"
	#log_message "do_push: $do_push"
	#log_message "need_push: $need_push"

    set fail 0
    set pushed 0
    set sftp_failures ""
    set sftp_statuses ""
    set need_runstate ""
    set do_runstate 0

    for { set i 0 } { $i < $num_slots } { incr i } {
        set slot [lindex $push_slots $i]
        if { ! [lindex $card_capabilities $i] } {
            # if this happens card is not capable of smConfig ignore this slot
            lappend need_runstate 0
            log_message "Test: Skipping slot $slot: does not support smConfig"
            continue
        }
        set running [ lindex $fm_running $i]
        if { ! [ string equal "$slot" "$my_slot" ] } {
            # slave MM
            if { ( $slave_runstate < 0 && $running )
                || ($slave_runstate == 1 && ! $running )
                || $slave_runstate == 2 } {
                lappend need_runstate $slave_runstate
                set do_runstate 1
                log_message "Test: Need to change runstate for FM in slot $slot"
            } else {
                lappend need_runstate 0
                log_message "Test: No need to change runstate for FM in slot $slot"
            }
        } else {
            # master MM
            if { ( $master_runstate < 0 && $running )
                || ($master_runstate == 1 && ! $running )
                || $master_runstate == 2 } {
                lappend need_runstate $master_runstate
                set do_runstate 1
                log_message "Test: Need to change runstate for FM in slot $slot"
            } else {
                lappend need_runstate 0
                log_message "Test: No need to change runstate for FM in slot $slot"
            }
        }

        if { $do_push && ( $push_all || [lindex $need_push $i] ) } {
            # push one slot at a time
            log_message "\nTest: sftp $sec_file to slot $slot\n"
            sftp_chassis_security_file $chassis $sec_file
            target_chassis_admin_sh $chassis

            # check status, must do immediately after sftp
            set status [get_chassis_sftp_status $slot 1]
            if { [ string equal "$status" "success" ] || [ string equal "$status"  "skipped" ] } {
                set pushed 1
                lappend sftp_failures 0
                lappend sftp_statuses "$status"
            } else {
                log_message "\nFAILURE: Failed to sftp FM security file to chassis $chassis slot $slot\n"
                set fail 1
                lappend sftp_failures 1
                lappend sftp_statuses "$status"
            }
            if { ! $push_all } {
                target_chassis_admin_sh_exit
            }
        } else {
            # we never sftp'ed to this slot
            lappend sftp_failures 0
            lappend sftp_statuses "skipped"
        }
    }

    # make sure we are logged in for operations below
    if { ! ( $do_push && $push_all ) } {
        target_chassis_admin_sh $chassis
    }

    # DEBUG
    #log_message "pushed: $pushed"
    #log_message "sftp_statuses: $sftp_statuses"
    #log_message "sftp_failures: $sftp_failures"
    #log_message "fail: $fail"

    if { $fail } {
        fail_test "Unable to sftp FM security file to requested slots in $chassis"
    }

    # DEBUG
	#log_message "do_runstate: $do_runstate"
	#log_message "need_runstate: $need_runstate"

	if { $do_runstate} {
		for { set i 0 } { $i < $num_slots } { incr i } {
			set slot [lindex $push_slots $i]
			set new_runstate [lindex $need_runstate $i]
			if { $new_runstate != 0 } {
				if { [ string equal "$slot" "$my_slot" ] } {
					target_chassis_admin_sh $chassis
				} else {
					target_chassis_admin_sh $chassis $slot
				}
				if { $new_runstate > 0 } {
					# easier to restart, will start if not already running
					chassis_cmd 60 0 "smControl restart"
					# don't wait, it would risk having mismatched esm configs
				} elseif { $new_runstate < 0 } {
					chassis_cmd 60 0 "smControl stop"
				}
				if { [ string equal "$slot" "$my_slot" ] } {
					target_chassis_admin_sh_exit
				} else {
					target_chassis_admin_sh_exit 1
				}
			}
		}
	}

	if { ! ( $do_push || $do_runstate ) } {
		# we are done, everything is already the way we want it
		if { $have_card } {
			skip_case "No action required"
		} else {
			skip_case "No selected slots of type $mgmt_card_type"
		}
		return 0
	}

	return 0
}

proc test_case_push_chassis_security_file { chassis sec_file {action "push"} { slots "all" } } {
##
## test_case_push_chassis_security_file
## -------------------------
## test case to push the given FM security file to the specified slots in the
## given chassis.
##
## Usage:
##	test_case_push_chassis_security_file chassis sec_file [action [slots]]
## Arguments:
##	chassis - IP address/name of chassis to sftp and telnet to
##	sec_file - local pathname of file to push
##	action - action after pushing firmware:
##			push - do not reboot, do not change as primary
##					is already primary, no change
##	slots - list of slot numbers to push to, default is "all" of given card type
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##
##  see push_chassis_security_file for error handling summary

	global env

	set base [exec basename $sec_file .pem]

	test_case "$chassis.$base" "upgrade $chassis $base" "upgrade FM security file on $chassis
Selected slots: $slots
Security File: $sec_file
Action: $action
File: TestTools/chassis.exp" noop noop {
		upvar chassis chassis
		upvar sec_file sec_file
		upvar action action
		upvar slots slots

	    push_chassis_security_file "$chassis" "$sec_file" "$action" "$slots"
	}
}

proc test_case_chassis_configure { chassis chassisIP} {

	global tools_case_status

	test_case "$chassis.emb.configure" "Chassis $chassis configure" "Configure $chassis
File: TestOnly/chassis.exp" case_setup case_cleanup {
		upvar chassis chassis
		upvar chassisIP chassisIP
		global env

		set syslogServer $env(SYSLOG_SERVER)
		set syslogPort $env(SYSLOG_PORT)
		set syslogFacility $env(SYSLOG_FACILITY)
		set ntpServer $env(NTP_SERVER)
		set tzOffset $env(TZ_OFFSET)
		set dstStart $env(DST_START)
		set dstEnd $env(DST_END)
		set linkWidthSelection $env(LINKWIDTH_SETTING)
		set setname $env(SET_NAME)
		set linkCRCMode $env(LINKCRCMODE)

		target_chassis_admin_sh $chassis

		set chassis_cmd_string "logSyslogConfig"
		set setSyslog 0
		if { [string equal "$syslogServer" "0.0.0.0"] == 0 } {
			append chassis_cmd_string " -h $syslogServer"
			set setSyslog 1
		}
		if { [string equal "$syslogPort" "-1"] == 0 } {
			append chassis_cmd_string " -p $syslogPort"
			set setSyslog 1
		}
		if { [string equal "$syslogFacility" "-1"] == 0 } {
			append chassis_cmd_string " -f $syslogFacility"
			set setSyslog 1
		}
		if { $setSyslog } {
			send_chassis_cmd $chassis_cmd_string
			expect_progress 60 {[\r\n]} { "Successfully" "facility" } { "Invalid" "usage" }
			check_chassis_exit_status 60 0
		}

		if { [string equal "$ntpServer" "0.0.0.0"] == 0 } {
			send_chassis_cmd "time -S $ntpServer"
			expect_progress 60 {[\r\n]} { "Configured" "NTP" } { "not" "valid" }
			check_chassis_exit_status 60 0
		}

		if { [string equal "$tzOffset" "100"] == 0 } {
			send_chassis_cmd "timeZoneConf $tzOffset"
			expect_progress 60 {[\r\n]} { "successfully" "configured" } { "invalid" "not found"  }
			check_chassis_exit_status 60 0
		}

		if { [string equal "$dstStart" "0 0 0"] == 0 } {
			send_chassis_cmd "timeDstConf $dstStart $dstEnd"
			expect_progress 60 {[\r\n]} { "Timezone" "DST" } { "bounds" "not found" }
			check_chassis_exit_status 60 0
		}

		if { [string equal "$linkWidthSelection" "0"] == 0 } {
			send_chassis_cmd "ismChassisSetWidth $linkWidthSelection"
			send_chassis_cmd "showLastRetCode"
			set out [expect_list 60 { "Last Exit Code: [0-9]+" } { "Error" }]
			regexp {([0-9]+)} $out ret
			if { ! [ string equal "$ret" "0"] } {
				if { [ string equal "$ret" "5"] } {
					skip_case "\n$chassis: Skipped changing chassis link width: ismChassisSetWidth command not supported"
				} else {
					fail_test "\n$chassis: Failed changing chassis link width: ismChassisSetWidth command failed with return code $ret"
				}
			}
		}

		if { [string equal "$chassisIP" "0.0.0.0"] == 0 } {
			send_chassis_cmd "setChassisIpAddr -h $chassisIP"
			expect_progress 60 {[\r\n]} { "reconnect" "updated" } { "valid" "usage" }
			check_chassis_exit_status 60 0
		}

		# $setname is y or n
		if { [string equal "$setname" "y"] } {
			send_chassis_cmd "setNodeDesc \"$chassis\""
			expect_progress 60 {[\r\n]} { "success" "changed" } { "usage:" "not found" }
			check_chassis_exit_status 60 0
		}

		if { [string equal "$linkCRCMode" "-1" ] == 0 } {
			send_chassis_cmd "ismChassisSetCrcMode $linkCRCMode"
			send_chassis_cmd "showLastRetCode"
			set out [expect_list 60 { "Last Exit Code: [0-9]+" } { "Error" }]
			regexp {([0-9]+)} $out ret
			if { ! [ string equal "$ret" "0"] } {
				if { [ string equal "$ret" "5" ] } {
					skip_case "\n$chassis: Skipped changing chassis link CRC mode: ismChassisSetCrcMode command not supported"
				} else {
					fail_test "\n$chassis: Failed changing chassis link CRC mode: ismChassisSetCrcMode command failed with return code $ret"
				}
			}
			check_chassis_exit_status 60 0
		}

		target_chassis_admin_sh_exit
	}
}

proc check_esm_config { config_file } {
##
## check_esm_config
## -------------------------
## check the syntax of the given esm config file
##
## Usage:
##	check_esm_config config_file
## Arguments:
##	config_file - local pathname of file
## Returns:
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global env

	if { [ file exists "/usr/lib/opa/fm_tools/config_check" ] } {
		set env(IFS_FM_BASE) ""
		set tooldir "/usr/lib/opa/fm_tools"
	} else {
		set env(IFS_FM_BASE) "/usr/lib/opa-fm"
		set tooldir "$env(IFS_FM_BASE)/bin"
	}
	log_message "IFS_FM_BASE=$env(IFS_FM_BASE)"
	log_message "tooldir=$tooldir"
	log_message ""
	if { [ file exists "$tooldir/config_check" ] } {
		log_message "$tooldir/config_check -c $config_file"
		if { [ catch { exec "$tooldir/config_check" -c "$config_file" } err_str ] != 0 } {
			puts "$tooldir/config_check -c $config_file"
			show_message "$err_str"
			error "$tooldir/config_check -c $config_file: failed: $err_str"
		}
	} else {
		skip_case "Unable to pre-check config file, Intel OPA FM not installed on this server"
	}
	return 0
}

proc sftp_esm_config { chassis config_file } {
##
## sftp_esm_config
## -------------------------
## push the given SM config file to the specified chassis
##
## Usage:
##	sftp_esm_config chassis config_file
## Arguments:
##	chassis - IP address/name of chassis to scp and telnet to
##	config_file - local pathname of file to push
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	chassis_sftp_cmd "sftp admin@\\\[$chassis\\\]:" "put $config_file /firmware/opafm.xml"

	return 0
}

proc get_esm_config { chassis config_file } {
##
## get_esm_config
## -------------------------
## get esm config file from the chassis
##
## Usage:
##	get_esm_config config_file
## Arguments:
##	chassis - IP address/name of chassis to scp and telnet to
##	config_file - local pathname of file to get to
## Returns:
##	1 - match, 0 - no match
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	chassis_sftp_cmd "sftp admin@\\\[$chassis\\\]:" "get /firmware/opafm.xml $config_file" 0

	return 0
}

proc compare_esm_config { chassis config_file mgmt_card_type } {
##
## compare_esm_config
## -------------------------
## return if the config_file matches esm config in the chassis
##
## Usage:
##	compare_esm_config chassis config_file
## Arguments:
##	chassis - IP address/name of chassis to scp and telnet to
##	config_file - local pathname of file to push
##	mgmt_card_type - Name of management card type
## Returns:
##	1 - match, 0 - no match
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting
	global test_tmp

	log_message "\nComparing $config_file to FM config...\n"
	set temp_file "$test_tmp/get_opafm.xml"
	get_esm_config "$chassis" "$test_tmp/get_opafm.xml"
	if { [catch { run_cmd "exec cmp -s $config_file $temp_file"} ] == 0 } {
		# match
		log_message "Test: Matches\n"
		return 1
	} else {
		log_message "Test: Does not Match\n"
		return 0
	}
}

proc is_esm_running { } {
##
## is_esm_running
## -------------------------
## check the esm status for the current slot in the current chassis
##
## Usage:
##	is_esm_running
## Arguments:
## Returns:
##	esm status
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	# DONE
	global spawn_id expect_out spawn_out timeout
	global expecting

	send_chassis_cmd "smControl status"
	# require trailing whitespace to anchor end of string
	set out [expect_list 60 "{Subnet manager is \[a-zA-Z0-9_ -\]+\\\.}" { "usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
	set ret ""
	regexp {is ([a-zA-Z0-9_ -]+)\.} $out line ret

	expect_chassis_prompt 60

	return [ string equal "$ret" "running"]
}

proc chassis_get_sm_boot_options { } {
##
## chassis_get_sm_boot_options
## -------------------------
## return SM boot options
##
## Usage:
##	chassis_get_sm_boot_options
## Arguments:
## Returns:
##	{ startAtBoot startOnSlaveCmu }
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	send_chassis_cmd "smConfig query"
	# the [:space:] bounds the + so we get all the data on the line
	set out1 [expect_list 60 "{startAtBoot: \[0-9A-Za-z\]+\[\[:space:\]\]}" { "usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
	set out2 [expect_list 60 "{startOnSlaveCmu: \[0-9A-Za-z/\]+\[\[:space:\]\]}" { "usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
	check_chassis_exit_status 60 0
	#log_message "out1=$out1"
	#log_message "out2=$out2"

	set ret1 ""
	set ret2 ""
	# throw away the \r\n, there may be trailing spaces, TCL lists will ignore
	regexp {startAtBoot: ([A-Za-z0-9]+)[[:space:]]} $out1 line ret1
	regexp {startOnSlaveCmu: ([A-Za-z0-9/]+)[[:space:]]} $out2 line ret2

	return "$ret1 $ret2"
}

proc push_esm_config { chassis config_file {action "push"} {bootstate ""} } {
##
## push_esm_config
## -------------------------
## push the given ESM config file to all the applicable slots in the
## given chassis.
##
## Usage:
##	push_esm_config chassis config_file [action [bootstate]]
## Arguments:
##	chassis - IP address/name of chassis to scp and telnet to
##	config_file - local pathname of file to push
##			if "", no file push will occur
##	action - action after pushing config_file:
##			push   - push, do not restart SM
##			select - same as push
##			run    - make sure FM running on master, not on slave
##			runall - make sure FM running on master and slave
##			restart - restart FM on master, make sure not running on slave
##			restartall - restart FM on master and slave
##			stop - stop FM on master and slave
##  bootstate - optional selection for FM bootstate
##			"" - no change to FM start at boot options
##          disable - disable FM start on all MM on boot
##          enable - enable FM start on master MM on boot, disable on slave MM
##          enableall - enable FM start on all MM on boot
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##
##	If the scp fails for some slots but succeeds for others.  The failed slots
##	are logged and an exception will be thrown, however the good slots
##	are processed to completion [except for the restart] (if possible)
##  so that they are not left in an odd state

	global spawn_id expect_out spawn_out timeout
	global expecting

	# variables:
	#	restart_only - no push, just restart FM
	set restart_only [string equal "$config_file" ""]

	# runstate: 0=no change, 1=running, 2=restart, -1=stop
	if { "$action" == "push" || "$action" == "select" } {
		set master_runstate 0
		set slave_runstate 0
	} elseif { "$action" == "stop" } {
		set master_runstate -1
		set slave_runstate -1
	} elseif { "$action" == "run" } {
		set master_runstate 1
		set slave_runstate -1
	} elseif { "$action" == "runall" } {
		set master_runstate 1
		set slave_runstate 1
	} elseif { "$action" == "restart" } {
		set master_runstate 2
		set slave_runstate -1
	} elseif { "$action" == "restartall" } {
		set master_runstate 2
		set slave_runstate 2
	} else {
		fail_test "Invalid FM config file action: $action"
	}
	if { "$bootstate" != "" && "$bootstate" != "disable" 
		&& "$bootstate" != "enable" && "$bootstate" != "enableall" } {
		fail_test "Invalid FM config bootstate: $bootstate"
	}

	# DEBUG
	#log_message "action: $action"
	#log_message "restart_only: $restart_only"
	#log_message "bootstate: $bootstate"

	target_chassis_admin_sh $chassis
	# check if chassis supports slaveCli, if not then skip chassis, older FW
	if { [ catch { set cap [check_chassis_capability "slaveCli"] } res ] != 0
	     	|| $cap < 1 } {
		skip_case "chassis does not support FM config via FastFabric"
	}

	log_message "\nTest: Getting Slot list from $chassis ...\n"
	set my_slot [ get_this_slot_number ]
	# get management card type for this chassis model
	set mgmt_card_type [ get_chassis_card_type $my_slot ]
	# all slots of matching card type (eg. all management cards)
	set mgmt_slots [get_chassis_slots $mgmt_card_type]
	set num_slots [llength $mgmt_slots]
	if { $num_slots == 0 } {
		# this should not happen, we should at least find card 0
		target_chassis_admin_sh_exit
		fail_test "No cards of type $mgmt_card_type"
	}
	# lets be paranoid
	if { ! [ string equal [get_chassis_card_type $my_slot] "$mgmt_card_type" ] } {
		target_chassis_admin_sh_exit
		fail_test "IP address given is not for chassis mgmt card $mgmt_card_type"
	}
	target_chassis_admin_sh_exit

	log_message "\nTest: Checking FM capabilities and status of $chassis ...\n"
	# flag per slot indicating if slot has smConfig capability
	# may not have smConfig on all cards, esp if ESM license key lacking
	set card_capabilities ""
	# flag per slot indicating if slot has FM currently running
	set fm_running ""
	# slot which supports sm CLI.  Prefer active mgmt card otherwise its
	# secondary mgmt card
	set mgmt_slot ""
	# see if any slots in chassis support smConfig, if so check FM status
	foreach slot $mgmt_slots {
		if { [ string equal "$slot" "$my_slot" ] } {
			target_chassis_admin_sh $chassis
		} else {
			target_chassis_admin_sh $chassis $slot
		}
		if { [ catch { set cap [check_chassis_capability "smConfig"] } res ] != 0
	     	|| $cap < 1 } {
			# slot will be skipped, doesn't support smConfig
			lappend card_capabilities 0
			lappend fm_running 0
		} else {
			lappend card_capabilities 1
			lappend fm_running [ is_esm_running ]
			if { [ string equal "$mgmt_slot" ""] || [ string equal "$slot" "$my_slot" ] } {
				set mgmt_slot "$slot"
			}
		}
		if { [ string equal "$slot" "$my_slot" ] } {
			target_chassis_admin_sh_exit
		} else {
			target_chassis_admin_sh_exit 1
		}
	}

	# DEBUG
	#log_message "mgmt_slots: $mgmt_slots"
	#log_message "mgmt_slot: $mgmt_slot"
	#log_message "num_slots: $num_slots"
	#log_message "card_capabilities: $card_capabilities"
	#log_message "fm_running: $fm_running"

	if { [ string equal "$mgmt_slot" "" ] } {
		skip_case "chassis does not have an ESM license key or does not support smConfig"
	}

	set do_push 0
	if { ! $restart_only } {
		log_message "\nTest: Checking FM config in $chassis ...\n"
		# we trust that secondary management cards will be synced with primary
		# so only need to upload this once
		set config_matches [compare_esm_config $chassis $config_file $mgmt_card_type ]

		# DEBUG
		#log_message "config_matches: $config_matches"

		log_message "\nTest: Determining operations needed to update to $config_file for $chassis ..."
		## now check that card is capable and determine if we need to push config

		if { ! $config_matches } {
			# config is not in chassis, need to push and possibly restart
			set do_push 1
			log_message "Test: Will need to push config to chassis"
		} else {
			# config is in chassis, can skip push
			set do_push 0
			log_message "Test: No need to push config to chassis"
		}
	}

	# we are assuming we can trust that primary mgmt card's config files
	# match those of secondary.  So FM runstate is only card specific aspect.
	set need_runstate ""
	set do_runstate 0
	for { set i 0 } { $i < $num_slots } { incr i } {
		set slot [lindex $mgmt_slots $i]
		if { ! [lindex $card_capabilities $i] } {
			# if this happens card is not capable of smConfig ignore this slot
			lappend need_runstate 0
			log_message "Test: Skipping slot $slot: does not support smConfig"
			continue
		}
		set running [ lindex $fm_running $i]
		if { ! [ string equal "$slot" "$my_slot" ] } {
			# slave MM
			if { ( $slave_runstate < 0 && $running )
				|| ($slave_runstate == 1 && ! $running )
				|| $slave_runstate == 2 } {
				lappend need_runstate $slave_runstate
				set do_runstate 1
				log_message "Test: Need to change runstate for FM in slot $slot"
			} else {
				lappend need_runstate 0
				log_message "Test: No need to change runstate for FM in slot $slot"
			}
		} else {
			# master MM
			if { ( $master_runstate < 0 && $running )
				|| ($master_runstate == 1 && ! $running )
				|| $master_runstate == 2 } {
				lappend need_runstate $master_runstate
				set do_runstate 1
				log_message "Test: Need to change runstate for FM in slot $slot"
			} else {
				lappend need_runstate 0
				log_message "Test: No need to change runstate for FM in slot $slot"
			}
		}
	}
	log_message "\n"

	# DEBUG
	#log_message "do_push: $do_push"
	#log_message "do_runstate: $do_runstate"
	#log_message "need_runstate: $need_runstate"

	if { $do_push } {
		set fail 0
		set scp_statuses ""

		# push is done once to chassis and automatically synced to all
		# mgmt cards
		log_message "Test: sftp $config_file to all applicable slots\n"
		sftp_esm_config $chassis $config_file

		# will check status of scp operations for slots we care about below
		log_message "\nTest: Verifying config was pushed to $chassis ...\n"
		target_chassis_admin_sh $chassis

		# check status, must do immediately after scp for each slot
		for { set i 0 } { $i < $num_slots } { incr i } {
			set slot [lindex $mgmt_slots $i]
			# do not expect "skipped" status
			set status [get_chassis_sftp_status $slot 1]
			if { [ string equal "$status" "success" ] } {
				lappend scp_statuses "$status"
			} else {
				log_message "\nFAILURE: Failed to scp config to chassis $chassis slot $slot\n"
				set fail 1
				lappend scp_statuses "$status"
			}
		}

		target_chassis_admin_sh_exit

		# DEBUG
		#log_message "scp_statuses: $scp_statuses"
		#log_message "fail: $fail"

		if { ! $fail } {
			# we trust that secondary management cards will be synced with
			# primary so only need to upload this once
			if { ! [compare_esm_config $chassis $config_file $mgmt_card_type ] } {
				# the config disappeared
				set fail 1
				log_message "\nFAILURE: Unexpected mismatch of config for chassis $chassis slot $slot\n"
			}
		}

		# if any of the above failed, do not change runstate it could make things worse
		if { $fail } {
			fail_test "Unable to scp config to required slots in $chassis"
		}
	}

	# DEBUG
	#log_message "do_runstate: $do_runstate"
	#log_message "need_runstate: $need_runstate"

	if { $do_runstate} {
		for { set i 0 } { $i < $num_slots } { incr i } {
			set slot [lindex $mgmt_slots $i]
			set new_runstate [lindex $need_runstate $i]
			if { $new_runstate != 0 } {
				if { [ string equal "$slot" "$my_slot" ] } {
					target_chassis_admin_sh $chassis
				} else {
					target_chassis_admin_sh $chassis $slot
				}
				if { $new_runstate > 0 } {
					# easier to restart, will start if not already running
					chassis_cmd 60 0 "smControl restart"
					# don't wait, it would risk having mismatched esm configs
				} elseif { $new_runstate < 0 } {
					chassis_cmd 60 0 "smControl stop"
				}
				if { [ string equal "$slot" "$my_slot" ] } {
					target_chassis_admin_sh_exit
				} else {
					target_chassis_admin_sh_exit 1
				}
			}
		}
	}

	# adjust start at boot options
	set do_boot 0
	if { "$bootstate" != "" } {
		if { ! [ string equal "$mgmt_slot" "$my_slot" ] } {
			fail_test "Master MM does not have SM license key, unable to configure start options in $chassis"
		}
		log_message "\nTest: Checking FM Start At Boot Options for $chassis ...\n"
		target_chassis_admin_sh $chassis
		# get present options to old_startAtBootArg and old_startOnSlaveCmuArg
		set boot_options [ chassis_get_sm_boot_options ]
		set old_startAtBootArg [lindex $boot_options 0 ]
		set old_startOnSlaveCmuArg [lindex $boot_options 1 ]

		# determine desired settings
		if { "$bootstate" == "disable" } {
			set startAtBootArg "no"
			set startOnSlaveCmuArg "no"
		} elseif { "$bootstate" == "enable" } {
			set startAtBootArg "yes"
			set startOnSlaveCmuArg "no"
		} elseif { "$bootstate" == "enableall" } {
			set startAtBootArg "yes"
			set startOnSlaveCmuArg "yes"
		}
		# do what is needed
		set arg1 ""
		set arg2 ""
		if { "$startAtBootArg" != "$old_startAtBootArg" } {
			set arg1 "startAtBoot $startAtBootArg"
		}
		if { "$startOnSlaveCmuArg" != "$old_startOnSlaveCmuArg"
			 && "$old_startOnSlaveCmuArg" != "N/A" } {
			set arg2 "startOnSlaveCmu $startOnSlaveCmuArg"
		}
		if { "$arg1" != "" || "$arg2" != "" } {
			log_message "\nTest: Changing FM Start At Boot Options for $chassis ...\n"
			chassis_cmd 60 0 "smConfig $arg1 $arg2"
			set do_boot 1
		} else {
			log_message "\nTest: No Change to FM Start At Boot Options for $chassis ...\n"
		}
		target_chassis_admin_sh_exit
	}

	if { ! $do_push && ! $do_runstate && ! $do_boot } {
		# we are done, everything is already the way we want it
		# report skip so user knows we didn't change anything on this chassis
		skip_case "No action required"
		return 0
	}

	return 0
}

proc test_case_push_esm_config { chassis config_file {action "push"} {bootstate ""} } {
##
## test_case_push_esm_config
## -------------------------
## test case to push the given config file to all applicable slots in
## the given chassis.
##
## Usage:
##	test_case_push_esm_config chassis config_file [action [bootstate]]
## Arguments:
##	chassis - IP address/name of chassis to scp and telnet to
##	config_file - local pathname of file to push
##	action - action after pushing firmware:
##			push   - push, do not restart SM
##			select - same as push
##			run - restart FM on master, make sure not running on slave
##			runall - restart FM on master and slave
##			stop - stop FM on master and slave
##  bootstate - optional selection for FM bootstate
##			"" - no change to FM start at boot options
##          disable - disable FM start on all MM on boot
##          enable - enable FM start on master MM on boot, disable on slave MM
##          enableall - enable FM start on all MM on boot
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##
##  see push_esm_config for error handling summary

	global env

	test_case "$chassis.fmconfig" "update $chassis $config_file" "update FM config file on $chassis
Config File: $config_file
Action: $action
BootState: $bootstate
File: TestTools/chassis.exp" noop noop {
		upvar chassis chassis
		upvar config_file config_file
		upvar action action
		upvar bootstate bootstate

		# translate action, when pushing config file we want to use restart
		# so that the new file ends up "running"
		if { "$action" == "run" } {
			set action "restart"
		} elseif { "$action" == "runall" } {
			set action "restartall"
		}

		push_esm_config "$chassis" "$config_file" "$action" "$bootstate"
	}
}

proc test_case_get_esm_config { chassis config_file } {
##
## test_case_get_esm_config
## -------------------------
## test case to get the given config file from the given chassis.
##
## Usage:
##	test_case_get_esm_config chassis config_file
## Arguments:
##	chassis - IP address/name of chassis to scp and telnet to
##	config_file - local pathname of file to fetch to
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##
##  see push_esm_config for error handling summary

	global env

	test_case "$chassis.fm_get_config" "get $chassis $config_file" "get FM config file from $chassis
To Config File: $config_file
File: TestTools/chassis.exp" noop noop {
		upvar chassis chassis
		upvar config_file config_file
		upvar action action

		# check if chassis supports slaveCli, if not then skip chassis, older FW
		target_chassis_admin_sh $chassis
		if { [ catch { set cap [check_chassis_capability "slaveCli"] } res ] != 0
	     		|| $cap < 1 } {
			skip_case "chassis does not support FM config via FastFabric"
		}

		# TBD - make this common code with fmconfig
		log_message "\nTest: Getting Slot list from $chassis ...\n"
		set my_slot [ get_this_slot_number ]
		# get management card type for this chassis model
		set mgmt_card_type [ get_chassis_card_type $my_slot]
		# all slots of matching card type (eg. all management cards)
		set mgmt_slots [get_chassis_slots $mgmt_card_type]
		set num_slots [llength $mgmt_slots]
		if { $num_slots == 0 } {
			# this should not happen, we should at least find card 0
			target_chassis_admin_sh_exit
			fail_test "No cards of type $mgmt_card_type"
		}
		# lets be paranoid
		if { ! [ string equal [get_chassis_card_type $my_slot] "$mgmt_card_type" ] } {
			target_chassis_admin_sh_exit
			fail_test "IP address given is not for chassis mgmt card $mgmt_card_type"
		}
		target_chassis_admin_sh_exit

		log_message "\nTest: Checking FM capabilities and status of $chassis ...\n"
		# slot which supports sm CLI.  Prefer active mgmt card otherwise its
		# secondary mgmt card
		set mgmt_slot ""
		# see if any slots in chassis support smConfig
		foreach slot $mgmt_slots {
			if { [ string equal "$slot" "$my_slot" ] } {
				target_chassis_admin_sh $chassis
			} else {
				target_chassis_admin_sh $chassis $slot
			}
			if { [ catch { set cap [check_chassis_capability "smConfig"] } res ] != 0
	     		|| $cap < 1 } {
				# slot will be skipped, doesn't support smConfig
			} else {
				if { [ string equal "$mgmt_slot" ""] || [ string equal "$slot" "$my_slot" ] } {
					set mgmt_slot "$slot"
				}
			}
			if { [ string equal "$slot" "$my_slot" ] } {
				target_chassis_admin_sh_exit
			} else {
				target_chassis_admin_sh_exit 1
			}
		}

		if { [ string equal "$mgmt_slot" "" ] } {
			skip_case "chassis does not have an ESM license key or does not support smConfig"
		}

		log_message "mkdir -p [exec dirname $config_file]"
		exec mkdir -p "[exec dirname $config_file]"
		get_esm_config "$chassis" "$config_file"
	}
}


proc test_case_chassis_getconfig { chassis {fd ""}} {
##
## test_case_chassis_getconfig
## -------------------------
## test case to get basic config from the given chassis.
##
## Usage:
##	test_case_chassis_getconfig chassis [fd]
## Arguments:
##	chassis - IP address/name of chassis to scp and telnet to
##	fd - file to write config info to
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##

	global env

	test_case "$chassis.getconfig" "get $chassis" "get basic configuration from $chassis
File: TestTools/chassis.exp" noop noop {
		upvar chassis chassis
		upvar fd fd
		set ntpVal ""
		set sysLogVal ""
		set timeZone ""
		set out ""
		set linkWidth ""
		set nodeDesc ""
		set act_fw_version ""
		set pri_fw_version ""
		set linkCRCmode ""

		# check if chassis supports slaveCli, if not then skip chassis, older FW
		target_chassis_admin_sh $chassis
		# slot 0 is alias for mgmt card in all chassis models
		set my_slot [ get_this_slot_number ]
		set act_fw_version [get_chassis_firmware_version $my_slot "act"]
		set pri_fw_version [get_chassis_firmware_version $my_slot "pri"]
		send_chassis_cmd "time"
		set out [expect_list 240 { "[Cc]onfigured.+\n" } { "Usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
		regexp {([A-Za-z0-9:. ]+)} $out tmp ntpVal
		expect_chassis_prompt 60
		send_chassis_cmd "logSyslogConfig"
		set out [expect_list 240 { "logSyslogConfig" "[Ss]yslog.+\n" } { "Usage" "Invalid" "Error" "Failed" "roblem" "not found"}  ]
		regexp {([A-Za-z0-9:. ]+)} $out tmp sysLogVal
		expect_chassis_prompt 60
		send_chassis_cmd "timeZoneConf"
		set out [expect_list 240 { "timeZoneConf" ".+[tT]ime.+\n" } { "Usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
		regexp {([-A-Za-z0-9:. ]+)} $out tmp timeZone
		expect_chassis_prompt 60
		send_chassis_cmd "ismChassisSetWidth"
		set out [expect_list 240 { "ismChassisSetWidth" "link width supported: [^\r\n]*[\r\n]"  } { "Usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
		# "4X" or "4X/8X" or "UNKNOWN" or similar
		regexp {supported: ([^\r\n]+)[\r\n]} $out tmp linkWidth
		expect_chassis_prompt 60
		send_chassis_cmd "showNodeDesc"
		set out [expect_list 240 { "showNodeDesc" "Node" "\(SMA\)" "Description.+[\r\n]"  } { "Usage" "Invalid" "Error" "Failed" "roblem" "not found"} ]
		regexp {.+= (.+)[\r\n]} $out tmp nodeDesc
		expect_chassis_prompt 60
 		send_chassis_cmd "ismChassisSetCrcMode"
		set out [expect_list 240 { "ismChassisSetCrcMode" "CRC_MODE: [^\r\n]*[\r\n]" } { "Usage" "Invalid" "Error" "Failed" "roblem" "not found" } ]
		regexp {CRC_MODE: ([^\r\n]+)[\r\n]} $out tmp linkCRCmode
		expect_chassis_prompt 60
		set out "
    Firmware Active        : $act_fw_version
    Firmware Primary       : $pri_fw_version
    Syslog Configuration   : $sysLogVal
    NTP                    : $ntpVal
    Time Zone              : $timeZone
    LinkWidth Support      : $linkWidth
    Node Description       : $nodeDesc
    Link CRC Mode          : $linkCRCmode"
		show_message "\n$chassis:$out"
		if { ! [ string equal "$fd" "" ] } {
			puts $fd "$out"
		}
		target_chassis_admin_sh_exit
	}
}

proc get_esm_security_files { chassis security_files dest_dir } {
##
## get_esm_security_files
## -------------------------
## get esm security files from the chassis
##
## Usage:
##	get_esm_security security_files
## Arguments:
##	chassis - IP address/name of chassis to scp and telnet to
##	security_files - local pathname of file to get to
## Returns:
##	1 - match, 0 - no match
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine

	global spawn_id expect_out spawn_out timeout
	global expecting

	chassis_sftp_cmd "sftp admin@\\\[$chassis\\\]:" "get /firmware/$security_files $dest_dir" 0

	return 0
}

proc test_case_get_esm_security_files { chassis security_files dest_dir } {
##
## test_case_get_esm_security_file
## -------------------------
## test case to get the given security files from the given chassis.
##
## Usage:
##	test_case_get_esm_security_files chassis security_files
## Arguments:
##	chassis - IP address/name of chassis to scp and telnet to
##	security_files - security files to fetch
## Returns:
##	0 -> success
##	-code error on failure
## Additional Information:
##	The global timeout is changed by this routine
##
##  see push_esm_security_files for error handling summary

	global env

	test_case "$chassis.fm_get_security_files" "get $chassis $security_files $dest_dir" "get FM security files from $chassis
Get Security Files: $security_files
Dest Directory:     $dest_dir
File: TestTools/chassis.exp" noop noop {
		upvar chassis chassis
		upvar security_files security_files
		upvar action action
		upvar dest_dir dest_dir

		# check if chassis supports slaveCli, if not then skip chassis, older FW
		target_chassis_admin_sh $chassis
		if { [ catch { set cap [check_chassis_capability "slaveCli"] } res ] != 0
	     		|| $cap < 1 } {
			skip_case "chassis does not support FM config via FastFabric"
		}

		# TBD - make this common code with fmconfig
		log_message "\nTest: Getting Slot list from $chassis ...\n"
		set my_slot [ get_this_slot_number ]
		# get management card type for this chassis model
		set mgmt_card_type [ get_chassis_card_type $my_slot]
		# all slots of matching card type (eg. all management cards)
		set mgmt_slots [get_chassis_slots $mgmt_card_type]
		set num_slots [llength $mgmt_slots]
		if { $num_slots == 0 } {
			# this should not happen, we should at least find card 0
			target_chassis_admin_sh_exit
			fail_test "No cards of type $mgmt_card_type"
		}
		# lets be paranoid
		if { ! [ string equal [get_chassis_card_type $my_slot] "$mgmt_card_type" ] } {
			target_chassis_admin_sh_exit
			fail_test "IP address given is not for chassis mgmt card $mgmt_card_type"
		}

        # all security files on the master management card
        set sec_files [ list_esm_security_files ]
		set num_sec_files [llength $sec_files]
		if { $num_sec_files == 0 } {
			target_chassis_admin_sh_exit
			fail_test "No FM security files found"
		}

		target_chassis_admin_sh_exit

		log_message "\nTest: Checking FM capabilities and status of $chassis ...\n"
		# slot which supports sm CLI.  Prefer active mgmt card otherwise its
		# secondary mgmt card
		set mgmt_slot ""
		# see if any slots in chassis support smConfig
		foreach slot $mgmt_slots {
			if { [ string equal "$slot" "$my_slot" ] } {
				target_chassis_admin_sh $chassis
			} else {
				target_chassis_admin_sh $chassis $slot
			}
			if { [ catch { set cap [check_chassis_capability "smConfig"] } res ] != 0
	     		|| $cap < 1 } {
				# slot will be skipped, doesn't support smConfig
			} else {
				if { [ string equal "$mgmt_slot" ""] || [ string equal "$slot" "$my_slot" ] } {
					set mgmt_slot "$slot"
				}
			}
			if { [ string equal "$slot" "$my_slot" ] } {
				target_chassis_admin_sh_exit
			} else {
				target_chassis_admin_sh_exit 1
			}
		}

		if { [ string equal "$mgmt_slot" "" ] } {
			skip_case "chassis does not have an ESM license key or does not support smConfig"
		}

		log_message "mkdir -p $dest_dir"
        exec mkdir -p $dest_dir
        foreach sec_file $sec_files {
            get_esm_security_files "$chassis" "$sec_file" "$dest_dir"
        }
	}
}