File: confConsole.py

package info (click to toggle)
dynagen 0.11.0-5
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 2,572 kB
  • ctags: 1,087
  • sloc: python: 21,332; sh: 30; makefile: 20
file content (2309 lines) | stat: -rw-r--r-- 90,046 bytes parent folder | download | duplicates (3)
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
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
confConsole.py
Copyright (C) 2007  Pavel Skovajsa

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
"""

import os
import cmd
import re
from dynamips_lib import DynamipsError, DynamipsWarning, GENERIC_7200_PAS, GENERIC_3600_NMS, GENERIC_1700_NMS, GENERIC_2600_NMS, GENERIC_3700_NMS

# Regex matching slot definitions. Used in pre_cmd() function in confRouterConsole
SLOT_RE = re.compile('^slot[0-6]', re.IGNORECASE)

# determine if we are in the debugger
try:
    DBGPHideChildren
except NameError:
    DEBUGGER = False
else:
    DEBUGGER = True

###some functions from console.py##################


def error(msg):
    """Print out an error message"""

    print '*** Error:', str(msg)


def debug(string):
    """ Print string if debugging is true"""

    import __main__
    # Debug level 2, console debugs
    if __main__.dynagen.debuglevel >= 2:
        print '  DEBUG: ' + str(string)


##############end of some functions from console.py##################


class AbstractConsole(cmd.Cmd):
    """abstract console class, all other console and confconsole classes inherit behavior from it"""

    def __init__(self):
        cmd.Cmd.__init__(self)
        # Import the main namespace for use in this module
        # Yes, normally this is bad mojo, but I'm doing this to provide the console
        # access to the entire namespace in case the user wants to futz with stuff
        import __main__
        self.namespace = __main__
        self.debuglevel = self.namespace.dynagen.debuglevel

## Override methods in Cmd object ##

    def preloop(self):
        """Initialization before prompting user for commands.
        Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
        """

        cmd.Cmd.preloop(self)  ## sets up command completion
        self._hist = []  ## No history yet
        self._locals = {}  ## Initialize execution namespace for user
        self._globals = {}
        # Give the console access to the namespace
        self._globals['namespace'] = self.namespace

    def postloop(self):
        """Take care of any unfinished business.
        Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
        """

        cmd.Cmd.postloop(self)  ## Clean up command completion
        print 'Exiting...'

    def precmd(self, line):
        """ This method is called after the line has been input but before
                    it has been interpreted. If you want to modifdy the input line
                    before execution (for example, variable substitution) do it here.
        """

        self._hist += [line.strip()]
        tokens = line.split()
        try:
            if tokens[0].lower() == 'con':
                tokens[0] = 'console'
                line = (' ').join(tokens)
        except IndexError:
            pass

        return line

    def postcmd(self, stop, line):
        """If you want to stop the console, return something that evaluates to true.
        If you want to do some post command processing, do it here.
        """

        return stop

    def emptyline(self):
        """Do nothing on empty input line"""

        pass

    def do_py(self, line):
        """py <python statement(s)>
\tExecute python statements"""

        if line == '?':
            print self.do_py.__doc__
            return

        try:
            exec line in self._locals, self._globals
        except Exception, e:
            print e.__class__, ':', e

    def default(self, line):
        """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code.
        """

        error('unknown command')

    def do_hist(self, args):
        """Print a list of commands that have been entered"""

        print self._hist

    def do_exit(self, args):
        """Exits from the console"""

        return -1

    def do_end(self, args):
        """Exits from the console"""

        return -1

    def do_help(self, args):
        """Get help on commands
        'help' or '?' with no arguments prints a list of commands for which help is available
        'help <command>' or '? <command>' gives help on <command>
        """

        ## The only reason to define this method is for the help text in the doc string
        cmd.Cmd.do_help(self, args)


class confDefaultsConsole(AbstractConsole):
    """console class for managing the device Defaults console"""

    def __init__(self, prompt, dynagen, dynamips_server):
        AbstractConsole.__init__(self)
        self.prompt = prompt[:-1] + '-)'
        self.dynagen = dynagen
        self.dynamips_server = dynamips_server
        self.d = self.dynamips_server.host + ':' + str(self.dynamips_server.port)
        self.config = self.dynagen.defaults_config[self.d]
        self.adaptertuple = ()
        self.max_slots = 6
        self.min_slot = 0
        self.chassis = 'None'
        self.default_image = 'None'
        self.default_ghostios = 'False'
        self.default_cnfg = 'None'
        self.default_conf = 'None'
        self.default_confreg = '0x2102'
        self.default_aux = 'None'
        self.default_image = 'None'
        self.default_idlepc = 'None'
        self.default_exec_area = 'None'
        self.default_mmap = True
        self.default_sparsemem = 'False'
        self.default_ghostsize = None

    def do_exit(self, args):
        """Exits from the console"""

        #if there are no scalars in self.config delete it
        if self.config.keys() == []:
            del self.dynagen.defaults_config[self.d][self.chassis]
            self.dynamips_server.configchange = True
        return -1

    def precmd(self, line):
        ''' This method is called after the line has been input but before
                    it has been interpreted. If you want to modifdy the input line
                    before execution (for example, variable substitution) do it here.
                This is the tricky part how we use this method over here in confDefaultsRouterCOnsole. By entering
                "slot1 = NM-16ESW" the method do_slot will not be called just because there is that number 1 over there.
                So we will do a trick - change the line internally to "slot 1 = NM-16ESW".
        '''

        self._hist += [line.strip()]
        if len(line) <= 4:
            return line

        first_four_letters = line[:4]

        if first_four_letters == 'slot':
            return 'slot ' + line[4:]
        else:
            return line

    def clean_args(self, args):
        """clean the arguments from spaces and stuff"""

        #get rid of that '='
        argument = args.strip('=')
        #get rid of starting space
        argument = argument.strip()
        return argument

    def set_int_option(self, option, args):
        """set integer type option in config"""

        if '?' in args or args.strip() == '':
            print getattr(self, 'do_' + option).__doc__
            return

        argument = self.clean_args(args)
        try:
            option_value = int(argument)
            if getattr(self, 'default_' + option) == option_value:
                if self.config.has_key(option):
                    del self.config[option]
            else:
                self.config[option] = option_value
        except (TypeError, ValueError):
            error('enter number, not: ' + argument)
        self.dynamips_server.configchange = True

    def set_string_option(self, option, args):
        """set string type option in config"""

        if '?' in args or args.strip() == '':
            print getattr(self, 'do_' + option).__doc__
            return

        option_value = self.clean_args(args)
        if getattr(self, 'default_' + option) == option_value:
            if self.config.has_key(option):
                del self.config[option]
        else:
            self.config[option] = option_value
        self.dynamips_server.configchange = True

    def do_no(self, args):
        """no <option> = <option_value>
\tunset the option_value from option. This will effectivelly set the option the the default value."""

        if '?' in args or args.strip() == '':
            print self.do_no.__doc__
            return
        argument = args.split('=')
        if len(argument) != 2:
            error('Invalid syntax')
            return
        else:
            #check if there is a setting equal to what it is saying
            (lside, rside) = argument
            lside = lside.strip()
            rside = rside.strip()
            try:
                if str(self.config[lside]) == rside:
                    #emit the command lside = default_option, this will effectivelly make the command not visible in config
                    self.onecmd(lside + '=' + str(getattr(self, 'default_' + lside)))
                else:
                    error('Bad ' + lside + 'value: ' + rside)
            except KeyError:
                error('Bad option: ' + lside)

    def do_image(self, args):
        """image = <IOS image>
\tset image to <IOS image>"""

        if '?' in args or args.strip() == '':
            print self.do_image.__doc__
            return

        image = self.clean_args(args)
        if self.default_image == image:
            if self.config.has_key('image'):
                del self.config['image']
        else:
            self.config['image'] = image
            #try to find idlepc value for this image in idlepc db
            imagename = os.path.basename(image)
            if self.dynagen.useridledb:
                if imagename in self.dynagen.useridledb:
                    print imagename + ' found in user idlepc database\nSetting idlepc value to ' + self.dynagen.useridledb[imagename]
                    self.config['idlepc'] = self.dynagen.useridledb[imagename]
            self.dynamips_server.configchange = True

    def do_ram(self, args):
        """ram = <ram size>
\tset amount of Virtual RAM to allocate to each router instance to <ram size> MB"""

        self.set_int_option('ram', args)

    def do_nvram(self, args):
        """nvram = <nvram size>
\tset nvram size to <nvram size> MB"""

        self.set_int_option('nvram', args)

    def do_disk0(self, args):
        """disk0 = <disk0 size>
\tset size of PCMCIA ATA disk0 to <disk0 size> MB"""

        self.set_int_option('disk0', args)

    def do_disk1(self, args):
        """disk1 = <disk1 size>
\tset size of PCMCIA ATA disk1 to <disk1 size> MB"""

        self.set_int_option('disk1', args)

    def do_cnfg(self, args):
        """cnfg = <configuration file>
\tconfiguration file to import. This is the fully qualified path relative to the system running dynamips."""

        self.set_string_option('cnfg', args)

    def do_confreg(self, args):
        """confreg = <confreg value>
\tset configuration register value to <confreg value>"""

        self.set_string_option('confreg', args)

    def do_sparsemem(self, args):
        """sparsemem = True|False
\tenable or dissable sparse memory method for memory allocation"""

        self.set_string_option('sparsemem', args)

    def do_ghostios(self, args):
        """ghostios = {True|False}
