def delete_flow_classifier(flow_classifier_id): url = "/v2.0/sfc/flow_classifiers" req = requests.delete("{}{}/{}".format(base_url, url, flow_classifier_id), headers={'X-Auth-Token': client.get_token()}) if req.status_code != 204: log.error(req.text) abort(req.status_code, req.text)
def _delete_port_pair_group(port_pair_group_id): url = "/v2.0/sfc/port_pair_groups" req = requests.delete("{}{}/{}".format(base_url, url, port_pair_group_id), headers={'X-Auth-Token': client.get_token()}) if req.status_code != 204: log.error(req.text) abort(req.status_code, req.text)
def _get_existing_port_pair_groups(sfc_id): req = requests.get("{}{}{}".format(base_url, "/v2.0/sfc/port_chains/", sfc_id), headers={'X-Auth-Token': client.get_token()}) if req.status_code != 200: log.error(req.text) abort(req.status_code, req.text) req = req.json() return req["port_chain"]["port_pair_groups"]
def _create_port_pairs(postfix_name, port_ids_list, allow_existing_pp=False): # create port_pairs from ports. Use the same port for ingress and egress req = requests.get("{}{}".format(base_url, "/v2.0/sfc/port_pairs"), headers={'X-Auth-Token': client.get_token()}) if req.status_code != 200: log.error(req.text) abort(req.status_code, req.text) req = req.json() existing_port_pairs = req["port_pairs"] port_pairs_list = [] for i, port_ids in enumerate(port_ids_list): port_pairs = [] for j, port_id in enumerate(port_ids): to_create_new = True if (allow_existing_pp): for port_pair in existing_port_pairs: if port_pair["ingress"] == port_id and port_pair["egress"] == port_id: port_pairs.append(port_pair["id"]) to_create_new = False break if to_create_new: body = { "port_pair": { "ingress": port_id, "egress": port_id, "name": "pp_{}{}_{}".format(i, j, postfix_name), } } req = requests.post("{}{}".format(base_url, "/v2.0/sfc/port_pairs"), json=body, headers={'X-Auth-Token': client.get_token()}) if req.status_code == 201: port_pairs.append(req.json()["port_pair"]["id"]) else: log.error(req.text) abort(req.status_code, req.text) port_pairs_list.append(port_pairs) return port_pairs_list
def _get_all_port_pairs_of_sfc(sfc_id): port_pairs = [] req = requests.get("{}{}{}".format(base_url, "/v2.0/sfc/port_chains/", sfc_id), headers={'X-Auth-Token': client.get_token()}) if req.status_code != 200: log.error(req.text) abort(req.status_code, req.text) req = req.json() pp_groups = req["port_chain"]["port_pair_groups"] for pp_group in pp_groups: req = requests.get("{}{}{}".format(base_url, "/v2.0/sfc/port_pair_groups/", pp_group), headers={'X-Auth-Token': client.get_token()}) if req.status_code != 200: log.error(req.text) abort(req.status_code, req.text) req = req.json() port_pairs.extend(req["port_pair_group"]["port_pairs"]) return port_pairs
def destroy_server(vnf_id): if zun_client.is_container(vnf_id): zun_client.zun_client.containers.kill(vnf_id) else: base_url = client.base_urls["compute"] url = "/servers/{}".format(vnf_id) headers = {'X-Auth-Token': client.get_token()} req = requests.delete("{}{}".format(base_url, url), headers=headers) if req.status_code != 204: log.error(req.text) abort(req.status_code, req.text)
def _delete_port_pair_group_recursive(port_pair_group_id): url = "/v2.0/sfc/port_pair_groups" req = requests.get("{}{}/{}".format(base_url, url, port_pair_group_id), headers={'X-Auth-Token': client.get_token()}) if req.status_code != 200: log.error(req.text) abort(req.status_code, req.text) req = req.json() port_pairs = req["port_pair_group"]["port_pairs"] _delete_port_pair_group(port_pair_group_id) for port_pair in port_pairs: _delete_port_pair(port_pair)
def _update_port_pair_groups(old_pp_groups, new_port_pairs_list): for i, pp_group in enumerate(old_pp_groups): body = { "port_pair_group": { "port_pairs": new_port_pairs_list[i], } } req = requests.put("{}{}{}".format(base_url, "/v2.0/sfc/port_pair_groups/", pp_group), json=body, headers={'X-Auth-Token': client.get_token()}) if req.status_code != 200: log.error(req.text) abort(req.status_code, req.text)
def is_vnf_active(vnf_id): if zun_client.is_container(vnf_id): vnf = zun_client.client.containers.get(vnf_id) return vnf.status == "Running" else: base_url = client.base_urls["compute"] url = "/servers/{}".format(vnf_id) headers = {'X-Auth-Token': client.get_token()} req = requests.get("{}{}".format(base_url, url), headers=headers) if req.status_code == 200: return (req.json()["server"]["status"] == 'ACTIVE') else: abort(req.status_code, req.text)
def _create_vm(vnf_spec): base_url = client.base_urls["compute"] headers = {'X-Auth-Token': client.get_token()} if vnf_spec.user_data is not None: user_data = base64.b64encode(vnf_spec.user_data.encode('ascii')) else: user_data = '' if vnf_spec.vnf_name: _server_name = vnf_spec.vnf_name else: _server_name = "vnf_{}".format(str(uuid.uuid4())) data = { "server": { "name": _server_name, "imageRef": vnf_spec.image_id, "flavorRef": vnf_spec.flavor_id, "user_data": user_data, "networks": [ { "uuid": mgmt_net_id }, { "uuid": data_net_id }, ], } } if vnf_spec.node_name is not None: # FIXME: this assume the availability zone is nova. However, if # the host does not belong to nova zone, somehow command still works ... data["server"]["availability_zone"] = "nova:{}".format( vnf_spec.node_name) url = "/servers" req = requests.post("{}{}".format(base_url, url), json=data, headers=headers) # if req is accepted (202), then return ID of server with success code (200) if req.status_code == 202: return req.json()["server"]["id"], 200 else: log.error(req.text) abort(req.status_code, req.text)
def create_flow_classifier(sfcr_spec): url = "/v2.0/sfc/flow_classifiers" # Originally, Openstack allow more relaxed (and flexible) conflict check, # but this can cause later conflict when creating sfc. # Thus we enforce a strict flow-classifier check # at the begining to prevent conflict. for sfcr in db.get_all_sfcrs(): # sfcr_spec contain all needed infor for sfcr_confict checking if _sfcr_conflict(sfcr, sfcr_spec): error_message = "sfcr conflict with sfcr: %s" %(sfcr.id) log.error(error_message) abort(400, error_message) body = dict() # logical_source_port is required body["logical_source_port"] = _get_data_port(sfcr_spec.source_client) if sfcr_spec.destination_client is not None: body["logical_destination_port"] = _get_data_port(sfcr_spec.destination_client) if sfcr_spec.src_ip_prefix is not None: body["source_ip_prefix"] = sfcr_spec.src_ip_prefix if sfcr_spec.dst_ip_prefix is not None: body["destination_ip_prefix"] = sfcr_spec.dst_ip_prefix if sfcr_spec.src_port_min is not None: body["source_port_range_min"] = sfcr_spec.src_port_min if sfcr_spec.src_port_max is not None: body["source_port_range_max"] = sfcr_spec.src_port_max if sfcr_spec.dst_port_min is not None: body["destination_port_range_min"] = sfcr_spec.dst_port_min if sfcr_spec.dst_port_max is not None: body["destination_port_range_max"] = sfcr_spec.dst_port_max if sfcr_spec.proto is not None: body["protocol"] = sfcr_spec.proto body = {"flow_classifier": body} req = requests.post("{}{}".format(base_url, url), json=body, headers={'X-Auth-Token': client.get_token()}) if req.status_code == 201: return req.json()["flow_classifier"]["id"] else: log.error(req.text) abort(req.status_code, req.text)
def stop_server(vnf_id): if zun_client.is_container(vnf_id): zun_client.client.containers.stop(vnf_id) else: base_url = client.base_urls["compute"] url = "/servers/{}/action".format(vnf_id) headers = {'X-Auth-Token': client.get_token()} data = {"os-stop": "dummy"} req = requests.post("{}{}".format(base_url, url), json=data, headers=headers) if req.status_code != 202: log.error(req.text) abort(req.status_code, req.text)
def _get_data_port(vnf_instance_id): url = "v2.0/ports?device_id={}".format(vnf_instance_id) req = requests.get("{}{}".format(base_url, url), headers={'X-Auth-Token': client.get_token()}) if req.status_code != 200: log.error(req.text) abort(req.status_code, req.text) req = req.json() for port in req["ports"]: if port["network_id"] == data_net_id: return port["id"] error_message = "no data port for vnf id: {}".format(vnf_instance_id) log.error(error_message) abort(400, error_message)
def _update_sfc_flow_classifiers(sfc, sfcr_ids, update_db=True): body = { "port_chain": { "flow_classifiers": sfcr_ids, } } req = requests.put("{}{}{}".format(base_url, "/v2.0/sfc/port_chains/", sfc.id), json=body, headers={'X-Auth-Token': client.get_token()}) if req.status_code != 200: log.error(req.text) abort(req.status_code, req.text) if update_db: sfc.sfcr_ids = sfcr_ids db.update_sfc(sfc)
def _create_port_chain(postfix_name, port_pair_groups, flow_classifiers, is_symmetric): body = { "port_chain": { "flow_classifiers": flow_classifiers, "port_pair_groups": port_pair_groups, "name": "pc_{}".format(postfix_name), "chain_parameters": { "symmetric": True if is_symmetric else False } } } req = requests.post("{}{}".format(base_url, "/v2.0/sfc/port_chains"), json=body, headers={'X-Auth-Token': client.get_token()}) if req.status_code == 201: return req.json()["port_chain"]["id"] else: log.error(req.text) abort(req.status_code, req.text)
def _create_port_pair_groups(postfix_name, port_pairs_list): # create port pair group for each port_pairs port_pair_groups = [] for i, port_pairs in enumerate(port_pairs_list): body = { "port_pair_group": { "port_pairs": port_pairs, "name": "ppg_{}_{}".format(i, postfix_name), } } req = requests.post("{}{}".format(base_url, "/v2.0/sfc/port_pair_groups"), json=body, headers={'X-Auth-Token': client.get_token()}) if req.status_code == 201: port_pair_groups.append(req.json()["port_pair_group"]["id"]) else: log.error(req.text) abort(req.status_code, req.text) return port_pair_groups