Exemple #1
0
 def __init__(self, filename, options):
     if 'dom' in options:
         self.dom = options['dom']
     else:
         self.dom = parse(filename)
     scenario = get_first_child_by_tag_name(self.dom, 'scenario')
     if scenario:
         version = scenario.getAttribute('version')
         if not version:
             version = self.DEFAULT_SCENARIO_VERSION
         self.version = version
     elif get_first_child_by_tag_name(self.dom, 'Scenario'):
         self.version = '0.0'
     else:
         self.version = 'unknown'
Exemple #2
0
 def parse_session_origin(self, session_config):
     """
     Parse the first origin tag and set the CoreLocation reference
     point appropriately.
     """
     # defaults from the CORE GUI
     self.session.location.setrefgeo(47.5791667, -122.132322, 2.0)
     self.session.location.refscale = 150.0
     origin = xmlutils.get_first_child_by_tag_name(session_config, 'origin')
     if not origin:
         return
     lat = origin.getAttribute('lat')
     lon = origin.getAttribute('lon')
     alt = origin.getAttribute('alt')
     if lat and lon and alt:
         self.session.location.setrefgeo(float(lat), float(lon), float(alt))
         self.location_refgeo_set = True
     scale100 = origin.getAttribute("scale100")
     if scale100:
         self.session.location.refscale = float(scale100)
     point = xmlutils.get_first_child_text_trim_by_tag_name(origin, 'point')
     if point:
         xyz = point.split(',')
         if len(xyz) == 2:
             xyz.append('0.0')
         if len(xyz) == 3:
             self.session.location.refxyz = (float(xyz[0]), float(xyz[1]),
                                             float(xyz[2]))
             self.location_refxyz_set = True
Exemple #3
0
 def parse_session_origin(self, session_config):
     """
     Parse the first origin tag and set the CoreLocation reference
     point appropriately.
     """
     # defaults from the CORE GUI
     self.session.location.setrefgeo(47.5791667, -122.132322, 2.0)
     self.session.location.refscale = 150.0
     origin = xmlutils.get_first_child_by_tag_name(session_config, 'origin')
     if not origin:
         return
     lat = origin.getAttribute('lat')
     lon = origin.getAttribute('lon')
     alt = origin.getAttribute('alt')
     if lat and lon and alt:
         self.session.location.setrefgeo(float(lat), float(lon), float(alt))
         self.location_refgeo_set = True
     scale100 = origin.getAttribute("scale100")
     if scale100:
         self.session.location.refscale = float(scale100)
     point = xmlutils.get_first_child_text_trim_by_tag_name(origin, 'point')
     if point:
         xyz = point.split(',')
         if len(xyz) == 2:
             xyz.append('0.0')
         if len(xyz) == 3:
             self.session.location.refxyz = (float(xyz[0]), float(xyz[1]), float(xyz[2]))
             self.location_refxyz_set = True
Exemple #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])
Exemple #5
0
 def parse_default_services(self):
     # defaults from the CORE GUI
     self.default_services = {
         'router': ['zebra', 'OSPFv2', 'OSPFv3', 'IPForward'],
         'host': ['DefaultRoute', 'SSH'],
         'PC': ['DefaultRoute', ],
         'mdr': ['zebra', 'OSPFv3MDR', 'IPForward'],
     }
     default_services = xmlutils.get_first_child_by_tag_name(self.scenario, 'CORE:defaultservices')
     if not default_services:
         return
     for device in xmlutils.iter_children_with_name(default_services, 'device'):
         device_type = device.getAttribute('type')
         if not device_type:
             logger.warn('parse_default_services: no type attribute found for device')
             continue
         services = []
         for service in xmlutils.iter_children_with_name(device, 'service'):
             name = service.getAttribute('name')
             if name:
                 services.append(str(name))
         self.default_services[device_type] = services
     # store default services for the session
     for t, s in self.default_services.iteritems():
         self.session.services.default_services[t] = s
         logger.info('default services for node type \'%s\' set to: %s' % (t, s))
Exemple #6
0
 def parse_default_services(self):
     # defaults from the CORE GUI
     self.default_services = {
         'router': ['zebra', 'OSPFv2', 'OSPFv3', 'IPForward'],
         'host': ['DefaultRoute', 'SSH'],
         'PC': [
             'DefaultRoute',
         ],
         'mdr': ['zebra', 'OSPFv3MDR', 'IPForward'],
     }
     default_services = xmlutils.get_first_child_by_tag_name(
         self.scenario, 'CORE:defaultservices')
     if not default_services:
         return
     for device in xmlutils.iter_children_with_name(default_services,
                                                    'device'):
         device_type = device.getAttribute('type')
         if not device_type:
             logger.warn(
                 'parse_default_services: no type attribute found for device'
             )
             continue
         services = []
         for service in xmlutils.iter_children_with_name(device, 'service'):
             name = service.getAttribute('name')
             if name:
                 services.append(str(name))
         self.default_services[device_type] = services
     # store default services for the session
     for t, s in self.default_services.iteritems():
         self.session.services.defaultservices[t] = s
         logger.info('default services for node type \'%s\' set to: %s' %
                     (t, s))
Exemple #7
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])
Exemple #8
0
 def get_scenario(dom):
     scenario = xmlutils.get_first_child_by_tag_name(dom, 'scenario')
     if not scenario:
         raise ValueError('no scenario element found')
     version = scenario.getAttribute('version')
     if version and version != '1.0':
         raise ValueError('unsupported scenario version found: \'%s\'' % version)
     return scenario