\tenable or disable IOS ghosting"""

        if '?' in args or args.strip() == '':
            print self.do_ghostios.__doc__
            return

        ghostios = self.clean_args(args)
        if ghostios in ('True', 'False'):
            if self.default_ghostios == ghostios:
                if self.config.has_key('ghostios'):
                    del self.config['ghostios']
            else:
                if self.config.has_key('image'):
                    self.config['ghostios'] = bool(ghostios)
                else:
                    error('specify first the IOS image and AFTER that turn on IOS ghosting')
        else:
            #implement IOS ghosting and port all other instances into it
            error('the only possible options are True or False, not: ' + args)
        self.dynamips_server.configchange = True

    def do_idlepc(self, args):
        """idlepc = <idlepc_value>
\tSet the Idle PC value"""

        self.set_string_option('idlepc', args)

    def do_idlemax(self, args):
        """idlemax = <number>
\tadvanced manipulation of idlepc"""

        self.set_int_option('idlemax', args)

    def do_ghostsize(self, args):
        """ghostsize = <number>
\tsets the size of the ram allocated to the ghost IOS image
Must be at least as large as the device with the most ram that
will use this ghost image"""

        self.set_int_option('ghostsize', args)

    def do_idlesleep(self, args):
        """idlesleep = <number>
\tadvanced manipulation of idlepc"""

        self.set_int_option('idlesleep', args)

    def do_slot(self, args):
        if '?' in args or args.strip() == '':
            print self.do_slot.__doc__
            return
        argument = args.split('=')
        if len(argument) == 2:
            argument[0] = argument[0].strip()
            argument[1] = argument[1].strip()
            try:
                slot_number = int(argument[0])
                #check if we can fit this adapter into the router model
                if slot_number < self.min_slot or slot_number > self.max_slots:
                    error('use slot correct number 1-' + str(self.max_slots) + ' to specify slot number')
                    return
                #check if the user did not write slot x = None, then remove the adaptor from slot
                if argument[1] == 'None':
                    if self.config.has_key('slot' + argument[0]):
                        del self.config['slot' + argument[0]]
                        return
                #check if router supports this adapter type
                if argument[1] not in self.adaptertuple:
                    error('incorrect adapter specified: ' + argument[1])
                    return
                #add the adapter
                self.config['slot' + argument[0]] = argument[1]
            except ValueError:
                error('use slot correct number 1-' + str(self.max_slots) + ' to specify slot number')
                return
        else:
            error('incorect syntax: ' + args)

        self.dynamips_server.configchange = True


class conf1700DefaultsConsole(confDefaultsConsole):

    """this is defaults configuration console for Cisco1700 routers. The only difference is in chassis variable"""

    def __init__(
        self,
        prompt,
        dynagen,
        dynamips_server,
        chassis,
        ):
        confDefaultsConsole.__init__(self, prompt, dynagen, dynamips_server)
        self.prompt = self.prompt[:-1] + chassis + ')'
        self.chassis = chassis
        if chassis in self.config:
            self.config = self.config[chassis]
        else:
            self.config[chassis] = {}
            self.config = self.config[chassis]

        self.adaptertuple = GENERIC_1700_NMS
        self.default_ram = 64
        self.default_nvram = 32
        self.default_disk0 = 0
        self.default_disk1 = 0
        self.max_slots = 0


class conf2600DefaultsConsole(confDefaultsConsole):

    """this is defaults configuration console for Cisco2600 routers. The only difference is in chassis variable"""

    def __init__(
        self,
        prompt,
        dynagen,
        dynamips_server,
        chassis,
        ):
        confDefaultsConsole.__init__(self, prompt, dynagen, dynamips_server)
        self.prompt = self.prompt[:-1] + chassis + ')'
        self.chassis = chassis
        if chassis in self.config:
            self.config = self.config[chassis]
        else:
            self.config[chassis] = {}
            self.config = self.config[chassis]

        self.adaptertuple = GENERIC_2600_NMS
        self.default_ram = 64
        self.default_nvram = 128
        self.default_disk0 = 8
        self.default_disk1 = 8
        self.max_slots = 1

    def do_slot(self, args):
        """slot<number> = <slot_type>
\tAdd an adaptor into the router
\tUse this if the automatic creation of adaptor does not suit you. Possible values are:
\tNM-1FE-TX    (FastEthernet, 1 port)
\tNM-1E        (Ethernet, 1 port)
\tNM-4E        (Ethernet, 4 ports)
\tNM-16ESW     (Ethernet switch module, 16 ports)"""

        confDefaultsConsole.do_slot(self, args)


class conf2691DefaultsConsole(confDefaultsConsole):

    """this is defaults configuration console for Cisco2691, Cisco3725 and Cisco3745. The only difference is in chassis variable"""

    def __init__(
        self,
        prompt,
        dynagen,
        dynamips_server,
        chassis,
        ):
        confDefaultsConsole.__init__(self, prompt, dynagen, dynamips_server)
        self.prompt = self.prompt[:-1] + chassis + ')'
        self.chassis = chassis
        if chassis in self.config:
            self.config = self.config[chassis]
        else:
            self.config[chassis] = {}
            self.config = self.config[chassis]
        self.default_ram = 128
        self.adaptertuple = GENERIC_3700_NMS
        if chassis == '2691':
            self.max_slots = 1
            self.default_nvram = 55
            self.default_disk0 = 16
            self.default_disk1 = 0
        elif chassis == '3725':
            self.max_slots = 2
            #fill 3725 defaults
            self.default_nvram = 55
            self.default_disk0 = 16
            self.default_disk1 = 0
        elif chassis == '3745':
            self.max_slots = 4
            self.default_nvram = 151
            self.default_disk0 = 16
            self.default_disk1 = 0

    def do_slot(self, args):
        """slot<number> = <slot_type>
\tAdd an adaptor into the router
\tUse this if the automatic creation of adaptor does not suit you. Possible values are:
\tNM-1FE-TX    (FastEthernet, 1 port)
\tNM-4T        (Serial, 4 ports)
\tNM-16ESW     (Ethernet switch module, 16 ports)"""

        confDefaultsConsole.do_slot(self, args)


class conf3600DefaultsConsole(confDefaultsConsole):

    """this is defaults configuration console for Cisco3620, Cisco3640 and Cisco3660. The only difference is in chassis variable"""

    def __init__(
        self,
        prompt,
        dynagen,
        dynamips_server,
        chassis,
        ):
        confDefaultsConsole.__init__(self, prompt, dynagen, dynamips_server)
        self.prompt = self.prompt[:-1] + chassis + ')'
        self.adaptertuple = GENERIC_3600_NMS
        self.chassis = chassis
        if chassis in self.config:
            self.config = self.config[chassis]
        else:
            self.config[chassis] = {}
            self.config = self.config[chassis]
        if chassis == '3620':
            self.max_slots = 1
        if chassis == '3640':
            self.max_slots = 3
        if chassis == '3660':
            self.max_slots = 6
        #fill 3600 defaults
        self.default_ram = 128
        self.default_nvram = 128
        self.default_disk0 = 0
        self.default_disk1 = 0
        self.default_iomem = None

    def do_slot(self, args):
        """slot<number> = <slot_type>
\tAdd an adaptor into the router. Leopard-2FE is in slot0 by default.
\tUse this if the automatic creation of adaptor does not suit you. Possible values are:
\tNM-1E        (Ethernet, 1 port)
\tNM-4E        (Ethernet, 4 ports)
\tNM-1FE-TX    (FastEthernet, 1 port)
\tNM-4T        (Serial, 4 ports)
\tNM-16ESW     (Ethernet switch module, 16 ports)"""

        confDefaultsConsole.do_slot(self, args)

    def do_iomem(self, args):
        """iomem = <number>
\tPercentage of router RAM to allocate for iomem"""

        self.set_int_option('iomem', args)


class conf7200DefaultsConsole(confDefaultsConsole):

    def __init__(self, prompt, dynagen, dynamips_server):
        confDefaultsConsole.__init__(self, prompt, dynagen, dynamips_server)
        self.prompt = self.prompt[:-1] + '7200)'

        self.adaptertuple = GENERIC_7200_PAS
        self.npeTuple = (
            'npe-100',
            'npe-150',
            'npe-175',
            'npe-200',
            'npe-225',
            'npe-300',
            'npe-400',
            'npe-g1',
            'npe-g2',
            )
        self.max_slots = 6
        self.chassis = '7200'
        if '7200' in self.config:
            self.config = self.config['7200']
        else:
            self.config['7200'] = {}
            self.config = self.config['7200']
        self.default_ram = 256
        self.default_nvram = 128
        self.default_disk0 = 64
        self.default_disk1 = 0
        self.default_npe = 'npe-200'
        self.default_midplane = 'vxr'

    def do_slot(self, args):
        """slot<number> = <slot_type>
\tAdd an adaptor into the router.
\tUse this if the automatic creation of adaptor does not suit you. Possible values are:
\tPA-GE        (GigabitEthernet, 1 port)
\tPA-FE-TX     (FastEthernet, 1 port)
\tPA-2FE-TX    (FastEthernet, 2 ports)
\tPA-4E        (Ethernet, 4 ports)
\tPA-8E        (Ethernet, 8 ports)
\tPA-4T+       (Serial, 4 ports)
\tPA-8T        (Serial, 8 ports)
\tPA-A1        (ATM, 1 port)
\tPA-POS-OC3   (Packet over Sonet, 1 port)"""

        confDefaultsConsole.do_slot(self, args)

    def do_npe(self, args):
        """npe = <npe type>
