Virtlab:Řídící server/ParserEquipment.php.inc

Z VirtlabWiki

Přejít na: navigace, hledání

Tato třída pomáhá vytahovat data o vybavení z XML souboru. Ten je naparsován třídou virtlabXmlParser.

Popis funkcí

function __construct($file, $is_file=0, $cache_dev=1) 
konstruktor třídy v PHP5
public function virtlabParserEquipment($file, $is_file=0, $cache_dev=1) 
konstruktor třídy. Jednotlivé parametry mají tento význam:
$file 
obsahuje cestu k XML souboru s popisem vybavení, nebo jsou v této proměnné uložena přímo XML data (o variantě rozhoduje parametr $is_file)
$is_file 
udává jestli je atribut $file casta k souboru, nebo přímo XML data
$cache_dev 
nastavuje, jestli se maji data o vybavení uložit do proměnné, ze které si při potřebě načítají, nebo se vždy čtou z původního výstupu třídy virtlabXmlParser
public function getDevices() 
vrátí původní pole s kompletním vybavením
public function getDevicesCount() 
vrátí počet zařízení
public function getDevice($index) 
vrátí určené zařízení (podle parametru $index, který odpovídá jejímu indexu v celkovém poli včech zařízení)
public function getDeviceType($index) 
vrátí typ zadaného zařízení
public function getDeviceName($index) 
vrátí název zadaného zařízení
public function getDeviceSerial($index) 
vrátí seriové číslo zařízení
public function getDevicePlatform($index) 
vrátí platformu zařízení
public function getDeviceByName($name, &$index) 
vrátí požadované zařízení (určené jeho názvem). Do parametru $index se uloží jeho index.
public function getDeviceOS($name, $variant=1) 
vrátí veryi OS zařízení. Parametr $variant určuje jestli má být výstup upravený nebo v podobě, jako v původním poli.
public function getDeviceInterfaces($name) 
vrátí rozhraní zadaného zařízení
public function getDeviceInterfacesCount($name) 
vrátí počet rozhraní zadaného zařízení
public function getDeviceInterface($name, $index, $variant=1) 
vrátí určené rozhraní (určené parametrem $index) určeného zařízení
public function getDeviceInterfaceTechnology($name, $index) 
vratí technologii rozhraní
public function getDeviceInterfaceEthertype($name, $index) 
vrátí verzi ethernetu rozhraní. Pokud je technologie serial vrací NULL.
public function getDeviceInterfaceMaxbps($name, $index) 
vrátí maximální rychlost rozhraní. Pokud je technologie ethernet vrací NULL.
public function getDeviceInterfaceFeatures($name, $index) 
vrátí speciální vlastnosti zadaného rozhraní
public function getDeviceFeatures($name) 
vrátí speciální vlastnosti zařízení
public function getDevicesFeatures() 
vrátí všechny speciální vlastnosti všech zařízení
public function getDevicesByType($type) 
vrátí jména zařízení daného typu
public function getDevicesTypes() 
vrátí typy zařízení, které se ve vybavení vyskytují
public function getDeviceInterfacesFeatures($name) 
vrátí speciální vlastnosti všech rozhraní daného zařízení
public function getDevicesInterfacesFeatures() 
vrátí speciální vlastnosti všech rozhraní všech zařízení
public function getDevicesList() 
vrátí seznam názvů všech zařízení
public function getDeviceInterfaceName($name, $index) 
vrátí jméno zadaného rozhraní

Příklady

$parser->getDeviceInterface("r1",0,0);
Array
(
   [name] => INTERFACE
   [attribs] => Array
       (
           [TECHNOLOGY] => serial
           [CONNECT_GROUP] => 1
           [NAME] => s0/1/0
       )
   [child] => Array
       (
           [0] => Array
               (
                   [name] => MAX_BPS
                   [content] => 128000
               )
           [1] => Array
               (
                   [name] => INT_FEATURE
                   [content] => ...
               )
       )
)
$parser->getDeviceInterface("r1",0,1);
Array
(
   [technology] => serial
   [connect_group] => 1
   [name] => s0/1/0
)
$parser->getDeviceInterface("r3",1,1);
Array
(
   [technology] => ethernet
   [connect_group] => 3
   [name] => fa0/0
   [ether_type] => fast
)
$parser->getDeviceInterfaceTechnology("r3",1);
ethernet
$parser->getDeviceInterfaceEthertype("r3",1);
fast
$parser->getDevicesByType("router");
Array
(
   [0] => r1
   [1] => r3
   [2] => r5
   [3] => r7
)
$parser->getDevicesTypes();
Array
(
   [0] => router
   [1] => switch
)
$parser->getDevicesInterfacesFeatures();
Array
(
   [0] => 802.1q
   [1] => +++
   [2] => xxx
   [3] => ...
)
$parser->getDevicesList();
Array
(
   [4] => swa
   [3] => r7
   [2] => r5
   [1] => r3
   [0] => r1
)
$parser->getDeviceInterfaceName("r1",0);
s0/1/0

