2013-03-18 01:06:52 +04:00
#
# Base class for all VM devices
#
2013-10-28 00:59:47 +04:00
# Copyright 2008, 2013 Red Hat, Inc.
2013-03-18 01:06:52 +04:00
# Cole Robinson <crobinso@redhat.com>
#
2018-03-20 22:00:02 +03:00
# This work is licensed under the GNU GPLv2.
# See the COPYING file in the top-level directory.
2013-03-18 01:06:52 +04:00
2014-02-25 23:56:54 +04:00
import logging
2013-07-12 23:16:29 +04:00
import os
2013-09-11 18:16:12 +04:00
import re
2015-03-27 01:04:23 +03:00
import string # pylint: disable=deprecated-module
2013-03-18 01:06:52 +04:00
2018-02-14 23:35:53 +03:00
from . xmlapi import XMLAPI
2014-09-12 23:59:22 +04:00
from . import util
2013-03-18 01:06:52 +04:00
2013-08-09 04:47:17 +04:00
2014-04-03 02:39:43 +04:00
# pylint: disable=protected-access
2013-07-15 17:49:46 +04:00
# This whole file is calling around into non-public functions that we
# don't want regular API users to touch
2013-03-18 01:06:52 +04:00
2014-02-10 23:47:20 +04:00
_trackprops = bool ( " VIRTINST_TEST_SUITE " in os . environ )
2013-07-12 23:16:29 +04:00
_allprops = [ ]
_seenprops = [ ]
2013-03-18 01:06:52 +04:00
2018-02-08 01:27:56 +03:00
class _XMLChildList ( list ) :
"""
Little wrapper for a list containing XMLChildProperty output .
This is just to insert a dynamically created add_new ( ) function
which instantiates and appends a new child object
"""
def __init__ ( self , childclass , copylist , xmlbuilder ) :
list . __init__ ( self )
self . _childclass = childclass
self . _xmlbuilder = xmlbuilder
for i in copylist :
self . append ( i )
def new ( self ) :
"""
Instantiate a new child object and return it
"""
return self . _childclass ( self . _xmlbuilder . conn )
def add_new ( self ) :
"""
Instantiate a new child object , append it , and return it
"""
obj = self . new ( )
self . _xmlbuilder . add_child ( obj )
return obj
2013-09-11 02:32:10 +04:00
class XMLChildProperty ( property ) :
"""
Property that points to a class used for parsing a subsection of
of the parent XML . For example when we deligate parsing
/ domain / cpu / feature of the / domain / cpu class .
2013-09-11 18:16:12 +04:00
@child_classes : Single class or list of classes to parse state into
2018-02-08 01:27:56 +03:00
The list option is used by Guest . _devices for parsing all
devices into a single list
2013-09-11 18:16:12 +04:00
@relative_xpath : Relative location where the class is rooted compared
to its _XML_ROOT_PATH . So interface xml can have nested
interfaces rooted at / interface / bridge / interface , so we pass
. / bridge / interface here for example .
2013-09-11 02:32:10 +04:00
"""
2013-09-11 19:47:09 +04:00
def __init__ ( self , child_classes , relative_xpath = " . " , is_single = False ) :
2013-09-11 02:32:10 +04:00
self . child_classes = util . listify ( child_classes )
2013-09-11 18:16:12 +04:00
self . relative_xpath = relative_xpath
2013-09-11 19:47:09 +04:00
self . is_single = is_single
2013-09-19 21:25:52 +04:00
self . _propname = None
2013-09-11 19:47:09 +04:00
if self . is_single and len ( self . child_classes ) > 1 :
raise RuntimeError ( " programming error: Can ' t specify multiple "
" child_classes with is_single " )
2013-09-11 02:32:10 +04:00
property . __init__ ( self , self . _fget )
def __repr__ ( self ) :
return " <XMLChildProperty %s %s > " % ( str ( self . child_classes ) , id ( self ) )
def _findpropname ( self , xmlbuilder ) :
2013-09-19 21:25:52 +04:00
if self . _propname is None :
for key , val in xmlbuilder . _all_child_props ( ) . items ( ) :
if val is self :
self . _propname = key
break
if self . _propname is None :
raise RuntimeError ( " Didn ' t find expected property= %s " % self )
return self . _propname
2013-09-11 02:32:10 +04:00
2013-09-11 19:47:09 +04:00
def _get ( self , xmlbuilder ) :
2013-09-11 02:32:10 +04:00
propname = self . _findpropname ( xmlbuilder )
2013-09-11 19:47:09 +04:00
if propname not in xmlbuilder . _propstore and not self . is_single :
2013-09-11 02:32:10 +04:00
xmlbuilder . _propstore [ propname ] = [ ]
return xmlbuilder . _propstore [ propname ]
def _fget ( self , xmlbuilder ) :
2013-09-11 19:47:09 +04:00
if self . is_single :
return self . _get ( xmlbuilder )
2018-02-08 01:27:56 +03:00
return _XMLChildList ( self . child_classes [ 0 ] ,
self . _get ( xmlbuilder ) ,
xmlbuilder )
2013-09-11 19:47:09 +04:00
def clear ( self , xmlbuilder ) :
if self . is_single :
self . _get ( xmlbuilder ) . clear ( )
else :
for obj in self . _get ( xmlbuilder ) [ : ] :
2015-09-04 22:45:45 +03:00
xmlbuilder . remove_child ( obj )
2013-09-11 02:32:10 +04:00
2013-09-20 18:47:36 +04:00
def append ( self , xmlbuilder , newobj ) :
# Keep the list ordered by the order of passed in child classes
objlist = self . _get ( xmlbuilder )
if len ( self . child_classes ) == 1 :
objlist . append ( newobj )
return
idx = 0
2016-04-18 23:42:12 +03:00
for idx , obj in enumerate ( objlist ) :
2013-09-20 18:47:36 +04:00
obj = objlist [ idx ]
if ( obj . __class__ not in self . child_classes or
( self . child_classes . index ( newobj . __class__ ) <
self . child_classes . index ( obj . __class__ ) ) ) :
break
idx + = 1
objlist . insert ( idx , newobj )
2013-09-11 02:32:10 +04:00
def remove ( self , xmlbuilder , obj ) :
2013-09-11 19:47:09 +04:00
self . _get ( xmlbuilder ) . remove ( obj )
def set ( self , xmlbuilder , obj ) :
xmlbuilder . _propstore [ self . _findpropname ( xmlbuilder ) ] = obj
2013-09-11 02:32:10 +04:00
2013-09-11 19:47:09 +04:00
def get_prop_xpath ( self , xmlbuilder , obj ) :
relative_xpath = self . relative_xpath + " / " + obj . _XML_ROOT_NAME
2013-09-11 18:16:12 +04:00
match = re . search ( " % \ ((.*) \ ) " , self . relative_xpath )
if match :
valuedict = { }
for paramname in match . groups ( ) :
valuedict [ paramname ] = getattr ( xmlbuilder , paramname )
relative_xpath = relative_xpath % valuedict
2013-09-11 19:47:09 +04:00
return relative_xpath
2013-09-11 18:16:12 +04:00
2013-09-11 02:32:10 +04:00
2013-07-14 02:56:09 +04:00
class XMLProperty ( property ) :
2018-02-23 04:44:09 +03:00
def __init__ ( self , xpath ,
2015-09-06 00:20:43 +03:00
set_converter = None , validate_cb = None ,
2013-09-03 10:47:26 +04:00
is_bool = False , is_int = False , is_yesno = False , is_onoff = False ,
2014-12-04 23:27:30 +03:00
default_cb = None , default_name = None , do_abspath = False ) :
2013-07-14 01:54:46 +04:00
"""
2016-07-18 19:46:59 +03:00
Set a XMLBuilder class property that maps to a value in an XML
document , indicated by the passed xpath . For example , for a
< domain > < name > the definition may look like :
2013-07-14 01:54:46 +04:00
2016-07-18 19:46:59 +03:00
name = XMLProperty ( " ./name " )
2013-07-14 01:54:46 +04:00
2016-07-18 19:46:59 +03:00
When building XML from scratch ( virt - install ) , ' name ' works
similar to a regular class property ( ) . When parsing and editing
existing guest XML , we use the xpath value to get / set the value
in the parsed XML document .
2013-07-14 01:54:46 +04:00
2018-02-14 15:17:31 +03:00
: param xpath : xpath string which maps to the associated property
2013-07-14 01:54:46 +04:00
in a typical XML document
2018-02-14 15:17:31 +03:00
: param name : Just a string to print for debugging , only needed
2013-07-14 06:13:13 +04:00
if xpath isn ' t specified.
2018-02-14 15:17:31 +03:00
: param set_converter : optional function for converting the property
2013-07-14 01:54:46 +04:00
value from the virtinst API to the guest XML . For example ,
2014-06-17 05:33:14 +04:00
the Guest . memory API was once in MiB , but the libvirt domain
memory API is in KiB . So , if xpath is specified , on a ' get '
2013-07-14 07:07:01 +04:00
operation we convert the XML value with int ( val ) / 1024.
2018-02-14 15:17:31 +03:00
: param validate_cb : Called once when value is set , should
2013-07-18 01:58:24 +04:00
raise a RuntimeError if the value is not proper .
2018-02-14 15:17:31 +03:00
: param is_bool : Whether this is a boolean property in the XML
: param is_int : Whether this is an integer property in the XML
: param is_yesno : Whether this is a yes / no property in the XML
: param is_onoff : Whether this is an on / off property in the XML
: param default_cb : If building XML from scratch , and this property
2013-07-15 01:02:42 +04:00
is never explicitly altered , this function is called for setting
a default value in the XML , and for any ' get ' call before the
first explicit ' set ' .
2018-02-14 15:17:31 +03:00
: param default_name : If the user does a set and passes in this
2013-07-15 18:57:16 +04:00
value , instead use the value of default_cb ( )
2018-02-14 15:17:31 +03:00
: param do_abspath : If True , run os . path . abspath on the passed value
2013-07-14 01:54:46 +04:00
"""
2013-07-12 23:16:29 +04:00
self . _xpath = xpath
2015-09-06 00:20:43 +03:00
if not self . _xpath :
raise RuntimeError ( " XMLProperty: xpath must be passed. " )
2013-09-19 21:25:52 +04:00
self . _propname = None
2013-07-12 23:16:29 +04:00
2013-07-24 20:51:53 +04:00
self . _is_bool = is_bool
2013-07-14 05:49:32 +04:00
self . _is_int = is_int
2013-07-16 21:04:24 +04:00
self . _is_yesno = is_yesno
2013-09-02 18:56:22 +04:00
self . _is_onoff = is_onoff
2014-09-23 22:25:14 +04:00
self . _do_abspath = do_abspath
2013-07-14 01:54:46 +04:00
2013-07-18 01:58:24 +04:00
self . _validate_cb = validate_cb
2013-07-14 01:54:46 +04:00
self . _convert_value_for_setter_cb = set_converter
2013-07-15 01:02:42 +04:00
self . _default_cb = default_cb
2013-07-15 18:57:16 +04:00
self . _default_name = default_name
2013-07-15 01:02:42 +04:00
2013-07-17 17:57:15 +04:00
if sum ( [ int ( bool ( i ) ) for i in
2013-09-02 18:56:22 +04:00
[ self . _is_bool , self . _is_int ,
self . _is_yesno , self . _is_onoff ] ] ) > 1 :
2013-07-15 01:02:42 +04:00
raise RuntimeError ( " Conflict property converter options. " )
2013-07-14 01:54:46 +04:00
2013-07-15 18:57:16 +04:00
if self . _default_name and not self . _default_cb :
raise RuntimeError ( " default_name requires default_cb. " )
2015-04-22 21:44:52 +03:00
self . _is_tracked = False
if _trackprops :
2013-07-18 01:58:24 +04:00
_allprops . append ( self )
2013-07-14 01:54:46 +04:00
2013-07-18 01:58:24 +04:00
property . __init__ ( self , fget = self . getter , fset = self . setter )
2013-07-14 01:54:46 +04:00
2013-07-12 23:16:29 +04:00
def __repr__ ( self ) :
2015-09-06 00:20:43 +03:00
return " <XMLProperty %s %s > " % ( str ( self . _xpath ) , id ( self ) )
2013-07-12 23:16:29 +04:00
2013-07-14 01:54:46 +04:00
####################
# Internal helpers #
####################
def _findpropname ( self , xmlbuilder ) :
"""
Map the raw property ( ) instance to the param name it ' s exposed
as in the XMLBuilder class . This is just for debug purposes .
"""
2013-09-19 21:25:52 +04:00
if self . _propname is None :
for key , val in xmlbuilder . _all_xml_props ( ) . items ( ) :
if val is self :
self . _propname = key
break
if self . _propname is None :
raise RuntimeError ( " Didn ' t find expected property= %s " % self )
return self . _propname
2013-07-12 18:49:15 +04:00
2013-07-24 20:51:53 +04:00
def _convert_get_value ( self , val ) :
2016-04-18 23:42:12 +03:00
# pylint: disable=redefined-variable-type
2013-07-24 20:51:53 +04:00
if self . _default_name and val == self . _default_name :
ret = val
elif self . _is_bool :
ret = bool ( val )
2013-07-15 00:05:59 +04:00
elif self . _is_int and val is not None :
2013-07-16 05:52:18 +04:00
intkwargs = { }
2013-07-15 17:49:46 +04:00
if " 0x " in str ( val ) :
2013-07-16 05:52:18 +04:00
intkwargs [ " base " ] = 16
2013-07-17 17:57:15 +04:00
ret = int ( val , * * intkwargs )
2013-07-16 21:04:24 +04:00
elif self . _is_yesno and val is not None :
2013-07-17 17:57:15 +04:00
ret = bool ( val == " yes " )
2013-09-02 18:56:22 +04:00
elif self . _is_onoff and val is not None :
ret = bool ( val == " on " )
2013-07-17 17:57:15 +04:00
else :
ret = val
return ret
2013-07-14 05:04:27 +04:00
2013-07-24 22:37:07 +04:00
def _convert_set_value ( self , xmlbuilder , val ) :
if self . _default_name and val == self . _default_name :
val = self . _default_cb ( xmlbuilder )
2014-09-23 22:25:14 +04:00
elif self . _do_abspath and val is not None :
val = os . path . abspath ( val )
2013-09-02 18:56:22 +04:00
elif self . _is_onoff and val is not None :
val = bool ( val ) and " on " or " off "
2013-07-25 23:27:42 +04:00
elif self . _is_yesno and val is not None :
val = bool ( val ) and " yes " or " no "
elif self . _is_int and val is not None :
intkwargs = { }
if " 0x " in str ( val ) :
intkwargs [ " base " ] = 16
val = int ( val , * * intkwargs )
2013-07-24 22:37:07 +04:00
if self . _convert_value_for_setter_cb :
val = self . _convert_value_for_setter_cb ( xmlbuilder , val )
return val
2013-07-18 01:58:24 +04:00
def _prop_is_unset ( self , xmlbuilder ) :
2013-07-14 17:31:14 +04:00
propname = self . _findpropname ( xmlbuilder )
2013-07-25 19:02:56 +04:00
return ( propname not in xmlbuilder . _propstore )
2013-07-18 01:58:24 +04:00
2013-07-24 22:37:07 +04:00
def _default_get_value ( self , xmlbuilder ) :
"""
Return ( can use default , default value )
"""
ret = ( False , - 1 )
2013-07-25 20:34:37 +04:00
if not xmlbuilder . _xmlstate . is_build :
return ret
2013-07-24 22:37:07 +04:00
if not self . _prop_is_unset ( xmlbuilder ) :
return ret
if not self . _default_cb :
return ret
if self . _default_name :
return ( True , self . _default_name )
return ( True , self . _default_cb ( xmlbuilder ) )
2013-07-18 01:58:24 +04:00
def _set_default ( self , xmlbuilder ) :
"""
Encode the property default into the XML and propstore , but
only if a default is registered , and only if the property was
not already explicitly set by the API user .
This is called during the get_xml_config process and shouldn ' t
be called from outside this file .
"""
2013-07-24 22:37:07 +04:00
candefault , val = self . _default_get_value ( xmlbuilder )
if not candefault :
2013-07-18 01:58:24 +04:00
return
2013-07-24 22:37:07 +04:00
self . setter ( xmlbuilder , val , validate = False )
2013-07-14 17:31:14 +04:00
2013-07-18 01:58:24 +04:00
def _nonxml_fset ( self , xmlbuilder , val ) :
2013-07-14 17:31:14 +04:00
"""
2013-07-18 01:58:24 +04:00
This stores the value in XMLBuilder . _propstore
2013-07-14 17:31:14 +04:00
dict as propname - > value . This saves us from having to explicitly
track every variable .
"""
2013-07-25 19:02:56 +04:00
propstore = xmlbuilder . _propstore
proporder = xmlbuilder . _proporder
2013-07-14 17:31:14 +04:00
propname = self . _findpropname ( xmlbuilder )
propstore [ propname ] = val
if propname in proporder :
proporder . remove ( propname )
proporder . append ( propname )
2013-07-18 01:58:24 +04:00
def _nonxml_fget ( self , xmlbuilder ) :
"""
The flip side to nonxml_fset , fetch the value from
XMLBuilder . _propstore
"""
2013-07-24 22:37:07 +04:00
candefault , val = self . _default_get_value ( xmlbuilder )
if candefault :
return val
2013-07-18 01:58:24 +04:00
propname = self . _findpropname ( xmlbuilder )
2013-07-25 19:02:56 +04:00
return xmlbuilder . _propstore . get ( propname , None )
2013-07-18 01:58:24 +04:00
2013-09-11 19:47:09 +04:00
def clear ( self , xmlbuilder ) :
2018-02-14 23:35:53 +03:00
# We only unset the cached data, since XML will be cleared elsewhere
propstore = xmlbuilder . _propstore
propname = self . _findpropname ( xmlbuilder )
if propname in propstore :
self . setter ( xmlbuilder , None )
2013-07-14 17:31:14 +04:00
##################################
# The actual getter/setter impls #
##################################
2013-07-18 01:58:24 +04:00
def getter ( self , xmlbuilder ) :
2013-07-25 19:02:56 +04:00
"""
Fetch value at user request . If we are parsing existing XML and
the user hasn ' t done a ' set ' yet, return the value from the XML,
otherwise return the value from propstore
2013-07-25 20:34:37 +04:00
If this is a built from scratch object , we never pull from XML
since it ' s known to the empty, and we may want to return
a ' default ' value
2013-07-25 19:02:56 +04:00
"""
2015-04-22 21:44:52 +03:00
if _trackprops and not self . _is_tracked :
_seenprops . append ( self )
self . _is_tracked = True
2016-04-18 23:42:12 +03:00
# pylint: disable=redefined-variable-type
2013-07-25 20:34:37 +04:00
if ( self . _prop_is_unset ( xmlbuilder ) and
not xmlbuilder . _xmlstate . is_build ) :
2013-07-24 22:37:07 +04:00
val = self . _get_xml ( xmlbuilder )
else :
val = self . _nonxml_fget ( xmlbuilder )
ret = self . _convert_get_value ( val )
return ret
2013-07-14 01:54:46 +04:00
2013-07-24 22:37:07 +04:00
def _get_xml ( self , xmlbuilder ) :
2013-07-25 19:02:56 +04:00
"""
Actually fetch the associated value from the backing XML
"""
2018-02-14 23:35:53 +03:00
xpath = xmlbuilder . _xmlstate . make_abs_xpath ( self . _xpath )
return xmlbuilder . _xmlstate . xmlapi . get_xpath_content (
xpath , self . _is_bool )
2013-03-18 01:06:52 +04:00
2013-07-24 22:37:07 +04:00
def setter ( self , xmlbuilder , val , validate = True ) :
2013-07-25 19:02:56 +04:00
"""
Set the value at user request . This just stores the value
in propstore . Setting the actual XML is only done at
get_xml_config time .
"""
2015-04-22 21:44:52 +03:00
if _trackprops and not self . _is_tracked :
_seenprops . append ( self )
self . _is_tracked = True
2013-07-24 22:37:07 +04:00
if validate and self . _validate_cb :
self . _validate_cb ( xmlbuilder , val )
self . _nonxml_fset ( xmlbuilder ,
self . _convert_set_value ( xmlbuilder , val ) )
2013-03-18 01:06:52 +04:00
2018-02-14 17:57:22 +03:00
def _set_xml ( self , xmlbuilder , setval ) :
2013-07-25 19:02:56 +04:00
"""
Actually set the passed value in the XML document
"""
2018-02-14 23:35:53 +03:00
xpath = xmlbuilder . _xmlstate . make_abs_xpath ( self . _xpath )
xmlbuilder . _xmlstate . xmlapi . set_xpath_content ( xpath , setval )
2013-07-15 00:01:38 +04:00
2013-04-13 22:34:52 +04:00
2013-07-25 19:02:56 +04:00
class _XMLState ( object ) :
2017-03-06 02:32:53 +03:00
def __init__ ( self , root_name , parsexml , parentxmlstate ,
relative_object_xpath ) :
self . _root_name = root_name
2018-02-23 22:39:47 +03:00
self . _namespace = " "
if " : " in self . _root_name :
ns = self . _root_name . split ( " : " ) [ 0 ]
self . _namespace = " xmlns: %s = ' %s ' " % ( ns , XMLAPI . NAMESPACES [ ns ] )
2013-09-11 18:16:12 +04:00
# xpath of this object relative to its parent. So for a standalone
# <disk> this is empty, but if the disk is the forth one in a <domain>
# it will be set to ./devices/disk[4]
2013-09-11 19:47:09 +04:00
self . _relative_object_xpath = relative_object_xpath or " "
2013-09-11 18:16:12 +04:00
# xpath of the parent. For a disk in a standalone <domain>, this
# is empty, but if the <domain> is part of a <domainsnapshot>,
# it will be "./domain"
2017-03-06 02:32:53 +03:00
self . _parent_xpath = (
2018-02-14 23:35:53 +03:00
parentxmlstate and parentxmlstate . abs_xpath ( ) ) or " "
2013-07-25 19:02:56 +04:00
2018-02-14 18:48:53 +03:00
self . xmlapi = None
2013-07-25 19:02:56 +04:00
self . is_build = False
2017-03-06 02:32:53 +03:00
if not parsexml and not parentxmlstate :
2013-07-25 19:02:56 +04:00
self . is_build = True
2018-02-07 23:38:30 +03:00
self . parse ( parsexml , parentxmlstate )
2017-03-06 02:32:53 +03:00
2018-02-07 23:38:30 +03:00
def parse ( self , parsexml , parentxmlstate ) :
2017-03-06 02:32:53 +03:00
if parentxmlstate :
2018-02-07 23:38:30 +03:00
self . is_build = parentxmlstate . is_build or self . is_build
2018-02-14 18:48:53 +03:00
self . xmlapi = parentxmlstate . xmlapi
2017-03-06 02:32:53 +03:00
return
2014-02-25 21:45:15 +04:00
2018-02-23 22:39:47 +03:00
# Make sure passed in XML has required xmlns inserted
2017-03-06 02:32:53 +03:00
if not parsexml :
2018-02-23 22:39:47 +03:00
parsexml = " < %s %s /> " % ( self . _root_name , self . _namespace )
elif self . _namespace and " xmlns " not in parsexml :
2018-02-26 22:56:24 +03:00
parsexml = parsexml . replace ( " < " + self . _root_name ,
" < " + self . _root_name + self . _namespace )
2014-02-25 21:45:15 +04:00
2017-03-06 02:32:53 +03:00
try :
2018-02-14 23:35:53 +03:00
self . xmlapi = XMLAPI ( parsexml )
2017-07-24 11:26:48 +03:00
except Exception :
2017-03-06 02:32:53 +03:00
logging . debug ( " Error parsing xml= \n %s " , parsexml )
raise
2013-09-11 18:16:12 +04:00
def set_relative_object_xpath ( self , xpath ) :
self . _relative_object_xpath = xpath or " "
def set_parent_xpath ( self , xpath ) :
self . _parent_xpath = xpath or " "
2018-02-14 23:35:53 +03:00
def _join_xpath ( self , x1 , x2 ) :
if x1 . endswith ( " / " ) :
x1 = x1 [ : - 1 ]
if x2 . startswith ( " . " ) :
x2 = x2 [ 1 : ]
return x1 + x2
2013-07-25 19:02:56 +04:00
2018-02-14 23:35:53 +03:00
def abs_xpath ( self ) :
return self . _join_xpath ( self . _parent_xpath or " . " ,
self . _relative_object_xpath or " . " )
def make_abs_xpath ( self , xpath ) :
"""
2018-03-20 19:18:35 +03:00
Convert a relative xpath to an absolute xpath . So for DeviceDisk
2018-02-14 23:35:53 +03:00
that ' s part of a Guest, accessing driver_name will do convert:
. / driver / @name
to an absolute xpath like :
. / devices / disk [ 3 ] / driver / @name
"""
return self . _join_xpath ( self . abs_xpath ( ) or " . " , xpath )
2013-09-11 18:16:12 +04:00
2013-07-25 19:02:56 +04:00
2013-07-14 02:56:09 +04:00
class XMLBuilder ( object ) :
2013-03-18 01:06:52 +04:00
"""
Base for all classes which build or parse domain XML
"""
2013-07-16 20:48:52 +04:00
# Order that we should apply values to the XML. Keeps XML generation
# consistent with what the test suite expects.
2013-07-15 17:49:46 +04:00
_XML_PROP_ORDER = [ ]
2013-09-11 19:47:09 +04:00
# Name of the root XML element
_XML_ROOT_NAME = None
2013-07-18 01:58:24 +04:00
2015-03-27 01:04:23 +03:00
# In some cases, libvirt can incorrectly generate unparseable XML.
# These are libvirt bugs, but this allows us to work around it in
# for specific XML classes.
#
# Example: nodedev 'system' XML:
# https://bugzilla.redhat.com/show_bug.cgi?id=1184131
_XML_SANITIZE = False
2017-03-06 02:32:53 +03:00
def __init__ ( self , conn , parsexml = None ,
parentxmlstate = None , relative_object_xpath = None ) :
2013-03-18 01:06:52 +04:00
"""
Initialize state
2018-03-20 19:18:35 +03:00
: param conn : VirtinstConnection to validate device against
2018-02-14 15:17:31 +03:00
: param parsexml : Optional XML string to parse
2013-09-11 19:47:09 +04:00
The rest of the parameters are for internal use only
2013-03-18 01:06:52 +04:00
"""
2013-07-24 16:46:55 +04:00
self . conn = conn
2013-03-18 01:06:52 +04:00
2015-03-27 01:04:23 +03:00
if self . _XML_SANITIZE :
2017-10-11 14:35:58 +03:00
if hasattr ( parsexml , ' decode ' ) :
parsexml = parsexml . decode ( " ascii " , " ignore " ) . encode ( " ascii " )
else :
parsexml = parsexml . encode ( " ascii " , " ignore " ) . decode ( " ascii " )
2015-03-27 01:04:23 +03:00
parsexml = " " . join ( [ c for c in parsexml if c in string . printable ] )
2013-07-12 18:49:15 +04:00
self . _propstore = { }
self . _proporder = [ ]
2013-09-11 19:47:09 +04:00
self . _xmlstate = _XMLState ( self . _XML_ROOT_NAME ,
2017-03-06 02:32:53 +03:00
parsexml , parentxmlstate ,
relative_object_xpath )
2013-09-11 18:16:12 +04:00
2013-09-19 21:25:52 +04:00
self . _initial_child_parse ( )
def _initial_child_parse ( self ) :
2013-09-11 18:16:12 +04:00
# Walk the XML tree and hand of parsing to any registered
# child classes
2017-10-11 14:35:46 +03:00
for xmlprop in list ( self . _all_child_props ( ) . values ( ) ) :
2013-09-11 19:47:09 +04:00
if xmlprop . is_single :
child_class = xmlprop . child_classes [ 0 ]
prop_path = xmlprop . get_prop_xpath ( self , child_class )
obj = child_class ( self . conn ,
2017-03-06 02:32:53 +03:00
parentxmlstate = self . _xmlstate ,
2013-09-11 19:47:09 +04:00
relative_object_xpath = prop_path )
xmlprop . set ( self , obj )
continue
if self . _xmlstate . is_build :
continue
2013-09-11 18:16:12 +04:00
for child_class in xmlprop . child_classes :
prop_path = xmlprop . get_prop_xpath ( self , child_class )
2018-02-14 23:35:53 +03:00
nodecount = self . _xmlstate . xmlapi . count (
self . _xmlstate . make_abs_xpath ( prop_path ) )
2013-09-11 19:47:09 +04:00
for idx in range ( nodecount ) :
idxstr = " [ %d ] " % ( idx + 1 )
obj = child_class ( self . conn ,
2017-03-06 02:32:53 +03:00
parentxmlstate = self . _xmlstate ,
2013-09-11 19:47:09 +04:00
relative_object_xpath = ( prop_path + idxstr ) )
xmlprop . append ( self , obj )
2013-09-19 21:25:52 +04:00
2018-02-14 23:35:53 +03:00
def __repr__ ( self ) :
return " < %s %s %s > " % ( self . __class__ . __name__ . split ( " . " ) [ - 1 ] ,
self . _XML_ROOT_NAME , id ( self ) )
2013-07-25 19:02:56 +04:00
############################
# Public XML managing APIs #
############################
2013-07-24 16:46:55 +04:00
2013-07-24 19:32:30 +04:00
def get_xml_config ( self ) :
2013-07-25 19:02:56 +04:00
"""
Return XML string of the object
"""
2018-02-23 03:49:08 +03:00
xmlapi = self . _xmlstate . xmlapi
if self . _xmlstate . is_build :
xmlapi = xmlapi . copy_api ( )
self . _add_parse_bits ( xmlapi )
ret = xmlapi . get_xml ( self . _xmlstate . make_abs_xpath ( " . " ) )
if ret and not ret . endswith ( " \n " ) :
ret + = " \n "
return ret
2013-07-24 16:46:55 +04:00
2016-05-20 21:23:39 +03:00
def clear ( self , leave_stub = False ) :
2013-07-25 19:02:56 +04:00
"""
Wipe out all properties of the object
2016-05-20 21:23:39 +03:00
: param leave_stub : if True , don ' t unlink the top stub node,
see virtinst / cli usage for an explanation
2013-07-25 19:02:56 +04:00
"""
2018-02-14 23:35:53 +03:00
props = list ( self . _all_xml_props ( ) . values ( ) )
props + = list ( self . _all_child_props ( ) . values ( ) )
for prop in props :
prop . clear ( self )
2013-03-18 01:06:52 +04:00
2018-02-14 23:35:53 +03:00
is_child = bool ( re . match ( " ^.* \ [ \ d+ \ ]$ " , self . _xmlstate . abs_xpath ( ) ) )
2016-05-20 21:23:39 +03:00
if is_child or leave_stub :
2015-09-05 00:03:31 +03:00
# User requested to clear an object that is the child of
# another object (xpath ends in [1] etc). We can't fully remove
# the node in that case, since then the xmlbuilder object is
# no longer valid, and all the other child xpaths will be
# pointing to the wrong node. So just stub out the content
2018-02-14 23:35:53 +03:00
self . _xmlstate . xmlapi . node_clear ( self . _xmlstate . abs_xpath ( ) )
2015-09-05 00:03:31 +03:00
else :
2018-02-14 23:35:53 +03:00
self . _xmlstate . xmlapi . node_force_remove ( self . _xmlstate . abs_xpath ( ) )
2014-01-25 23:57:10 +04:00
2013-07-25 19:02:56 +04:00
def validate ( self ) :
"""
Validate any set values and raise an exception if there ' s
a problem
"""
pass
2013-07-15 02:31:33 +04:00
2013-10-06 21:17:35 +04:00
def set_defaults ( self , guest ) :
2013-07-25 19:02:56 +04:00
"""
Encode any default values if needed
"""
2013-10-06 21:17:35 +04:00
ignore = guest
2013-07-15 02:31:33 +04:00
2018-02-14 23:35:53 +03:00
def get_xml_id ( self ) :
"""
Return the location of the object in the XML document . This is
basically the absolute xpath , but the value returned should be
treated as opaque , it ' s just for cross XML comparisons. Used
in virt - manager code
"""
return self . _xmlstate . abs_xpath ( )
2013-07-15 02:31:33 +04:00
2013-07-25 19:02:56 +04:00
################
# Internal API #
################
2013-09-23 15:37:33 +04:00
def __get_prop_cache ( self , cachename , checkclass ) :
if not hasattr ( self . __class__ , cachename ) :
2013-09-20 19:30:33 +04:00
ret = { }
for c in reversed ( type . mro ( self . __class__ ) [ : - 1 ] ) :
for key , val in c . __dict__ . items ( ) :
2013-09-23 02:13:24 +04:00
if isinstance ( val , checkclass ) :
2013-09-20 19:30:33 +04:00
ret [ key ] = val
2013-09-23 15:37:33 +04:00
setattr ( self . __class__ , cachename , ret )
return getattr ( self . __class__ , cachename )
def _all_xml_props ( self ) :
"""
Return a list of all XMLProperty instances that this class has .
"""
cachename = self . __class__ . __name__ + " _cached_xml_props "
return self . __get_prop_cache ( cachename , XMLProperty )
2013-07-25 19:02:56 +04:00
2013-09-11 02:32:10 +04:00
def _all_child_props ( self ) :
2013-07-25 19:02:56 +04:00
"""
2013-09-11 02:32:10 +04:00
Return a list of all XMLChildProperty instances that this class has .
2013-07-25 19:02:56 +04:00
"""
2013-09-23 15:37:33 +04:00
cachename = self . __class__ . __name__ + " _cached_child_props "
return self . __get_prop_cache ( cachename , XMLChildProperty )
2013-09-20 19:30:33 +04:00
2015-09-05 00:03:31 +03:00
def _find_child_prop ( self , child_class , return_single = False ) :
2013-09-11 02:32:10 +04:00
xmlprops = self . _all_child_props ( )
2017-10-11 14:35:46 +03:00
for xmlprop in list ( xmlprops . values ( ) ) :
2015-09-05 00:03:31 +03:00
if xmlprop . is_single and not return_single :
2013-09-11 19:47:09 +04:00
continue
2013-09-11 02:32:10 +04:00
if child_class in xmlprop . child_classes :
return xmlprop
raise RuntimeError ( " programming error: "
" Didn ' t find child property for child_class= %s " %
child_class )
2018-02-14 23:35:53 +03:00
def _set_xpaths ( self , parent_xpath , relative_object_xpath = - 1 ) :
"""
Change the object hierarchy ' s cached xpaths
"""
self . _xmlstate . set_parent_xpath ( parent_xpath )
if relative_object_xpath != - 1 :
self . _xmlstate . set_relative_object_xpath ( relative_object_xpath )
for propname in self . _all_child_props ( ) :
for p in util . listify ( getattr ( self , propname , [ ] ) ) :
p . _set_xpaths ( self . _xmlstate . abs_xpath ( ) )
def _set_child_xpaths ( self ) :
"""
Walk the list of child properties and make sure their
xpaths point at their particular element . Needs to be called
whenever child objects are added or removed
"""
typecount = { }
for propname , xmlprop in self . _all_child_props ( ) . items ( ) :
for obj in util . listify ( getattr ( self , propname ) ) :
idxstr = " "
if not xmlprop . is_single :
class_type = obj . __class__
if class_type not in typecount :
typecount [ class_type ] = 0
typecount [ class_type ] + = 1
idxstr = " [ %d ] " % typecount [ class_type ]
prop_path = xmlprop . get_prop_xpath ( self , obj )
obj . _set_xpaths ( self . _xmlstate . abs_xpath ( ) ,
prop_path + idxstr )
2014-01-28 04:54:41 +04:00
def _parse_with_children ( self , * args , * * kwargs ) :
"""
Set new backing XML objects in ourselves and all our child props
"""
2018-02-07 23:38:30 +03:00
self . _xmlstate . parse ( * args , * * kwargs )
2014-01-28 04:54:41 +04:00
for propname in self . _all_child_props ( ) :
for p in util . listify ( getattr ( self , propname , [ ] ) ) :
2018-02-14 23:35:53 +03:00
p . _parse_with_children ( None , self . _xmlstate )
2014-01-28 04:54:41 +04:00
2015-09-04 22:45:45 +03:00
def add_child ( self , obj ) :
2013-09-11 02:32:10 +04:00
"""
Insert the passed XMLBuilder object into our XML document . The
object needs to have an associated mapping via XMLChildProperty
"""
xmlprop = self . _find_child_prop ( obj . __class__ )
2013-09-11 18:16:12 +04:00
xml = obj . get_xml_config ( )
2013-09-11 02:32:10 +04:00
xmlprop . append ( self , obj )
self . _set_child_xpaths ( )
if not obj . _xmlstate . is_build :
2018-02-14 23:35:53 +03:00
use_xpath = obj . _xmlstate . abs_xpath ( ) . rsplit ( " / " , 1 ) [ 0 ]
indent = 2 * obj . _xmlstate . abs_xpath ( ) . count ( " / " )
self . _xmlstate . xmlapi . node_add_xml (
2018-03-20 17:35:22 +03:00
util . xml_indent ( xml , indent ) , use_xpath )
2017-03-06 02:32:53 +03:00
obj . _parse_with_children ( None , self . _xmlstate )
2013-09-10 03:45:19 +04:00
2015-09-04 22:45:45 +03:00
def remove_child ( self , obj ) :
2013-07-25 19:02:56 +04:00
"""
Remove the passed XMLBuilder object from our XML document , but
2015-09-04 22:45:45 +03:00
ensure its data isn ' t altered.
2013-07-25 19:02:56 +04:00
"""
2013-09-11 02:32:10 +04:00
xmlprop = self . _find_child_prop ( obj . __class__ )
xmlprop . remove ( self , obj )
2018-02-14 23:35:53 +03:00
xpath = obj . _xmlstate . abs_xpath ( )
2013-09-11 02:32:10 +04:00
xml = obj . get_xml_config ( )
2018-02-14 23:35:53 +03:00
obj . _set_xpaths ( None , None )
2014-01-28 04:54:41 +04:00
obj . _parse_with_children ( xml , None )
2018-02-14 23:35:53 +03:00
self . _xmlstate . xmlapi . node_force_remove ( xpath )
2013-09-11 02:32:10 +04:00
self . _set_child_xpaths ( )
2015-09-04 23:07:01 +03:00
def list_children_for_class ( self , klass ) :
"""
Return a list of all XML child objects with the passed class
"""
ret = [ ]
2017-10-11 14:35:46 +03:00
for prop in list ( self . _all_child_props ( ) . values ( ) ) :
2015-09-04 23:07:01 +03:00
ret + = [ obj for obj in util . listify ( prop . _get ( self ) )
if obj . __class__ == klass ]
return ret
2015-09-05 00:03:31 +03:00
def child_class_is_singleton ( self , klass ) :
"""
Return True if the passed class is registered as a singleton
child property
"""
return self . _find_child_prop ( klass , return_single = True ) . is_single
2013-07-25 19:02:56 +04:00
#################################
# Private XML building routines #
#################################
2013-07-24 22:37:07 +04:00
2018-02-14 18:48:53 +03:00
def _add_parse_bits ( self , xmlapi ) :
2013-07-24 16:46:55 +04:00
"""
2013-07-25 19:02:56 +04:00
Callback that adds the implicitly tracked XML properties to
the backing xml .
2013-07-24 16:46:55 +04:00
"""
2013-07-25 19:02:56 +04:00
origproporder = self . _proporder [ : ]
origpropstore = self . _propstore . copy ( )
2018-02-14 18:48:53 +03:00
origapi = self . _xmlstate . xmlapi
2013-07-25 19:02:56 +04:00
try :
2018-02-14 18:48:53 +03:00
self . _xmlstate . xmlapi = xmlapi
return self . _do_add_parse_bits ( )
2013-07-25 19:02:56 +04:00
finally :
2018-02-14 18:48:53 +03:00
self . _xmlstate . xmlapi = origapi
2013-07-25 19:02:56 +04:00
self . _proporder = origproporder
self . _propstore = origpropstore
2013-03-18 01:06:52 +04:00
2018-02-14 18:48:53 +03:00
def _do_add_parse_bits ( self ) :
2013-07-18 01:58:24 +04:00
# Set all defaults if the properties have one registered
2013-09-11 02:32:10 +04:00
xmlprops = self . _all_xml_props ( )
2013-09-20 19:17:11 +04:00
childprops = self . _all_child_props ( )
2013-07-12 18:49:15 +04:00
2017-10-11 14:35:46 +03:00
for prop in list ( xmlprops . values ( ) ) :
2013-07-25 20:34:37 +04:00
prop . _set_default ( self )
2013-07-15 01:02:42 +04:00
# Set up preferred XML ordering
do_order = self . _proporder [ : ]
2013-07-15 17:49:46 +04:00
for key in reversed ( self . _XML_PROP_ORDER ) :
2013-09-20 19:17:11 +04:00
if key not in xmlprops and key not in childprops :
raise RuntimeError ( " programming error: key ' %s ' must be "
" xml prop or child prop " % key )
2013-07-15 01:02:42 +04:00
if key in do_order :
do_order . remove ( key )
do_order . insert ( 0 , key )
2013-09-20 19:17:11 +04:00
elif key in childprops :
2013-07-18 01:58:24 +04:00
do_order . insert ( 0 , key )
2013-07-15 01:02:42 +04:00
2018-02-20 19:41:04 +03:00
for key in sorted ( list ( childprops . keys ( ) ) ) :
2013-09-20 19:17:11 +04:00
if key not in do_order :
do_order . append ( key )
2013-07-15 01:02:42 +04:00
# Alter the XML
for key in do_order :
2013-07-18 01:58:24 +04:00
if key in xmlprops :
2018-02-14 17:57:22 +03:00
xmlprops [ key ] . _set_xml ( self , self . _propstore [ key ] )
2013-09-20 19:17:11 +04:00
elif key in childprops :
for obj in util . listify ( getattr ( self , key ) ) :
2018-02-14 18:48:53 +03:00
obj . _add_parse_bits ( self . _xmlstate . xmlapi )