\tset NPE type. Choose 'npe-100', 'npe-150', 'npe-175', 'npe-200', 'npe-225', 'npe-300' or 'npe-400' """

        if '?' in args or args.strip() == '':
            print self.do_npe.__doc__
            return

        npe = self.clean_args(args)
        if npe in self.npeTuple:
            if self.default_npe == npe:
                if self.config.has_key('npe'):
                    del self.config['npe']
            else:
                self.config['npe'] = npe
        else:
            error('this NPE type does not exist: ' + args + '\nProper npe types are: ' + str(self.npeTuple))

        self.dynamips_server.configchange = True

    def do_midplane(self, args):
        '''midplane = <midplane type>
\tset midplane type. Choose "std" or "vxr" '''

        if '?' in args or args.strip() == '':
            print self.do_midplane.__doc__
            return

        midplane = self.clean_args(args)
        if midplane in ('std', 'vxr'):
            if self.default_midplane == midplane:
                if self.config.has_key('midplane'):
                    del self.config['midplane']
            else:
                self.config['midplane'] = midplane
        else:
            error('this midplane type does not exist: ' + args)

        self.dynamips_server.configchange = True

class confRouterConsole(confDefaultsConsole):

    def __init__(self, router, prompt, dynagen):
        AbstractConsole.__init__(self)
        self.router = router
        self.prompt = prompt[:-1] + '-router ' + router.name + ')'
        self.dynagen = dynagen
        self.d = self.router.dynamips.host + ':' + str(self.router.dynamips.port)
        self.r = 'ROUTER ' + self.router.name
        self.dynagen.update_running_config()
        self.running_config = self.dynagen.running_config[self.d][self.r]
        self.defaults_config = self.dynagen.defaults_config[self.d][self.router.model_string]

    def do_exit(self, args):
        """Exits from the console"""

        return -1

    def precmd(self, line):
        ''' This method is called after the line has been input but before
                    it has been interpreted. If you want to modifdy the input line
                    before execution (for example, variable substitution) do it here.
                This is the tricky part how we use this method over here in confRouterCOnsole. By entering
                "f2/0 = R2 f2/0" the method do_f will not be called just because there is that number 2 over there.
                So we will do a trick - change the line internally to "f 2/0 = R2 f2/0".
            '''

        #self._hist += [ line.strip() ]
        changed_line = line
        argument = line.split('=')
        if len(argument) != 2:
            return line
        (lside, rside) = argument
        #if the left side is "slot <number>"
        if SLOT_RE.search(lside):
            changed_line = line[:4] + ' ' + line[4:]
        elif self.namespace.interface_re.search(lside) or self.namespace.interface_noport_re.search(lside):

        #if the left side is <int><slot_number>/<port_number>
            #if this is two letter version of interfaces
            if (line[0].upper() == 'G' and line[1].upper() == 'I') or (line[0].upper() == 'F' and line[1].upper() == 'A') or (line[0].upper() == 'E' and line[1].upper() == 'T') or (line[0].upper() == 'A' and line[1].upper() == 'T') or (line[0].upper() == 'S' and line[1].upper() == 'E') or (line[0].upper() == 'P' and line[1].upper() == 'O'):
                changed_line = line[0] + line[1] + ' ' + line[2:]
            else:
                changed_line = line[0] + ' ' + line[1:]

        return changed_line

    def generic_connect(self, interface, args):
        if '?' in args or args.strip() == '':
            print getattr(self, 'do_' + interface).__doc__
            return
        self.connect(interface + args)

    def do_g(self, args):
        """g<int1> = <router_name> <int2>
\tmake a new GigabitEthernet connection between int1 and int2"""

        self.generic_connect('g', args)

    def do_gi(self, args):
        """gi<int1> = <router_name> <int2>
\tmake a new GigabitEthernet connection between int1 and int2"""

        self.generic_connect('gi', args)

    def do_f(self, args):
        """f<int1> = <router_name> <int2>
\tmake a new FastEthernet connection between int1 and int2"""

        self.generic_connect('f', args)

    def do_fa(self, args):
        """fa<int1> = <router_name> <int2>
\tmake a new FastEthernet connection between int1 and int2"""

        self.generic_connect('fa', args)

    def do_e(self, args):
        """e<int1> = <router_name> <int2>
\tmake a new Ethernet connection between int1 and int2"""

        self.generic_connect('e', args)

    def do_et(self, args):
        """et<int1> = <router_name> <int2>
\tmake a new Ethernet connection between int1 and int2"""

        self.generic_connect('et', args)

    def do_s(self, args):
        """s<int1> = <router_name> <int2>
\tmake a new Serial connection between int1 and int2"""

        self.generic_connect('s', args)

    def do_se(self, args):
        """se<int1> = <router_name> <int2>
\tmake a new Serial connection between int1 and int2"""

        self.generic_connect('se', args)

    def connect(self, args):
        #check whether this is a properly formatted connect command
        argument = args.split('=')
        if len(argument) != 2:
            error('invalid syntax, not enough arguments: ' + args)
            return

        source = argument[0].strip()
        destination = argument[1].strip()
        try:
            result = self.dynagen.connect(self.router, source, destination)
        except DynamipsError, e:
            err = e[0]
            error('Connecting %s %s to %s resulted in:    %s' % (self.router.name, source, destination, err))
            return

        if result == False:
            error('Attempt to connect %s %s to unknown device: "%s"' % (self.router.name, source, destination))

    def no_slot(self, adapter):
        #remove all connections that are in this slot
        #emit "no" version of all slot subcommands
        for scalar in self.running_config.scalars:
            if self.namespace.interface_re.search(scalar):
                #check if this is the connection for current adapter
                if int(scalar[1]) == adapter.slot:
                    #emit the 'no' version of the command
                    command = 'no ' + scalar + ' = ' + self.running_config[scalar]
                    self.onecmd(command)
        #determine whether this is a slot that can be removed (f.e. PA_C7200_IO_FE cannot be removed)
        if adapter.can_be_removed():
            adapter.remove()
            self.router.slot[adapter.slot] = None

        self.dynagen.update_running_config()

    def do_no(self, args):
        """no <int1> = <router_name> <int2>
\tdelete a connection between int1 and int2
no <option> = <option_value>
\tunset the option_value from option. This will effectivelly set the option the the default value"""

        if '?' in args or args.strip() == '':
            print self.do_no.__doc__
            return

        #check whether this is a properly formatted "no" command
        argument = args.split('=')
        if len(argument) != 2:
            error('invalid syntax: ' + args)
            return

        #if this is "no fa1/2 = R2 f0/0" type of command
        if self.namespace.interface_re.search(argument[0].strip()) or self.namespace.interface_noport_re.search(argument[0].strip()):
            source = argument[0].strip().lower()
            destination = argument[1].strip().lower()

            #check if this connection already exists. If it exists we should be having it in running_config
            self.dynagen.update_running_config()

            h = self.router.dynamips.host + ':' + str(self.router.dynamips.port)
            r = 'ROUTER ' + self.router.name
            source = argument[0].strip()
            dest = argument[1].strip()
            try:
                if self.dynagen.running_config[h][r][source].lower() == destination:
                    try:
                        result = self.dynagen.disconnect(self.router, source, dest)
                    except DynamipsError, e:
                        err = e[0]
                        error('Disconnecting %s %s from %s resulted in:    %s' % (self.router.name, source, dest, err))
                        return
                    if result == False:
                        error('Attempt to disconnect %s %s from unknown device: "%s"' % (self.router.name, source, dest))
                else:
                    error('Disconnecting %s %s from %s resulted in:    connection does not exist' % (self.router.name, source, dest))
            except KeyError:
                error('Disconnecting %s %s from %s resulted in:    cannot find %s ' % (self.router.name, source, dest, source))
        elif SLOT_RE.search(argument[0].strip()):
        #if this is a 'no slot1' type of command
            (lside, rside) = argument
            lside = lside.strip()
            rside = rside.strip()
            try:
                slot = int(lside[4])
            except ValueError:
                error('Bad syntax')
                return
            #check if this is the same thing as in the router slot
            if self.router.slot[slot] == None:
                error('This slot is already empty')
                return
            adapter = self.router.slot[slot]
            adapter_name = adapter.adapter

            if adapter_name == rside:
                #remove all connections that are in this slot
                self.no_slot(adapter)
            else:
                error('Bad ' + lside + 'value: ' + rside)
        else:
            #this is "no ram = 160" type command
            #check if there is a setting equal to what it is saying
            (lside, rside) = argument
            lside = lside.strip()
            rside = rside.strip()
            try:
                if lside == 'ghostios':
                    if self.router.ghost_status == 2:
                        ghostios = True
                    elif self.router.ghost_status == 3:
                        ghostios = False
                    if rside in ['True', 'False']:
                        if bool(rside) == ghostios:
                            self.onecmd('ghostios = ' + str(not ghostios))
                    else:
                        error('Bad ' + lside + 'value: ' + rside)
                    return
                if str(getattr(self.router, lside)) == rside:
                    #emit the command lside = default_option, this will effective make the command not visible in config
                    if self.defaults_config.has_key(lside):
                        self.onecmd(lside + '=' + self.defaults_config[lside])
                    else:
                        self.onecmd(lside + '=' + str(getattr(self.router, 'default_' + lside)))
                else:
                    error('Bad ' + lside + 'value: ' + rside)
            except ValueError, AttributeError:
                error('Bad option: ' + lside)

    def clean_args(self, args):
        #get rid of that '='
        argument = args.strip('=')
        #get rid of starting space
        argument = argument.strip()
        return argument

    def set_int_option(self, option, args):
        """This method overrides the method in confDefaultsConsole. This is the only functionality difference"""

        if '?' in args or args.strip() == '':
            print getattr(self, 'do_' + option).__doc__
            return

        argument = self.clean_args(args)
        try:
            option_value = int(argument)
            if getattr(self.router, option) != option_value:
                try:
                    setattr(self.router, option, option_value)
                except DynamipsError, e:
                    error(e)
        except (TypeError, ValueError):
            error('enter number, not: ' + argument)

    def set_string_option(self, option, args):
        """This method overrides the method in confDefaultsConsole. This is the only functionality difference"""

        if '?' in args or args.strip() == '':
            print getattr(self, 'do_' + option).__doc__
            return

        option_value = self.clean_args(args)
        if getattr(self.router, option) != option_value:
            try:
                setattr(self.router, option, option_value)
                return [True, option_value]
            except DynamipsError, e:
                error(e)
        else:
            return [False, None]

    def do_image(self, args):
        """image = <IOS image>
