def post(self, dpid=None, group_id=None): logger.debug("requst url - %s", self.get_request_uri()) logger.debug("requst params - dpid : %s, group_id: %s", dpid, group_id) ret = {} body = None group = None mdata_list = [] act_len = 0 try: version = mul.nbapi_get_switch_version_with_id(int(dpid, 16)) if version == 0: raise Exception, 'no such switch' elif version == 1: raise Exception, 'Not supported switch OFP version' body = GroupSchema().deserialize(json.loads(self.request.body)) group = mul.prepare_add_group(str(body['group_id']), str(body['type'])) for action_bucket in body['action_buckets']: mdata = mul.nbapi_group_mdata_alloc(int(dpid, 16)) if mdata is None: raise Exception, 'failed to set mdata' mdata_list.append(mdata) for action in action_bucket['actions']: check = mul.nbapi_action_to_mdata(mdata, str(action['action']), str(action['value'])) if check is not 0: raise Exception, 'Malformed action data '+str(action['action'])+" : "+str(action['value']) mul.nbapi_group_action_add(act_len, group, mdata, str(action_bucket['weight']), str(action_bucket['ff_port']), str(action_bucket['ff_group'])) act_len +=1 check = mul.nbapi_group_add(act_len, int(dpid, 16), group) if check == 0: ret.update({"group_id" : str(body['group_id'])}) else: raise Exception, 'failed to add group' except Exception, e: ret.update({"error_message" : "failed to add group", "reason" : str(e)})
def __add_flow(self, dpid, requestbody): body = FlowSchema().deserialize(requestbody) #logger.debug(body) drop = check = 0 mdata = mul.nbapi_mdata_alloc(int(dpid, 16)) if mdata is None: return 'switch not exist' for instruction in body['instructions']: if instruction['instruction'] == 'APPLY_ACTIONS': check = mul.nbapi_mdata_inst_apply(mdata) elif instruction['instruction'] == 'WRITE_ACTIONS': check = mul.nbapi_mdata_inst_write(mdata) elif instruction['instruction'] == 'METER': check = mul.nbapi_mdata_inst_meter(mdata, int(instruction['value'])) elif instruction['instruction'] == 'GOTO_TABLE': check = mul.nbapi_mdata_inst_goto(mdata, int(instruction['value'])) if check != 0: return 'failed to set instruction' + str( instruction['instruction']) check = -1 for action in instruction['actions']: if 'DROP' == str(action['action']): drop = 1 check = 0 else: check = mul.nbapi_action_to_mdata(mdata, str(action['action']), str(action['value'])) if check != 0: return 'failed to set action ' + action[ 'action'] + ' : ' + action['value'] + str(check) start = datetime.datetime.now() check = mul.add_static_flow(int(dpid, 16), int(body['priority']), str(body['barrier']), str(body['stat']), mdata, drop, str(body['dl_src']), str(body['dl_dst']), str(body['dl_type']), str(body['dl_vlan']), str(body['dl_vlan_pcp']), str(body['mpls_label']), str(body['mpls_tc']), str(body['mpls_bos']), str(body['nw_src']), str(body['nw_src6']), str(body['nw_dst']), str(body['nw_dst6']), str(body['nw_proto']), str(body['nw_tos']), str(body['tp_dst']), str(body['tp_src']), str(body['in_port']), str(body['table_id'])) total = datetime.datetime.now() - start if check == 0: #flow_id = self.__make_flow_id(int(dpid, 16), flow, mask, int(body['priority'])) return "%10f" % float(total.total_seconds() * 10000) #return 'success' else: return 'flow already exist'
def __add_flow(self, dpid, requestbody): body=FlowSchema().deserialize(requestbody) #logger.debug(body) drop = check = 0 mdata = mul.nbapi_mdata_alloc(int(dpid, 16)) if mdata is None: return 'switch not exist' for instruction in body['instructions']: if instruction['instruction'] == 'APPLY_ACTIONS': check = mul.nbapi_mdata_inst_apply(mdata) elif instruction['instruction'] == 'WRITE_ACTIONS': check = mul.nbapi_mdata_inst_write(mdata) elif instruction['instruction'] == 'METER': check = mul.nbapi_mdata_inst_meter(mdata, int(instruction['value'])) elif instruction['instruction'] == 'GOTO_TABLE': check = mul.nbapi_mdata_inst_goto(mdata, int(instruction['value'])) if check != 0: return 'failed to set instruction'+str(instruction['instruction']) check = -1 for action in instruction['actions']: if 'DROP' == str(action['action']): drop = 1 check = 0 else: check = mul.nbapi_action_to_mdata(mdata, str(action['action']), str(action['value'])) if check != 0: return 'failed to set action '+action['action']+' : '+action['value']+str(check) start=datetime.datetime.now() check = mul.add_static_flow(int(dpid,16), int(body['priority']),str(body['barrier']),str(body['stat']), mdata, drop, str(body['dl_src']),str(body['dl_dst']),str(body['dl_type']),str(body['dl_vlan']),str(body['dl_vlan_pcp']),str(body['mpls_label']),str(body['mpls_tc']),str(body['mpls_bos']),str(body['nw_src']),str(body['nw_src6']),str(body['nw_dst']),str(body['nw_dst6']),str(body['nw_proto']),str(body['nw_tos']),str(body['tp_dst']),str(body['tp_src']),str(body['in_port']),str(body['table_id'])) total=datetime.datetime.now()-start if check == 0: #flow_id = self.__make_flow_id(int(dpid, 16), flow, mask, int(body['priority'])) return "%10f"%float(total.total_seconds()*10000) #return 'success' else: return 'flow already exist'
def post(self, dpid, flow_id=None): logger.debug("requset url - %s", self.get_request_uri()) logger.debug("request params - dpid: %s, flow_id: %s", dpid, flow_id) ret = {} flow = mask = mdata = None new_flow_id = None try: body = FlowSchema().deserialize(json.loads(self.request.body)) logger.debug(str(body)) version = int(mul.nbapi_get_switch_version_with_id(int(dpid,16))) if int(version)==0: raise Exception, 'switch does not exist' self.__match_check(version, body) self.__inst_action_check(version, body) nw_src = str(body['nw_src']) if int(body['dl_type'], 16) != 0x86dd else str(body['nw_src6']) nw_dst = str(body['nw_dst']) if int(body['dl_type'], 16) != 0x86dd else str(body['nw_dst6']) flow = mul.nbapi_make_flow(str(body['dl_src']) , str(body['dl_dst']) , str(body['dl_type']) , str(body['dl_vlan']) , str(body['dl_vlan_pcp']), str(body['mpls_label']), str(body['mpls_tc']) , str(body['mpls_bos']) , nw_dst, nw_src, str(body['nw_proto']), str(body['nw_tos']) , str(body['tp_dst']) , str(body['tp_src']) , str(body['in_port']) , str(body['table_id']) ) if flow == None: raise Exception, 'flow failed to alloc' mask = mul.nbapi_make_mask( str(body['dl_src']), str(body['dl_dst']), str(body['dl_type']), str(body['dl_vlan']), str(body['dl_vlan_pcp']), str(body['mpls_label']), str(body['mpls_tc']), str(body['mpls_bos']), nw_dst, nw_src, str(body['nw_proto']), str(body['nw_tos']), str(body['tp_dst']), str(body['tp_src']), str(body['in_port'])) if mask == None: raise Exception, 'mask failed to alloc' mdata = mul.nbapi_mdata_alloc(int(dpid, 16)) if mdata == None: raise Exception, 'cannot set mdata' drop = check = 0 for instruction in body['instructions']: if instruction['instruction'] == 'APPLY_ACTIONS': if version != 1: check = mul.nbapi_mdata_inst_apply(mdata) elif instruction['instruction'] == 'WRITE_ACTIONS': check = mul.nbapi_mdata_inst_write(mdata) elif instruction['instruction'] == 'METER': check = mul.nbapi_mdata_inst_meter(mdata, int(instruction['value'])) elif instruction['instruction'] == 'GOTO_TABLE': check = mul.nbapi_mdata_inst_goto(mdata, int(instruction['value'])) if check != 0: raise Exception, 'failed to set'+str(instruction['instruction']) check = -1 for action in instruction['actions']: if 'DROP' == str(action['action']): drop = 1 check = 0 else: check = mul.nbapi_action_to_mdata(mdata, str(action['action']), str(action['value'])) if check != 0: raise Exception, 'Malformed action data'+action['action']+' : '+action['value']+str(check) flags = mul.C_FL_ENT_STATIC if str(body['barrier']) != 'disable': flags |= mul.C_FL_ENT_BARRIER if str(body['stat']) != 'disable': flags |= mul.C_FL_ENT_GSTATS check = mul.add_static_flow(int(dpid, 16), flow, mask, int(body['priority']), mdata, flags, drop) if check == 0: sleep(0.5) new_flow_id = self.__make_flow_id(int(dpid,16), flow, mask, int(body['priority'])) #new_flow_id = self.__check_flow_realy_in_switch(int(dpid, 16), flow, mask, int(body['priority'])) #new_flow_id = FlowHolder.getInstance().save(int(dpid,16) , flow, mask, int(body['priority'])) #if new_flow_id == None: # raise Exception, 'this flow is rejected by switch' if flow_id and flow_id!=new_flow_id: ret = self.delete(None, flow_id) else: ret = { 'flow_id' : str(new_flow_id) } else: raise Exception, 'flow already exist' except Exception, e: ret.update({"error_message" : "failed to add flow!", "reason" : str(e)})