/virttest/libvirt_xml/nwfilter_protocols/icmp.py

https://github.com/avocado-framework/avocado-vt · Python · 131 lines · 101 code · 8 blank · 22 comment · 1 complexity · 1311775ba161c76849835663326f81f4 MD5 · raw file

  1. """
  2. icmp protocl support class(es)
  3. http://libvirt.org/formatnwfilter.html#nwfelemsRulesProtoICMP
  4. """
  5. from virttest.libvirt_xml import accessors, xcepts
  6. from virttest.libvirt_xml.nwfilter_protocols import base
  7. class Icmp(base.TypedDeviceBase):
  8. """
  9. Create new Icmp xml instances
  10. Properties:
  11. attrs: libvirt_xml.nwfilter_protocols.Icmp.Attr instance
  12. """
  13. __slots__ = ('attrs',)
  14. def __init__(self, type_name='file', virsh_instance=base.base.virsh):
  15. accessors.XMLElementNest('attrs', self, parent_xpath='/',
  16. tag_name='icmp', subclass=self.Attr,
  17. subclass_dargs={
  18. 'virsh_instance': virsh_instance})
  19. super(Icmp, self).__init__(protocol_tag='icmp', type_name=type_name,
  20. virsh_instance=virsh_instance)
  21. def new_attr(self, **dargs):
  22. """
  23. Return a new Attr instance and set properties from dargs
  24. :param dargs: dict of attributes
  25. :return: new Attr instance
  26. """
  27. new_one = self.Attr(virsh_instance=self.virsh)
  28. for key, value in list(dargs.items()):
  29. setattr(new_one, key, value)
  30. return new_one
  31. def get_attr(self):
  32. """
  33. Return icmp attribute dict
  34. :return: None if no icmp in xml, dict of icmp's attributes.
  35. """
  36. try:
  37. icmp_node = self.xmltreefile.reroot('/icmp')
  38. except KeyError as detail:
  39. raise xcepts.LibvirtXMLError(detail)
  40. node = icmp_node.getroot()
  41. icmp_attr = dict(list(node.items()))
  42. return icmp_attr
  43. class Attr(base.base.LibvirtXMLBase):
  44. """
  45. Icmp attribute XML class
  46. Properties:
  47. srcmacaddr: string, MAC address of sender
  48. srcmacmask: string, Mask applied to MAC address of sender
  49. dstmacaddr: string, MAC address of destination
  50. dstmacmask: string, Mask applied to MAC address of destination
  51. srcipaddr: string, Source IP address
  52. srcipmask: string, Mask applied to source IP address
  53. dstipaddr: string, Destination IP address
  54. dstipmask: string, Mask applied to destination IP address
  55. srcipfrom: string, Start of range of source IP address
  56. srcipto: string, End of range of source IP address
  57. dstipfrom: string, Start of range of destination IP address
  58. dstipto: string, End of range of destination IP address
  59. type: string, ICMP type
  60. code: string, ICMP code
  61. comment: string, text with max. 256 characters
  62. state: string, comma separated list of NEW,ESTABLISHED,RELATED,INVALID or NONE
  63. ipset: The name of an IPSet managed outside of libvirt
  64. ipsetflags: flags for the IPSet; requires ipset attribute
  65. """
  66. __slots__ = ('srcmacaddr', 'srcmacmask', 'dstmacaddr', 'dstmacmask',
  67. 'srcipaddr', 'srcipmask', 'dstipaddr', 'dstipmask',
  68. 'srcipfrom', 'srcipto', 'dstipfrom', 'dstipto',
  69. 'type', 'code', 'dscp', 'comment', 'state', 'ipset',
  70. 'ipsetflags')
  71. def __init__(self, virsh_instance=base.base.virsh):
  72. accessors.XMLAttribute('srcmacaddr', self, parent_xpath='/',
  73. tag_name='icmp', attribute='srcmacaddr')
  74. accessors.XMLAttribute('srcmacmask', self, parent_xpath='/',
  75. tag_name='icmp', attribute='srcmacmask')
  76. accessors.XMLAttribute('dstmacaddr', self, parent_xpath='/',
  77. tag_name='icmp', attribute='dstmacaddr')
  78. accessors.XMLAttribute('dstmacmask', self, parent_xpath='/',
  79. tag_name='icmp', attribute='dstmacmask')
  80. accessors.XMLAttribute('srcipaddr', self, parent_xpath='/',
  81. tag_name='icmp', attribute='srcipaddr')
  82. accessors.XMLAttribute('srcipmask', self, parent_xpath='/',
  83. tag_name='icmp', attribute='srcipmask')
  84. accessors.XMLAttribute('dstipaddr', self, parent_xpath='/',
  85. tag_name='icmp', attribute='dstipaddr')
  86. accessors.XMLAttribute('dstipmask', self, parent_xpath='/',
  87. tag_name='icmp', attribute='dstipmask')
  88. accessors.XMLAttribute('srcipfrom', self, parent_xpath='/',
  89. tag_name='icmp', attribute='srcipfrom')
  90. accessors.XMLAttribute('srcipto', self, parent_xpath='/',
  91. tag_name='icmp', attribute='srcipto')
  92. accessors.XMLAttribute('dstipfrom', self, parent_xpath='/',
  93. tag_name='icmp', attribute='dstipfrom')
  94. accessors.XMLAttribute('dstipto', self, parent_xpath='/',
  95. tag_name='icmp', attribute='dstipto')
  96. accessors.XMLAttribute('type', self, parent_xpath='/',
  97. tag_name='icmp', attribute='type')
  98. accessors.XMLAttribute('code', self, parent_xpath='/',
  99. tag_name='icmp', attribute='code')
  100. accessors.XMLAttribute('dscp', self, parent_xpath='/',
  101. tag_name='icmp', attribute='dscp')
  102. accessors.XMLAttribute('comment', self, parent_xpath='/',
  103. tag_name='icmp', attribute='comment')
  104. accessors.XMLAttribute('state', self, parent_xpath='/',
  105. tag_name='icmp', attribute='state')
  106. accessors.XMLAttribute('ipset', self, parent_xpath='/',
  107. tag_name='icmp', attribute='ipset')
  108. accessors.XMLAttribute('ipsetflags', self, parent_xpath='/',
  109. tag_name='icmp', attribute='ipsetflags')
  110. super(self.__class__, self).__init__(virsh_instance=virsh_instance)
  111. self.xml = '<icmp/>'