def get_nodes_links(slice, chosen_group=None):
    nodes = []
    links = []

    agg_ids = []
    id_to_idx = {}

    vt_aggs = get_vm_aggregates(slice)

    # Getting image for the nodes
    # FIXME: avoid to ask the user for the complete name of the method here! he should NOT know it
    try:
        image_url = reverse('img_media_vt_plugin', args=("server-tiny.png",))
    except:
        image_url = 'server-tiny.png'

    # For every Virtualization AM
    for i, agg in enumerate(vt_aggs):
        agg_ids.append(agg.pk)
        vt_servers = VTServer.objects.filter(
            aggregate__pk=agg.pk,
            available=True,
        )

        serverInSameIsland = False

        # For every server of the Virtualization AM
        for n in vt_servers:
            vmNames = []
            for name in  n.vms.all().filter(sliceId = slice.uuid).values_list('name', flat=True):
                vmNames.append(str(name))
            vmInterfaces = []
            j=1 #FIXME XXX: eth0 is mgmt 
            for inter in n.getNetworkInterfaces():
                inter = inter[1] #WTF: why QuerySet is not iterable straight away, and have to wrap it via enumerate
                if not inter.isMgmt:
                    vmInterfaces.append(dict(name="eth"+str(j), switch=str(inter.switchID), port=str(inter.port)))
                    j+=1
            nodes.append(Node(name = n.name, value = n.id, description = get_node_description(n, vmNames, vmInterfaces),
                              type = "Virtualized server", image = image_url, aggregate = agg,
                              # Extra parameters for VM nodes (will be used to show connections to switches)
                              vmNames = vmNames, vmInterfaces = vmInterfaces
                             )
                        )

            # For every interface of the server
            for j,inter in enumerate(n.ifaces.filter(isMgmt = False)):
                #first check datapathId exists.
                try:
                    switch_id = PluginCommunicator.get_object_id(slice, "openflow", "OpenFlowSwitch", name=inter.switchID)
                    port_id = PluginCommunicator.get_object_id(slice,"openflow", "OpenFlowInterface", switch=switch_id, port_num=inter.port)
                except Exception as e:
#                    print "[WARNING] Problem retrieving links insde plugin 'vt_plugin'. Details: %s" % str(e)
                    continue
                links.append(Link(source = str(n.id), target = str(switch_id),
                                 value = "rsc_id_" + str(port_id) + "-" + str(inter.ifaceName) + ":" + str(inter.port)
                                 )
                            )
    return [nodes, links]
    def __init__(self, *args, **kwargs):
        # XXX Critical: remove 'slice' from kwargs before calling parent constructor
        # Otherwise signature method will changeand problems will arise!
        self.slice = kwargs.pop('slice', None)
        super(OpenFlowSliceControllerForm, self).__init__(*args, **kwargs)
        slice_vms = None
        # Note: instance (or OpenFlowSliceInfo) is not initially filled.
        # When no controller has been set yet, the dropdown is empty...
        try:
            slice_vms = PluginCommunicator.get_objects(self.slice, "vt_plugin", "VM", sliceId=self.slice.uuid)
        except Exception as e:
            print "OpenFlow plug-in: cannot find list of VMs. Exception: %s" % str(e)
        
        # Fill with initial info
        try:
            controller_address = self.instance.__dict__["controller_url"].split(":")
            self.fields["controller_protocol"].initial = controller_address[0]
            self.fields["controller_ip"].initial = controller_address[1]
            try:
                # Obtain index for VM in dropdown list through the slice controller IP
                chosen_controller_pk = slice_vms.get(ifaces__ip=controller_address[1]).id
                self.fields["slice_vms"].initial = chosen_controller_pk
            except:
                pass
            self.fields["controller_port"].initial = controller_address[2]
        except:
            pass
        # Fill dropdown with data from the database. Call PluginCommunicator to get VMs
        if slice_vms:
            self.fields["slice_vms"].queryset = slice_vms
