Example #1
0
 def parse_addresses(self, interface):
     mac = []
     ipv4 = []
     ipv6 = []
     hostname = []
     for address in xmlutils.iter_children_with_name(interface, 'address'):
         addr_type = address.getAttribute('type')
         if not addr_type:
             msg = 'no type attribute found for address ' \
                   'in interface: \'%s\'' % interface.toxml('utf-8')
             logger.warn(msg)
             assert False  # XXX for testing
         addr_text = xmlutils.get_child_text_trim(address)
         if not addr_text:
             msg = 'no text found for address ' \
                   'in interface: \'%s\'' % interface.toxml('utf-8')
             logger.warn(msg)
             assert False  # XXX for testing
         if addr_type == 'mac':
             mac.append(addr_text)
         elif addr_type == 'IPv4':
             ipv4.append(addr_text)
         elif addr_type == 'IPv6':
             ipv6.append(addr_text)
         elif addr_type == 'hostname':
             hostname.append(addr_text)
         else:
             msg = 'skipping unknown address type \'%s\' in ' \
                   'interface: \'%s\'' % (addr_type, interface.toxml('utf-8'))
             logger.warn(msg)
             assert False  # XXX for testing
     return mac, ipv4, ipv6, hostname
Example #2
0
    def parse_network_channel(self, channel):
        element = self.search_for_element(channel, 'type', lambda x: not x.hasAttributes())
        channel_type = xmlutils.get_child_text_trim(element)
        link_params = self.parse_parameter_children(channel)

        mobility = xmlutils.get_first_child_by_tag_name(channel, 'CORE:mobility')
        if mobility:
            mobility_model_name = xmlutils.get_first_child_text_trim_by_tag_name(mobility, 'type')
            mobility_params = self.parse_parameter_children(mobility)
        else:
            mobility_model_name = None
            mobility_params = None

        if channel_type == 'wireless':
            self.set_wireless_link_parameters(channel, link_params, mobility_model_name, mobility_params)
        elif channel_type == 'ethernet':
            # TODO: maybe this can be done in the loop below to avoid
            # iterating through channel members multiple times
            self.set_ethernet_link_parameters(channel, link_params, mobility_model_name, mobility_params)
        else:
            raise NotImplementedError

        layer2_device = []
        for dev, if_name in self.iter_network_member_devices(channel):
            if self.device_type(dev) in self.layer2_device_types:
                layer2_device.append((dev, if_name))

        assert len(layer2_device) <= 2
        if len(layer2_device) == 2:
            self.link_layer2_devices(layer2_device[0][0], layer2_device[0][1],
                                     layer2_device[1][0], layer2_device[1][1])
Example #3
0
 def parse_addresses(self, interface):
     mac = []
     ipv4 = []
     ipv6 = []
     hostname = []
     for address in xmlutils.iter_children_with_name(interface, 'address'):
         addr_type = address.getAttribute('type')
         if not addr_type:
             msg = 'no type attribute found for address ' \
                   'in interface: \'%s\'' % interface.toxml('utf-8')
             logger.warn(msg)
             assert False  # XXX for testing
         addr_text = xmlutils.get_child_text_trim(address)
         if not addr_text:
             msg = 'no text found for address ' \
                   'in interface: \'%s\'' % interface.toxml('utf-8')
             logger.warn(msg)
             assert False  # XXX for testing
         if addr_type == 'mac':
             mac.append(addr_text)
         elif addr_type == 'IPv4':
             ipv4.append(addr_text)
         elif addr_type == 'IPv6':
             ipv6.append(addr_text)
         elif addr_type == 'hostname':
             hostname.append(addr_text)
         else:
             msg = 'skipping unknown address type \'%s\' in ' \
                   'interface: \'%s\'' % (addr_type, interface.toxml('utf-8'))
             logger.warn(msg)
             assert False  # XXX for testing
     return mac, ipv4, ipv6, hostname
Example #4
0
    def parse_network_channel(self, channel):
        element = self.search_for_element(channel, 'type', lambda x: not x.hasAttributes())
        channel_type = xmlutils.get_child_text_trim(element)
        link_params = self.parse_parameter_children(channel)

        mobility = xmlutils.get_first_child_by_tag_name(channel, 'CORE:mobility')
        if mobility:
            mobility_model_name = xmlutils.get_first_child_text_trim_by_tag_name(mobility, 'type')
            mobility_params = self.parse_parameter_children(mobility)
        else:
            mobility_model_name = None
            mobility_params = None

        if channel_type == 'wireless':
            self.set_wireless_link_parameters(channel, link_params, mobility_model_name, mobility_params)
        elif channel_type == 'ethernet':
            # TODO: maybe this can be done in the loop below to avoid
            # iterating through channel members multiple times
            self.set_ethernet_link_parameters(channel, link_params, mobility_model_name, mobility_params)
        else:
            raise NotImplementedError

        layer2_device = []
        for dev, if_name in self.iter_network_member_devices(channel):
            if self.device_type(dev) in self.layer2_device_types:
                layer2_device.append((dev, if_name))

        assert len(layer2_device) <= 2
        if len(layer2_device) == 2:
            self.link_layer2_devices(layer2_device[0][0], layer2_device[0][1],
                                     layer2_device[1][0], layer2_device[1][1])