Zdrojový kód

<?php

class virtlabParserEquipment {
    private $parsed = array();
    private $devices = array();
    private $cached_devices = 0;
    
    function __construct($file, $is_file=0, $cache_dev=1) {
        $this->virtlabParserEquipment($file, $is_file, $cache_dev);
    }//konstruktor
    
    private function virtlabParserEquipment($file, $is_file=0, $cache_dev=1) {
        $parser = new virtlabXmlParser(1);

        if($is_file) $parser->parse($file);
        else $parser->parse_data($file);

        $this->parsed = $parser->output;
        unset($parser);

        if($cache_dev) {
            $this->devices = $this->getDevices();
            $this->cached_devices = 1;
        }
    }//function

    
    public function getDevices() {
        if(!$this->cached_devices) {
            $temp = $this->parsed[0]["child"];
            return $temp;
        }
        else return $this->devices;
    }//function

    public function getDevicesCount() {
        if(!$this->cached_devices)
        {    
            $temp = $this->getDevices();
            return count($temp);
        }
        else return count($this->devices);
    }//function


    public function getDevice($index) {
        $temp = $this->getDevices();
        return $temp[$index];
    }//function    


    public function getDeviceType($index) {
        $temp = $this->getDevice($index);
        return $temp["attribs"]["TYPE"];
    }//function


    public function getDeviceName($index) {
        $temp = $this->getDevice($index);
        return $temp["attribs"]["NAME"];
    }//function


    public function getDeviceSerial($index) {
        $temp = $this->getDevice($index);
        return $temp["attribs"]["SERIAL_NUMBER"];
    }//function


    public function getDevicePlatform($index) {
        $temp = $this->getDevice($index);
        return $temp["attribs"]["PLATFORM"];
    }//function


    public function getDeviceByName($name, &$index) {
        $result = array();
        for($i = $this->getDevicesCount(); $i>=0; $i--) {
            if($name==$this->getDeviceName($i)) {
                $index = $i;
                return $this->getDevice($i);
            }//if
        }//for
        return NULL;
    }//function


    public function getDeviceOS($name, $variant=1) {
        $temp = $this->getDeviceByName($name,$idx);
        if($temp["child"]) {
            $temp2 = $temp["child"][0];

        if(!isset($temp2)) return NULL;
        if($variant) {
            //$temp3["major"] = $temp2["child"][0]["content"];
            //if($temp2["child"][1]) $temp3["minor"] = $temp2["child"][1]["content"];
            //if($temp2["child"][2]) $temp3["other"] = $temp2["child"][2]["content"];
            //return $temp3;
            return $temp2["content"];
        }
        else return $temp2;
        }//if
        else return NULL;
    }//function


    public function getDeviceInterfaces($name) {
        $temp = $this->getDeviceByName($name,$idx);
        if($temp["child"][1]["name"]!="INTERFACES") return NULL;
        return $temp["child"][1];
    }//function

    
    public function getDeviceInterfacesCount($name) {
        $temp = $this->getDeviceInterfaces($name);
        if(!$temp["child"]) return NULL;
        return count($temp["child"]);
    }//function

    
    public function getDeviceInterface($name, $index, $variant=1) {
        $temp = $this->getDeviceInterfaces($name);
        if(!$temp["child"][$index]) return NULL;
        if($variant) {
            $temp2 = array();
            $temp2["technology"] = $temp["child"][$index]["attribs"]["TECHNOLOGY"];
            $temp2["connect_group"] = $temp["child"][$index]["attribs"]["CONNECT_GROUP"];
            $temp2["name"] = $temp["child"][$index]["attribs"]["NAME"];
            if($temp["child"][$index]["attribs"]["ETHER_TYPE"])
                $temp2["ether_type"] = $temp["child"][$index]["attribs"]["ETHER_TYPE"];
            return $temp2;
        }
        else return $temp["child"][$index];
    }//function

    
    public function getDeviceInterfaceTechnology($name, $index) {
        $temp = $this->getDeviceInterface($name, $index, 1);
        return $temp["technology"];
    }//function


