Example #1
0
 def rest_call(self, url, method, data=None, params=None):
     code, result = self._rest_request(method, url, data, params)
     if code == requests.codes.unauthorized:
         self.renew_token()
         code, result = self._rest_request(method, url, data, params)
         if code == requests.codes.unauthorized:
             raise exceptions.HttpNotAuthError()
     # Because data can be deleted on VCFC ui.
     # We return 204 for such situation.
     if code == requests.codes.not_found:
         if method == "DELETE":
             code = requests.codes.no_content
         elif method != 'GET':
             code = requests.codes.all_good
     if method != 'GET' and not (requests.codes.ok <= code <
                                 requests.codes.multiple_choices):
         msg = (_LE("Failed %(method)s operation on %(url)s "
                    "status code: %(code)s") % {
                        "method": method,
                        "url": url,
                        "code": code
                    })
         LOG.exception(msg)
         raise q_exc.BadRequest(resource=method,
                                msg="invalid response status code!!")
     return code, result
    def create_floatingip(self, context, floatingip, **kwargs):
        LOG.debug("VCFC create_floatingip called")
        LOG.debug("ctx = %s", context.__dict__)
        LOG.debug("floating ip from north is %s", floatingip)

        floatingip_neutron = super(H3CL3RouterPlugin, self). \
            create_floatingip(context, floatingip)
        floatingip_vcfc = copy.deepcopy(floatingip_neutron)
        LOG.debug(" floatingip content from db is %s", floatingip_neutron)

        if 'fixed_ip_address' in floatingip_vcfc:
            del floatingip_vcfc['fixed_ip_address']

        resource_path = h_const.FLOATINGIPS_RESOURCE
        request_dic = {"floatingips": [floatingip_vcfc]}
        LOG.debug("create floating ip into vcfc is %s", request_dic)
        try:
            response_status, resp_body = self.client.rest_call(
                resource_path, "POST", request_dic)
        except Exception as e:
            with excutils.save_and_reraise_exception():
                LOG.exception(_LE("H3C failed to create floating ip: %s"), e)
                super(H3CL3RouterPlugin,
                      self).delete_floatingip(context,
                                              floatingip_neutron['id'])
        LOG.debug("after create floating ip "
                  "the resp_body from vcfc is %s", resp_body)
        LOG.debug(
            "after create floating ip "
            "the resp_status from vcfc is %s", response_status)

        LOG.debug("floatingip resp_body content from vcfc is %s", resp_body)
        LOG.debug("VCFC create_floatingip exited")
        return floatingip_neutron
Example #3
0
 def _parse_l3_vni_ranges(self):
     try:
         self.l3_vni_ranges = self.parse_l3_vni_ranges(
             cfg.CONF.VCFCONTROLLER.l3_vni_ranges)
     except Exception as e:
         LOG.exception(
             _LE("Failed to parse l3_vni_ranges. "
                 "Service terminated!: %s"), e)
         sys.exit(1)
     LOG.info(_("L3 vni ranges: %s"), self.l3_vni_ranges)
Example #4
0
 def rest_call(self, path, method, body=None, params=None):
     url = self.urljoin(self.base_url, path)
     data = jsonutils.dumps(body) if body is not None else None
     if hasattr(self, 'json_logger'):
         try:
             msg = "%s : %s\n\n%s\n\n\n\n\n\n" % (
                 method, url, json.dumps(body, indent=4))
             self.json_logger.debug(msg)
         except Exception as e:
             LOG.error(_LE("Failed to write json log, error is %s"), e)
     return self.client.rest_call(url, method, data, params)
Example #5
0
 def __init__(self):
     self.base_url = cfg.CONF.VCFCONTROLLER.url
     self.output_json_log = cfg.CONF.VCFCONTROLLER.output_json_log
     self.white_list = cfg.CONF.VCFCONTROLLER.white_list
     if self.white_list is True:
         self.client = RestClientNoAuth()
     else:
         self.client = RestClientTokenAuth()
     if self.output_json_log is True:
         try:
             self.json_logger = self.set_json_logger()
         except Exception as e:
             LOG.error(_LE("Failed to open json logger, error is %s"), e)
    def get_vds_id_from_vcfc(self, vds_name):
        """Get vds from vcfc.

        :param vds_name: used for filter vds
        """
        vds_uuid = None
        get_path = "%s?name=%s" % (h_const.VDS_RESOURCE, vds_name)
        try:
            resp_status, resp_dict = self.client.rest_call(get_path, 'GET')
            vds_uuid = resp_dict['vds'][0]['uuid']
        except Exception as e:
            LOG.error(_LE("H3C driver get vds id exception: %s"), e)
        LOG.debug("H3C driver get vds id %s by vdsname %s", vds_uuid, vds_name)
        return vds_uuid