\tset image to <IOS image>"""

        [result, image] = self.set_string_option('image', args)
        if result:
            imagename = os.path.basename(image)
            if self.dynagen.useridledb:
                if imagename in self.dynagen.useridledb:
                    print imagename + ' found in user idlepc database\nSetting idlepc value to ' + self.dynagen.useridledb[imagename]
                    self.do_idlepc(self.dynagen.useridledb[imagename])

    def do_ghostios(self, args):
        """ghostios = {True|False}
\tEnable or disable IOS ghosting"""

        if '?' in args or args.strip() == '':
            print self.do_ghostios.__doc__
            return

        ghostios = self.clean_args(args)
        if ghostios in ('True', 'False'):
            if ghostios == 'True':
                ghost_file = self.router.imagename + '.ghost'
                try:
                    self.router.ghost_status = 2
                    self.router.ghost_file = ghost_file
                except DynamipsError, e:
                    error(e)
            else:
                self.router.ghost_status = 3
        else:
            error('the only possible options are True or False, not: ' + args)

    def do_slot(self, args):
        if '?' in args or args.strip() == '':
            print self.do_slot.__doc__
            return

        argument = args.split('=')
        if len(argument) == 2:
            argument[0] = argument[0].strip()
            slot_type = argument[1].strip()
            try:
                slot_number = int(argument[0])
                self.dynagen.setproperty(self.router, 'slot' + str(slot_number), slot_type)
            except (TypeError, ValueError, DynamipsError), e:
                error(e)
        else:
            error('incorect syntax: ' + args)


    #there are all other function over here (like do_ram, do_nvram etc.) that are inherited from confDefaultsConsole
    #as we have overriden the set_string_option functions they should behave the way we want them


class conf7200RouterConsole(confRouterConsole):

    def do_p(self, args):
        """p <int1> = <router_name> <int2>
\tmake a new POS OC3 connection between int1 and int2"""

        self.generic_connect('p', args)

    def do_po(self, args):
        """po<int1> = <router_name> <int2>
\tmake a new POS OC3 connection between int1 and int2"""

        self.generic_connect('po', args)

    def do_a(self, args):
        """a<int1> = <router_name> <int2>
\take a new ATM connection between int1 and int2"""

        self.generic_connect('a', args)

    def do_at(self, args):
        """at<int1> = <router_name> <int2>
\tmake a new ATM connection between int1 and int2"""

        self.generic_connect('at', args)

    def do_npe(self, args):
        """npe = <npe type>
\tset NPE type. Choose 'npe-100', 'npe-150', 'npe-175', 'npe-200', 'npe-225', 'npe-300', 'npe-400','npe-g1' or 'npe-g2' """

        self.set_string_option('npe', args)

    def do_midplane(self, args):
        """midplane = <midplane type>
\tset midplane type. Choose 'std' or 'vxr' """

        self.set_string_option('midplane', args)

    def do_slot(self, args):
        """slot<number> = <slot_type>
\tAdd an adaptor into the router.
\tUse this if the automatic creation of adaptor does not suit you. Possible values are:
\tPA-GE        (GigabitEthernet, 1 port)
\tPA-FE-TX     (FastEthernet, 1 port)
\tPA-2FE-TX    (FastEthernet, 2 ports)
\tPA-4E        (Ethernet, 4 ports)
\tPA-8E        (Ethernet, 8 ports)
\tPA-4T+       (Serial, 4 ports)
\tPA-8T        (Serial, 8 ports)
\tPA-A1        (ATM, 1 port)
\tPA-POS-OC3   (Packet over Sonet, 1 port)"""

        confRouterConsole.do_slot(self, args)


class conf3600RouterConsole(confRouterConsole):

    def do_iomem(self, args):
        """iomem = <number>
\tPercentage of router RAM to allocate for iomem"""

        self.set_int_option('iomem', args)

    def do_slot(self, args):
        """slot<number> = <slot_type>
\tAdd an adaptor into the router. Leopard-2FE is in the slot0 by default.
\tUse this if the automatic creation of adaptor does not suit you. Possible values are:
\tNM-1E        (Ethernet, 1 port)
\tNM-4E        (Ethernet, 4 ports)
\tNM-1FE-TX    (FastEthernet, 1 port)
\tNM-4T        (Serial, 4 ports)
\tNM-16ESW     (Ethernet switch module, 16 ports)"""

        confRouterConsole.do_slot(self, args)


class conf2691RouterConsole(confRouterConsole):

    def do_slot(self, args):
        """slot<number> = <slot_type>
\tAdd an adaptor into the router.
\tUse this if the automatic creation of adaptor does not suit you. Possible values are:
\tNM-1FE-TX    (FastEthernet, 1 port)
\tNM-4T        (Serial, 4 ports)
\tNM-16ESW     (Ethernet switch module, 16 ports)"""

        confRouterConsole.do_slot(self, args)


class conf2600RouterConsole(confRouterConsole):

    def do_slot(self, args):
        """slot<number> = <slot_type>
