2007-03-21 19:28:36 +03:00
#
2013-05-22 16:59:27 +04:00
# Copyright (C) 2006, 2013 Red Hat, Inc.
2007-03-21 19:28:36 +03:00
# Copyright (C) 2006 Daniel P. Berrange <berrange@redhat.com>
#
# 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
2007-11-20 19:12:20 +03:00
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
2007-03-21 19:28:36 +03:00
#
2013-07-12 18:53:30 +04:00
2013-03-29 22:38:28 +04:00
import ipaddr
2013-07-12 18:53:30 +04:00
2013-09-21 04:40:07 +04:00
from virtinst import Network
2013-07-12 18:53:30 +04:00
2010-12-09 20:37:48 +03:00
from virtManager . libvirtobject import vmmLibvirtObject
2007-03-21 19:28:36 +03:00
2013-04-13 22:34:52 +04:00
2013-09-21 04:40:07 +04:00
def _make_addr_str ( addrStr , prefix , netmaskStr ) :
if prefix :
return str ( ipaddr . IPNetwork ( str ( addrStr ) + " / " +
str ( prefix ) ) . masked ( ) )
elif netmaskStr :
netmask = ipaddr . IPAddress ( netmaskStr )
network = ipaddr . IPAddress ( addrStr )
return str ( ipaddr . IPNetwork ( str ( network ) + " / " +
str ( netmask ) ) . masked ( ) )
else :
return str ( ipaddr . IPNetwork ( str ( addrStr ) ) )
2009-12-01 19:49:08 +03:00
2013-09-21 04:40:07 +04:00
class vmmNetwork ( vmmLibvirtObject ) :
2013-07-07 16:42:57 +04:00
def __init__ ( self , conn , backend , key ) :
2013-09-21 04:40:07 +04:00
vmmLibvirtObject . __init__ ( self , conn , backend , key , parseclass = Network )
2013-07-07 16:42:57 +04:00
self . _active = True
2013-07-07 16:05:23 +04:00
self . _support_isactive = None
self . tick ( )
2010-12-09 20:37:48 +03:00
2013-09-21 04:40:07 +04:00
##########################
# Required class methods #
##########################
2010-12-09 20:37:48 +03:00
def get_name ( self ) :
2013-07-07 16:42:57 +04:00
return self . _backend . name ( )
2010-12-09 20:37:48 +03:00
def _XMLDesc ( self , flags ) :
2013-07-07 16:42:57 +04:00
return self . _backend . XMLDesc ( flags )
2010-12-09 20:37:48 +03:00
def _define ( self , xml ) :
2013-07-07 16:42:57 +04:00
return self . conn . define_network ( xml )
2010-12-09 20:37:48 +03:00
2013-09-21 04:40:07 +04:00
###########
# Actions #
###########
def _backend_get_active ( self ) :
if self . _support_isactive is None :
self . _support_isactive = self . conn . check_net_support (
self . _backend ,
self . conn . SUPPORT_NET_ISACTIVE )
if not self . _support_isactive :
return True
return bool ( self . _backend . isActive ( ) )
2013-07-07 16:42:57 +04:00
def _set_active ( self , state ) :
if state == self . _active :
2013-07-07 16:05:23 +04:00
return
self . idle_emit ( state and " started " or " stopped " )
2013-07-07 16:42:57 +04:00
self . _active = state
2007-04-03 22:01:45 +04:00
2007-03-21 19:28:36 +03:00
def is_active ( self ) :
2013-07-07 16:42:57 +04:00
return self . _active
2007-03-21 19:28:36 +03:00
2013-07-07 19:06:15 +04:00
def _kick_conn ( self ) :
self . conn . schedule_priority_tick ( pollnet = True )
2007-03-30 05:23:17 +04:00
def start ( self ) :
2013-07-07 16:42:57 +04:00
self . _backend . create ( )
2013-07-07 19:06:15 +04:00
self . _kick_conn ( )
2007-03-30 05:23:17 +04:00
2007-04-03 22:01:45 +04:00
def stop ( self ) :
2013-07-07 16:42:57 +04:00
self . _backend . destroy ( )
2013-07-07 19:06:15 +04:00
self . _kick_conn ( )
2007-04-03 22:01:45 +04:00
def delete ( self ) :
2013-07-07 16:42:57 +04:00
self . _backend . undefine ( )
self . _backend = None
2013-07-07 19:06:15 +04:00
self . _kick_conn ( )
2007-04-03 22:01:45 +04:00
2007-03-30 23:02:09 +04:00
def get_autostart ( self ) :
2013-07-07 16:42:57 +04:00
return self . _backend . autostart ( )
2013-09-21 04:40:07 +04:00
def set_autostart ( self , value ) :
self . _backend . setAutostart ( value )
2013-07-07 16:05:23 +04:00
def tick ( self ) :
2013-07-07 16:42:57 +04:00
self . _set_active ( self . _backend_get_active ( ) )
2013-07-07 16:05:23 +04:00
2013-09-21 04:40:07 +04:00
###############
# XML parsing #
###############
2013-07-07 16:05:23 +04:00
2013-09-21 04:40:07 +04:00
def get_uuid ( self ) :
return self . _get_xmlobj ( ) . uuid
def get_bridge_device ( self ) :
return self . _get_xmlobj ( ) . bridge
def get_name_domain ( self ) :
return self . _get_xmlobj ( ) . domain_name
def get_ipv6_enabled ( self ) :
return self . _get_xmlobj ( ) . ipv6
def get_ipv4_forward_mode ( self ) :
return self . _get_xmlobj ( ) . forward . mode
def pretty_forward_mode ( self ) :
return self . _get_xmlobj ( ) . forward . pretty_desc ( )
def can_pxe ( self ) :
2013-09-23 01:04:22 +04:00
return self . _get_xmlobj ( ) . can_pxe ( )
2013-09-21 04:40:07 +04:00
def _get_static_route ( self , family ) :
xmlobj = self . _get_xmlobj ( )
route = None
for r in xmlobj . routes :
if ( r . family == family or ( family == " ipv4 " and not r . family ) ) :
route = r
2013-05-14 20:15:02 +04:00
break
2013-09-21 04:40:07 +04:00
if not route :
return [ None , None ]
2013-05-14 20:15:02 +04:00
2013-09-21 04:40:07 +04:00
routeAddr = _make_addr_str ( route . address , route . prefix , route . netmask )
routeVia = str ( ipaddr . IPAddress ( str ( route . gateway ) ) )
2013-05-14 20:15:02 +04:00
2013-09-21 04:40:07 +04:00
if not routeAddr or not routeVia :
return [ None , None ]
return [ routeAddr , routeVia ]
def _get_network ( self , family ) :
2013-03-29 22:38:28 +04:00
dhcpstart = None
dhcpend = None
2013-09-21 04:40:07 +04:00
xmlobj = self . _get_xmlobj ( )
ip = None
for i in xmlobj . ips :
if ( i . family == family or
( family == " ipv4 " and not i . family ) ) :
if i . ranges :
ip = i
dhcpstart = i . ranges [ 0 ] . start
dhcpend = i . ranges [ 0 ] . end
2013-03-29 22:38:28 +04:00
break
2013-09-21 04:40:07 +04:00
if not ip :
for i in xmlobj . ips :
if ( i . family == family or
( family == " ipv4 " and not i . family ) ) :
ip = i
2013-03-29 22:38:28 +04:00
break
2013-09-21 04:40:07 +04:00
ret = None
if ip :
ret = _make_addr_str ( ip . address , ip . prefix , ip . netmask )
2013-03-29 22:38:28 +04:00
2013-09-21 04:40:07 +04:00
dhcp = [ None , None ]
2013-03-29 22:38:28 +04:00
if dhcpstart and dhcpend :
2013-09-21 04:40:07 +04:00
dhcp = [ str ( ipaddr . IPAddress ( dhcpstart ) ) ,
str ( ipaddr . IPAddress ( dhcpend ) ) ]
return [ ret , dhcp ]
2007-04-03 22:01:45 +04:00
2013-09-21 04:40:07 +04:00
def get_ipv4_network ( self ) :
ret = self . _get_network ( " ipv4 " )
return ret + [ self . _get_static_route ( " ipv4 " ) ]
def get_ipv6_network ( self ) :
ret = self . _get_network ( " ipv6 " )
return ret + [ self . _get_static_route ( " ipv6 " ) ]