def adminFinishImport (self): try: ConfigDB.importing = False return jsonify(None) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def adminOfferVlanTags(self, set=None, use_json = True): if use_json == True: if not request.json: return try: if use_json == True: self.validate(request.json, [("vlan_set", (int))]) vlan_set = request.json["vlan_set"] else: vlan_set = 0 if use_json == True: #use json arg if vlan_set == 0: returnval = [x for x in range(1,4095) if x not in self.adminListAllocatedVlans(False) and x not in locsettings.UNALLOWED_VLANS] elif vlan_set in range(1,4095): returnval = [x for x in range(1,4095) if x not in self.adminListAllocatedVlans(False) and x not in locsettings.UNALLOWED_VLANS][:vlan_set] else: returnval = None else: #override json arg if set in range(1,4095): returnval = [x for x in range(1,4095) if x not in self.adminListAllocatedVlans(False) and x not in locsettings.UNALLOWED_VLANS][:set] else: returnval = None if (use_json == True): return jsonify({"offered-vlan-tags" : returnval}) else: return returnval except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def listUserURNs (self): try: u = ConfigDB.getUser(request.environ["USER"]) return jsonify(list(ConfigDB.getConfigItemByKey("geni.approval.user-urns").getValue(u))) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def jsonGetVersion (self): try: import foam.version return jsonify({"version" : foam.version.VERSION}) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def processExpirationEmails(self): now = _asUTC(datetime.datetime.utcnow()) self._log.debug("Expiration check: %s" % (str(now))) day_alert = [] week_alert = [] try: slivers = GeniDB.getSliverList(False) for sliver in slivers: sobj = GeniDB.getSliverObj(sliver["sliver_urn"]) (urn, action) = sobj.emailCheck(now) if action == 1: day_alert.append(urn) self._log.info( "[%s] Sent email for expiry within 30 hours" % (urn)) elif action == 2: week_alert.append(urn) self._log.info("[%s] Sent email for expiry within 7 days" % (urn)) else: self._log.debug( "[%s] Expiration check: No email required for sliver expiration (%s)" % (urn, sobj.getExpiration())) return jsonify({ "status": "success", "day_alerts": day_alert, "week_alerts": week_alert }) except Exception, e: self._log.exception("Exception") return jsonify(None, code=2, msg=traceback.format_exc())
def showSliver (self): # from foam.core.tracer import Tracer # Tracer.enable() if not request.json: return try: return_obj = {} self.validate(request.json, [("sliver_urn", (unicode,str))]) sobj = GeniDB.getSliverObj(request.json["sliver_urn"]) return_obj["sliver"] = sobj if request.json.has_key("flowspace") and request.json["flowspace"]: return_obj["flowspace"] = sobj.generateFlowEntries() if request.json.has_key("flowspec") and request.json["flowspec"]: return_obj["flowspec"] = sobj.json_flowspec() if request.json.has_key("rspec") and request.json["rspec"]: return_obj["rspec"] = GeniDB.getRspec(request.json["sliver_urn"]) # path = Tracer.disable() # self._log.debug("Tracer path: %s" % (path)) return jsonify(return_obj) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def approveSliver (request, logger): try: jsonValidate(request.json, [("sliver_urn", (unicode,str)), ("priority", int)], logger) if (not request.json.has_key("sliver_urn")) or (not request.json.has_key("priority")): return jsonify({"exception" : "You must specify a sliver_urn and priority"}) slice_name = GeniDB.getFlowvisorSliceName(sliver_urn=request.json["sliver_urn"]) if FV.sliceExists(slice_name): return jsonify({"Fault" : "Flowvisor slice '%s' already exists" % (slice_name)}) sobj = GeniDB.getSliverObj(request.json["sliver_urn"]) GeniDB.setSliverStatus(request.json["sliver_urn"], True) GeniDB.setSliverPriority(request.json["sliver_urn"], request.json["priority"]) GeniDB.commit() foam.geni.approval.AppData.addSliver(sobj) sobj.createSlice() sobj.insertFlowspace(request.json["priority"]) sobj.insertVirtualLink() data = GeniDB.getSliverData(sobj.getURN(), True) foam.task.emailApproveSliver(data) return jsonify(None) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def adminRebuildFlowvisorCache (self): try: FV.rebuildCache() return jsonify(None) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def listPortGroups (self): try: groups = AppData.getPortGroups() l = [str(x) for x in groups.keys()] return jsonify(l) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def listPortGroups(self): try: groups = AppData.getPortGroups() l = [str(x) for x in groups.keys()] return jsonify(l) except Exception, e: self._log.exception("Exception") return jsonify(None, code=2, msg=traceback.format_exc())
def getSliverFlowspace (self): if not request.json: return try: self.validate(request.json, [("sliver_urn", (unicode,str))]) sobj = GeniDB.getSliverObj(request.json["sliver_urn"]) return jsonify({"flowspace" : sobj.generateFlowEntries()}) except JSONValidationError, e: return jsonify(e.__json__())
def startCoverage(self): try: import figleaf self._log.info("Starting coverage tracking") figleaf.start() return jsonify(None) except Exception, e: self._log.exception("Exception") return jsonify(None, code=2, msg=traceback.format_exc())
def stopTrace (self): try: from foam.core.tracer import Tracer path = Tracer.disable() self._log.info("Tracing disabled") return jsonify({"output-path" : path}) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def startTrace(self): try: from foam.core.tracer import Tracer self._log.info("Tracing enabled") Tracer.enable() return jsonify(None) except Exception, e: self._log.exception("Exception") return jsonify(None, code=2, msg=traceback.format_exc())
def startCoverage (self): try: import figleaf self._log.info("Starting coverage tracking") figleaf.start() return jsonify(None) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def startTrace (self): try: from foam.core.tracer import Tracer self._log.info("Tracing enabled") Tracer.enable() return jsonify(None) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def stopTrace(self): try: from foam.core.tracer import Tracer path = Tracer.disable() self._log.info("Tracing disabled") return jsonify({"output-path": path}) except Exception, e: self._log.exception("Exception") return jsonify(None, code=2, msg=traceback.format_exc())
def getFVSliceName (self): if not request.json: return try: self.validate(request.json, [("slice_urn", (unicode,str))]) name = GeniDB.getFlowvisorSliceName(request.json["slice_urn"]) return jsonify({"name" : name}) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def adminSetSliverExpiration (self): if not request.json: return try: objs = self.validate(request.json, [("datetime", types.DateTime), ("urn", types.SliverURN)]) GeniDB.updateSliverExpiration(objs["urn"], objs["datetime"]) return jsonify({"status" : "success"}) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def showPortGroup (self): if not request.json: return try: objs = self.validate(request.json, [("group-id", (unicode, str))]) pg = AppData.getPortGroup(objs["group-id"]) return jsonify(pg) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def listUserURNs(self): try: u = ConfigDB.getUser(request.environ["USER"]) return jsonify( list( ConfigDB.getConfigItemByKey( "geni.approval.user-urns").getValue(u))) except Exception, e: self._log.exception("Exception") return jsonify(None, code=2, msg=traceback.format_exc())
def showPortGroup(self): if not request.json: return try: objs = self.validate(request.json, [("group-id", (unicode, str))]) pg = AppData.getPortGroup(objs["group-id"]) return jsonify(pg) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code=1, msg=jd["exception"])
def setLocation (self): if not request.json: return try: self.validate(request.json, [("lat", float), ("long", float), ("dpid", (unicode,str)), ("country", (unicode,str))]) GeniDB.setLocation(request.json["dpid"], request.json["country"], request.json["lat"], request.json["long"]) return jsonify({"status" : "success"}) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def removeUserURN(self): if not request.json: return try: objs = self.validate(request.json, [("urn", (unicode, str))]) u = ConfigDB.getUser(request.environ["USER"]) AppData.removeUserURN(objs["urn"], u) return jsonify(None) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code=1, msg=jd["exception"])
def removeUserURN (self): if not request.json: return try: objs = self.validate(request.json, [("urn", (unicode, str))]) u = ConfigDB.getUser(request.environ["USER"]) AppData.removeUserURN(objs["urn"], u) return jsonify(None) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def expedientStampFSwithVlan(self): try: THIS_SITE_TAG = ConfigDB.getConfigItemByKey("geni.site-tag").getValue() filedir = './opt/ofelia/ofam/local/db' filename = os.path.join(filedir, 'expedient_slices_info.json') if os.path.isfile(filename): f = open(filename, 'r') slice_info_dict = json.load(f) f.close() self.validate(request.json, [("urn", (str)), ("vlan_stamp_start", (int)), ("vlan_stamp_end", (int))]) slice_id = request.json["urn"].split("+slice+")[1].split(":")[0].split("id_")[1].split("name_")[0] vlan_stamp_start = request.json["vlan_stamp_start"] vlan_stamp_end = request.json["vlan_stamp_end"] if (slice_id == "") or (slice_id not in slice_info_dict): self._log.exception("The slice id you have specified is non-existent") raise Exception if vlan_stamp_start == 0: self._log.exception("You must provide a valid vlan stamp! Be careful with the provision, it is up to you") raise Exception updated_slice_info_dict = slice_info_dict.copy() for sliv_pos, sliver in enumerate(slice_info_dict[slice_id]['switch_slivers']): for sfs_pos, sfs in enumerate(sliver['flowspace']): updated_slice_info_dict[slice_id]['switch_slivers'][sliv_pos]['flowspace'][sfs_pos]['vlan_id_start'] = vlan_stamp_start updated_slice_info_dict[slice_id]['switch_slivers'][sliv_pos]['flowspace'][sfs_pos]['vlan_id_end'] = vlan_stamp_end all_efs = self.create_slice_fs(updated_slice_info_dict[slice_id]['switch_slivers']) slice_of_rspec = create_ofv3_rspec(slice_id, updated_slice_info_dict[slice_id]['project_name'], updated_slice_info_dict[slice_id]['project_desc'], \ updated_slice_info_dict[slice_id]['slice_name'], updated_slice_info_dict[slice_id]['slice_desc'], \ updated_slice_info_dict[slice_id]['controller_url'], updated_slice_info_dict[slice_id]['owner_email'], \ updated_slice_info_dict[slice_id]['owner_password'], \ updated_slice_info_dict[slice_id]['switch_slivers'], all_efs) self._log.info(slice_of_rspec) #print the new rspec in the log for debugging #form the slice URN according to http://groups.geni.net/geni/wiki/GeniApiIdentifiers slice_urn = "urn:publicid:IDN+openflow:foam:"+ str(THIS_SITE_TAG) +"+slice+" + "id_" + str(slice_id) + "name_" + str(updated_slice_info_dict[slice_id]['slice_name']) creds = [] #creds are not needed at least for now: to be fixed user_info = {} user_info["urn"] = "urn:publicid:IDN+openflow:foam"+ str(THIS_SITE_TAG) +"+ch+" + "user+" + str(updated_slice_info_dict[slice_id]['owner_email']) #temp hack user_info["email"] = str(updated_slice_info_dict[slice_id]['owner_email']) old_exp_shutdown_success = self.gapi_DeleteSliver(slice_urn, creds, []) creation_result = self.gapi_CreateSliver(slice_urn, creds, slice_of_rspec, user_info) #store updated dict as a json file in foam db folder filedir = './opt/ofelia/ofam/local/db' filename = os.path.join(filedir, 'expedient_slices_info.json') tempfilename = os.path.join(filedir, 'expedient_slices_info.json.temp.' + str(time.time()) + str(random.randint(1,10000))) f = open(tempfilename, 'w') json.dump(updated_slice_info_dict, f) f.close() os.rename(tempfilename, filename) return jsonify({"slice-stamped" : "yes"}) else: self._log.exception("The expedient slice info dict file is non-existent!") raise Exception except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def removeDatapath (self): if not request.json: return try: self.validate(request.json, [("dpid", (unicode,str))]) GeniDB.removeDatapath(request.json["dpid"]) return jsonify({"status" : "success"}) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def createPortGroup(self): if not request.json: return try: objs = self.validate(request.json, [("name", (unicode, str)), ("desc", (unicode, str))]) u = ConfigDB.getUser(request.environ["USER"]) pg = AppData.createPortGroup(objs["name"], objs["desc"]) return jsonify(str(pg.uuid)) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code=1, msg=jd["exception"])
def stopCoverage(self): try: import figleaf figleaf.stop() self._log.info("Coverage tracking stopped") path = "/opt/ofelia/ofam/local/log/figleaf.%f.log" % (time.time()) figleaf.write_coverage(path) self._log.info("Coverage written to %s" % (path)) return jsonify({"output-path": path}) except Exception, e: self._log.exception("Exception") return jsonify(None, code=2, msg=traceback.format_exc())
def stopCoverage (self): try: import figleaf figleaf.stop() self._log.info("Coverage tracking stopped") path = "/opt/ofelia/ofam/local/log/figleaf.%f.log" % (time.time()) figleaf.write_coverage(path) self._log.info("Coverage written to %s" % (path)) return jsonify({"output-path" : path}) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def createPortGroup (self): if not request.json: return try: objs = self.validate(request.json, [("name", (unicode, str)), ("desc", (unicode, str))]) u = ConfigDB.getUser(request.environ["USER"]) pg = AppData.createPortGroup(objs["name"], objs["desc"]) return jsonify(str(pg.uuid)) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def setConfig (self): if not request.json: return try: objs = self.validate(request.json, [("key", (unicode,str)), ("value", (dict, int, unicode, str))]) u = ConfigDB.getUser(request.environ["USER"]) key = request.json["key"] ConfigDB.getConfigItemByKey(request.json["key"]).write(request.json["value"], u) return jsonify({"status" : "success"}) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def removePortFromGroup(self): if not request.json: return try: objs = self.validate(request.json, [("group-id", (unicode, str)), ("dpid", (unicode, str)), ("port-num", (int))]) pg = AppData.getPortGroup(objs["group-id"]) pg.removePort(objs["dpid"], objs["port-num"]) return jsonify(None) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code=1, msg=jd["exception"])
def removePortFromGroup (self): if not request.json: return try: objs = self.validate(request.json, [("group-id", (unicode, str)), ("dpid", (unicode, str)), ("port-num", (int))]) pg = AppData.getPortGroup(objs["group-id"]) pg.removePort(objs["dpid"], objs["port-num"]) return jsonify(None) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def deleteSliver (self): if not request.json: return try: self.validate(request.json, [("sliver_urn", (unicode,str))]) data = GeniDB.getSliverData(request.json["sliver_urn"], True) foam.geni.lib.deleteSliver(sliver_urn=request.json["sliver_urn"]) foam.task.emailJSONDeleteSliver(data) return jsonify(None) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def getConfig (self): if not request.json: return try: objs = self.validate(request.json, [("key", (unicode,str))]) u = ConfigDB.getUser(request.environ["USER"]) # Don't look here - stupidity to get around the fact that we don't # have output processors if objs["key"] == "geni.max-lease": val = ConfigDB.getConfigItemByKey("geni.max-lease").getValue(u) return jsonify({"value" : str(val)}) else: return jsonify({"value" : ConfigDB.getConfigItemByKey(request.json["key"]).getValue(u)}) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def autoApproveSliver (self): if not request.json: return try: TokenVerifier.checkToken("approve-sliver", request.json["sliver_urn"]) return foam.geni.lib.approveSliver(request, self._log) except TokenError, e: return jsonify({"exception" : traceback.format_exc()})
def setAdminPasswd(self): if not request.json: return try: u = ConfigDB.getUser(request.environ["USER"]) u.assertPrivilege(self.attrSetAdminPasswd) opts = self.validate(request.json, [("passwd", (unicode, str))]) self._log.info("Updating foamadmin password") self._htp.update("foamadmin", opts["passwd"]) self._htp.save() return jsonify(None) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code=1, msg=jd["exception"])
def autoApproveSliver(self): if not request.json: return try: TokenVerifier.checkToken("approve-sliver", request.json["sliver_urn"]) return foam.geni.lib.approveSliver(request, self._log) except TokenError, e: return jsonify({"exception": traceback.format_exc()})
def adminImportSliver (self): if not request.json: return try: objs = self.validate(request.json, [("slice_urn", (str, unicode)), ("sliver_urn", (str, unicode)), ("fvslicename", (str, unicode)), ("req_rspec", (str, unicode)), ("manifest_rspec", (str, unicode)), ("exp", (types.DateTime)), ("priority", (int)), ("status", (bool)), ("deleted", (bool))]) self._log.info("Importing sliver %s" % (objs["sliver_urn"])) obj = foam.geni.lib.importSliver(objs) return jsonify(None) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def adminListAllocatedVlansSorted(self): #use_json = True #if not request.json: # return try: used_vlans = self.adminListAllocatedVlans(False) sorted_vlans = [0 for x in xrange(4)] sorted_vlans[0] = [x for x in used_vlans if x <= 1000] sorted_vlans[1] = [x for x in used_vlans if x > 1000 and x <= 2000] sorted_vlans[2] = [x for x in used_vlans if x > 2000 and x <= 3000] sorted_vlans[3] = [x for x in used_vlans if x > 3000] #if (use_json == True): return jsonify({"allocated-vlans-sorted" : sorted_vlans}) #else: # return sorted_vlans except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])
def expireSlivers (self): now = _asUTC(datetime.datetime.utcnow()) exc_stack = [] expired_slivers = [] try: slivers = GeniDB.getExpiredSliverList(now) for sliver in slivers: try: data = GeniDB.getSliverData(sliver["sliver_urn"], True) TokenVerifier.checkToken("expire-sliver", sliver["sliver_urn"]) foam.geni.lib.deleteSliver(sliver_urn = sliver["sliver_urn"]) foam.task.emailExpireSliver(data) expired_slivers.append(data["sliver_urn"]) except TokenError, e: exc_stack.append(jsonify({"exception" : traceback.format_exc()})) return jsonify({"expired" : expired_slivers})
def emailPendingQueue (self): try: lines = [] pending_list = GeniDB.getSliverList(False, None) for sliver in pending_list: sobj = GeniDB.getSliverObj(sliver["sliver_urn"]) lines.append("Sliver URN: %s" % sobj.getURN()) lines.append(" User: %s [%s]" % (sobj.getEmail(), sobj.getUserURN())) lines.append("") if lines: self._log.info("[Daily Queue] Sending email for %d sliver(s)" % (len(lines)/3)) queue = "\n".join(lines) foam.task.emailPendingQueue({"pending-queue" : queue}) return jsonify(None) self._log.info("[Daily Queue] No pending slivers to email") return jsonify(None) except Exception, e: self._log.exception("Exception") return jsonify(None, code = 2, msg = traceback.format_exc())
def setTrigger (self): if not request.json: return try: obj = self.validate(request.json, [("type", types.TriggerType), ("start", (str,unicode)), ("end", (str,unicode)), ("event", types.EventType), ("action", None)]) GeniDB.addTrigger(obj["type"], obj["start"], obj["end"], obj["event"], obj["action"]) except JSONValidationError, e: jd = e.__json__() return jsonify(jd, code = 1, msg = jd["exception"])