\tAdd an adaptor into the router.
\tUse this if the automatic creation of adaptor does not suit you. Possible values are:
\tNM-1E        (Ethernet, 1 port)
\tNM-4E        (Ethernet, 4 ports)
\tNM-1FE-TX    (FastEthernet, 1 port)
\tNM-16ESW     (Ethernet switch module, 16 ports)"""

        confRouterConsole.do_slot(self, args)


class conf1700RouterConsole(confRouterConsole):

    pass


class AbstractConfSWConsole(AbstractConsole):
    """abstract console for all dynamips emulated switches, that implement the common behaviour"""

    def __init__(self):
        AbstractConsole.__init__(self)

    def do_1(self, args):
        self.connect('1' + args)

    def do_2(self, args):
        self.connect('2' + args)

    def do_3(self, args):
        self.connect('3' + args)

    def do_4(self, args):
        self.connect('4' + args)

    def do_5(self, args):
        self.connect('5' + args)

    def do_6(self, args):
        self.connect('6' + args)

    def do_7(self, args):
        self.connect('7' + args)

    def do_8(self, args):
        self.connect('8' + args)

    def do_9(self, args):
        self.connect('9' + args)

    def precmd(self, line):
        ''' This method is called after the line has been input but before
                it has been interpreted. If you want to modifdy the input line
                before execution (for example, variable substitution) do it here.
            This is the tricky part how we use this method over here in confFRSWConsole. By entering
            "1:110 = 2:110" the method do_1 will not be called just because there is that ":" over there.
            So we will do a trick - change the line internally to "1 :110 = 2:110".
        '''

        argument = line.split('=')
        if len(argument) != 2 or line[:2] == 'no':
            return line
        else:
            changed_line = line[0] + ' ' + line[1:]
            return changed_line

class confFRSWConsole(AbstractConfSWConsole):

    def __init__(self, frsw, prompt, dynagen):
        AbstractConfSWConsole.__init__(self)
        self.frsw = frsw
        self.prompt = prompt[:-1] + '-frsw ' + frsw.name + ')'
        self.dynagen = dynagen
        self.d = self.frsw.dynamips.host + ':' + str(self.frsw.dynamips.port)
        self.f = 'FRSW ' + self.frsw.name
        self.dynagen.update_running_config()
        self.running_config = self.dynagen.running_config[self.d][self.f]

    def connect(self, args):
        params = args.split('=')
        if len(params) == 2:
            left_side = params[0].split(':')
            right_side = params[1].split(':')
            if len(left_side) == 2 and len(right_side) == 2:
                try:
                    port1 = int(left_side[0])
                    dlci1 = int(left_side[1])
                    port2 = int(right_side[0])
                    dlci2 = int(right_side[1])
                    #check whether this connection already exists:
                    if self.dynagen.running_config[self.d][self.f].has_key(params[0].strip()):
                        error('semantic error in: ' + args + ' this connection already exists')
                    else:
                        self.frsw.map(port1, dlci1, port2, dlci2)
                        self.frsw.map(port2, dlci2, port1, dlci1)
                        self.dynagen.update_running_config()
                except AttributeError:
                    error('semantic error in: ' + args)
                    return
                except DynamipsError, e:
                    error(e)
                    return
            else:
                error('invalid syntax in: ' + args)
        else:
            error('invalid syntax in: ' + args)

    def do_no(self, args):
        params = args.split('=')
        if len(params) == 2:
            left_side = params[0].split(':')
            right_side = params[1].split(':')
            if len(left_side) == 2 and len(right_side) == 2:
                try:
                    port1 = int(left_side[0])
                    dlci1 = int(left_side[1])
                    port2 = int(right_side[0])
                    dlci2 = int(right_side[1])
                    left_side = params[0].strip()
                    right_side = params[1].strip()
                    #check whether this connection already exists:
                    if self.dynagen.running_config[self.d][self.f][left_side] == right_side:
                        self.frsw.unmap(port1, dlci1, port2, dlci2)
                        self.frsw.unmap(port2, dlci2, port1, dlci1)
                        self.dynagen.update_running_config()

                    else:
                        error('semantic error in: ' + args + ' this connection does not exist')
                except AttributeError:
                    error('semantic error in: ' + args)
                    return
                except DynamipsError, e:
                    error(e)
                    return
                except DynamipsWarning, e:
                    error(e)
                    return
            else:
                error('invalid syntax in: ' + args)
        else:
            error('invalid syntax in: ' + args)

class confETHSWConsole(AbstractConfSWConsole):

    def __init__(self, ethsw, prompt, dynagen):
        AbstractConfSWConsole.__init__(self)
        self.ethsw = ethsw
        self.prompt = prompt[:-1] + '-ethsw ' + ethsw.name + ')'
        self.dynagen = dynagen
        self.d = self.ethsw.dynamips.host + ':' + str(self.ethsw.dynamips.port)
        self.e = 'ETHSW ' + self.ethsw.name
        self.dynagen.update_running_config()
        self.running_config = self.dynagen.running_config[self.d][self.e]

    def connect(self, args):
        params = args.split('=')
        if self.dynagen.running_config[self.d][self.e].has_key(params[0].strip()):
            error('semantic error in: ' + args + ' , this connection already exists')
            return
        self.dynagen.ethsw_map(self.ethsw, params[0].strip(), params[1].strip())

    def do_no(self, args):
        params = args.split('=')
        left_side = params[0].strip()
        right_side = params[1].strip()
        try:
            if self.dynagen.running_config[self.d][self.e][left_side] == right_side:
                self.ethsw.unset_port(int(left_side))
            else:
                error('this mapping does not exist')
        except IndexError:
            error('this mapping does not exist')
        except DynamipsError, e:
            error(e)

class confATMBRConsole(AbstractConfSWConsole):

    def __init__(self, atmbr, prompt, dynagen):
        AbstractConfSWConsole.__init__(self)
        self.atmbr = atmbr
        self.prompt = prompt[:-1] + '-atmbr ' + atmbr.name + ')'
        self.dynagen = dynagen
        self.d = self.atmbr.dynamips.host + ':' + str(self.atmbr.dynamips.port)
        self.a = 'ATMBR ' + self.atmbr.name
        self.dynagen.update_running_config()
        self.running_config = self.dynagen.running_config[self.d][self.a]

    def connect(self, args):
        params = args.split('=')
        if len(params) == 2:
            left_side = params[0]
            right_side = params[1].split(':')
            if len(right_side) == 3:
                try:
                    port1 = int(left_side)
                    port2 = int(right_side[0])
                    vpi2 = int(right_side[1])
                    vci2 = int(right_side[2])
                    #check whether this connection already exists:
                    if self.dynagen.running_config[self.d][self.a].has_key(params[0].strip()):
                        error('semantic error in: ' + args + ' this connection already exists')
                    else:
                        self.atmbr.configure(port1, port2, vpi2, vci2)
                        self.dynagen.update_running_config()
                except AttributeError:
                    error('semantic error in: ' + args)
                    return
                except DynamipsError, e:
                    error(e)
                    return
            else:
                error('invalid syntax in: ' + args)
        else:
            error('invalid syntax in: ' + args)

    def do_no(self, args):
        params = args.split('=')
        if len(params) == 2:
            left_side = params[0]
            right_side = params[1].split(':')
            if len(right_side) == 3:
                try:
                    port1 = int(left_side)
                    port2 = int(right_side[0])
                    vpi2 = int(right_side[1])
                    vci2 = int(right_side[2])
                    left_side = params[0].strip()
                    right_side = params[1].strip()
                    #check whether this connection already exists:
                    if self.dynagen.running_config[self.d][self.a][left_side] == right_side:
                        self.atmbr.unconfigure(port1, port2, vpi2, vci2)
                        self.dynagen.update_running_config()

                    else:
                        error('semantic error in: ' + args + ' this connection does not exist')
                except AttributeError:
                    error('semantic error in: ' + args)
                    return
                except DynamipsError, e:
                    error(e)
                    return
                except DynamipsWarning, e:
                    error(e)
                    return
            else:
                error('invalid syntax in: ' + args)
        else:
            error('invalid syntax in: ' + args)

class confATMSWConsole(AbstractConfSWConsole):

    def __init__(self, atmsw, prompt, dynagen):
        AbstractConfSWConsole.__init__(self)
        self.atmsw = atmsw
        self.prompt = prompt[:-1] + '-atmsw ' + atmsw.name + ')'
        self.dynagen = dynagen
        self.d = self.atmsw.dynamips.host + ':' + str(self.atmsw.dynamips.port)
        self.a = 'ATMSW ' + self.atmsw.name
        self.dynagen.update_running_config()
        self.running_config = self.dynagen.running_config[self.d][self.a]

    def connect(self, args):
        params = args.split('=')
        if len(params) == 2:
            try:
                left_side = params[0].split(':')
                right_side = params[1].split(':')
                if len(left_side) == 2 and len(right_side) == 2:
                    port1 = int(left_side[0])
                    vpi1 = int(left_side[1])
                    port2 = int(right_side[0])
                    vpi2 = int(right_side[1])
                    #check whether this connection already exists:
                    if self.dynagen.running_config[self.d][self.a].has_key(params[0].strip()):
                        error('semantic error in: ' + args + ' This connection already exists')
                    else:
                        self.atmsw.mapvp(port1, vpi1, port2, vpi2)
                        self.atmsw.mapvp(port2, vpi2, port1, vpi1)
                        self.dynagen.update_running_config()

                elif len(left_side) == 3 and len(right_side) == 3:
                    port1 = int(left_side[0])
                    vpi1 = int(left_side[1])
                    vci1 = int(left_side[2])
                    port2 = int(right_side[0])
                    vpi2 = int(right_side[1])
                    vci2 = int(right_side[2])
                    #check whether this connection already exists:
                    if self.dynagen.running_config[self.d][self.a].has_key(params[0].strip()):
                        error('semantic error in: ' + args + ' this connection already exists')
                    else:
                        self.atmsw.mapvc(port1, vpi1, vci1, port2, vpi2, vci2)
                        self.atmsw.mapvc(port2, vpi2, vci2, port1, vpi1, vci1)
                        self.dynagen.update_running_config()
                else:
                    error('invalid syntax in: ' + args)
            except AttributeError:
                error('semantic error in: ' + args)
                return
            except DynamipsError, e:
                error(e)
                return
        else:
            error('invalid syntax in: ' + args)

    def do_no(self, args):
        params = args.split('=')
        if len(params) == 2:
            left_side = params[0].split(':')
            right_side = params[1].split(':')
            try:
                if len(left_side) == 2 and len(right_side) == 2:
                    port1 = int(left_side[0])
                    dlci1 = int(left_side[1])
                    port2 = int(right_side[0])
                    dlci2 = int(right_side[1])
                    left_side = params[0].strip()
                    right_side = params[1].strip()
                    #check whether this connection already exists:
                    if self.dynagen.running_config[self.d][self.a][left_side] == right_side:
                        self.atmsw.unmapvp(port1, dlci1, port2, dlci2)
                        self.atmsw.unmapvp(port2, dlci2, port1, dlci1)
                        self.dynagen.update_running_config()

                    else:
                        error('semantic error in: ' + args + ' this connection does not exist')

                if len(left_side) == 3 and len(right_side) == 3:
                    port1 = int(left_side[0])
                    vpi1 = int(left_side[1])
                    vci1 = int(left_side[2])
                    port2 = int(right_side[0])
                    vpi2 = int(right_side[1])
                    vci2 = int(right_side[2])
                    left_side = params[0].strip()
                    right_side = params[1].strip()
                    if self.dynagen.running_config[self.d][self.a][left_side] == right_side:
                        self.atmsw.unmapvc(port1, vpi1, vci1, port2, vpi2, vci2)
                        self.atmsw.unmapvc(port2, vpi2, vci2, port1, vpi1, vci1)
                        self.dynagen.update_running_config()
                    else:
                        error('semantic error in: ' + args + ' this connection does not exist')
                else:
                    error('invalid syntax in: ' + args)
            except AttributeError:
                error('semantic error in: ' + args)
                return
            except DynamipsError, e:
                error(e)
                return
            except DynamipsWarning, e:
                error(e)
                return
        else:
            error('invalid syntax in: ' + args)

class confHypervisorConsole(AbstractConsole):

    def __init__(self, dynamips_server, dynagen):
        AbstractConsole.__init__(self)
        self.prompt = '=>(config-' + dynamips_server.host + ':' + str(dynamips_server.port) + ')'
        self.dynagen = dynagen
        self.dynamips_server = dynamips_server
        self.d = self.dynamips_server.host + ':' + str(self.dynamips_server.port)
        self.dynagen.update_running_config()

        self.routerInstanceMap = {
            '1710': self.namespace.C1700,
            '1720': self.namespace.C1700,
            '1721': self.namespace.C1700,
            '1750': self.namespace.C1700,
            '1751': self.namespace.C1700,
            '1760': self.namespace.C1700,
            '2691': self.namespace.C2691,
            '3620': self.namespace.C3600,
            '3640': self.namespace.C3600,
            '3660': self.namespace.C3600,
            '3725': self.namespace.C3725,
            '3745': self.namespace.C3745,
            '7200': self.namespace.C7200,
            '2610': self.namespace.C2600,
            '2611': self.namespace.C2600,
            '2620': self.namespace.C2600,
            '2621': self.namespace.C2600,
            '2610XM': self.namespace.C2600,
            '2611XM': self.namespace.C2600,
            '2620XM': self.namespace.C2600,
            '2621XM': self.namespace.C2600,
            '2650XM': self.namespace.C2600,
            '2651XM': self.namespace.C2600,
            }
        self.routerConsoleInstanceMap = {
            '1710': conf1700RouterConsole,
            '1720': conf1700RouterConsole,
            '1721': conf1700RouterConsole,
            '1750': conf1700RouterConsole,
            '1751': conf1700RouterConsole,
            '1760': conf1700RouterConsole,
            '2691': conf2691RouterConsole,
            '3620': conf3600RouterConsole,
            '3640': conf3600RouterConsole,
            '3660': conf3600RouterConsole,
            '3725': conf2691RouterConsole,
            '3745': conf2691RouterConsole,
            '7200': conf7200RouterConsole,
            '2610': conf2600RouterConsole,
            '2611': conf2600RouterConsole,
            '2620': conf2600RouterConsole,
            '2621': conf2600RouterConsole,
            '2610XM': conf2600RouterConsole,
            '2611XM': conf2600RouterConsole,
            '2620XM': conf2600RouterConsole,
            '2621XM': conf2600RouterConsole,
            '2650XM': conf2600RouterConsole,
            '2651XM': conf2600RouterConsole,
            }
        self.defaultConsoleInstanceMap = {
            '1710': conf1700DefaultsConsole,
            '1720': conf1700DefaultsConsole,
            '1721': conf1700DefaultsConsole,
            '1750': conf1700DefaultsConsole,
            '1751': conf1700DefaultsConsole,
            '1760': conf1700DefaultsConsole,
            '2691': conf2691DefaultsConsole,
            '3620': conf3600DefaultsConsole,
            '3640': conf3600DefaultsConsole,
            '3660': conf3600DefaultsConsole,
            '3725': conf2691DefaultsConsole,
            '3745': conf2691DefaultsConsole,
            '7200': conf7200DefaultsConsole,
            '2610': conf2600DefaultsConsole,
            '2611': conf2600DefaultsConsole,
            '2620': conf2600DefaultsConsole,
            '2621': conf2600DefaultsConsole,
            '2610XM': conf2600DefaultsConsole,
            '2611XM': conf2600DefaultsConsole,
            '2620XM': conf2600DefaultsConsole,
            '2621XM': conf2600DefaultsConsole,
            '2650XM': conf2600DefaultsConsole,
            '2651XM': conf2600DefaultsConsole,
            }

    def do_reset(self, args):
        """reset
