Ejemplo n.º 1
0
    def delete_snat_vn(self, si_obj):
        vn_name = '%s_%s' % (svc_info.get_snat_left_vn_prefix(),
                             si_obj.name)
        vn_fq_name = si_obj.fq_name[:-1] + [vn_name]
        try:
            vn_obj = self._vnc_lib.virtual_network_read(fq_name=vn_fq_name)
        except NoIdError:
            return

        vn = VirtualNetworkSM.get(vn_obj.uuid)
        if not vn:
            return

        for vmi_id in vn.virtual_machine_interfaces:
            try:
                self._vnc_lib.ref_update('virtual-machine-interface',
                    vmi_id, 'virtual-network', vn.uuid, None, 'DELETE')
            except NoIdError:
                pass

        for iip_id in vn.instance_ips:
            try:
                self._vnc_lib.instance_ip_delete(id=iip_id)
            except NoIdError:
                pass

        try:
            self._vnc_lib.virtual_network_delete(id=vn.uuid)
        except (RefsExistError, NoIdError):
            pass
    def sandesh_si_handle_request(self, req):
        si_resp = sandesh.ServiceInstanceListResp(si_names=[])
        for si in ServiceInstanceSM.values():
            if req.si_name and req.si_name != si.name:
                continue

            st = ServiceTemplateSM.get(si.service_template)
            sandesh_si = sandesh.ServiceInstance(
                name=(':').join(si.fq_name),
                si_type=st.virtualization_type,
                si_state=si.state)

            sandesh_vm_list = []
            for vm_id in si.virtual_machines:
                vm = VirtualMachineSM.get(vm_id)
                if not vm:
                    continue
                vm_str = ("%s: %s" % (vm.name, vm.uuid))

                vr_name = 'None'
                vr = VirtualRouterSM.get(vm.virtual_router)
                if vr:
                    vr_name = vr.name

                ha_str = "active"
                if vm.index < len(si.local_preference):
                    if vm.index >= 0:
                        ha = si.local_preference[vm.index]
                        if ha and int(ha) == svc_info.get_standby_preference():
                            ha_str = "standby"
                        if ha:
                            ha_str = ha_str + ': ' + str(ha)
                    else:
                        ha_str = "unknown"

                vm = sandesh.ServiceInstanceVM(name=vm_str,
                                               vr_name=vr_name,
                                               ha=ha_str)
                sandesh_vm_list.append(vm)
            sandesh_si.vm_list = list(sandesh_vm_list)

            for nic in si.vn_info:
                vn = VirtualNetworkSM.get(nic['net-id'])
                if not vn:
                    continue
                if nic['type'] == svc_info.get_left_if_str():
                    sandesh_si.left_vn = [vn.name, vn.uuid]
                if nic['type'] == svc_info.get_right_if_str():
                    sandesh_si.right_vn = [vn.name, vn.uuid]
                if nic['type'] == svc_info.get_management_if_str():
                    sandesh_si.management_vn = [vn.name, vn.uuid]

            si_resp.si_names.append(sandesh_si)

        si_resp.response(req.context())
Ejemplo n.º 3
0
    def sandesh_si_handle_request(self, req):
        si_resp = sandesh.ServiceInstanceListResp(si_names=[])
        for si in ServiceInstanceSM.values():
            if req.si_name and req.si_name != si.name:
                continue

            st = ServiceTemplateSM.get(si.service_template)
            sandesh_si = sandesh.ServiceInstance(
                name=(':').join(si.fq_name), si_type=st.virtualization_type,
                si_state=si.state)

            sandesh_vm_list = []
            for vm_id in si.virtual_machines:
                vm = VirtualMachineSM.get(vm_id)
                if not vm:
                    continue
                vm_str = ("%s: %s" % (vm.name, vm.uuid))

                vr_name = 'None'
                vr = VirtualRouterSM.get(vm.virtual_router)
                if vr:
                    vr_name = vr.name

                ha_str = "active"
                if vm.index < len(si.local_preference):
                    if vm.index >= 0:
                        ha = si.local_preference[vm.index]
                        if ha and int(ha) == svc_info.get_standby_preference():
                            ha_str = "standby"
                        if ha:
                            ha_str = ha_str + ': ' + str(ha)
                    else:
                        ha_str = "unknown"

                vm = sandesh.ServiceInstanceVM(
                        name=vm_str, vr_name=vr_name, ha=ha_str)
                sandesh_vm_list.append(vm)
            sandesh_si.vm_list = list(sandesh_vm_list)

            for nic in si.vn_info:
                vn = VirtualNetworkSM.get(nic['net-id'])
                if not vn:
                    continue
                if nic['type'] == svc_info.get_left_if_str():
                    sandesh_si.left_vn = [vn.name, vn.uuid]
                if nic['type'] == svc_info.get_right_if_str():
                    sandesh_si.right_vn = [vn.name, vn.uuid]
                if nic['type'] == svc_info.get_management_if_str():
                    sandesh_si.management_vn = [vn.name, vn.uuid]

            si_resp.si_names.append(sandesh_si)

        si_resp.response(req.context())
