# Path: orangengine/models/base/baseaddress.py
# class BaseAddress(BaseObject):
# 
#     AddressTypes = enum('IPv4', 'DNS', 'RANGE', 'ANY')
#     TypeMap = bidict({
#         AddressTypes.IPv4: 'ipv4',
#         AddressTypes.DNS: 'dns',
#         AddressTypes.RANGE: 'range',
#         AddressTypes.ANY: 'any',
#     })
# 
#     def __init__(self, name, value, a_type):
#         """init address object"""
# 
#         self.name = name
#         self.value = value
#         self.a_type = a_type
# 
#     def __getattr__(self, item):
# 
#         if item == 'value':
#             return self.value
#         else:
#             raise AttributeError
# 
#     def table_value(self, with_names):
#         if with_names:
#             return self.name + " - " + self.value
#         else:
#             return self.value
# 
#     @classmethod
#     def from_criteria(cls, criteria):
#         """Create an instance from the provided criteria
#         """
# 
#         return cls(criteria['name'], criteria['value'], cls.TypeMap[criteria['type']])
# 
#     def serialize(self):
#         """Searialize self to a json acceptable data structure
#         """
#         return {
#             'name': self.name,
#             'type': self.TypeMap[self.a_type],
#             'value': self.value,
#         }
# 
# Path: orangengine/models/base/baseaddressgroup.py
# class BaseAddressGroup(BaseObject):
# 
#     def __init__(self, name):
#         """init a address group object"""
# 
#         self.name = name
#         self.elements = list()
# 
#     def add(self, address):
#         """add an address(group) object to the elements list"""
# 
#         self.elements.append(address)
# 
#     def __getattr__(self, item):
#         """
#         yeild the values of the underlying objects
#         """
#         if item == 'value':
#             return [a.value for a in self.elements]
#         else:
#             raise AttributeError
# 
#     def table_value(self, with_names):
#         value = "Group: " + self.name + "\n"
#         for a in self.elements:
#             value = value + "   " + a.table_value(with_names) + "\n"
#         return value.rstrip('\n')  # remove the last new line
# 
#     def serialize(self):
#         """Searialize self to a json acceptable data structure
#         """
# 
#         elements = []
#         for e in self.elements:
#             elements.append(e.serialize())
# 
#         return {
#             'name': self.name,
#             'elements': elements
#         }
# 
# Path: orangengine/utils.py
# def create_element(tag, text=None, parent=None):
#     # create an ambiguous xml element
#     if parent is not None:
#         e = letree.SubElement(parent, tag)
#     else:
#         e = letree.Element(tag)
#     if text:
#         e.text = text
#     return e


from orangengine.models.base import BaseAddressGroup, BaseAddress
from orangengine.utils import create_element
# -*- coding: utf-8 -*-


class JuniperSRXAddressGroup(BaseAddressGroup):

    def __init__(self, name):

        super(JuniperSRXAddressGroup, self).__init__(name)

    def to_xml(self):
        """Map address group objects to juniper SRX config tree elements
        """

        addressgroup_element = create_element('address-set')
        create_element('name', text=self.name, parent=addressgroup_element)

        for a in self.elements:
            if isinstance(a, BaseAddress):
                address_element = create_element('address', parent=addressgroup_element)
            else:

# Path: orangengine/models/base/baseaddress.py
# class BaseAddress(BaseObject):
# 
#     AddressTypes = enum('IPv4', 'DNS', 'RANGE', 'ANY')
#     TypeMap = bidict({
#         AddressTypes.IPv4: 'ipv4',
#         AddressTypes.DNS: 'dns',
#         AddressTypes.RANGE: 'range',
#         AddressTypes.ANY: 'any',
#     })
# 
#     def __init__(self, name, value, a_type):
#         """init address object"""
# 
#         self.name = name
#         self.value = value
#         self.a_type = a_type
# 
#     def __getattr__(self, item):
# 
#         if item == 'value':
#             return self.value
#         else:
#             raise AttributeError
# 
#     def table_value(self, with_names):
#         if with_names:
#             return self.name + " - " + self.value
#         else:
#             return self.value
# 
#     @classmethod
#     def from_criteria(cls, criteria):
#         """Create an instance from the provided criteria
#         """
# 
#         return cls(criteria['name'], criteria['value'], cls.TypeMap[criteria['type']])
# 
#     def serialize(self):
#         """Searialize self to a json acceptable data structure
#         """
#         return {
#             'name': self.name,
#             'type': self.TypeMap[self.a_type],
#             'value': self.value,
#         }
# 
# Path: orangengine/models/base/baseaddressgroup.py
# class BaseAddressGroup(BaseObject):
# 
#     def __init__(self, name):
#         """init a address group object"""
# 
#         self.name = name
#         self.elements = list()
# 
#     def add(self, address):
#         """add an address(group) object to the elements list"""
# 
#         self.elements.append(address)
# 
#     def __getattr__(self, item):
#         """
#         yeild the values of the underlying objects
#         """
#         if item == 'value':
#             return [a.value for a in self.elements]
#         else:
#             raise AttributeError
# 
#     def table_value(self, with_names):
#         value = "Group: " + self.name + "\n"
#         for a in self.elements:
#             value = value + "   " + a.table_value(with_names) + "\n"
#         return value.rstrip('\n')  # remove the last new line
# 
#     def serialize(self):
#         """Searialize self to a json acceptable data structure
#         """
# 
#         elements = []
#         for e in self.elements:
#             elements.append(e.serialize())
# 
#         return {
#             'name': self.name,
#             'elements': elements
#         }
# 
# Path: orangengine/utils.py
# def create_element(tag, text=None, parent=None):
#     # create an ambiguous xml element
#     if parent is not None:
#         e = letree.SubElement(parent, tag)
#     else:
#         e = letree.Element(tag)
#     if text:
#         e.text = text
#     return e


# Path: orangengine/models/juniper/addressgroup.py
from orangengine.models.base import BaseAddressGroup, BaseAddress
from orangengine.utils import create_element

# -*- coding: utf-8 -*-


class JuniperSRXAddressGroup(BaseAddressGroup):

    def __init__(self, name):

        super(JuniperSRXAddressGroup, self).__init__(name)

    def to_xml(self):
        """Map address group objects to juniper SRX config tree elements
        """

        addressgroup_element = create_element('address-set')
        create_element('name', text=self.name, parent=addressgroup_element)

        for a in self.elements:
            if isinstance(a, BaseAddress):
                address_element = create_element('address', parent=addressgroup_element)
            else:

                address_element = create_element('address-set', parent=addressgroup_element)