\tresets current hypervisor"""

        #reset frontend - go through all routers, and delete everything
        for r in self.dynagen.defaults_config[self.d]:
            router_model = self.dynagen.defaults_config[self.d][r]
            # compare whether this is defaults section
            if router_model.name in self.namespace.DEVICETUPLE:
                self.onecmd('no defaults ' + router_model.name)

        #reset backend
        self.dynamips_server.reset()
        #update running_config
        self.dynagen.update_running_config()

    def do_3620(self, args):
        """3620
\tset defaults for Cisco 3620 in this hypervisor. Every new 3620 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['3620'](self.prompt, self.dynagen, self.dynamips_server, '3620')
        nested_cmd.cmdloop()

    def do_3640(self, args):
        """3640
\tset defaults for Cisco 3640 in this hypervisor. Every new 3640 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['3640'](self.prompt, self.dynagen, self.dynamips_server, '3640')
        nested_cmd.cmdloop()

    def do_3660(self, args):
        """3660
\tset defaults for Cisco 3660 in this hypervisor. Every new 3660 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['3660'](self.prompt, self.dynagen, self.dynamips_server, '3660')
        nested_cmd.cmdloop()

    def do_2691(self, args):
        """2691
\tset defaults for Cisco 2691 in this hypervisor. Every new 2691 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2691'](self.prompt, self.dynagen, self.dynamips_server, '2691')
        nested_cmd.cmdloop()

    def do_3725(self, args):
        """3725
\tset defaults for Cisco 3725 in this hypervisor. Every new 3725 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['3725'](self.prompt, self.dynagen, self.dynamips_server, '3725')
        nested_cmd.cmdloop()

    def do_3745(self, args):
        """3745
set defaults for Cisco 3745 in this hypervisor. Every new 3745 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['3745'](self.prompt, self.dynagen, self.dynamips_server, '3745')
        nested_cmd.cmdloop()

    def do_7200(self, args):
        """7200
\tset defaults for Cisco 7200 in this hypervisor. Every new 7200 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['7200'](self.prompt, self.dynagen, self.dynamips_server)
        nested_cmd.cmdloop()

    def do_2610(self, args):
        """2610
\tset defaults for Cisco 2610 in this hypervisor. Every new 2610 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2610'](self.prompt, self.dynagen, self.dynamips_server, '2610')
        nested_cmd.cmdloop()

    def do_2611(self, args):
        """2611
\tset defaults for Cisco 2611 in this hypervisor. Every new 2611 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2611'](self.prompt, self.dynagen, self.dynamips_server, '2611')
        nested_cmd.cmdloop()

    def do_2620(self, args):
        """2620
\tset defaults for Cisco 2620 in this hypervisor. Every new 2620 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2620'](self.prompt, self.dynagen, self.dynamips_server, '2620')
        nested_cmd.cmdloop()

    def do_2621(self, args):
        """2621
\tset defaults for Cisco 2621 in this hypervisor. Every new 2621 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2621'](self.prompt, self.dynagen, self.dynamips_server, '2621')
        nested_cmd.cmdloop()

    def do_2610XM(self, args):
        """2610XM
\tset defaults for Cisco 2610XM in this hypervisor. Every new 2610XM router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2610XM'](self.prompt, self.dynagen, self.dynamips_server, '2610XM')
        nested_cmd.cmdloop()

    def do_2611XM(self, args):
        """2611XM
\tset defaults for Cisco 2611XM in this hypervisor. Every new 2611XM router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2611XM'](self.prompt, self.dynagen, self.dynamips_server, '2611XM')
        nested_cmd.cmdloop()

    def do_2620XM(self, args):
        """2620XM
\tset defaults for Cisco 2620XM in this hypervisor. Every new 2620XM router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2620XM'](self.prompt, self.dynagen, self.dynamips_server, '2620XM')
        nested_cmd.cmdloop()

    def do_2621XM(self, args):
        """2621XM
\tset defaults for Cisco 2621XM in this hypervisor. Every new 2621XM router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2621XM'](self.prompt, self.dynagen, self.dynamips_server, '2621XM')
        nested_cmd.cmdloop()

    def do_2650XM(self, args):
        """2650XM
\tset defaults for Cisco 2650XM in this hypervisor. Every new 2650XM router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2650XM'](self.prompt, self.dynagen, self.dynamips_server, '2650XM')
        nested_cmd.cmdloop()

    def do_2651XM(self, args):
        """2651XM
\tset defaults for Cisco 2651XM in this hypervisor. Every new 2651XM router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['2651XM'](self.prompt, self.dynagen, self.dynamips_server, '2651XM')
        nested_cmd.cmdloop()

    def do_1710(self, args):
        """1710
\tset defaults for Cisco 1710 in this hypervisor. Every new 1710 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['1710'](self.prompt, self.dynagen, self.dynamips_server, '1710')
        nested_cmd.cmdloop()

    def do_1720(self, args):
        """1720
\tset defaults for Cisco 1720 in this hypervisor. Every new 1720 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['1720'](self.prompt, self.dynagen, self.dynamips_server, '1720')
        nested_cmd.cmdloop()

    def do_1721(self, args):
        """1721
\tset defaults for Cisco 1721 in this hypervisor. Every new 1721 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['1721'](self.prompt, self.dynagen, self.dynamips_server, '1721')
        nested_cmd.cmdloop()

    def do_1750(self, args):
        """1750
\tset defaults for Cisco 1750 in this hypervisor. Every new 1750 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['1750'](self.prompt, self.dynagen, self.dynamips_server, '1750')
        nested_cmd.cmdloop()

    def do_1751(self, args):
        """1751
\tset defaults for Cisco 1751 in this hypervisor. Every new 1751 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['1751'](self.prompt, self.dynagen, self.dynamips_server, '1751')
        nested_cmd.cmdloop()

    def do_1760(self, args):
        """1760
\tset defaults for Cisco 1760 in this hypervisor. Every new 1760 router will be created using these."""

        nested_cmd = self.defaultConsoleInstanceMap['1760'](self.prompt, self.dynagen, self.dynamips_server, '1760')
        nested_cmd.cmdloop()

    def do_router(self, args):
        """router <router_name>
\tswitch into configuration mode of the specific router eg. 'router R1'
router <new router name> model <router_model>
\tcreate new router of specific model with settings copied from the defaults section.Proper values are:
 1710, 1720, 1721, 1750, 1751, 1760, 2610, 2611, 2620, 2621, 2610XM, 2611XM, 2620XM, 2621XM, 2650XM, 2651XM, 2691, 3725, 3745, 3620, 3640, 3660, 7200
