示例#1
0
def switch_to_other_class(con, name, cmp_classname, classname, cmp_attribs, attribs):
    if not name:
        name = con.name
    try:
        dev = con.device
    except:
        print "No device connected to console '%s'" % con.name
        raise CliError

    breakpoints = SIM_get_attribute(con, "break-string")

    newname = "new_" + name
    if "console" in classname:
        obj_name = "con"
        attribs += [["device", dev]]
    else:
        obj_name = "link"
    try:
        if dev.component:
            basename = component_basename(cmp_classname)
            obj_cmp = SIM_create_object(cmp_classname, get_available_object_name(basename), cmp_attribs)
            pre_obj = get_component_object(obj_cmp, obj_name)
            if "console" in classname:
                pre_obj.device = dev
            instantiate_cmd(False, [obj_cmp])
            obj = get_component_object(obj_cmp, obj_name)
        else:
            # support component-less configurations
            obj = SIM_create_object(classname, newname, attribs + [["queue", SIM_current_processor()]])
    except Exception, msg:
        print "Failed creating %s '%s': %s" % (classname, newname, msg)
        raise CliError
示例#2
0
def switch_to_other_class(con, name,
                          cmp_classname, classname, cmp_attribs, attribs):
    if not name:
        name = con.name
    try:
        dev = con.device
    except:
        print "No device connected to console '%s'" % con.name
        raise CliError

    breakpoints = SIM_get_attribute(con, "break-string")

    newname = "new_" + name
    if 'console' in classname:
        obj_name = 'con'
        attribs += [['device', dev]]
    else:
        obj_name = 'link'
    try:
        if dev.component:
            basename = component_basename(cmp_classname)
            obj_cmp = SIM_create_object(
                cmp_classname,
                get_available_object_name(basename), cmp_attribs)
            pre_obj = get_component_object(obj_cmp, obj_name)
            if 'console' in classname:
                pre_obj.device = dev
            instantiate_cmd(False, [obj_cmp])
            obj = get_component_object(obj_cmp, obj_name)
        else:
            # support component-less configurations
            obj = SIM_create_object(classname, newname,
                                    attribs +
                                    [['queue', SIM_current_processor()]])
    except Exception, msg:
        print "Failed creating %s '%s': %s" % (classname, newname, msg)
        raise CliError
def get_service_node(link, tgt_ip, sn_ip, create_sn):

    queue = get_first_queue()  # may raise exception
    link_created = 0
    sn_cmp = None

    if not link:
        links = [
            x for x in SIM_all_objects() if instance_of(x, "ethernet-link")
        ]
        if len(links) > 1:
            SIM_command_has_problem()
            print("There are more than one ethernet-link, please specify " +
                  "which one the simulated machine is connected to.")
            raise Exception
        elif len(links) == 0:
            # create component
            SIM_get_class('std-ethernet-link')
            basename = component_basename('std-ethernet-link')
            link_cmp = SIM_create_object('std-ethernet-link',
                                         get_available_object_name(basename),
                                         [])
            instantiate_cmd(False, [link_cmp])
            link = get_component_object(link_cmp, "link")
            link_created = 1
            if SIM_is_interactive():
                print "No ethernet-link found, creating '%s'." % link.name
        else:
            link = links[0]
    link_cmp = link.component
    # find service nodes on link
    sns = [
        x.service_node for x in SIM_all_objects()
        if instance_of(x, "service-node-device") and x.link == link
    ]
    if len(sns) == 0:
        if not create_sn:
            return None
        if not sn_ip:
            if not tgt_ip:
                return None
            sn_ip = inet_ntoa(inet_aton(tgt_ip)[0:3] + '\x01')
        # create component
        SIM_get_class('std-service-node')
        basename = component_basename('std-service-node')
        sn_cmp = SIM_create_object('std-service-node',
                                   get_available_object_name(basename), [])
        instantiate_cmd(False, [sn_cmp])
        get_component(sn_cmp).add_connector(sn_ip, "255.255.255.0")
        sn = get_component_object(sn_cmp, "sn")
        if SIM_is_interactive():
            print("No service-node found, creating '%s' with IP '%s'." %
                  (sn.name, sn_ip))
    else:
        sn = sns[0]
        if sn_ip and sn_ip != get_sn_ip(sn, link):
            print("Using already existing service node with IP %s." %
                  get_sn_ip(sn, link))
    if len(sns) > 1:
        print "More than one service-node, using '%s'." % sn.name

    # connect devices to our newly created link
    if link_created:
        devs = [
            x for x in SIM_all_objects() if instance_of(x, "ethernet_device")
        ]
        for dev in devs:
            if not dev.link:
                dev_cmp = dev.component
                if dev_cmp and link_cmp:
                    if components_connected(dev_cmp, link_cmp):
                        continue
                    # connect using components
                    connect_cmd(link_cmp, None, dev_cmp, None, True)
                    print("Connecting component '%s' to '%s'" %
                          (dev_cmp.name, link_cmp.name))
                else:
                    dev.link = link
                    print("Connecting device '%s' to '%s'" %
                          (dev.name, link.name))
    elif sn_cmp:
        # a service-node component was created, but no link
        if link_cmp:
            # connect using components
            connect_cmd(link_cmp, None, sn_cmp, None, True)
            print("Connecting component '%s' to '%s'" %
                  (sn_cmp.name, link_cmp.name))
        else:
            snd = [
                x for x in SIM_all_objects() if
                (instance_of(x, "service-node-device") and x.service_node == sn
                 and x.ip_address == sn_ip and x.netmask == "255.255.255.0")
            ][0]
            snd.link = link
            print("Connecting device '%s' to '%s'" % (snd.name, link.name))
    return (link, sn)