Example #7
0
 def __init__(self):
     self.base_url = cfg.CONF.VCFCONTROLLER.url
     self.auth_payload = {
         "login": {
             'user': cfg.CONF.VCFCONTROLLER.username,
             'password': cfg.CONF.VCFCONTROLLER.password,
             'domain': cfg.CONF.VCFCONTROLLER.domain
         }
     }
     self.timeout = cfg.CONF.VCFCONTROLLER.timeout
     self.retry = cfg.CONF.VCFCONTROLLER.retry
     self.token = None
     try:
         self.renew_token(0)
     except Exception as e:
         LOG.error(_LE("initialize token error: %s"), e)
Example #8
0
 def _rest_request(self, method, url, data, params, timeout=None):
     if timeout is None:
         timeout = float(self.timeout)
     retry = self.retry
     resp = None
     result = None
     headers = {
         'Content-Type': 'application/json',
         'Accept': 'application/json',
         'Cache-Control': 'no-cache',
         'X-Auth-Token': self.token
     }
     while True:
         try:
             resp = requests.request(method,
                                     url,
                                     headers=headers,
                                     data=data,
                                     params=params,
                                     timeout=timeout,
                                     verify=False)
             break
         except Exception as e:
             with excutils.save_and_reraise_exception(
                     reraise=False) as ctxt:
                 LOG.error(
                     _LE("Exception %(exc)s: VCFC exception, "
                         "traceback: %(tb)s"), {
                             'exc': e,
                             'tb': traceback.format_exc()
                         })
                 retry -= 1
                 if retry < 0:
                     ctxt.reraise = True
     code = resp.status_code
     if method != "DELETE" and code != requests.codes.not_found:
         try:
             result = resp.json()
         except Exception:
             raise exceptions.JsonDecodingError(resource="%s %s" %
                                                (method, url))
     return code, result
    def update_router(self, context, rid, router):
        LOG.debug("VCFC update_router called")
        LOG.debug("router id from north is %s", rid)
        LOG.debug("router from north is %s", router)

        orig_router_dict = copy.deepcopy(router)

        original_router = super(H3CL3RouterPlugin,
                                self).get_router(context, rid)
        router_in_db = self._update_router_without_check_rescheduling(
            context, rid, router)

        payload = {
            'gw_exists':
            router['router'].get(EXTERNAL_GW_INFO, q_const.ATTR_NOT_SPECIFIED)
            != q_const.ATTR_NOT_SPECIFIED
        }
        try:
            resp_status, resp_body = self.process_update_router(router_in_db)
        except Exception as e:
            with excutils.save_and_reraise_exception():
                LOG.exception(
                    _LE("starting rollback because "
                        "H3C l3 driver failed to update router: %s"), e)
                rollback_router = {"router": {}}
                for i in orig_router_dict["router"]:
                    if i in original_router:
                        rollback_router["router"][i] = original_router[i]
                self._update_router_without_check_rescheduling(
                    context, rid, rollback_router)
        LOG.debug("after update router the resp_body from vcfc is %s",
                  resp_body)
        LOG.debug("after update router the resp_status from vcfc is %s",
                  resp_status)
        if self.enable_l3_router_rpc_notify:
            self.notify_router_updated(context, rid, payload)

        LOG.debug("VCFC update_router exited")
        return router_in_db
 def _update_router_without_check_rescheduling(self, context, rid, router):
     r = router['router']
     with context.session.begin(subtransactions=True):
         # check if route exists and have permission to access
         router_db = self._get_router(context, rid)
         if 'routes' in r:
             self._update_extra_routes(context, router_db, r['routes'])
             try:
                 self.sync_vcfc_route_entries(context, rid, router)
             except Exception as e:
                 LOG.error(_LE("H3C driver route entries id exception: %s"),
                           e)
         routes = self._get_extra_routes_by_router_id(context, rid)
     gw_info = r.pop(EXTERNAL_GW_INFO, q_const.ATTR_NOT_SPECIFIED)
     if gw_info != q_const.ATTR_NOT_SPECIFIED:
         # Update the gateway outside of the DB update since it involves L2
         # calls that don't make sense to rollback and may cause deadlocks
         # in a transaction.
         self._update_router_gw_info(context, rid, gw_info)
     router_db = self._update_router_db(context, rid, r)
     router_updated = self._make_router_dict(router_db)
     router_updated['routes'] = routes
     return router_updated
    def create_router(self, context, router):
        LOG.debug("VCFC create_router called")
        LOG.debug("ctx = %s", context.__dict__)
        LOG.debug("router from north is %s", router)

        router_in_db = super(H3CL3RouterPlugin,
                             self).create_router(context, router)
        router_for_creation = copy.deepcopy(router_in_db)

        if self.enable_l3_vxlan is True:
            try:
                segment_id = self.h3c_l3_vxlan.create_l3_segments(
                    context, router_in_db['id'])
            except Exception as e:
                with excutils.save_and_reraise_exception():
                    LOG.exception(
                        _LE("H3C failed to create router with vni: "
                            "%s"), e)
                    super(H3CL3RouterPlugin,
                          self).delete_router(context, router_in_db['id'])
            router_for_creation['provider:segmentation_id'] = segment_id
        try:
            response_status, response = self.process_create_router(
                router_for_creation)
        except Exception as e:
            with excutils.save_and_reraise_exception():
                LOG.exception(
                    _LE("H3C l3 driver create router"
                        "to vcfc exception: %s"), e)
                if self.enable_l3_vxlan is True:
                    self.h3c_l3_vxlan.release_segment(context.session,
                                                      router_in_db['id'])
                super(H3CL3RouterPlugin,
                      self).delete_router(context, router_in_db['id'])
        LOG.debug("after create router"
                  "the resp_body from vcfc is %s", response)
        LOG.debug("after create router"
                  "the resp_status from vcfc is %s", response_status)

        if (EXTERNAL_GW_INFO in router_in_db
                and router_in_db[EXTERNAL_GW_INFO]) is not None:
            try:
                resp_status, resp_body = self.process_update_router(
                    router_in_db)
            except Exception as e:
                with excutils.save_and_reraise_exception():
                    LOG.exception(
                        _LE("starting rollback because "
                            "H3C l3 driver failed to "
                            "update router gateway info: %s"), e)
                    try:
                        response_status = self.process_delete_router(
                            router_in_db)
                    except Exception as e:
                        LOG.exception(
                            _LE("H3C l3 driver delete router "
                                "from vcfc exception: %s"), e)
                    LOG.debug(
                        "H3C l3 driver delete router "
                        "from vcfc response_status is %s", response_status)
                    if self.enable_l3_vxlan is True:
                        self.h3c_l3_vxlan.release_segment(
                            context.session, router_in_db['id'])
                    super(H3CL3RouterPlugin,
                          self).delete_router(context, router_in_db['id'])

            LOG.debug(
                "after update router gateway info "
                "the resp_body from vcfc is %s", resp_body)
            LOG.debug(
                "after update router gateway info "
                "the resp_status from vcfc is %s", resp_status)

        # create route tables
        routetable_name = router_in_db['name']
        routetable_id = router_in_db['id']
        routetables = {
            "id": routetable_id,
            "name": routetable_name,
            "router_id": routetable_id
        }
        resource_path = h_const.ROUTETABLES_RESOURCE
        request_dic = {"routetables": [routetables]}
        response_status, response = self.client.rest_call(
            resource_path, "POST", request_dic)
        LOG.debug("after create route tables the resp_body from vcfc is %s",
                  response)
        LOG.debug("after create route tables the resp_status from vcfc is %s",
                  response_status)

        # binding route table to router
        resource_path = h_const.ROUTER_RESOURCE % routetable_id
        request_dic = {"route_table_id": routetable_id}
        self.client.rest_call(resource_path, "PUT", request_dic)

        LOG.debug("VCFC create_router exited")
        return router_in_db