router <new_router_name>
\tcreate new 7200 router"""

        if '?' in args or args.strip() == '':
            print self.do_router.__doc__
            return

        params = args.split(' ')
        if len(params) == 1:
            #we are going to jump into router config mode
            #find out if the router_name exists
            try:
                router = self.dynagen.devices[params[0]]
                #check whether we are on the correct hypervisor
                if router.dynamips != self.dynamips_server:
                    error('this device is on different hypervisor: ' + router.dynamips.host + ':' + str(router.dynamips.port))
                    return
                #ok router_name exists, so let's run the nested Cmd console
                model = router.model_string
                nested_cmd = self.routerConsoleInstanceMap[model](router, self.prompt, self.dynagen)
                nested_cmd.cmdloop()
            except KeyError:
                #this router_name does not exist so let's create a 7200 with that name
                self.do_router(args + ' model 7200')
        elif len(params) == 3 and params[1] == 'model':

            #we are going to create a router of specific model
            #check whether the router_name does not exist
            if params[0] in self.dynagen.devices:
                error(params[0] + ' router already exists!')
                return
            #first let's gather all defaults/setting for each model from running config
            self.dynagen.update_running_config()

            devdefaults = {}
            for key in self.namespace.DEVICETUPLE:
                devdefaults[key] = {}

            config = self.dynagen.defaults_config

            #go through all section under dynamips server in running config and populate the devdefaults with model defaults
            for r in config[self.d]:
                router_model = config[self.d][r]
                # compare whether this is defaults section
                if router_model.name in self.namespace.DEVICETUPLE and router_model.name == params[2]:
                    # Populate the appropriate dictionary
                    for scalar in router_model.scalars:
                        if router_model[scalar] != None:
                            devdefaults[router_model.name][scalar] = router_model[scalar]

            model = params[2]
            #check whether a defaults section for this router type exists
            if model in self.namespace.DEVICETUPLE:
                if devdefaults[model] == {} and not devdefaults[model].has_key('image'):
                    error('Create a defaults section for ' + model + ' first! Minimum setting is image name')
                    return
                elif not devdefaults[model].has_key('image'):
                    error('Specify image name for ' + model + ' routers first!')
                    return
            else:
                error('Bad model: ' + model)
                return

            #now we have everything ready to create routers
            router = self.routerInstanceMap[model](self.dynamips_server, chassis=model, name=params[0])
            self.dynagen.setdefaults(router, devdefaults[model])

            #implement IOS ghosting when creating the router from configConsole
            #use devdefaults to find out whether we have ghostios = True, and simply set the ghostios
            if devdefaults[model].has_key('ghostios'):
                if devdefaults[model]['ghostios']:
                    router.ghost_status = 2
                    router.ghost_file = router.formatted_ghost_file()

            #add router to frontend
            self.dynagen.devices[params[0]] = router
            debug('Router ' + router.name + ' created')
            #and let's jump into the confRouterConsole

            nested_cmd = self.routerConsoleInstanceMap[model](self.dynagen.devices[params[0]], self.prompt, self.dynagen)
            nested_cmd.cmdloop()
        else:

             #bad number of args or 'model' not in the middle
            error('Bad syntax: ' + args)
            return

    def no_router(self, params):
        """delete the router and its connections"""

        try:
            router = self.dynagen.devices[params[1]]
        except (KeyError, AttributeError):
            error('this device does not exist: ' + params[1])
            return
        #check whether we are on correct hypervisor
        if router.dynamips != self.dynamips_server:
            error('this device is on different hypervisor: ' + router.dynamips.host + ':' + str(router.dynamips.port))
            return

        #stop the router
        if router.state != 'stopped':
            router.stop()

        #delete all router connections
        #find the router section in running config and emit 'no' version of all subcommands command
        router_section = None
        #update the config in case it wasn't updates
        self.dynagen.update_running_config()

        for r in self.dynagen.running_config[self.d]:
            router_section = self.dynagen.running_config[self.d][r]
            #if r is no a section, but a string....like workingdir
            if isinstance(router_section, str) or isinstance(router_section, int):
                continue
            router_section_name = router_section.name
            router_name = router_section_name.split(' ')
            if len(router_name) == 2 and router_name[1] == router.name:
                break
        for scalar in router_section.scalars:
            if self.namespace.interface_re.search(scalar) or self.namespace.interface_noport_re.search(scalar):
                nested_cmd = self.routerConsoleInstanceMap[router.model_string](router, self.prompt, self.dynagen)
                #emit the 'no' version of the command
                nested_cmd.onecmd('no ' + scalar + ' = ' + router_section[scalar])

        #now delete the router from back-end
        router.delete()
        del router
        #delete router from front-end
        del self.dynagen.devices[params[1]]
        print 'Router ' + params[1] + ' on ' + self.d + ' deleted'

        #update the config
        self.dynagen.update_running_config()

    def no_frsw(self, params):
        """delete the frsw and all its connections"""
        #check if the frsw exists
        try:
            frsw = self.dynagen.devices[params[1]]
        except (KeyError, AttributeError):
            error('this device does not exist: ' + params[1])
            return
        #check whether we are on correct hypervisor
        if frsw.dynamips != self.dynamips_server:
            error('this device is on different hypervisor: ' + frsw.dynamips.host + ':' + str(frsw.dynamips.port))
            return

        #delete all frsw mappings
        frsw_section = self.dynagen.running_config[self.d]['FRSW ' + frsw.name]
        for scalar in frsw_section.scalars:
            nested_cmd = confFRSWConsole(frsw, self.prompt, self.dynagen)
            #if the mapping got disconnected, before, do not try to disconnect it again
            if self.dynagen.running_config[self.d]['FRSW ' + frsw.name].has_key(scalar):
                nested_cmd.onecmd('no ' + scalar + ' = ' + frsw_section[scalar])
            self.dynagen.update_running_config()

        self.__delete_all_connections_to_sw(frsw)

        #now delete the frsw from backend
        frsw.delete()
        #delete router from front-end
        del self.dynagen.devices[params[1]]
        print 'Frame-relay switch ' + params[1] + ' deleted'
        #update the config
        self.dynagen.update_running_config()

    def __delete_all_connections_to_sw(self, device):
        """go through all hypervisors and routers and emit "no" version of all connection command that are refering this device. Used in FRSW, ATMSW disconnection"""

        #go through all hypervisors and routers and emit "no" version of all ATMSW subcommands
        for h in self.dynagen.running_config:
            if h == 'debug' or h == 'autostart':
                continue
            for r in self.dynagen.running_config[h]:
                if r == 'workingdir' or r == 'udp':
                    continue
                router_section = self.dynagen.running_config[h][r]
                #if this is a router section
                if router_section.name[:6].lower() == 'router':
                    for scalar in router_section.scalars:
                        #if this is a ATMSW connection f.e. s1/0 = ATM 1
                        if self.namespace.interface_re.search(scalar) or self.namespace.interface_noport_re.search(scalar):
                            try:
                                (router_name, dest_int) = router_section[scalar].split(' ')
                            except ValueError:
                                continue
                            if router_name == device.name:
                                #emit the 'no s1/0 = ATM 1' commmand
                                router = self.dynagen.devices[router_section.name[7:]]
                                nested_cmd = self.routerConsoleInstanceMap[router.model_string](router, self.prompt, self.dynagen)
                                nested_cmd.onecmd('no ' + scalar + ' = ' + router_section[scalar])

    def no_atmsw(self, params):
        """delete the atmsw and all its connections"""
        #check if the atmsw exists
        try:
            atmsw = self.dynagen.devices[params[1]]
        except (KeyError, AttributeError):
            error('this device does not exist: ' + params[1])
            return
        #check whether we are on correct hypervisor
        if atmsw.dynamips != self.dynamips_server:
            error('this device is on different hypervisor: ' + atmsw.dynamips.host + ':' + str(atmsw.dynamips.port))
            return

        #delete all atmsw mappings
        atmsw_section = self.dynagen.running_config[self.d]['ATMSW ' + atmsw.name]
        for scalar in atmsw_section.scalars:
            nested_cmd = confATMSWConsole(atmsw, self.prompt, self.dynagen)
            if self.dynagen.running_config[self.d]['ATMSW ' + atmsw.name].has_key(scalar):
                nested_cmd.onecmd('no ' + scalar + ' = ' + atmsw_section[scalar])
            self.dynagen.update_running_config()

        self.__delete_all_connections_to_sw(atmsw)

        #now delete the atmsw from backend
        atmsw.delete()
        #delete router from front-end
        del self.dynagen.devices[params[1]]
        print 'ATM switch ' + params[1] + ' deleted'
        #update the config
        self.dynagen.update_running_config()

    def no_atmbr(self, params):
        """delete the atmbr and all its connections"""
        #check if the atmbr exists
        try:
            atmbr = self.dynagen.devices[params[1]]
        except (KeyError, AttributeError):
            error('this device does not exist: ' + params[1])
            return
        #check whether we are on correct hypervisor
        if atmbr.dynamips != self.dynamips_server:
            error('this device is on different hypervisor: ' + atmbr.dynamips.host + ':' + str(atmbr.dynamips.port))
            return

        #delete all atmbr mappings
        atmbr_section = self.dynagen.running_config[self.d]['ATMBR ' + atmbr.name]
        for scalar in atmbr_section.scalars:
            nested_cmd = confATMBRConsole(atmbr, self.prompt, self.dynagen)
            if self.dynagen.running_config[self.d]['ATMBR ' + atmbr.name].has_key(scalar):
                nested_cmd.onecmd('no ' + scalar + ' = ' + atmbr_section[scalar])
            self.dynagen.update_running_config()

        self.__delete_all_connections_to_sw(atmbr)

        #now delete the atmbr from backend
        atmbr.delete()
        #delete router from front-end
        del self.dynagen.devices[params[1]]
        print 'ATM bridge ' + params[1] + ' deleted'
        #update the config
        self.dynagen.update_running_config()

    def no_defaults(self, params):
        """delete the model default section, delete all the routers of this model that are under current hypervisor"""

        try:
            defaults_section = self.dynagen.defaults_config[self.d][params[1]]
        except (KeyError, AttributeError):
            error('this defaults section does not exist: ' + params[1])
            return

        #go throught all routers for this hypervisor
        for device in self.dynagen.devices.values():
            if device.dynamips == self.dynamips_server:
                if isinstance(device, self.namespace.Router) and device.model_string == params[1]:
                    self.onecmd('no router ' + device.name)

        #delete the defaults section
        del self.dynagen.defaults_config[self.d][params[1]]
        print 'Defaults section ' + params[1] + ' on ' + self.d + ' deleted'

    def do_no(self, args):
        """no router <router_name>
