¡@

Home 

OpenStack Study: router_operations.py

OpenStack Index

**** CubicPower OpenStack Study ****

def handler(event, handler):

    def wrap(f):

        if event not in handler.keys():

            new_func_list = [f]

            handler[event] = new_func_list

        else:

            handler[event].append(f)

        @wraps(f)

        def wrapped_f(*args, **kwargs):

            return f(*args, **kwargs)

        return wrapped_f

    return wrap

@handler(p_con.Events.CREATE_ROUTER, handlers)

**** CubicPower OpenStack Study ****

def _create_dva_and_assign_address(api, tenant_id, neutron_router,

                                   flavor, utif_info=None,

                                   ip_allocation_info=None):

    """Creates a new router, and assign the gateway interface if any."""

    dva = api.create_router(tenant_id=tenant_id,

                            router_id=neutron_router["id"],

                            name=neutron_router["name"],

                            flavor=flavor,

                            up=neutron_router["admin_state_up"])

    try:

        if utif_info:

            api.grow_interface(utif_info, neutron_router["admin_state_up"],

                               tenant_id, neutron_router["id"])

        if ip_allocation_info:

            dva = api.allocate_address(neutron_router["id"],

                                       neutron_router["admin_state_up"],

                                       ip_allocation_info)

    except h_exc.PreliminaryOperationsFailed as ex:

        raise h_exc.BrokenInterface(err_msg=ex.message)

    state = api.extract_dva_state(dva)

    return state

@handler(p_con.Events.UPDATE_ROUTER, handlers)

**** CubicPower OpenStack Study ****

def _update_dva_and_assign_address(api, tenant_id, neutron_router,

                                   utif_info=None, ip_allocation_info=None,

                                   routes_info=[]):

    name = neutron_router["name"]

    up = neutron_router["admin_state_up"]

    r_id = neutron_router["id"]

    if ip_allocation_info or routes_info:

        up = True

    dva = api.update_dva(tenant_id=tenant_id, router_id=r_id, name=name,

                         up=up, utif_info=utif_info)

    if ip_allocation_info:

        api.allocate_address(r_id, up, ip_allocation_info)

    if routes_info:

        api.delete_extra_routes(r_id, up)

        api.set_extra_routes(r_id, neutron_router["admin_state_up"],

                             routes_info)

    return api.extract_dva_state(dva)

@handler(p_con.Events.DELETE_ROUTER, handlers)

**** CubicPower OpenStack Study ****

def _delete_dva(api, tenant_id, neutron_router):

    try:

        api.delete_dva(tenant_id, neutron_router["id"])

    except h_exc.DvaNotFound:

        LOG.warning(_("The router %s had no physical representation,"

                      "likely already deleted"), neutron_router["id"])

    return p_con.Status.DELETED

@handler(p_con.Events.GROW_ROUTER_IF, handlers)

**** CubicPower OpenStack Study ****

def _grow_dva_iface_and_assign_address(api, tenant_id, neutron_router,

                                       utif_info=None,

                                       ip_allocation_info=None):

    try:

        dva = api.grow_interface(utif_info, neutron_router["admin_state_up"],

                                 tenant_id, neutron_router["id"])

        if ip_allocation_info:

            dva = api.allocate_address(neutron_router["id"],

                                       neutron_router["admin_state_up"],

                                       ip_allocation_info)

    except h_exc.PreliminaryOperationsFailed as ex:

        raise h_exc.BrokenInterface(err_msg=ex.message)

    state = api.extract_dva_state(dva)

    return state

@handler(p_con.Events.SHRINK_ROUTER_IF, handlers)

**** CubicPower OpenStack Study ****

def _shrink_dva_iface(api, tenant_id, neutron_router, port_id):

    try:

        dva = api.shrink_interface(tenant_id, neutron_router["id"],

                                   neutron_router["admin_state_up"], port_id)

    except h_exc.InterfaceNotFound:

        LOG.warning(_("Interface %s not found in the heleos back-end,"

                      "likely already deleted"), port_id)

        return (p_con.Status.ACTIVE if neutron_router["admin_state_up"] else

                p_con.Status.READY)

    except h_exc.PreliminaryOperationsFailed as ex:

        raise h_exc.BrokenInterface(err_msg=ex.message)

    state = api.extract_dva_state(dva)

    return state

@handler(p_con.Events.SET_NAT_RULE, handlers)

**** CubicPower OpenStack Study ****

def _create_nat_rule(api, tenant_id, neutron_router, nat_info=None):

    dva = api.create_nat_entry(neutron_router["id"],

                               neutron_router["admin_state_up"], nat_info)

    state = api.extract_dva_state(dva)

    return state

@handler(p_con.Events.RESET_NAT_RULE, handlers)

**** CubicPower OpenStack Study ****

def _delete_nat_rule(api, tenant_id, neutron_router, floating_ip_id):

    dva = api.remove_nat_entry(neutron_router["id"],

                               neutron_router["admin_state_up"],

                               floating_ip_id)

    state = api.extract_dva_state(dva)

    return state