r3328 ios12.connector: also include VLAN table into output
authorDenis Ovsienko <infrastation@yandex.ru>
Sat, 6 Mar 2010 13:38:56 +0000 (13:38 +0000)
committerDenis Ovsienko <infrastation@yandex.ru>
Sat, 6 Mar 2010 13:38:56 +0000 (13:38 +0000)
iosReadVLANConfig(): renamed to ios12ReadVLANConfig() for consistency, start with 2-node array
iosPickInterfaceCommand(): renamed to ios12ScanTopLevel(), added VLAN table detection and ifname mapping
getDevice8021QConfig(): adjusted accordingly
renderObjectVLANSync(): don't perform ifname mapping here, use 'portdata' sub-node
iosPickSwitchportCommand(): renamed to ios12PickSwitchportCommand()
ios12PickVLANCommand(): new function
fdry5ReadVLANConfig(): start with 2-node array, perform VLAN list maintenance
fdry5PickVLANSubcommand(): omit condition, which is always met

gateways/deviceconfig/ios12.connector
inc/functions.php
inc/gateways.php
inc/interface.php

index 77bc52d..c381bc3 100755 (executable)
@@ -45,7 +45,7 @@ prepare_connect_commands()
 CMDS1=`mktemp /tmp/cisco.connector.XXXX`
 [ -f "$CMDS1" ] || exit 5
 prepare_connect_commands $ENDPOINT
-printf 'term len 0\nshow run\nquit\n' >> $CMDS1
+printf 'term len 0\nshow run\n! END OF CONFIG\nshow vlan brief\n! END OF VLAN LIST\nquit\n' >> $CMDS1
 nc $ENDPOINT 23 < $CMDS1 > "$OUT1"
 if fgrep -q '% Bad passwords' "$OUT1"; then
        rm -f "$CMDS1"
index be464e2..71ee88e 100644 (file)
@@ -2277,25 +2277,40 @@ function formatVLANName ($vlaninfo)
 
 // Read given running-config and return a list of work items in a format
 // similar to the one, which setSwitchVLANConfig() understands.
-function iosReadVLANConfig ($input)
+function ios12ReadVLANConfig ($input)
 {
-       $ret = array();
-       $procfunc = 'iosPickInterfaceCommand';
+       $ret = array
+       (
+               'vlanlist' => array(),
+               'portdata' => array(),
+       );
+       $procfunc = 'ios12ScanTopLevel';
        foreach (explode ("\n", $input) as $line)
                $procfunc = $procfunc ($ret, $line);
        return $ret;
 }
 
-function iosPickInterfaceCommand (&$work, $line)
+function ios12ScanTopLevel (&$work, $line)
 {
        $matches = array();
-       if (!preg_match ('@^interface ((Ethernet|FastEthernet|GigabitEthernet|TenGigabitEthernet)[[:digit:]]+(/[[:digit:]]+)*)$@', $line, $matches))
+       switch (TRUE)
+       {
+       case (preg_match ('@^interface ((Ethernet|FastEthernet|GigabitEthernet|TenGigabitEthernet)[[:digit:]]+(/[[:digit:]]+)*)$@', $line, $matches)):
+               // map interface name
+               $matches[1] = preg_replace ('@^Ethernet(.+)$@', 'et\\1', $matches[1]);
+               $matches[1] = preg_replace ('@^FastEthernet(.+)$@', 'fa\\1', $matches[1]);
+               $matches[1] = preg_replace ('@^GigabitEthernet(.+)$@', 'gi\\1', $matches[1]);
+               $matches[1] = preg_replace ('@^TenGigabitEthernet(.+)$@', 'te\\1', $matches[1]);
+               $work['current'] = array ('port_name' => $matches[1]);
+               return 'ios12PickSwitchportCommand'; // switch to interface block reading
+       case (preg_match ('/^VLAN Name                             Status    Ports$/', $line, $matches)):
+               return 'ios12PickVLANCommand';
+       default:
                return __FUNCTION__; // continue scan
-       $work['current'] = array ('port_name' => $matches[1]);
-       return 'iosPickSwitchportCommand'; // switch to interface block reading
+       }
 }
 