\tdelete the router and all its connections
no defaults <router_model>
\tdelete the defaults model section, and all routers of this model"""

        if '?' in args or args.strip() == '':
            print self.do_no.__doc__
            return

        #check whether we have proper args
        params = args.split(' ')
        if len(params) != 2:
            error('syntax error, bad number of arguments: ' + args)
            return
        if params[0] == 'router':
            self.no_router(params)
        elif params[0] == 'defaults':
            self.no_defaults(params)
        elif params[0] == 'frsw':
            self.no_frsw(params)
        elif params[0] == 'atmsw':
            self.no_atmsw(params)
        elif params[0] == 'atmbr':
            self.no_atmbr(params)
        else:
            error('syntax error in second argument ' + args)
            return

    def clean_args(self, args):
        #get rid of that '='
        argument = args.strip('=')
        #get rid of starting space
        argument = argument.strip()
        return argument

    def do_workingdir(self, args):
        """workingdir = <OS path>
\tset the working directory for this hypervisor where all temp file are stored"""

        if '?' in args or args.strip() == '':
            print self.do_workingdir.__doc__
            return

        argument = self.clean_args(args)
        workingdir = self.dynamips_server.workingdir
        if workingdir != argument:
            try:
                workingdir = '"' + argument + '"'
                self.dynamips_server.workingdir = workingdir
            except DynamipsError, e:
                error(e)

    def do_frsw(self, args):
        """frsw <frsw name>
\tgo into frame-relay switch conf mode
frsw <new frsw_name>
\tcreate new frame-relay switch of this name"""

        if '?' in args or args.strip() == '':
            print self.do_frsw.__doc__
            return
        try:
            frsw = self.dynagen.devices[args]
            #check whether we are on correct hypervisor
            if frsw.dynamips != self.dynamips_server:
                error('this device is on different hypervisor: ' + frsw.dynamips.host + ':' + str(frsw.dynamips.port))
                return
            #ok device_name exists, so let's run the nested Cmd console
            if isinstance(frsw, self.namespace.FRSW):
                nested_cmd = confFRSWConsole(frsw, self.prompt, self.dynagen)
                nested_cmd.cmdloop()
            else:
                error(args + ' is not a FRSW switch')
        except KeyError:
            #frsw does not exist so let's create it
            frsw = self.namespace.FRSW(self.dynamips_server, name=args)
            #add it to frontend
            self.dynagen.devices[args] = frsw
            debug('FRSW switch ' + frsw.name + ' created')
            #and let's jump into the confFRSWConsole
            nested_cmd = confFRSWConsole(frsw, self.prompt, self.dynagen)
            nested_cmd.cmdloop()

    def do_atmsw(self, args):
        """atmsw <atmsw name>
\tgo into ATM switch conf mode
atmsw <new atmsw_name>
\tcreate new ATM switch of this name"""

        if '?' in args or args.strip() == '':
            print self.do_atmsw.__doc__
            return
        try:
            atmsw = self.dynagen.devices[args]
            #check whether we are on correct hypervisor
            if atmsw.dynamips != self.dynamips_server:
                error('this device is on different hypervisor: ' + atmsw.dynamips.host + ':' + str(atmsw.dynamips.port))
                return
            #ok device_name exists, so let's run the nested Cmd console
            if isinstance(atmsw, self.namespace.ATMSW):
                nested_cmd = confATMSWConsole(atmsw, self.prompt, self.dynagen)
                nested_cmd.cmdloop()
            else:
                error(args + ' is not a ATMSW switch')
        except KeyError:
            #atmsw does not exist so let's create it
            atmsw = self.namespace.ATMSW(self.dynamips_server, name=args)
            #add it to frontend
            self.dynagen.devices[args] = atmsw
            debug('ATMSW switch ' + atmsw.name + ' created')
            #and let's jump into the confFRSWConsole
            nested_cmd = confATMSWConsole(atmsw, self.prompt, self.dynagen)
            nested_cmd.cmdloop()

    def do_atmbr(self, args):
        """atmbr <atmbr name>
\tgo into ATM Bridge conf mode
atmbr <new atmbr_name>
\tcreate new ATM Bridge of this name"""

        if '?' in args or args.strip() == '':
            print self.do_atmbr.__doc__
            return
        try:
            atmbr = self.dynagen.devices[args]
            #check whether we are on correct hypervisor
            if atmbr.dynamips != self.dynamips_server:
                error('this device is on different hypervisor: ' + atmbr.dynamips.host + ':' + str(atmbr.dynamips.port))
                return
            #ok device_name exists, so let's run the nested Cmd console
            if isinstance(atmbr, self.namespace.ATMBR):
                nested_cmd = confATMBRConsole(atmbr, self.prompt, self.dynagen)
                nested_cmd.cmdloop()
            else:
                error(args + ' is not a ATMBR switch')
        except KeyError:
            #atmbr does not exist so let's create it
            try:
                atmbr = self.namespace.ATMBR(self.dynamips_server, name=args)
                #add it to frontend
                self.dynagen.devices[args] = atmbr
                debug('ATMBR switch ' + atmbr.name + ' created')
                #and let's jump into the confATMBRConsole
                nested_cmd = confATMBRConsole(atmbr, self.prompt, self.dynagen)
                nested_cmd.cmdloop()
            except DynamipsError, e:
                print 'dynamips error in creating ATMBR bridge, ' + str(e)

    def do_ethsw(self, args):
        """ethsw <ethsw name>
\tgo into Ethernet switch conf mode
ethsw <new ethsw_name>
\tcreate new Ethernet switch of this name"""

        if '?' in args or args.strip() == '':
            print self.do_ethsw.__doc__
            return
        try:
            ethsw = self.dynagen.devices[args]
            #check whether we are on correct hypervisor
            if ethsw.dynamips != self.dynamips_server:
                error('this device is on different hypervisor: ' + ethsw.dynamips.host + ':' + str(ethsw.dynamips.port))
                return
            #ok device_name exists, so let's run the nested Cmd console
            if isinstance(ethsw, self.namespace.ETHSW):
                nested_cmd = confETHSWConsole(ethsw, self.prompt, self.dynagen)
                nested_cmd.cmdloop()
            else:
                error(args + ' is not a ethsw switch')
        except KeyError:
            #ethsw does not exist so let's create it
            try:
                ethsw = self.namespace.ETHSW(self.dynamips_server, name=args)
                #add it to frontend
                self.dynagen.devices[args] = ethsw
                debug('ethsw switch ' + ethsw.name + ' created')
                #and let's jump into the confethswConsole
                nested_cmd = confETHSWConsole(ethsw, self.prompt, self.dynagen)
                nested_cmd.cmdloop()
            except DynamipsError, e:
                print 'dynamips error in creating ethsw bridge, ' + str(e)


class confConsole(AbstractConsole):

    def __init__(self, dynagen, console):
        AbstractConsole.__init__(self)
        self.prompt = '=>(config)'
        self.dynagen = dynagen
        self.console = console
        #set default values
        self.default_autostart = True
        self.default_debug = 0
        realpath = os.path.realpath(self.dynagen.global_filename)
        workingdir = os.path.dirname(realpath)
        self.default_workingdir = workingdir

    def clean_args(self, args):
        #get rid of that '='
        argument = args.strip('=')
        #get rid of starting space
        argument = argument.strip()
        return argument

    def set_option(self, option, type, args):
        if '?' in args or args.strip() == '':
            print getattr(self, 'do_' + option).__doc__
            return

        argument = self.clean_args(args)
        if type == 'bool':
            if argument == 'True':
                option_value = True
            elif argument == 'False':
                option_value = False
            else:
                error('incorrect syntax, enter True or False: ' + argument)
                return
        elif type == 'int':
            try:
                option_value = int(argument)
            except TypeError:
                error('incorrect syntax,enter an integer: ' + argument)
                return
        elif type == 'string':
            option_value = argument
        try:
            if getattr(self, 'default_' + option) == option_value:
                if self.dynagen.defaults_config.has_key(option):
                    del self.dynagen.defaults_config[option]
                    return [True, option_value]
                return [True, option_value]
            else:
                self.dynagen.defaults_config[option] = option_value
                return [True, option_value]
        except (TypeError, ValueError):
            error('syntax error: ' + argument)

    def do_workingdir(self, args):
        """workingdir = <OS path>
\tset the working directory for this hypervisor where all temp file are stored"""

        self.set_option('workingdir', 'string', args)

    def do_autostart(self, args):
        """autostart = True|False
\tshould all devices be automatically started?"""

        self.set_option('autostart', 'bool', args)

    def do_debug(self, args):
        """debug = <int>
\tdebug output level. Higher numbers produce increasing levels of verbosity. 0 means none (the default). 1 is the same as the -d command line switch """

        result = self.set_option('debug', 'int', args)
        if result[0]:
            self.dynagen.debuglevel = result[1]
            debuglevel = self.dynagen.debuglevel
            if debuglevel == 0:
                self.namespace.setdebug(False)
            else:
                self.namespace.setdebug(True)

    def do_hypervisor(self, args):
        """hypervisor <hypervisor address>:<hypervisor port>
\tswitch into configuration mode of the specific hypervisor eg. 'hypervisor localhost'"""

        if args.strip() != '':
            self.console.do_conf(args)

    def do_no(self, args):
        """no <option> = <option_value>
\tunset the option_value from option. This will effectivelly set the option the the default value."""

        if '?' in args or args.strip() == '':
            print self.do_no.__doc__
            return

        #check whether this is a properly formatted 'no' command
        argument = args.split('=')
        if len(argument) != 2:
            error('invalid syntax: ' + args)
            return

        (lside, rside) = argument
        lside = lside.strip()
        rside = rside.strip()
        try:
            if str(self.dynagen.defaults_config[lside]) == rside:
                #emit the command lside = default_option, this will effective make the command not visible in config
                self.onecmd(lside + '=' + str(getattr(self, 'default_' + lside)))
            else:
                error('Bad ' + lside + 'value: ' + rside)
        except KeyError:
            #either a bad option or there is already a default value in defaults_config
            pass