def tunnelQueryInstance(): calldic = idic() resp = call(TunnelQueryInstance, calldic.get_args()) varprt(resp) respdic = odic(calldic) return json.dumps(respdic)
def tunnelDelete(): calldic = idic() resp = call(TunnelDelete, calldic.get_args()) varprt(resp) respdic = odic(calldic) return json.dumps(respdic)
def tunnelConstrait(): calldic = idic() resp = call(TunnelConstrait, calldic.get_args()) varprt(resp) respdic = odic(calldic) return json.dumps(respdic)
def ms_flow_get_flow(calldic=None): ''' The request: { "args": { "router_uid": int(xxx) | "vlink_uid": int(xxx) | }, "request": "ms_flow_get_flow", "trans_id": 1464768264, "ts": "20160601160424" } response: { "result": { "flows": [ { "bps": 23400, "dst": "1.2.3.0", // srcAddr "src": "219.141.189.200", // dstAddr "uid": "xxx" // vlink_uid }, { "bps": 43287, "dst": "1.2.3.1", "src": "219.141.189.201", "uid": "f_1" }, { "bps": 63174, "dst": "1.2.3.2", "src": "219.141.189.202", "uid": "f_2" } ] }, "err_code": 0, "msg": "Demo response", "response": "ms_flow_get_flow", "trans_id": 1464768264, "ts": "20160601160424" } ''' calldic = calldic or idic() varprt(calldic) respdic = odic(calldic) respdic.result = DotDict() vlink_uid = calldic.args.vlink_uid equip_uid = calldic.args.equip_uid if vlink_uid: respdic.result.flows = get_vlink_flow(vlink_uid) elif equip_uid: respdic.result.flows = get_equip_flow(equip_uid) return json.dumps(respdic)
def get_vlink_flow(uid): flows = [] show = {"_id": 0, "bps": 1, "time.log": 1, "addr.src": 1, "addr.dst": 1} # vlink.sport, vlink.dport => db.ports.ui vlink = getvlink(uid) if not vlink: klog.e("NotFound: vlink: uid:", uid) return [] klog.d(varfmt(vlink, "vlink")) sportip = vlink.get("sportip") dportip = vlink.get("dportip") src_equip = equip_fr_port_ip(sportip) or equip_fr_port_ip( vlink["sloopbackip"]) dst_equip = equip_fr_port_ip(dportip) or equip_fr_port_ip( vlink["dloopbackip"]) if not src_equip: klog.e("NotFound: src_equip: sportip:", sportip) return [] next_hop_uid = dst_equip.get("uid", "<Null>") match = {"loopback.cur": src_equip["ip_str"], "port.nexthop": dportip} klog.d(varfmt(match, "db.logs.find: match")) logs = db.logs.find(match, show) for log in logs: try: timediff = time.time() - log["time"]["log"] if timediff > conf.DB_LOG_AGE_SKIP: continue _flow = "(%s) >>> (%s)@(%s) >>> (%s)@(%s) >>> (%s)" % ( log["addr"]["src"], sportip, src_equip["ip_str"], dportip, dst_equip["ip_str"], log["addr"]["dst"]) flow = { "_flow": _flow, "bps": log["bps"], "src": log["addr"]["src"], "dst": log["addr"]["dst"], "uid": uid, "next_hop_uid": next_hop_uid } flows.append(flow) except: klog.e(traceback.format_exc()) klog.e("BAD LOG: ", varfmt(log, color=True)) varprt(match, "find vlink") return flows
def dotan(self, req=None): klog.d("dotan") ''' req: { "args": { "hop_list": [ << "PE11A", "PE21A" ], "from_router_name": "", << "to_router_name": "", << "bandwidth": "", << "to_router_uid": "", << "from_router_uid": "", << "callback": "http://127.0.0.1/path", !! "name": "", << "priority": 7, << "delay": "" << }, "request": "ms_controller_add_lsp", "ts": "20160718091442", "trans_id": 1468804482 } resp: { "uid": "lsp_0", !! "from_router_name": "", << "to_router_name": "", << "bandwidth": "", << "to_router_uid": "", << "from_router_uid": "", << "name": "", << "hop_list": [], << "path": [], ?? "status": 0, !! "priority": 7, << "delay": "", << "user_data": { !! "tunnel_name": "xxxxxxxxxxxx" } } ''' args = dotmap.DotMap(req["args"]) def getloopback(router_uid): e = einfo.fr_uid(router_uid) return e.get("ip_str") if e else None fr_ip = getloopback(args.get("from_router_uid")) or "222.222.222.222" if not fr_ip: klog.e("dotan") return None to_ip = getloopback(args.get("to_router_uid")) or "221.221.221.221" if not to_ip: klog.e("dotan") return None hdr = token.todic() tunnel_name = args.get("name") klog.d(tunnel_name) # 1. Fill the parameter will be sent to web service dic = dotmap.DotMap() dic["tunnel-name"] = tunnel_name dic["tunnel-type"] = "te" dic["manage-protocol"] = "netconf" dic["control-mode"] = "delegate" dic["path-setup-type"] = "rsvp-te" dic["source"] = { "ne-id": "d056ee16-63da-4621-a210-740a72c6b468", # FIXME "ip-address": fr_ip } dic["destination"] = { "ne-id": "bbf4f50c-32cd-4bfb-ade8-e803d4334af0", # FIXME "ip-address": to_ip } status, reason, res = hgetx(self, self.method, dic, hdr) varprt(hdr, "HDR") varprt(dic, "DIC") klog.d("status:%s" % status) if status != 201: klog.e("status is not 201, it is %d" % status) return None newlsp = dmstr(res) # 2. ensure approve (doc: 2.4.4) by PceReoptimizationByTunnel req = { "auto-approve": True, "reoptimization-by-tunnelname": { "tunnel-name": tunnel_name } } res = call(PceReoptimizationByTunnel, req) klog.d(varfmt(res, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")) if not res: return None # 3. prepare the return value ret = dotmap.DotMap() ret.name = args.name ret.from_router_name = args.from_router_name ret.from_router_uid = args.from_router_uid ret.to_router_name = args.to_router_name ret.to_router_uid = args.to_router_uid ret.bandwidth = args.bandwidth ret.delay = args.delay ret.priority = args.priority ret.uid = args.uid ret.path = [] # FIXME? ret.status = 0 ret.hop_list = [] # FIXME: Should overwrite by returned information? # 4. Get hop_list and check status to ensure the lsp is created successfully start = time.time() while True: if time.time() - start > 30: return None obj = call(LspInfos) for d in obj.get("lsp-info", ()): if d.get("tunnel-name") == tunnel_name: # Skip backup lsp role = d["lsp-role"] if role != "master": continue # XXX: wake till it up? oper_state = d.get("oper-state") ret.status = 1 if oper_state == "operate-up" else 0 # Fill the hop_list for hop in d.hops.hop: # klog.d("%s" % varfmt(hop, "hop in d.hops.hop")) loopback = hop["lsr-id"] e = einfo.fr_loopback(loopback) if e: # klog.d(varfmt(e, "equip")) ret.hop_list.append(e.get("uid")) # Fill user_data and return ret.user_data.create_info = args ret.user_data.tunnel_name = tunnel_name ret.user_data.path = ret.path ret.user_data.status = ret.status ret.user_data.hop_list = ret.hop_list ret.user_data.from_router_uid = args.from_router_uid return ret time.sleep(0.5) # 4. Return return ret