Ejemplo n.º 4
0
    def delete_snat_vn(self, si_obj):
        vn_name = '%s_%s' % (svc_info.get_snat_left_vn_prefix(), si_obj.name)
        vn_fq_name = si_obj.fq_name[:-1] + [vn_name]
        try:
            vn_obj = self._vnc_lib.virtual_network_read(fq_name=vn_fq_name)
        except vnc_exc.NoIdError:

            self.logger.debug("Unable to find virtual network %s. " \
                              "Delete of SNAT instance %s failed." % \
                              (vn_name, si_obj.name))
            return

        vn = VirtualNetworkSM.get(vn_obj.uuid)
        if not vn:
            return

        for vmi_id in vn.virtual_machine_interfaces:
            try:
                self._vnc_lib.ref_update('virtual-machine-interface', vmi_id,
                                         'virtual-network', vn.uuid, None,
                                         'DELETE')
            except vnc_exc.NoIdError:
                self.logger.debug( \
                    "Update of vnc lib for vmi %s virtual network %s failed" % \
                    (vmi_id, vn_name))
                pass

        for iip_id in vn.instance_ips:
            try:
                self._vnc_lib.instance_ip_delete(id=iip_id)
            except vnc_exc.NoIdError:
                self.logger.debug( \
                     "Unable to find IIP %s in virtual network %s." % \
                     (iip_id, vn_name))
                pass

        try:
            self._vnc_lib.virtual_network_delete(id=vn.uuid)
        except (vnc_exc.RefsExistError, vnc_exc.NoIdError):
            self.logger.debug("Delete of virtual network %s failed." % \
                              (vn_name))
            pass
Ejemplo n.º 5
0
    def delete_snat_vn(self, si_obj):
        vn_name = '%s_%s' % (svc_info.get_snat_left_vn_prefix(),
                             si_obj.name)
        vn_fq_name = si_obj.fq_name[:-1] + [vn_name]
        try:
            vn_obj = self._vnc_lib.virtual_network_read(fq_name=vn_fq_name)
        except vnc_exc.NoIdError:

            self.logger.debug("Unable to find virtual network %s. " \
                              "Delete of SNAT instance %s failed." % \
                              (vn_name, si_obj.name))
            return

        vn = VirtualNetworkSM.get(vn_obj.uuid)
        if not vn:
            return

        for vmi_id in vn.virtual_machine_interfaces:
            try:
                self._vnc_lib.ref_update('virtual-machine-interface',
                    vmi_id, 'virtual-network', vn.uuid, None, 'DELETE')
            except vnc_exc.NoIdError:
                self.logger.debug( \
                    "Update of vnc lib for vmi %s virtual network %s failed" % \
                    (vmi_id, vn_name))
                pass

        for iip_id in vn.instance_ips:
            try:
                self._vnc_lib.instance_ip_delete(id=iip_id)
            except vnc_exc.NoIdError:
                self.logger.debug( \
                     "Unable to find IIP %s in virtual network %s." % \
                     (iip_id, vn_name))
                pass

        try:
            self._vnc_lib.virtual_network_delete(id=vn.uuid)
        except (vnc_exc.RefsExistError, vnc_exc.NoIdError):
            self.logger.debug("Delete of virtual network %s failed." % \
                              (vn_name))
            pass