Exemple #9
0
 def parse_session_config(self):
     session_config = xmlutils.get_first_child_by_tag_name(self.scenario, 'CORE:sessionconfig')
     if not session_config:
         return
     self.parse_session_origin(session_config)
     self.parse_session_options(session_config)
     self.parse_session_hooks(session_config)
     self.parse_session_metadata(session_config)
Exemple #10
0
 def parse_session_metadata(self, session_config):
     metadata = xmlutils.get_first_child_by_tag_name(session_config, 'metadata')
     if not metadata:
         return
     params = self.parse_parameter_children(metadata)
     for name, value in params.iteritems():
         if name and value:
             self.session.metadata.add_item(str(name), str(value))
Exemple #11
0
 def parse_session_options(self, session_config):
     options = xmlutils.get_first_child_by_tag_name(session_config, 'options')
     if not options:
         return
     params = self.parse_parameter_children(options)
     for name, value in params.iteritems():
         if name and value:
             setattr(self.session.options, str(name), str(value))
Exemple #12
0
 def get_scenario(dom):
     scenario = xmlutils.get_first_child_by_tag_name(dom, 'scenario')
     if not scenario:
         raise ValueError('no scenario element found')
     version = scenario.getAttribute('version')
     if version and version != '1.0':
         raise ValueError('unsupported scenario version found: \'%s\'' % version)
     return scenario
Exemple #13
0
 def parse_session_config(self):
     session_config = xmlutils.get_first_child_by_tag_name(self.scenario, 'CORE:sessionconfig')
     if not session_config:
         return
     self.parse_session_origin(session_config)
     self.parse_session_options(session_config)
     self.parse_session_hooks(session_config)
     self.parse_session_metadata(session_config)
Exemple #14
0
 def parse_session_metadata(self, session_config):
     metadata = xmlutils.get_first_child_by_tag_name(session_config, 'metadata')
     if not metadata:
         return
     params = self.parse_parameter_children(metadata)
     for name, value in params.iteritems():
         if name and value:
             self.session.metadata.set_config(str(name), str(value))
Exemple #15
0
 def parse_session_options(self, session_config):
     options = xmlutils.get_first_child_by_tag_name(session_config, 'options')
     if not options:
         return
     params = self.parse_parameter_children(options)
     for name, value in params.iteritems():
         if name and value:
             self.session.options.set_config(str(name), str(value))
Exemple #16
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))
Exemple #17
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))
Exemple #18
0
 def add_device_services(self, node, device, node_type):
     """
     Add services to the given node.
     """
     services = xmlutils.get_first_child_by_tag_name(
         device, 'CORE:services')
     if services:
         services_str = self.parse_device_services(services, node)
         logger.info('services for node \'%s\': %s' %
                     (node.name, services_str))
     elif node_type in self.default_services:
         services_str = None  # default services will be added
     else:
         return
     self.session.services.addservicestonode(node=node,
                                             nodetype=node_type,
                                             services_str=services_str)
Exemple #19
0
    def add_device_services(self, node, device, node_type):
        """
        Add services to the given node.
        """
        services = xmlutils.get_first_child_by_tag_name(device, 'CORE:services')
        if services:
            services_str = self.parse_device_services(services, node)
            logger.info('services for node \'%s\': %s' % (node.name, services_str))
        elif node_type in self.default_services:
            services_str = None  # default services will be added
        else:
            return
        if services_str:
            services_str = services_str.split("|")

        self.session.services.add_services(
            node=node,
            node_type=node_type,
            services=services_str
        )
Exemple #20
0
 def network_class(self, network, network_type):
     """
     Return the corresponding CORE network class for the given
     network/network_type.
     """
     if network_type in ['ethernet', 'satcom']:
         return nodeutils.get_node_class(NodeTypes.PEER_TO_PEER)
     elif network_type == 'wireless':
         channel = xmlutils.get_first_child_by_tag_name(network, 'channel')
         if channel:
             # use an explicit CORE type if it exists
             coretype = xmlutils.get_first_child_text_trim_with_attribute(channel, 'type', 'domain', 'CORE')
             if coretype:
                 if coretype == 'basic_range':
                     return nodeutils.get_node_class(NodeTypes.WIRELESS_LAN)
                 elif coretype.startswith('emane'):
                     return nodeutils.get_node_class(NodeTypes.EMANE)
                 else:
                     logger.warn('unknown network type: \'%s\'', coretype)
                     return xmlutils.xml_type_to_node_class(coretype)
         return nodeutils.get_node_class(NodeTypes.WIRELESS_LAN)
     logger.warn('unknown network type: \'%s\'', network_type)
     return None
Exemple #21
0
 def network_class(self, network, network_type):
     """
     Return the corresponding CORE network class for the given
     network/network_type.
     """
     if network_type in ['ethernet', 'satcom']:
         return nodeutils.get_node_class(NodeTypes.PEER_TO_PEER)
     elif network_type == 'wireless':
         channel = xmlutils.get_first_child_by_tag_name(network, 'channel')
         if channel:
             # use an explicit CORE type if it exists
             coretype = xmlutils.get_first_child_text_trim_with_attribute(channel, 'type', 'domain', 'CORE')
             if coretype:
                 if coretype == 'basic_range':
                     return nodeutils.get_node_class(NodeTypes.WIRELESS_LAN)
                 elif coretype.startswith('emane'):
                     return nodeutils.get_node_class(NodeTypes.EMANE)
                 else:
                     logger.warn('unknown network type: \'%s\'', coretype)
                     return xmlutils.xml_type_to_node_class(coretype)
         return nodeutils.get_node_class(NodeTypes.WIRELESS_LAN)
     logger.warn('unknown network type: \'%s\'', network_type)
     return None