#            self.fields["controller_ip"].widget = forms.HiddenInput()
        # If there are no VMs in the slice, there is no need to show the dropdown
        else:
            self.fields["slice_vms"].widget = forms.HiddenInput()
    def clean(self):
        # Manual IP by default. Check later on if user chose from the dropdown list
        controller_ip = ""
        try:
            controller_ip = self.cleaned_data["controller_ip"]
        except Exception as e:
           print "OpenFlow plug-in: user-seletected IP could not be retrieved. Exception: %s" % str(e)

        if not controller_ip:
            try:
                # Obtain IP from VM selected in the dropdown
                controller_vm = PluginCommunicator.get_object(self.slice, "vt_plugin", "VM", pk=self.cleaned_data["slice_vms"])
                controller_ip = controller_vm.ifaces.all().exclude(ip__isnull=True)[0].ip
            except Exception as e:
                print "OpenFlow plug-in: cannot retrieve IP from selected VM. Exception: %s" % str(e)
                raise ValidationError(u"Could not obtain IP from controller.")
       
        if "controller_port" in self.cleaned_data and "controller_protocol" in self.cleaned_data and controller_ip:
            self.cleaned_data["controller_url"] = "%s:%s:%s" % (self.cleaned_data["controller_protocol"], controller_ip, self.cleaned_data["controller_port"])
            if not self.controller_is_unique(self.cleaned_data["controller_url"]):
                raise ValidationError(u"Controller '%s:%s' is already being used. Choose another." % (controller_ip, self.cleaned_data["controller_port"]))
            # Remove data that will not be saved into the model
            #keys_to_pop = filter((lambda key: key  != "controller_url"), self.cleaned_data.keys())
            #[ self.cleaned_data.pop(key) for key in keys_to_pop ]
            # XXX Important. Update controller_url field in the model, by changing the instance
            self.instance.controller_url = str(self.cleaned_data["controller_url"])
        else:
            raise ValidationError(u"Could not obtain a valid controller address. Ensure your input is correct.")
        return self.cleaned_data
Exemple #4
0
    def __init__(self, *args, **kwargs):
        # XXX Critical: remove 'slice' from kwargs before calling parent constructor
        # Otherwise signature method will changeand problems will arise!
        self.slice = kwargs.pop('slice', None)
        super(OpenFlowSliceControllerForm, self).__init__(*args, **kwargs)
        slice_vms = None
        # Note: instance (or OpenFlowSliceInfo) is not initially filled.
        # When no controller has been set yet, the dropdown is empty...
        try:
            slice_vms = PluginCommunicator.get_objects(self.slice, "vt_plugin", "VM", sliceId=self.slice.uuid)
        except Exception as e:
            print "OpenFlow plug-in: cannot find list of VMs. Exception: %s" % str(e)
        
        # Fill with initial info
        try:
            controller_address = self.instance.__dict__["controller_url"].split(":")
            self.fields["controller_protocol"].initial = controller_address[0]
            self.fields["controller_ip"].initial = controller_address[1]
            try:
                # Obtain index for VM in dropdown list through the slice controller IP
                chosen_controller_pk = slice_vms.get(ifaces__ip=controller_address[1]).id
                self.fields["slice_vms"].initial = chosen_controller_pk
            except:
                pass
            self.fields["controller_port"].initial = controller_address[2]
        except:
            pass
        # Fill dropdown with data from the database. Call PluginCommunicator to get VMs
        if slice_vms:
            self.fields["slice_vms"].queryset = slice_vms
#            self.fields["controller_ip"].widget = forms.HiddenInput()
        # If there are no VMs in the slice, there is no need to show the dropdown
        else:
            self.fields["slice_vms"].widget = forms.HiddenInput()