Ejemplo n.º 6
0
    def _add_snat_instance(self, router_obj):
        try:
            vnc_rtr_obj = self._vnc_lib.logical_router_read(id=router_obj.uuid)
        except vnc_exc.NoIdError:
            # msg="Unable to read logical router to set the default gateway")
            return

        # Get netns SNAT service template
        try:
            st_obj = self._vnc_lib.service_template_read(
                fq_name=SNAT_SERVICE_TEMPLATE_FQ_NAME)
        except vnc_exc.NoIdError:
            # msg="Unable to read template to set the default gateway")
            return

        # Get the service instance if it exists
        si_obj = None
        si_uuid = router_obj.service_instance
        if si_uuid:
            try:
                si_obj = self._vnc_lib.service_instance_read(id=si_uuid)
            except vnc_exc.NoIdError:
                pass

        # Get route table for default route it it exists
        rt_obj = self._get_route_table(router_obj)

        project_fq_name = router_obj.fq_name[:-1]
        # Set the service instance
        si_created = False
        if not si_obj:
            si_name = 'snat_' + router_obj.uuid + '_' + str(uuid.uuid4())
            si_obj = ServiceInstance(si_name)
            si_obj.fq_name = project_fq_name + [si_name]
            si_created = True
        si_prop_obj = ServiceInstanceType(scale_out=ServiceScaleOutType(
            max_instances=2, auto_scale=True),
                                          auto_policy=False)

        # set right interface in order of [right, left] to match template
        vn_left_fq_name = self._get_snat_vn(si_obj)
        left_if = ServiceInstanceInterfaceType(virtual_network=vn_left_fq_name)
        virtual_network = router_obj.virtual_network
        vn_obj = VirtualNetworkSM.get(virtual_network)
        right_if = ServiceInstanceInterfaceType(
            virtual_network=':'.join(vn_obj.fq_name))
        si_prop_obj.set_interface_list([right_if, left_if])
        si_prop_obj.set_ha_mode('active-standby')

        si_obj.set_service_instance_properties(si_prop_obj)
        si_obj.set_service_template(st_obj)

        if si_created:
            self._vnc_lib.service_instance_create(si_obj)
        else:
            self._vnc_lib.service_instance_update(si_obj)

        # Set the route table
        route_obj = RouteType(prefix="0.0.0.0/0",
                              next_hop=si_obj.get_fq_name_str())
        rt_created = False
        if not rt_obj:
            rt_name = 'rt_' + router_obj.uuid
            rt_obj = RouteTable(name=rt_name)
            rt_obj.fq_name = project_fq_name + [rt_name]
            rt_created = True
        rt_obj.set_routes(RouteTableType.factory([route_obj]))
        if rt_created:
            self._vnc_lib.route_table_create(rt_obj)
        else:
            self._vnc_lib.route_table_update(rt_obj)

        # Associate route table to logical router
        vnc_rtr_obj.add_route_table(rt_obj)

        # Add logical gateway virtual network
        vnc_rtr_obj.set_service_instance(si_obj)
        self._vnc_lib.logical_router_update(vnc_rtr_obj)