示例#4
0
def get_service_node(link, tgt_ip, sn_ip, create_sn):

    queue = get_first_queue() # may raise exception
    link_created = 0
    sn_cmp = None

    if not link:
        links = [x for x in SIM_all_objects()
                 if instance_of(x, "ethernet-link")]
        if len(links) > 1:
            SIM_command_has_problem()
            print ("There are more than one ethernet-link, please specify " +
                   "which one the simulated machine is connected to.")
            raise Exception
        elif len(links) == 0:
            # create component
            SIM_get_class('std-ethernet-link')
            basename = component_basename('std-ethernet-link')
            link_cmp = SIM_create_object(
                'std-ethernet-link',
                get_available_object_name(basename), [])
            instantiate_cmd(False, [link_cmp])
            link = get_component_object(link_cmp, "link")
            link_created = 1
            if SIM_is_interactive():
                print "No ethernet-link found, creating '%s'." % link.name
        else:
            link = links[0]
    link_cmp = link.component
    # find service nodes on link
    sns = [x.service_node for x in SIM_all_objects()
           if instance_of(x, "service-node-device") and x.link == link]
    if len(sns) == 0:
        if not create_sn:
            return None
        if not sn_ip:
            if not tgt_ip:
                return None
            sn_ip = inet_ntoa(inet_aton(tgt_ip)[0:3] + '\x01')
        # create component
        SIM_get_class('std-service-node')
        basename = component_basename('std-service-node')
        sn_cmp = SIM_create_object(
            'std-service-node',
            get_available_object_name(basename), [])
        instantiate_cmd(False, [sn_cmp])
        get_component(sn_cmp).add_connector(sn_ip, "255.255.255.0")
        sn = get_component_object(sn_cmp, "sn")
        if SIM_is_interactive():
            print ("No service-node found, creating '%s' with IP '%s'."
                   % (sn.name, sn_ip))
    else:
        sn = sns[0]
        if sn_ip and sn_ip != get_sn_ip(sn, link):
            print ("Using already existing service node with IP %s."
                   % get_sn_ip(sn, link))
    if len(sns) > 1:
        print "More than one service-node, using '%s'." % sn.name

    # connect devices to our newly created link
    if link_created:
        devs = [x for x in SIM_all_objects()
                if instance_of(x, "ethernet_device")]
        for dev in devs:
            if not dev.link:
                dev_cmp = dev.component
                if dev_cmp and link_cmp:
                    if components_connected(dev_cmp, link_cmp):
                        continue
                    # connect using components
                    connect_cmd(link_cmp, None, dev_cmp, None, True)
                    print ("Connecting component '%s' to '%s'"
                           % (dev_cmp.name, link_cmp.name))
                else:
                    dev.link = link
                    print ("Connecting device '%s' to '%s'"
                           % (dev.name, link.name))
    elif sn_cmp:
        # a service-node component was created, but no link
        if link_cmp:
            # connect using components
            connect_cmd(link_cmp, None, sn_cmp, None, True)
            print ("Connecting component '%s' to '%s'"
                   % (sn_cmp.name, link_cmp.name))
        else:
            snd = [x for x in SIM_all_objects()
                   if (instance_of(x, "service-node-device")
                       and x.service_node == sn
                       and x.ip_address == sn_ip
                       and x.netmask == "255.255.255.0")][0]
            snd.link = link
            print ("Connecting device '%s' to '%s'"
                   % (snd.name, link.name))
    return (link, sn)