-function iosPickSwitchportCommand (&$work, $line)
+function ios12PickSwitchportCommand (&$work, $line)
 {
        if ($line[0] != ' ') // end of interface section
        {
@@ -2313,7 +2328,7 @@ function iosPickSwitchportCommand (&$work, $line)
                switch ($work['current']['mode'])
                {
                case 'access':
-                       $work[] = array
+                       $work['portdata'][] = array
                        (
                                'port_name' => $work['current']['port_name'],
                                'allowed' => array ($work['current']['access vlan']),
@@ -2328,7 +2343,7 @@ function iosPickSwitchportCommand (&$work, $line)
                                $work['current']['trunk native vlan'],
                                $work['current']['trunk allowed vlan']
                        ) ? $work['current']['trunk native vlan'] : 0;
-                       $work[] = array
+                       $work['portdata'][] = array
                        (
                                'port_name' => $work['current']['port_name'],
                                'allowed' => $work['current']['trunk allowed vlan'],
@@ -2339,7 +2354,7 @@ function iosPickSwitchportCommand (&$work, $line)
                        // dot1q-tunnel, dynamic, private-vlan --- skip these
                }
                unset ($work['current']);
-               return 'iosPickInterfaceCommand';
+               return 'ios12ScanTopLevel';
        }
        // not yet
        $matches = array();
@@ -2369,6 +2384,25 @@ function iosPickSwitchportCommand (&$work, $line)
        return __FUNCTION__;
 }
 
+function ios12PickVLANCommand (&$work, $line)
+{
+       $matches = array();
+       switch (TRUE)
+       {
+       case ($line == '---- -------------------------------- --------- -------------------------------'):
+               // ignore the rest of VLAN table header;
+               break;
+       case (preg_match ('@! END OF VLAN LIST$@', $line)):
+               return 'ios12ScanTopLevel';
+       case (preg_match ('@^([[:digit:]]+) {1,4}.{32} active    @', $line, $matches)):
+               if (!array_key_exists ($matches[1], $work['vlanlist']))
+                       $work['vlanlist'][] = $matches[1];
+               break;
+       default:
+       }
+       return __FUNCTION__;
+}
+
 function iosParseVLANString ($string)
 {
        $ret = array();
@@ -2386,7 +2420,11 @@ function iosParseVLANString ($string)
 // Another finite automata to read a dialect of Foundry configuration.
 function fdry5ReadVLANConfig ($input)
 {
-       $ret = array();
+       $ret = array
+       (
+               'vlanlist' => array(),
+               'portdata' => array(),
+       );
        $procfunc = 'fdry5ScanTopLevel';
        foreach (explode ("\n", $input) as $line)
                $procfunc = $procfunc ($ret, $line);
@@ -2399,6 +2437,8 @@ function fdry5ScanTopLevel (&$work, $line)
        switch (TRUE)
        {
        case (preg_match ('@^vlan ([[:digit:]]+)( name .+)? (by port)$@', $line, $matches)):
+               if (!array_key_exists ($matches[1], $work['vlanlist']))
+                       $work['vlanlist'][] = $matches[1];
                $work['current'] = array ('vlan_id' => $matches[1]);
                return 'fdry5PickVLANSubcommand';
        case (preg_match ('@^interface ethernet ([[:digit:]]+/[[:digit:]]+/[[:digit:]]+)$@', $line, $matches)):
@@ -2423,10 +2463,10 @@ function fdry5PickVLANSubcommand (&$work, $line)
        case (preg_match ('@^ tagged (.+)$@', $line, $matches)):
                // add current VLAN to 'allowed' list of each mentioned port
                foreach (fdry5ParsePortString ($matches[1]) as $port_name)
-                       if (NULL !== $key = scanArrayForItem ($work, 'port_name', $port_name))
-                               $work[$key]['allowed'][] = $work['current']['vlan_id'];
+                       if (NULL !== $key = scanArrayForItem ($work['portdata'], 'port_name', $port_name))
+                               $work['portdata'][$key]['allowed'][] = $work['current']['vlan_id'];
                        else
-                               $work[] = array
+                               $work['portdata'][] = array
                                (
                                        'port_name' => $port_name,
                                        'allowed' => array ($work['current']['vlan_id']),
@@ -2436,14 +2476,13 @@ function fdry5PickVLANSubcommand (&$work, $line)
        case (preg_match ('@^ untagged (.+)$@', $line, $matches)):
                // replace 'native' column of each mentioned port with current VLAN ID
                foreach (fdry5ParsePortString ($matches[1]) as $port_name)
-                       if (NULL != $key = scanArrayForItem ($work, 'port_name', $port_name))
+                       if (NULL != $key = scanArrayForItem ($work['portdata'], 'port_name', $port_name))
                        {
-                               $work[$key]['native'][] = $work['current']['vlan_id'];
-                               if (!in_array ($work['current']['vlan_id'], $work[$key]['allowed'])) // always true?
-                                       $work[$key]['allowed'][] = $work['current']['vlan_id'];
+                               $work['portdata'][$key]['native'] = $work['current']['vlan_id'];
+                               $work['portdata'][$key]['allowed'][] = $work['current']['vlan_id'];
                        }
                        else
-                               $work[] = array
+                               $work['portdata'][] = array
                                (
                                        'port_name' => $port_name,
                                        'allowed' => array ($work['current']['vlan_id']),
@@ -2461,12 +2500,12 @@ function fdry5PickInterfaceSubcommand (&$work, $line)
        {
                if (array_key_exists ('dual-mode', $work['current']))
                {
-                       if (NULL !== $key = scanArrayForItem ($work, 'port_name', $work['current']['port_name']))
+                       if (NULL !== $key = scanArrayForItem ($work['portdata'], 'port_name', $work['current']['port_name']))
                                // update existing record
-                               $work[$key]['native'] = $work['current']['dual-mode'];
+                               $work['portdata'][$key]['native'] = $work['current']['dual-mode'];
                        else
                                // add new
-                               $work[] = array
+                               $work['portdata'][] = array
                                (
                                        'port_name' => $work['current']['port_name'],
                                        'allowed' => array ($work['current']['dual-mode']),
index 49bf915..ba5a268 100644 (file)
@@ -305,7 +305,7 @@ function getDevice8021QConfig ($object_id)
                throw new RuntimeException ('cannot pick handler for this device');
        $reader = array
        (
-               'ios12' => 'iosReadVLANConfig',
+               'ios12' => 'ios12ReadVLANConfig',
                'fdry5' => 'fdry5ReadVLANConfig',
        );
        return $reader[$breed] (dos2unix (gwRetrieveDeviceConfig ($object_id, $breed)));
index 773d80d..be4f865 100644 (file)
@@ -6895,13 +6895,8 @@ function renderObjectVLANSync ($object_id)
                                $formports[$port_id]['port_name'] = $port['name'];
                                break;
                        }
-       foreach ($deviceconfig as $item)
+       foreach ($deviceconfig['portdata'] as $item)
        {
-               // map interface name
-               $item['port_name'] = preg_replace ('@^Ethernet(.+)$@', 'et\\1', $item['port_name']);
-               $item['port_name'] = preg_replace ('@^FastEthernet(.+)$@', 'fa\\1', $item['port_name']);
-               $item['port_name'] = preg_replace ('@^GigabitEthernet(.+)$@', 'gi\\1', $item['port_name']);
-               $item['port_name'] = preg_replace ('@^TenGigabitEthernet(.+)$@', 'te\\1', $item['port_name']);
                foreach (array_keys ($formports) as $tmpkey)
                        if ($formports[$tmpkey]['port_name'] == $item['port_name']) // comparing mapped value
                        {