Ejemplo n.º 7
0
    def _add_snat_instance(self, router_obj):
        try:
            vnc_rtr_obj = self._vnc_lib.logical_router_read(id=router_obj.uuid)
        except vnc_exc.NoIdError:
            # msg="Unable to read logical router to set the default gateway")
            return

        try:
            project_obj = self._vnc_lib.project_read(id=router_obj.parent_uuid)
        except vnc_exc.NoIdError:
            # msg="Unable to read project to set the default gateway")
            return

        # Get netns SNAT service template
        try:
            st_obj = self._vnc_lib.service_template_read(
                fq_name=SNAT_SERVICE_TEMPLATE_FQ_NAME)
        except vnc_exc.NoIdError:
            # msg="Unable to read template to set the default gateway")
            return

        # Get the service instance if it exists
        si_obj = None
        si_uuid = router_obj.service_instance
        if si_uuid:
            try:
                si_obj = self._vnc_lib.service_instance_read(id=si_uuid)
            except vnc_exc.NoIdError:
                pass

        # Get route table for default route it it exists
        rt_obj = self._get_route_table(router_obj, project_obj)

        # Set the service instance
        si_created = False
        if not si_obj:
            si_name = 'snat_' + router_obj.uuid + '_' + str(uuid.uuid4())
            si_obj = ServiceInstance(si_name, parent_obj=project_obj)
            si_created = True
        si_prop_obj = ServiceInstanceType(
            scale_out=ServiceScaleOutType(max_instances=2,
                                          auto_scale=True),
            auto_policy=True)

        # set right interface in order of [right, left] to match template
        vn_left_fq_name = self._get_snat_vn(project_obj, si_obj)
        left_if = ServiceInstanceInterfaceType(virtual_network=vn_left_fq_name)
        virtual_network = router_obj.virtual_network
        vn_obj = VirtualNetworkSM.get(virtual_network)
        right_if = ServiceInstanceInterfaceType(
            virtual_network=':'.join(vn_obj.fq_name))
        si_prop_obj.set_interface_list([right_if, left_if])
        si_prop_obj.set_ha_mode('active-standby')

        si_obj.set_service_instance_properties(si_prop_obj)
        si_obj.set_service_template(st_obj)

        if si_created:
            self._vnc_lib.service_instance_create(si_obj)
        else:
            self._vnc_lib.service_instance_update(si_obj)

        # Set the route table
        route_obj = RouteType(prefix="0.0.0.0/0",
                              next_hop=si_obj.get_fq_name_str())
        rt_created = False
        if not rt_obj:
            rt_name = 'rt_' + router_obj.uuid
            rt_obj = RouteTable(name=rt_name, parent_obj=project_obj)
            rt_created = True
        rt_obj.set_routes(RouteTableType.factory([route_obj]))
        if rt_created:
            self._vnc_lib.route_table_create(rt_obj)
        else:
            self._vnc_lib.route_table_update(rt_obj)

        # Associate route table to all private networks connected onto
        # that router
        net_uuids = self._get_net_uuids(router_obj.virtual_machine_interfaces)
        self._add_route_tables(net_uuids, rt_obj)

        # Add logical gateway virtual network
        vnc_rtr_obj.set_service_instance(si_obj)
        self._vnc_lib.logical_router_update(vnc_rtr_obj)
Ejemplo n.º 8
0
    def _add_snat_instance(self, router_obj):
        try:
            vnc_rtr_obj = self._vnc_lib.logical_router_read(id=router_obj.uuid)
        except vnc_exc.NoIdError:
            self.logger.debug("Unable to find logical router for id %s. " \
                              "Add SNAT instance failed." % \
                              (router_obj.uuid))
            return

        # Get netns SNAT service template
        try:
            st_obj = self._vnc_lib.service_template_read(
                fq_name=SNAT_SERVICE_TEMPLATE_FQ_NAME)
        except vnc_exc.NoIdError:
            self.logger.debug( \
                "Unable to read service template %s. " \
                " Add SNAT instance failed." % \
                (SNAT_SERVICE_TEMPLATE_FQ_NAME))
            return

        # Get the service instance if it exists
        si_obj = None
        si_uuid = router_obj.service_instance
        if si_uuid:
            try:
                si_obj = self._vnc_lib.service_instance_read(id=si_uuid)
            except vnc_exc.NoIdError:
                self.logger.debug("Service instance %s not found." % (si_uuid))
                pass

        # Get route table for default route it it exists
        rt_obj = self._get_route_table(router_obj)

        project_fq_name = router_obj.fq_name[:-1]
        # Set the service instance
        si_created = False
        if not si_obj:
            si_name = 'snat_' + router_obj.uuid + '_' + str(uuid.uuid4())
            si_obj = ServiceInstance(si_name)
            si_obj.fq_name = project_fq_name + [si_name]
            si_created = True
        si_prop_obj = ServiceInstanceType(
            scale_out=ServiceScaleOutType(max_instances=2,
                                          auto_scale=True),
            auto_policy=False)

        # set right interface in order of [right, left] to match template
        vn_left_fq_name = self._get_snat_vn(si_obj)
        left_if = ServiceInstanceInterfaceType(virtual_network=vn_left_fq_name)
        virtual_network = router_obj.virtual_network
        vn_obj = VirtualNetworkSM.get(virtual_network)
        right_if = ServiceInstanceInterfaceType(
            virtual_network=':'.join(vn_obj.fq_name))
        si_prop_obj.set_interface_list([right_if, left_if])
        si_prop_obj.set_ha_mode('active-standby')

        si_obj.set_service_instance_properties(si_prop_obj)
        si_obj.set_service_template(st_obj)

        if si_created:
            self._vnc_lib.service_instance_create(si_obj)
        else:
            self._vnc_lib.service_instance_update(si_obj)

        # Set the route table
        route_obj = RouteType(prefix="0.0.0.0/0",
                              next_hop=si_obj.get_fq_name_str())
        rt_created = False
        if not rt_obj:
            rt_name = 'rt_' + router_obj.uuid
            rt_obj = RouteTable(name=rt_name)
            rt_obj.fq_name = project_fq_name + [rt_name]
            rt_created = True
        rt_obj.set_routes(RouteTableType.factory([route_obj]))
        if rt_created:
            self._vnc_lib.route_table_create(rt_obj)
        else:
            self._vnc_lib.route_table_update(rt_obj)

        # Associate route table to logical router
        vnc_rtr_obj.add_route_table(rt_obj)

        # Add logical gateway virtual network
        vnc_rtr_obj.set_service_instance(si_obj)
        self._vnc_lib.logical_router_update(vnc_rtr_obj)