Exemple #5
0
    def clean(self):
        # Manual IP by default. Check later on if user chose from the dropdown list
        controller_ip = ""
        try:
            controller_ip = self.cleaned_data["controller_ip"]
        except Exception as e:
           print "OpenFlow plug-in: user-seletected IP could not be retrieved. Exception: %s" % str(e)

        if not controller_ip:
            try:
                # Obtain IP from VM selected in the dropdown
                controller_vm = PluginCommunicator.get_object(self.slice, "vt_plugin", "VM", pk=self.cleaned_data["slice_vms"])
                controller_ip = controller_vm.ifaces.all().exclude(ip__isnull=True)[0].ip
            except Exception as e:
                print "OpenFlow plug-in: cannot retrieve IP from selected VM. Exception: %s" % str(e)
                raise ValidationError(u"Could not obtain IP from controller.")
       
        if "controller_port" in self.cleaned_data and "controller_protocol" in self.cleaned_data and controller_ip:
            self.cleaned_data["controller_url"] = "%s:%s:%s" % (self.cleaned_data["controller_protocol"], controller_ip, self.cleaned_data["controller_port"])
            if not self.controller_is_unique(self.cleaned_data["controller_url"]):
                raise ValidationError(u"Controller '%s:%s' is already being used. Choose another." % (controller_ip, self.cleaned_data["controller_port"]))
            # Remove data that will not be saved into the model
            #keys_to_pop = filter((lambda key: key  != "controller_url"), self.cleaned_data.keys())
            #[ self.cleaned_data.pop(key) for key in keys_to_pop ]
            # XXX Important. Update controller_url field in the model, by changing the instance
            self.instance.controller_url = str(self.cleaned_data["controller_url"])
        else:
            raise ValidationError(u"Could not obtain a valid controller address. Ensure your input is correct.")
        return self.cleaned_data
def get_nodes_links(slice, chosen_group=None):
    nodes = []
    links = []

    agg_ids = []
    id_to_idx = {}

    vt_aggs = get_vm_aggregates(slice)

    # Getting image for the nodes
    # FIXME: avoid to ask the user for the complete name of the method here! he should NOT know it
    try:
        image_url = reverse('img_media_vt_plugin', args=("server-tiny.png", ))
    except:
        image_url = 'server-tiny.png'

    # For every Virtualization AM
    for i, agg in enumerate(vt_aggs):
        agg_ids.append(agg.pk)
        vt_servers = VTServer.objects.filter(
            aggregate__pk=agg.pk,
            available=True,
        )

        serverInSameIsland = False

        # For every server of the Virtualization AM
        for n in vt_servers:
            vmNames = []
            for name in n.vms.all().filter(sliceId=slice.uuid).values_list(
                    'name', flat=True):
                vmNames.append(str(name))
            vmInterfaces = []
            j = 1  #FIXME XXX: eth0 is mgmt
            for inter in n.getNetworkInterfaces():
                inter = inter[
                    1]  #WTF: why QuerySet is not iterable straight away, and have to wrap it via enumerate
                if not inter.isMgmt:
                    vmInterfaces.append(
                        dict(name="eth" + str(j),
                             switch=str(inter.switchID),
                             port=str(inter.port)))
                    j += 1
            nodes.append(
                Node(
                    name=n.name,
                    value=n.id,
                    description=get_node_description(n, vmNames, vmInterfaces),
                    type="Virtualized server",
                    image=image_url,
                    aggregate=agg,
                    # Extra parameters for VM nodes (will be used to show connections to switches)
                    vmNames=vmNames,
                    vmInterfaces=vmInterfaces))

            # For every interface of the server
            for j, inter in enumerate(n.ifaces.filter(isMgmt=False)):
                #first check datapathId exists.
                try:
                    switch_id = PluginCommunicator.get_object_id(
                        slice,
                        "openflow",
                        "OpenFlowSwitch",
                        name=inter.switchID)
                    port_id = PluginCommunicator.get_object_id(
                        slice,
                        "openflow",
                        "OpenFlowInterface",
                        switch=switch_id,
                        port_num=inter.port)
                except Exception as e:
                    #                    print "[WARNING] Problem retrieving links insde plugin 'vt_plugin'. Details: %s" % str(e)
                    continue
                links.append(
                    Link(source=str(n.id),
                         target=str(switch_id),
                         value="rsc_id_" + str(port_id) + "-" +
                         str(inter.ifaceName) + ":" + str(inter.port)))
    return [nodes, links]