Example #5
0
    def find_core_id(cls, node):
        def match(x):
            domain = x.getAttribute('domain')
            return domain == 'COREID'

        alias = cls.search_for_element(node, 'alias', match)
        if alias:
            return xmlutils.get_child_text_trim(alias)
        return None
Example #6
0
    def find_core_id(cls, node):
        def match(x):
            domain = x.getAttribute('domain')
            return domain == 'COREID'

        alias = cls.search_for_element(node, 'alias', match)
        if alias:
            return xmlutils.get_child_text_trim(alias)
        return None
Example #7
0
 def iter_network_member_devices(self, element):
     # element can be a network or a channel
     for interface in xmlutils.iter_children_with_attribute(element, 'member', 'type', 'interface'):
         if_id = xmlutils.get_child_text_trim(interface)
         assert if_id  # XXX for testing
         if not if_id:
             continue
         device, if_name = self.find_device_with_interface(if_id)
         assert device, 'no device for if_id: %s' % if_id  # XXX for testing
         if device:
             yield device, if_name
Example #8
0
 def iter_network_member_devices(self, element):
     # element can be a network or a channel
     for interface in xmlutils.iter_children_with_attribute(element, 'member', 'type', 'interface'):
         if_id = xmlutils.get_child_text_trim(interface)
         assert if_id  # XXX for testing
         if not if_id:
             continue
         device, if_name = self.find_device_with_interface(if_id)
         assert device, 'no device for if_id: %s' % if_id  # XXX for testing
         if device:
             yield device, if_name
Example #9
0
 def parse_session_hooks(self, session_config):
     """
     Parse hook scripts.
     """
     hooks = xmlutils.get_first_child_by_tag_name(session_config, 'hooks')
     if not hooks:
         return
     for hook in xmlutils.iter_children_with_name(hooks, 'hook'):
         filename = hook.getAttribute('name')
         state = hook.getAttribute('state')
         data = xmlutils.get_child_text_trim(hook)
         if data is None:
             data = ''  # allow for empty file
         hook_type = "hook:%s" % state
         self.session.set_hook(hook_type, file_name=str(filename), source_name=None, data=str(data))
Example #10
0
 def parse_parameter_children(cls, parent):
     params = {}
     for parameter in xmlutils.iter_children_with_name(parent, 'parameter'):
         param_name = parameter.getAttribute('name')
         assert param_name  # XXX for testing
         if not param_name:
             continue
         # TODO: consider supporting unicode; for now convert
         # to an ascii string
         param_name = str(param_name)
         param_val = cls.parse_xml_value(xmlutils.get_child_text_trim(parameter))
         # TODO: check if the name already exists?
         if param_name and param_val:
             params[param_name] = param_val
     return params
Example #11
0
 def parse_session_hooks(self, session_config):
     """
     Parse hook scripts.
     """
     hooks = xmlutils.get_first_child_by_tag_name(session_config, 'hooks')
     if not hooks:
         return
     for hook in xmlutils.iter_children_with_name(hooks, 'hook'):
         filename = hook.getAttribute('name')
         state = hook.getAttribute('state')
         data = xmlutils.get_child_text_trim(hook)
         if data is None:
             data = ''  # allow for empty file
         hook_type = "hook:%s" % state
         self.session.set_hook(hook_type, file_name=str(filename), source_name=None, data=str(data))
Example #12
0
 def parse_parameter_children(cls, parent):
     params = {}
     for parameter in xmlutils.iter_children_with_name(parent, 'parameter'):
         param_name = parameter.getAttribute('name')
         assert param_name  # XXX for testing
         if not param_name:
             continue
         # TODO: consider supporting unicode; for now convert
         # to an ascii string
         param_name = str(param_name)
         param_val = cls.parse_xml_value(xmlutils.get_child_text_trim(parameter))
         # TODO: check if the name already exists?
         if param_name and param_val:
             params[param_name] = param_val
     return params