Ejemplo n.º 9
0
    def add_snat_instance(self, router_obj):
        try:
            vnc_rtr_obj = self._vnc_lib.logical_router_read(id=router_obj.uuid)
        except vnc_exc.NoIdError:
            # msg="Unable to read logical router to set the default gateway")
            return

        try:
            project_obj = self._vnc_lib.project_read(id=router_obj.parent_uuid)
        except vnc_exc.NoIdError:
            # msg="Unable to read project to set the default gateway")
            return

        # Get netns SNAT service template
        try:
            st_obj = self._vnc_lib.service_template_read(
                fq_name=SNAT_SERVICE_TEMPLATE_FQ_NAME)
        except vnc_exc.NoIdError:
            # msg="Unable to read template to set the default gateway")
            return

        # Get the service instance if it exists
        si_name = 'si_' + router_obj.uuid
        si_fq_name = project_obj.fq_name + [si_name]
        try:
            si_obj = self._vnc_lib.service_instance_read(fq_name=si_fq_name)
            si_uuid = si_obj.uuid
        except vnc_exc.NoIdError:
            si_obj = None

        # Get route table for default route it it exists
        rt_name = 'rt_' + router_obj.uuid
        rt_fq_name = project_obj.fq_name + [rt_name]
        try:
            rt_obj = self._vnc_lib.route_table_read(fq_name=rt_fq_name)
            rt_uuid = rt_obj.uuid
        except vnc_exc.NoIdError:
            rt_obj = None

        # Set the service instance
        si_created = False
        if not si_obj:
            si_obj = ServiceInstance(si_name, parent_obj=project_obj)
            si_created = True
        si_prop_obj = ServiceInstanceType(scale_out=ServiceScaleOutType(
            max_instances=2, auto_scale=True),
                                          auto_policy=True)

        # set right interface in order of [right, left] to match template
        left_if = ServiceInstanceInterfaceType()
        virtual_network = router_obj.virtual_network
        vn_obj = VirtualNetworkSM.get(virtual_network)
        right_if = ServiceInstanceInterfaceType(
            virtual_network=':'.join(vn_obj.fq_name))
        si_prop_obj.set_interface_list([right_if, left_if])
        si_prop_obj.set_ha_mode('active-standby')

        si_obj.set_service_instance_properties(si_prop_obj)
        si_obj.set_service_template(st_obj)
        if si_created:
            si_uuid = self._vnc_lib.service_instance_create(si_obj)
        else:
            self._vnc_lib.service_instance_update(si_obj)

        # Set the route table
        route_obj = RouteType(prefix="0.0.0.0/0",
                              next_hop=si_obj.get_fq_name_str())
        rt_created = False
        if not rt_obj:
            rt_obj = RouteTable(name=rt_name, parent_obj=project_obj)
            rt_created = True
        rt_obj.set_routes(RouteTableType.factory([route_obj]))
        if rt_created:
            rt_uuid = self._vnc_lib.route_table_create(rt_obj)
        else:
            self._vnc_lib.route_table_update(rt_obj)

        # Associate route table to all private networks connected onto
        # that router
        for intf in router_obj.virtual_machine_interfaces or []:
            vmi_obj = VirtualMachineInterfaceSM.locate(intf)
            net_id = vmi_obj.virtual_network
            try:
                net_obj = self._vnc_lib.virtual_network_read(id=net_id)
            except vnc_exc.NoIdError:
                continue
            net_obj.set_route_table(rt_obj)
            self._vnc_lib.virtual_network_update(net_obj)

        # Add logical gateway virtual network
        vnc_rtr_obj.set_service_instance(si_obj)
        self._vnc_lib.logical_router_update(vnc_rtr_obj)