<s> import re <EOL> import logging <EOL> import xml . etree . ElementTree as ET <EOL> import pandevice <EOL> import base <EOL> import errors as err <EOL> try : <EOL> logging . getLogger ( __name__ ) . addHandler ( logging . NullHandler ( ) ) <EOL> except AttributeError as e : <EOL> class NullHandler ( logging . Handler ) : <EOL> def emit ( self , record ) : <EOL> pass <EOL> logging . NullHandler = NullHandler <EOL> logging . getLogger ( __name__ ) . addHandler ( logging . NullHandler ( ) ) <EOL> class Interface ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> name = None , <EOL> type = "<STR_LIT>" , <EOL> mode = "<STR_LIT>" , <EOL> tag = None , <EOL> zone = None , <EOL> subnets = None , <EOL> router = "<STR_LIT:default>" , <EOL> pan_device = None , <EOL> apply_changes = False , <EOL> parent = None , <EOL> state = None , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> self . type = type <EOL> self . mode = mode <EOL> self . _tag = None <EOL> self . tag = tag <EOL> self . _zone = zone <EOL> self . router = router <EOL> self . parent = parent <EOL> self . subinterfaces = { } <EOL> self . apply_changes = apply_changes <EOL> self . pan_device = pan_device <EOL> self . _subnets = None <EOL> self . subnets = subnets <EOL> self . state = state <EOL> if tag is not None and not re . search ( r"<STR_LIT>" , self . name ) : <EOL> self . name += "<STR_LIT:.>" + str ( self . tag ) <EOL> if tag is None and re . search ( r"<STR_LIT>" , self . name ) : <EOL> self . tag = self . name . split ( "<STR_LIT:.>" ) [ <NUM_LIT:1> ] <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( '<STR_LIT>' , <EOL> self . name , <EOL> self . state , <EOL> self . type , <EOL> self . mode , <EOL> self . zone , <EOL> ) <EOL> def __eq__ ( self , other ) : <EOL> return self . __dict__ == other . __dict__ <EOL> @ property <EOL> def subnets ( self ) : <EOL> return self . _subnets <EOL> @ property <EOL> def tag ( self ) : <EOL> return self . _tag <EOL> @ property <EOL> def zone ( self ) : <EOL> return self . _zone <EOL> @ subnets . setter <EOL> def subnets ( self , value ) : <EOL> if value is None : <EOL> self . _subnets = [ ] <EOL> elif issubclass ( value . __class__ , list ) : <EOL> self . _subnets = value <EOL> else : <EOL> self . _subnets = [ value ] <EOL> if self . apply_changes : <EOL> self . apply ( ) <EOL> @ tag . setter <EOL> def tag ( self , value ) : <EOL> if value is not None : <EOL> self . _tag = str ( value ) <EOL> else : <EOL> self . _tag = None <EOL> @ zone . setter <EOL> def zone ( self , value ) : <EOL> if self . _zone == value : <EOL> return <EOL> old_zone = self . _zone <EOL> self . _zone = value <EOL> if self . apply_changes : <EOL> if old_zone is not None : <EOL> xpath = pandevice . XPATH_ZONE + "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" % ( old_zone , self . name ) <EOL> self . pan_device . xapi . delete ( xpath ) <EOL> if self . _zone is not None : <EOL> xpath = pandevice . XPATH_ZONE + "<STR_LIT>" % ( self . _zone , ) <EOL> element = "<STR_LIT>" % ( self . name , ) <EOL> self . pan_device . xapi . set ( xpath , element ) <EOL> def is_up ( self ) : <EOL> if self . state == "<STR_LIT>" : <EOL> return True <EOL> else : <EOL> return False <EOL> def apply ( self ) : <EOL> if not self . pan_device : <EOL> return <EOL> self . pan_device . set_config_changed ( ) <EOL> xpath , element = self . _xpath ( ) <EOL> self . pan_device . xapi . edit ( xpath , ET . tostring ( element ) ) <EOL> if self . zone : <EOL> xpath = pandevice . XPATH_ZONE + "<STR_LIT>" % ( self . zone , ) <EOL> element = "<STR_LIT>" % ( self . name , ) <EOL> self . pan_device . xapi . set ( xpath , element ) <EOL> if self . router : <EOL> xpath = pandevice . XPATH_DEFAULT_VROUTER_INTERFACES <EOL> element = "<STR_LIT>" % ( self . name , ) <EOL> self . pan_device . xapi . set ( xpath , element ) <EOL> def delete ( self ) : <EOL> if not self . pan_device : <EOL> return <EOL> self . pan_device . set_config_changed ( ) <EOL> if self . router : <EOL> xpath = pandevice . XPATH_DEFAULT_VROUTER_INTERFACES + "<STR_LIT>" % ( self . name , ) <EOL> self . pan_device . xapi . delete ( xpath ) <EOL> if self . zone : <EOL> xpath = pandevice . XPATH_ZONE + "<STR_LIT>" "<STR_LIT>" % ( self . zone , self . name ) <EOL> self . pan_device . xapi . delete ( xpath ) <EOL> xpath , element = self . _xpath ( ) <EOL> self . pan_device . xapi . delete ( xpath ) <EOL> def _xpath ( self ) : <EOL> xpath = pandevice . XPATH_INTERFACES + "<STR_LIT>" % ( self . type , ) <EOL> if self . type == "<STR_LIT>" : <EOL> if self . parent : <EOL> xpath += "<STR_LIT>" % ( self . parent . name , <EOL> self . parent . mode , <EOL> self . name ) <EOL> root = ET . Element ( "<STR_LIT>" , { "<STR_LIT:name>" : self . name } ) <EOL> settings = root <EOL> else : <EOL> match = re . search ( r"<STR_LIT>" , self . name ) <EOL> if match : <EOL> xpath += "<STR_LIT>" % ( match . group ( <NUM_LIT:1> ) , self . mode , self . name ) <EOL> root = ET . Element ( "<STR_LIT>" , { "<STR_LIT:name>" : self . name } ) <EOL> settings = root <EOL> else : <EOL> xpath += "<STR_LIT>" % ( self . name , ) <EOL> root = ET . Element ( "<STR_LIT>" , { "<STR_LIT:name>" : self . name } ) <EOL> settings = ET . SubElement ( root , self . mode ) <EOL> elif self . type == "<STR_LIT>" : <EOL> xpath += "<STR_LIT>" % ( self . name , ) <EOL> root = ET . Element ( "<STR_LIT>" , { "<STR_LIT:name>" : self . name } ) <EOL> settings = root <EOL> else : <EOL> raise err . PanDeviceError ( "<STR_LIT>" % self . type ) <EOL> if self . mode == "<STR_LIT>" and self . subnets : <EOL> node = ET . SubElement ( settings , "<STR_LIT>" ) <EOL> for subnet in self . subnets : <EOL> ET . SubElement ( node , "<STR_LIT>" , { "<STR_LIT:name>" : subnet } ) <EOL> if self . tag : <EOL> node = ET . SubElement ( settings , "<STR_LIT>" ) <EOL> node . text = self . tag <EOL> return xpath , root <EOL> class VirtualRouter ( base . VsysImportMixin , base . PanObject ) : <EOL> ROOT = base . Root . DEVICE <EOL> XPATH = "<STR_LIT>" <EOL> SUFFIX = base . PanObject . ENTRY <EOL> def __init__ ( self , <EOL> name = "<STR_LIT:default>" , <EOL> interfaces = ( ) ) : <EOL> super ( VirtualRouter , self ) . __init__ ( name = name ) <EOL> self . interfaces = list ( interfaces ) <EOL> class StaticRoute ( base . PanObject ) : <EOL> XPATH = "<STR_LIT>" <EOL> SUFFIX = base . PanObject . ENTRY <EOL> def __init__ ( self , <EOL> name , <EOL> destination = None , <EOL> interface = None , <EOL> nexthop = None ) : <EOL> super ( StaticRoute , self ) . __init__ ( name = name ) <EOL> self . destination = destination <EOL> self . interface = interface <EOL> self . nexthop = nexthop <EOL> def element ( self ) : <EOL> element = ( "<STR_LIT>" + self . name + "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" + self . nexthop + "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> if self . interface is not None : <EOL> element += "<STR_LIT>" % self . interface <EOL> element += "<STR_LIT>" "<STR_LIT>" % self . destination <EOL> return element </s>