Example #13
0
 def parse_device_service(self, service, node):
     name = service.getAttribute('name')
     session_service = ServiceManager.get(name)
     if not session_service:
         assert False  # XXX for testing
     values = []
     startup_idx = service.getAttribute('startup_idx')
     if startup_idx:
         values.append('startidx=%s' % startup_idx)
     startup_time = service.getAttribute('start_time')
     if startup_time:
         values.append('starttime=%s' % startup_time)
     dirs = []
     for directory in xmlutils.iter_children_with_name(
             service, 'directory'):
         dirname = directory.getAttribute('name')
         dirs.append(str(dirname))
     if dirs:
         values.append("dirs=%s" % dirs)
     startup = []
     shutdown = []
     validate = []
     for command in xmlutils.iter_children_with_name(service, 'command'):
         command_type = command.getAttribute('type')
         command_text = xmlutils.get_child_text_trim(command)
         if not command_text:
             continue
         if command_type == 'start':
             startup.append(str(command_text))
         elif command_type == 'stop':
             shutdown.append(str(command_text))
         elif command_type == 'validate':
             validate.append(str(command_text))
     if startup:
         values.append('cmdup=%s' % startup)
     if shutdown:
         values.append('cmddown=%s' % shutdown)
     if validate:
         values.append('cmdval=%s' % validate)
     filenames = []
     files = []
     for f in xmlutils.iter_children_with_name(service, 'file'):
         filename = f.getAttribute('name')
         if not filename:
             continue
         filenames.append(filename)
         data = xmlutils.get_child_text_trim(f)
         if data:
             data = str(data)
         else:
             data = None
         typestr = 'service:%s:%s' % (name, filename)
         files.append((typestr, filename, data))
     if filenames:
         values.append('files=%s' % filenames)
     custom = service.getAttribute('custom')
     if custom and custom.lower() == 'true':
         self.session.services.setcustomservice(node.objid, session_service,
                                                values)
     # NOTE: if a custom service is used, setservicefile() must be
     # called after the custom service exists
     for typestr, filename, data in files:
         self.session.services.setservicefile(nodenum=node.objid,
                                              type=typestr,
                                              filename=filename,
                                              srcname=None,
                                              data=data)
     return str(name)
Example #14
0
    def parse_device_service(self, service, node):
        name = service.getAttribute('name')
        session_service = ServiceManager.get(name)
        if not session_service:
            assert False  # XXX for testing
        values = []
        startup_idx = service.getAttribute('startup_idx')
        if startup_idx:
            values.append('startidx=%s' % startup_idx)
        startup_time = service.getAttribute('start_time')
        if startup_time:
            values.append('starttime=%s' % startup_time)
        dirs = []
        for directory in xmlutils.iter_children_with_name(service, 'directory'):
            dirname = directory.getAttribute('name')
            dirs.append(str(dirname))
        if dirs:
            values.append("dirs=%s" % dirs)
        startup = []
        shutdown = []
        validate = []
        for command in xmlutils.iter_children_with_name(service, 'command'):
            command_type = command.getAttribute('type')
            command_text = xmlutils.get_child_text_trim(command)
            if not command_text:
                continue
            if command_type == 'start':
                startup.append(str(command_text))
            elif command_type == 'stop':
                shutdown.append(str(command_text))
            elif command_type == 'validate':
                validate.append(str(command_text))
        if startup:
            values.append('cmdup=%s' % startup)
        if shutdown:
            values.append('cmddown=%s' % shutdown)
        if validate:
            values.append('cmdval=%s' % validate)

        filenames = []
        files = []
        for f in xmlutils.iter_children_with_name(service, 'file'):
            filename = f.getAttribute('name')
            if not filename:
                continue
            filenames.append(filename)
            data = xmlutils.get_child_text_trim(f)
            if data:
                data = str(data)
            else:
                data = None
            typestr = 'service:%s:%s' % (name, filename)
            files.append((typestr, filename, data))

        if filenames:
            values.append('files=%s' % filenames)

        custom = service.getAttribute('custom')
        if custom and custom.lower() == 'true':
            self.session.services.set_service(node.objid, session_service.name)
            values = ConfigShim.str_to_dict("|".join(values))
            for key, value in values.iteritems():
                ServiceShim.setvalue(session_service, key, value)

        # NOTE: if a custom service is used, setservicefile() must be
        # called after the custom service exists
        for typestr, filename, data in files:
            svcname = typestr.split(":")[1]
            self.session.services.set_service_file(
                node_id=node.objid,
                service_name=svcname,
                file_name=filename,
                data=data
            )
        return str(name)