    public function getDeviceInterfaceEthertype($name, $index) {
        $temp = $this->getDeviceInterface($name, $index, 1);
        if($temp["technology"] == "ethernet") return $temp["ether_type"];
        else return NULL;
    }//function


    public function getDeviceInterfaceMaxbps($name, $index) {
        $temp = $this->getDeviceInterface($name, $index, 0);
        if($temp["child"][0]["name"]!="MAX_BPS") return NULL;
        return $temp["child"][0]["content"];
    }//function

    
    public function getDeviceInterfaceFeatures($name, $index) {
        $temp = $this->getDeviceInterface($name, $index, 0);
        $temp2 = array();
        if(is_array($temp) && $temp["child"])
            foreach($temp["child"] as $hodnota) {
                if($hodnota["name"]=="INT_FEATURE") {
                    array_push($temp2, $hodnota["content"]);
                }//if
            }//foreach
        if(!$temp2[0]) return NULL;
        else return $temp2;
    }//function


    public function getDeviceFeatures($name) {
        $temp = $this->getDeviceByName($name,$idx);
        foreach($temp["child"] as $hodnota) {
            if($hodnota["name"]=="SPECIAL") {
                $result = array();
                foreach($hodnota["child"] as $hodnota2) {
                    array_push($result, $hodnota2["content"]);
                }//foreach
                break;
            }//if
        }//foreach
        if(!$result[0]) return NULL;
        else return $result;
    }//function    


    public function getDevicesFeatures() {
        $feat = array();
        for($i = $this->getDevicesCount() - 1; $i >= 0; $i--) {
            $jmeno = $this->getDeviceName($i);
            $temp = $this->getDeviceFeatures($jmeno);
            if(is_array($temp))
                foreach($temp as $value)
                array_push($feat, $value);
        }//for
        return $feat;
    }


    public function getDevicesByType($type) {
        $temp = $this->getDevices();
        $temp2 = array();
        foreach($temp as $hodnota) {
            if($hodnota["attribs"]["TYPE"] == $type)
                array_push($temp2, $hodnota["attribs"]["NAME"]);
        }
        if(!$temp2[0]) return NULL;

        return Unique($temp2);
    }//function


    public function getDevicesTypes() {
        $temp = $this->getDevices();
        $temp2 = array();
        foreach($temp as $hodnota) {
            array_push($temp2, $hodnota["attribs"]["TYPE"]);
        }
        if(!$temp2[0]) return NULL;

        return Unique($temp2);
    }//function


    public function getDeviceInterfacesFeatures($name) {
        $feat = array();
        for($i = $this->getDeviceInterfacesCount($name) - 1; $i >= 0; $i--) {
            $temp = $this->getDeviceInterfaceFeatures($name, $i);
            if(is_array($temp))
                foreach($temp as $hodnota)
                    array_push($feat, $hodnota);
        }//for
        return $feat;
    }//function

    public function getDevicesInterfacesFeatures() {
        $feat = array();
        for($i = $this->getDevicesCount() - 1; $i >= 0; $i--) {
            $jmeno = $this->getDeviceName($i);
            $temp = $this->getDeviceInterfacesFeatures($jmeno);
            if(is_array($temp))
                foreach($temp as $hodnota)
                    array_push($feat, $hodnota);
        }//for
        return $feat;
    }//function

    public function getDevicesList() {
        $temp = array();
        for($i = $this->getDevicesCount() - 1; $i >= 0; $i--) {
            $temp[$i] = $this->getDeviceName($i);
        }//for
        return $temp;
    }//function

    public function getDeviceInterfaceName($name, $index) {
        $temp = $this->getDeviceInterface($name, $index, 1);
        return $temp["name"];
    }//function
}//class
?>
Osobní nástroje