def _delete_lrouter(self, context, id):
        if not self._is_advanced_service_router(context, id):
            super(NvpAdvancedPlugin, self)._delete_lrouter(context, id)
            if id in self._router_type:
                del self._router_type[id]
            return

        binding = vcns_db.get_vcns_router_binding(context.session, id)
        if binding:
            vcns_db.update_vcns_router_binding(
                context.session, id, status=service_constants.PENDING_DELETE)

            lswitch_id = binding['lswitch_id']
            edge_id = binding['edge_id']

            # delete lswitch
            try:
                self.vcns_driver.delete_lswitch(lswitch_id)
            except exceptions.ResourceNotFound:
                LOG.warning(_("Did not found lswitch %s in NVP"), lswitch_id)

            # delete edge
            jobdata = {
                'context': context
            }
            self.vcns_driver.delete_edge(id, edge_id, jobdata=jobdata)

        # delete LR
        nvplib.delete_lrouter(self.cluster, id)
        if id in self._router_type:
            del self._router_type[id]
 def edge_deploy_result(self, task):
     """callback when deployment task finished."""
     jobdata = task.userdata['jobdata']
     lrouter = jobdata['lrouter']
     context = jobdata['context']
     name = task.userdata['router_name']
     router_db = self.plugin._get_router(context, lrouter['uuid'])
     if task.status == TaskStatus.COMPLETED:
         LOG.debug(_("Successfully deployed %(edge_id)s for "
                     "router %(name)s"), {
                         'edge_id': task.userdata['edge_id'],
                         'name': name})
         if router_db['status'] == service_constants.PENDING_CREATE:
             router_db['status'] = service_constants.ACTIVE
             binding = vcns_db.get_vcns_router_binding(
                 context.session, lrouter['uuid'])
             # only update status to active if its status is pending create
             if binding['status'] == service_constants.PENDING_CREATE:
                 vcns_db.update_vcns_router_binding(
                     context.session, lrouter['uuid'],
                     status=service_constants.ACTIVE)
     else:
         LOG.debug(_("Failed to deploy Edge for router %s"), name)
         router_db['status'] = service_constants.ERROR
         vcns_db.update_vcns_router_binding(
             context.session, lrouter['uuid'],
             status=service_constants.ERROR)
 def _update_nat_rules(self, context, router):
     snat, dnat = self._get_nat_rules(context, router)
     binding = vcns_db.get_vcns_router_binding(context.session,
                                               router['id'])
     self.vcns_driver.update_nat_rules(router['id'],
                                       binding['edge_id'],
                                       snat, dnat)
    def _get_vse_status(self, context, id):
        binding = vcns_db.get_vcns_router_binding(context.session, id)

        edge_status_level = self.vcns_driver.get_edge_status(
            binding['edge_id'])
        edge_db_status_level = ROUTER_STATUS_LEVEL[binding.status]

        if edge_status_level > edge_db_status_level:
            return edge_status_level
        else:
            return edge_db_status_level
    def _update_router_gw_info(self, context, router_id, info):
        if not self._is_advanced_service_router(context, router_id):
            super(NvpAdvancedPlugin, self)._update_router_gw_info(
                context, router_id, info)
            return

        # get original gw_port config
        router = self._get_router(context, router_id)
        org_ext_net_id = router.gw_port_id and router.gw_port.network_id
        org_enable_snat = router.enable_snat
        orgaddr, orgmask, orgnexthop = self._get_external_attachment_info(
            context, router)

        super(NeutronPlugin.NvpPluginV2, self)._update_router_gw_info(
            context, router_id, info, router=router)

        new_ext_net_id = router.gw_port_id and router.gw_port.network_id
        new_enable_snat = router.enable_snat
        newaddr, newmask, newnexthop = self._get_external_attachment_info(
            context, router)

        binding = vcns_db.get_vcns_router_binding(context.session, router_id)

        if new_ext_net_id != org_ext_net_id and orgnexthop:
            # network changed, need to remove default gateway before vnic
            # can be configured
            LOG.debug(_("VCNS: delete default gateway %s"), orgnexthop)
            self._vcns_update_static_routes(context,
                                            router=router,
                                            edge_id=binding['edge_id'],
                                            nexthop=None)

        if orgaddr != newaddr or orgmask != newmask:
            self.vcns_driver.update_interface(
                router_id, binding['edge_id'],
                vcns_const.EXTERNAL_VNIC_INDEX,
                self.vcns_driver.external_network,
                newaddr, newmask)

        if orgnexthop != newnexthop:
            self._vcns_update_static_routes(context,
                                            router=router,
                                            edge_id=binding['edge_id'],
                                            nexthop=newnexthop)

        if (new_ext_net_id == org_ext_net_id and
            org_enable_snat == new_enable_snat):
            return

        self._update_nat_rules(context, router)
    def _update_interface(self, context, router):
        addr, mask, nexthop = self._get_external_attachment_info(
            context, router)

        secondary = []
        fip_qry = context.session.query(l3_db.FloatingIP)
        fip_db = fip_qry.filter_by(router_id=router['id']).all()
        for fip in fip_db:
            if fip.fixed_port_id:
                secondary.append(fip.floating_ip_address)

        binding = vcns_db.get_vcns_router_binding(context.session,
                                                  router['id'])
        self.vcns_driver.update_interface(
            router['id'], binding['edge_id'],
            vcns_const.EXTERNAL_VNIC_INDEX,
            self.vcns_driver.external_network,
            addr, mask, secondary=secondary)
    def _vcns_update_static_routes(self, context, **kwargs):
        router = kwargs.get('router')
        if router is None:
            router = self._get_router(context, kwargs['router_id'])

        edge_id = kwargs.get('edge_id')
        if edge_id is None:
            binding = vcns_db.get_vcns_router_binding(context.session,
                                                      router['id'])
            edge_id = binding['edge_id']

        skippable = True
        if 'nexthop' in kwargs:
            nexthop = kwargs['nexthop']
            # The default gateway and vnic config has dependencies, if we
            # explicitly specify nexthop to change, tell the driver not to
            # skip this route update
            skippable = False
        else:
            nexthop = self._get_external_gateway_address(context,
                                                         router)

        if 'subnets' in kwargs:
            subnets = kwargs['subnets']
        else:
            subnets = self._find_router_subnets_cidrs(context.elevated(),
                                                      router['id'])

        routes = []
        for subnet in subnets:
            routes.append({
                'cidr': subnet,
                'nexthop': vcns_const.INTEGRATION_LR_IPADDRESS.split('/')[0]
            })
        self.vcns_driver.update_routes(router['id'], edge_id, nexthop, routes,
                                       skippable)