<s> import traceback <EOL> import hashlib <EOL> from flask import request <EOL> from foam . flowvisor import Connection as FV <EOL> from foam . core . json import jsonify , jsonValidate , JSONValidationError <EOL> from foam . api . jsonrpc import Dispatcher , route <EOL> from foam import types <EOL> import foam . task <EOL> import foam . lib <EOL> import foam . geni . lib <EOL> from foam . geni . db import GeniDB <EOL> from foam . core . configdb import ConfigDB <EOL> import os , sys <EOL> import json <EOL> import foam . ofeliasettings . localsettings as locsettings <EOL> from foam . ethzlegacyoptinstuff . legacyoptin . optsmodels import Experiment , ExperimentFLowSpace <EOL> from foam . ethzlegacyoptinstuff . legacyoptin . flowspaceutils import dotted_ip_to_int , mac_to_int , int_to_dotted_ip , int_to_mac , parseFVexception <EOL> from foam . ethzlegacyoptinstuff . api_exp_to_rspecv3 . expdatatogeniv3rspec import * <EOL> import time , random <EOL> def _same ( val ) : <EOL> return "<STR_LIT:%s>" % val <EOL> class om_ch_translate ( object ) : <EOL> attr_funcs = { <EOL> "<STR_LIT>" : ( int_to_mac , mac_to_int , <NUM_LIT> , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( int_to_mac , mac_to_int , <NUM_LIT> , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( _same , int , <NUM_LIT:16> , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( _same , int , <NUM_LIT:12> , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( int_to_dotted_ip , dotted_ip_to_int , <NUM_LIT:32> , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( int_to_dotted_ip , dotted_ip_to_int , <NUM_LIT:32> , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( _same , int , <NUM_LIT:8> , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( _same , int , <NUM_LIT:16> , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( _same , int , <NUM_LIT:16> , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( _same , int , <NUM_LIT:16> , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> } <EOL> class AdminAPIv1 ( Dispatcher ) : <EOL> def __init__ ( self , app ) : <EOL> super ( AdminAPIv1 , self ) . __init__ ( "<STR_LIT>" , app . logger , app ) <EOL> self . vlan_automation_on = False <EOL> self . _log . info ( "<STR_LIT>" ) <EOL> def validate ( self , rjson , types ) : <EOL> return jsonValidate ( rjson , types , self . _log ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def getFVSliceName ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , ( unicode , str ) ) ] ) <EOL> name = GeniDB . getFlowvisorSliceName ( request . json [ "<STR_LIT>" ] ) <EOL> return jsonify ( { "<STR_LIT:name>" : name } ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" , "<STR_LIT:GET>" ] ) <EOL> def listSlivers ( self ) : <EOL> try : <EOL> deleted = False <EOL> status = <NUM_LIT:0> <EOL> if request . method == "<STR_LIT:POST>" : <EOL> if not request . json : <EOL> return "<STR_LIT>" <EOL> if request . json . has_key ( "<STR_LIT>" ) : <EOL> deleted = request . json [ "<STR_LIT>" ] <EOL> if request . json . has_key ( "<STR_LIT:status>" ) : <EOL> st = request . json [ "<STR_LIT:status>" ] . lower ( ) <EOL> if st == "<STR_LIT>" : <EOL> status = True <EOL> elif st == "<STR_LIT>" : <EOL> status = False <EOL> elif st == "<STR_LIT>" : <EOL> status = None <EOL> slivers = GeniDB . getSliverList ( deleted , status ) <EOL> return jsonify ( { "<STR_LIT>" : slivers } ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def approveSliver ( self ) : <EOL> if not request . json : <EOL> return <EOL> return foam . geni . lib . approveSliver ( request , self . _log ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def rejectSliver ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , ( unicode , str ) ) ] ) <EOL> slice_name = GeniDB . getFlowvisorSliceName ( sliver_urn = request . json [ "<STR_LIT>" ] ) <EOL> sobj = GeniDB . getSliverObj ( request . json [ "<STR_LIT>" ] ) <EOL> data = GeniDB . getSliverData ( sobj . getURN ( ) , True ) <EOL> GeniDB . setSliverStatus ( request . json [ "<STR_LIT>" ] , False ) <EOL> if FV . sliceExists ( slice_name ) : <EOL> FV . deleteSlice ( slice_name ) <EOL> foam . task . emailRejectSliver ( data ) <EOL> return jsonify ( None ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def disableSliver ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , ( unicode , str ) ) ] ) <EOL> slice_name = GeniDB . getFlowvisorSliceName ( sliver_urn = request . json [ "<STR_LIT>" ] ) <EOL> sobj = GeniDB . getSliverObj ( request . json [ "<STR_LIT>" ] ) <EOL> data = GeniDB . getSliverData ( sobj . getURN ( ) , True ) <EOL> GeniDB . setSliverStatus ( request . json [ "<STR_LIT>" ] , None ) <EOL> GeniDB . commit ( ) <EOL> if FV . sliceExists ( slice_name ) : <EOL> FV . deleteSlice ( slice_name ) <EOL> foam . task . emailDisableSliver ( data ) <EOL> return jsonify ( None ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def deleteSliver ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , ( unicode , str ) ) ] ) <EOL> data = GeniDB . getSliverData ( request . json [ "<STR_LIT>" ] , True ) <EOL> foam . geni . lib . deleteSliver ( sliver_urn = request . json [ "<STR_LIT>" ] ) <EOL> foam . task . emailJSONDeleteSliver ( data ) <EOL> return jsonify ( None ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def showSliver ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> return_obj = { } <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , ( unicode , str ) ) ] ) <EOL> sobj = GeniDB . getSliverObj ( request . json [ "<STR_LIT>" ] ) <EOL> return_obj [ "<STR_LIT>" ] = sobj <EOL> if request . json . has_key ( "<STR_LIT>" ) and request . json [ "<STR_LIT>" ] : <EOL> return_obj [ "<STR_LIT>" ] = sobj . generateFlowEntries ( ) <EOL> if request . json . has_key ( "<STR_LIT>" ) and request . json [ "<STR_LIT>" ] : <EOL> return_obj [ "<STR_LIT>" ] = sobj . json_flowspec ( ) <EOL> if request . json . has_key ( "<STR_LIT>" ) and request . json [ "<STR_LIT>" ] : <EOL> return_obj [ "<STR_LIT>" ] = GeniDB . getRspec ( request . json [ "<STR_LIT>" ] ) <EOL> return jsonify ( return_obj ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def getConfig ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> objs = self . validate ( request . json , [ ( "<STR_LIT:key>" , ( unicode , str ) ) ] ) <EOL> u = ConfigDB . getUser ( request . environ [ "<STR_LIT>" ] ) <EOL> if objs [ "<STR_LIT:key>" ] == "<STR_LIT>" : <EOL> val = ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( u ) <EOL> return jsonify ( { "<STR_LIT:value>" : str ( val ) } ) <EOL> else : <EOL> return jsonify ( { "<STR_LIT:value>" : ConfigDB . getConfigItemByKey ( request . json [ "<STR_LIT:key>" ] ) . getValue ( u ) } ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except TypeError , e : <EOL> return jsonify ( None , <NUM_LIT:3> , msg = "<STR_LIT>" % ( objs [ "<STR_LIT:key>" ] ) ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def setConfig ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> objs = self . validate ( request . json , [ ( "<STR_LIT:key>" , ( unicode , str ) ) , ( "<STR_LIT:value>" , ( dict , int , unicode , str ) ) ] ) <EOL> u = ConfigDB . getUser ( request . environ [ "<STR_LIT>" ] ) <EOL> key = request . json [ "<STR_LIT:key>" ] <EOL> ConfigDB . getConfigItemByKey ( request . json [ "<STR_LIT:key>" ] ) . write ( request . json [ "<STR_LIT:value>" ] , u ) <EOL> return jsonify ( { "<STR_LIT:status>" : "<STR_LIT:success>" } ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def getSliverFlowspace ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , ( unicode , str ) ) ] ) <EOL> sobj = GeniDB . getSliverObj ( request . json [ "<STR_LIT>" ] ) <EOL> return jsonify ( { "<STR_LIT>" : sobj . generateFlowEntries ( ) } ) <EOL> except JSONValidationError , e : <EOL> return jsonify ( e . __json__ ( ) ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:GET>" ] ) <EOL> def jsonGetVersion ( self ) : <EOL> try : <EOL> import foam . version <EOL> return jsonify ( { "<STR_LIT:version>" : foam . version . VERSION } ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def setLocation ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , float ) , ( "<STR_LIT>" , float ) , ( "<STR_LIT>" , ( unicode , str ) ) , ( "<STR_LIT>" , ( unicode , str ) ) ] ) <EOL> GeniDB . setLocation ( request . json [ "<STR_LIT>" ] , request . json [ "<STR_LIT>" ] , request . json [ "<STR_LIT>" ] , request . json [ "<STR_LIT>" ] ) <EOL> return jsonify ( { "<STR_LIT:status>" : "<STR_LIT:success>" } ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def addDatapath ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , ( unicode , str ) ) ] ) <EOL> GeniDB . addDatapath ( request . json [ "<STR_LIT>" ] ) <EOL> return jsonify ( { "<STR_LIT:status>" : "<STR_LIT:success>" } ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def removeDatapath ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , ( unicode , str ) ) ] ) <EOL> GeniDB . removeDatapath ( request . json [ "<STR_LIT>" ] ) <EOL> return jsonify ( { "<STR_LIT:status>" : "<STR_LIT:success>" } ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def setTrigger ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> obj = self . validate ( request . json , [ ( "<STR_LIT:type>" , types . TriggerType ) , ( "<STR_LIT:start>" , ( str , unicode ) ) , <EOL> ( "<STR_LIT:end>" , ( str , unicode ) ) , ( "<STR_LIT>" , types . EventType ) , <EOL> ( "<STR_LIT:action>" , None ) ] ) <EOL> GeniDB . addTrigger ( obj [ "<STR_LIT:type>" ] , obj [ "<STR_LIT:start>" ] , obj [ "<STR_LIT:end>" ] , obj [ "<STR_LIT>" ] , obj [ "<STR_LIT:action>" ] ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def adminSetSliverExpiration ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> objs = self . validate ( request . json , [ ( "<STR_LIT>" , types . DateTime ) , ( "<STR_LIT>" , types . SliverURN ) ] ) <EOL> GeniDB . updateSliverExpiration ( objs [ "<STR_LIT>" ] , objs [ "<STR_LIT>" ] ) <EOL> return jsonify ( { "<STR_LIT:status>" : "<STR_LIT:success>" } ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:GET>" ] ) <EOL> def adminRebuildFlowvisorCache ( self ) : <EOL> try : <EOL> FV . rebuildCache ( ) <EOL> return jsonify ( None ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:GET>" ] ) <EOL> def adminBeginImport ( self ) : <EOL> try : <EOL> ConfigDB . importing = True <EOL> return jsonify ( None ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:GET>" ] ) <EOL> def adminFinishImport ( self ) : <EOL> try : <EOL> ConfigDB . importing = False <EOL> return jsonify ( None ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" ] ) <EOL> def adminImportSliver ( self ) : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> objs = self . validate ( request . json , [ ( "<STR_LIT>" , ( str , unicode ) ) , <EOL> ( "<STR_LIT>" , ( str , unicode ) ) , <EOL> ( "<STR_LIT>" , ( str , unicode ) ) , <EOL> ( "<STR_LIT>" , ( str , unicode ) ) , <EOL> ( "<STR_LIT>" , ( str , unicode ) ) , <EOL> ( "<STR_LIT>" , ( types . DateTime ) ) , <EOL> ( "<STR_LIT>" , ( int ) ) , <EOL> ( "<STR_LIT:status>" , ( bool ) ) , <EOL> ( "<STR_LIT>" , ( bool ) ) ] ) <EOL> self . _log . info ( "<STR_LIT>" % ( objs [ "<STR_LIT>" ] ) ) <EOL> obj = foam . geni . lib . importSliver ( objs ) <EOL> return jsonify ( None ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" , "<STR_LIT:GET>" ] ) <EOL> def adminListVLANs ( self ) : <EOL> try : <EOL> global_vlan_list = { } <EOL> for i in range ( <NUM_LIT> ) : <EOL> global_vlan_list [ i ] = "<STR_LIT>" <EOL> slivers = GeniDB . getSliverList ( False , True , True ) <EOL> for sliv in slivers : <EOL> fspecs = sliv . getFlowspecs ( ) <EOL> for fs in fspecs : <EOL> for vlanid in fs . getVLANs ( ) : <EOL> global_vlan_list [ vlanid ] = "<STR_LIT>" <EOL> free_vlan_list = [ ] <EOL> for i in global_vlan_list . iterkeys ( ) : <EOL> if global_vlan_list [ i ] == "<STR_LIT>" : <EOL> free_vlan_list . append ( i ) <EOL> free_vlan_list . sort ( ) <EOL> allocated_vlan_list = [ ] <EOL> for i in global_vlan_list . iterkeys ( ) : <EOL> if global_vlan_list [ i ] == "<STR_LIT>" : <EOL> allocated_vlan_list . append ( i ) <EOL> allocated_vlan_list . sort ( ) <EOL> for vlan_id in locsettings . UNALLOWED_VLANS : <EOL> free_vlan_list . remove ( vlan_id ) <EOL> return jsonify ( { "<STR_LIT>" : free_vlan_list , "<STR_LIT>" : allocated_vlan_list } ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" , "<STR_LIT:GET>" ] ) <EOL> def adminListAllocatedVlans ( self , use_json = True ) : <EOL> try : <EOL> used_vlans = [ ] <EOL> slivers = GeniDB . getSliverList ( False , True , True ) <EOL> for sliv in slivers : <EOL> fspecs = sliv . getFlowspecs ( ) <EOL> for fs in fspecs : <EOL> for vlanid in fs . getVLANs ( ) : <EOL> if vlanid not in used_vlans : <EOL> used_vlans . append ( vlanid ) <EOL> used_vlans . sort ( ) <EOL> if ( use_json == True ) : <EOL> return jsonify ( { "<STR_LIT>" : used_vlans } ) <EOL> else : <EOL> return used_vlans <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" , "<STR_LIT:GET>" ] ) <EOL> def adminListAllocatedVlansSorted ( self ) : <EOL> try : <EOL> used_vlans = self . adminListAllocatedVlans ( False ) <EOL> sorted_vlans = [ <NUM_LIT:0> for x in xrange ( <NUM_LIT:4> ) ] <EOL> sorted_vlans [ <NUM_LIT:0> ] = [ x for x in used_vlans if x <= <NUM_LIT:1000> ] <EOL> sorted_vlans [ <NUM_LIT:1> ] = [ x for x in used_vlans if x > <NUM_LIT:1000> and x <= <NUM_LIT> ] <EOL> sorted_vlans [ <NUM_LIT:2> ] = [ x for x in used_vlans if x > <NUM_LIT> and x <= <NUM_LIT> ] <EOL> sorted_vlans [ <NUM_LIT:3> ] = [ x for x in used_vlans if x > <NUM_LIT> ] <EOL> return jsonify ( { "<STR_LIT>" : sorted_vlans } ) <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" , "<STR_LIT:GET>" ] ) <EOL> def adminOfferVlanTags ( self , set = None , use_json = True ) : <EOL> if use_json == True : <EOL> if not request . json : <EOL> return <EOL> try : <EOL> if use_json == True : <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , ( int ) ) ] ) <EOL> vlan_set = request . json [ "<STR_LIT>" ] <EOL> else : <EOL> vlan_set = <NUM_LIT:0> <EOL> if use_json == True : <EOL> if vlan_set == <NUM_LIT:0> : <EOL> returnval = [ x for x in range ( <NUM_LIT:1> , <NUM_LIT> ) if x not in self . adminListAllocatedVlans ( False ) and x not in locsettings . UNALLOWED_VLANS ] <EOL> elif vlan_set in range ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> returnval = [ x for x in range ( <NUM_LIT:1> , <NUM_LIT> ) if x not in self . adminListAllocatedVlans ( False ) and x not in locsettings . UNALLOWED_VLANS ] [ : vlan_set ] <EOL> else : <EOL> returnval = None <EOL> else : <EOL> if set in range ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> returnval = [ x for x in range ( <NUM_LIT:1> , <NUM_LIT> ) if x not in self . adminListAllocatedVlans ( False ) and x not in locsettings . UNALLOWED_VLANS ] [ : set ] <EOL> else : <EOL> returnval = None <EOL> if ( use_json == True ) : <EOL> return jsonify ( { "<STR_LIT>" : returnval } ) <EOL> else : <EOL> return returnval <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" , "<STR_LIT:GET>" ] ) <EOL> def expedientStampFSwithVlan ( self ) : <EOL> try : <EOL> THIS_SITE_TAG = ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) <EOL> filedir = '<STR_LIT>' <EOL> filename = os . path . join ( filedir , '<STR_LIT>' ) <EOL> if os . path . isfile ( filename ) : <EOL> f = open ( filename , '<STR_LIT:r>' ) <EOL> slice_info_dict = json . load ( f ) <EOL> f . close ( ) <EOL> self . validate ( request . json , [ ( "<STR_LIT>" , ( str ) ) , ( "<STR_LIT>" , ( int ) ) , ( "<STR_LIT>" , ( int ) ) ] ) <EOL> slice_id = request . json [ "<STR_LIT>" ] . split ( "<STR_LIT>" ) [ <NUM_LIT:1> ] . split ( "<STR_LIT::>" ) [ <NUM_LIT:0> ] . split ( "<STR_LIT>" ) [ <NUM_LIT:1> ] . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> vlan_stamp_start = request . json [ "<STR_LIT>" ] <EOL> vlan_stamp_end = request . json [ "<STR_LIT>" ] <EOL> if ( slice_id == "<STR_LIT>" ) or ( slice_id not in slice_info_dict ) : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> raise Exception <EOL> if vlan_stamp_start == <NUM_LIT:0> : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> raise Exception <EOL> updated_slice_info_dict = slice_info_dict . copy ( ) <EOL> for sliv_pos , sliver in enumerate ( slice_info_dict [ slice_id ] [ '<STR_LIT>' ] ) : <EOL> for sfs_pos , sfs in enumerate ( sliver [ '<STR_LIT>' ] ) : <EOL> updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] [ sliv_pos ] [ '<STR_LIT>' ] [ sfs_pos ] [ '<STR_LIT>' ] = vlan_stamp_start <EOL> updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] [ sliv_pos ] [ '<STR_LIT>' ] [ sfs_pos ] [ '<STR_LIT>' ] = vlan_stamp_end <EOL> all_efs = self . create_slice_fs ( updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] ) <EOL> slice_of_rspec = create_ofv3_rspec ( slice_id , updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] , updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] , updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] , updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] , updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] , updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] , updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] , updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] , all_efs ) <EOL> self . _log . info ( slice_of_rspec ) <EOL> slice_urn = "<STR_LIT>" + str ( THIS_SITE_TAG ) + "<STR_LIT>" + "<STR_LIT>" + str ( slice_id ) + "<STR_LIT>" + str ( updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] ) <EOL> creds = [ ] <EOL> user_info = { } <EOL> user_info [ "<STR_LIT>" ] = "<STR_LIT>" + str ( THIS_SITE_TAG ) + "<STR_LIT>" + "<STR_LIT>" + str ( updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] ) <EOL> user_info [ "<STR_LIT:email>" ] = str ( updated_slice_info_dict [ slice_id ] [ '<STR_LIT>' ] ) <EOL> old_exp_shutdown_success = self . gapi_DeleteSliver ( slice_urn , creds , [ ] ) <EOL> creation_result = self . gapi_CreateSliver ( slice_urn , creds , slice_of_rspec , user_info ) <EOL> filedir = '<STR_LIT>' <EOL> filename = os . path . join ( filedir , '<STR_LIT>' ) <EOL> tempfilename = os . path . join ( filedir , '<STR_LIT>' + str ( time . time ( ) ) + str ( random . randint ( <NUM_LIT:1> , <NUM_LIT> ) ) ) <EOL> f = open ( tempfilename , '<STR_LIT:w>' ) <EOL> json . dump ( updated_slice_info_dict , f ) <EOL> f . close ( ) <EOL> os . rename ( tempfilename , filename ) <EOL> return jsonify ( { "<STR_LIT>" : "<STR_LIT:yes>" } ) <EOL> else : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> raise Exception <EOL> except JSONValidationError , e : <EOL> jd = e . __json__ ( ) <EOL> return jsonify ( jd , code = <NUM_LIT:1> , msg = jd [ "<STR_LIT>" ] ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> return jsonify ( None , code = <NUM_LIT:2> , msg = traceback . format_exc ( ) ) <EOL> def get_direction ( self , direction ) : <EOL> if ( direction == '<STR_LIT>' ) : <EOL> return <NUM_LIT:0> <EOL> if ( direction == '<STR_LIT>' ) : <EOL> return <NUM_LIT:1> <EOL> if ( direction == '<STR_LIT>' ) : <EOL> return <NUM_LIT:2> <EOL> return <NUM_LIT:2> <EOL> def create_slice_fs ( self , switch_slivers ) : <EOL> all_efs = [ ] <EOL> for sliver in switch_slivers : <EOL> if "<STR_LIT>" in sliver : <EOL> dpid = sliver [ '<STR_LIT>' ] <EOL> else : <EOL> dpid = "<STR_LIT>" * <NUM_LIT:8> <EOL> dpid = dpid [ : - <NUM_LIT:1> ] <EOL> if len ( sliver [ '<STR_LIT>' ] ) == <NUM_LIT:0> : <EOL> efs = ExperimentFLowSpace ( ) <EOL> efs . dpid = dpid <EOL> efs . direction = <NUM_LIT:2> <EOL> all_efs . append ( efs ) <EOL> else : <EOL> for sfs in sliver [ '<STR_LIT>' ] : <EOL> efs = ExperimentFLowSpace ( ) <EOL> efs . dpid = dpid <EOL> if "<STR_LIT>" in sfs : <EOL> efs . direction = self . get_direction ( sfs [ '<STR_LIT>' ] ) <EOL> else : <EOL> efs . direction = <NUM_LIT:2> <EOL> fs = self . convert_star ( sfs ) <EOL> for attr_name , ( to_str , from_str , width , om_name , of_name ) in om_ch_translate . attr_funcs . items ( ) : <EOL> ch_start = "<STR_LIT>" % ( attr_name ) <EOL> ch_end = "<STR_LIT>" % ( attr_name ) <EOL> om_start = "<STR_LIT>" % ( om_name ) <EOL> om_end = "<STR_LIT>" % ( om_name ) <EOL> setattr ( efs , om_start , from_str ( fs [ ch_start ] ) ) <EOL> setattr ( efs , om_end , from_str ( fs [ ch_end ] ) ) <EOL> all_efs . append ( efs ) <EOL> return all_efs <EOL> def convert_star ( self , fs ) : <EOL> temp = fs . copy ( ) <EOL> for ch_name , ( to_str , from_str , width , om_name , of_name ) in om_ch_translate . attr_funcs . items ( ) : <EOL> ch_start = "<STR_LIT>" % ch_name <EOL> ch_end = "<STR_LIT>" % ch_name <EOL> if ch_start not in fs or fs [ ch_start ] == "<STR_LIT:*>" : <EOL> temp [ ch_start ] = to_str ( <NUM_LIT:0> ) <EOL> if ch_end not in fs or fs [ ch_end ] == "<STR_LIT:*>" : <EOL> temp [ ch_end ] = to_str ( <NUM_LIT:2> ** width - <NUM_LIT:1> ) <EOL> return temp <EOL> def gapi_CreateSliver ( self , slice_urn , credentials , rspec , users , force_approval = False , options = None ) : <EOL> from foam . geni . db import GeniDB , UnknownSlice , UnknownNode <EOL> import foam . geni . approval <EOL> import foam . geni . ofeliaapproval <EOL> import sfa <EOL> user_info = users <EOL> try : <EOL> if True : <EOL> try : <EOL> self . _log . debug ( "<STR_LIT>" % users ) <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> user_info [ "<STR_LIT>" ] = None <EOL> user_info [ "<STR_LIT:email>" ] = None <EOL> sliver = foam . geni . lib . createSliver ( slice_urn , credentials , rspec , user_info ) <EOL> style = ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) <EOL> if style == foam . geni . approval . NEVER : <EOL> approve = False <EOL> elif style == foam . geni . approval . ALWAYS : <EOL> approve = True <EOL> else : <EOL> approve = foam . geni . ofeliaapproval . of_analyzeForApproval ( sliver ) <EOL> if approve or force_approval : <EOL> pid = foam . task . approveSliver ( sliver . getURN ( ) , AUTO_SLIVER_PRIORITY ) <EOL> self . _log . debug ( "<STR_LIT>" % pid ) <EOL> data = GeniDB . getSliverData ( sliver . getURN ( ) , True ) <EOL> foam . task . emailCreateSliver ( data ) <EOL> return self . successResult ( GeniDB . getManifest ( sliver . getURN ( ) ) ) <EOL> return <EOL> except foam . geni . lib . RspecParseError , e : <EOL> self . _log . info ( str ( e ) ) <EOL> e . _foam_logged = True <EOL> raise e <EOL> except foam . geni . lib . RspecValidationError , e : <EOL> self . _log . info ( str ( e ) ) <EOL> e . _foam_logged = True <EOL> raise e <EOL> except foam . geni . lib . DuplicateSliver , ds : <EOL> self . _log . info ( "<STR_LIT>" % ( ds . slice_urn ) ) <EOL> ds . _foam_logged = True <EOL> raise ds <EOL> except foam . geni . lib . UnknownComponentManagerID , ucm : <EOL> raise Fault ( "<STR_LIT>" , "<STR_LIT>" % ( ucm . cid ) ) <EOL> except ( foam . geni . lib . UnmanagedComponent , UnknownNode ) , uc : <EOL> self . _log . info ( "<STR_LIT>" % ( uc . cid ) ) <EOL> f = Fault ( "<STR_LIT>" , "<STR_LIT>" % ( uc . cid ) ) <EOL> f . _foam_logged = True <EOL> raise f <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> raise e <EOL> def gapi_DeleteSliver ( self , slice_urn , credentials , options = None ) : <EOL> from foam . geni . db import GeniDB , UnknownSlice , UnknownNode <EOL> import foam . geni . approval <EOL> import foam . geni . ofeliaapproval <EOL> import sfa <EOL> try : <EOL> if True : <EOL> if GeniDB . getSliverURN ( slice_urn ) is None : <EOL> raise Fault ( "<STR_LIT>" , "<STR_LIT>" % ( slice_urn ) ) <EOL> return self . errorResult ( <NUM_LIT:12> , "<STR_LIT>" ) <EOL> sliver_urn = GeniDB . getSliverURN ( slice_urn ) <EOL> data = GeniDB . getSliverData ( sliver_urn , True ) <EOL> foam . geni . lib . deleteSliver ( sliver_urn = sliver_urn ) <EOL> foam . task . emailGAPIDeleteSliver ( data ) <EOL> return self . successResult ( True ) <EOL> return self . successResult ( False ) <EOL> except UnknownSlice , x : <EOL> self . _log . info ( "<STR_LIT>" % ( slice_urn ) ) <EOL> x . _foam_logged = True <EOL> raise x <EOL> except Exception , e : <EOL> self . _log . exception ( "<STR_LIT>" ) <EOL> raise e <EOL> def successResult ( self , value ) : <EOL> code_dict = dict ( geni_code = <NUM_LIT:0> ) <EOL> return dict ( code = code_dict , <EOL> value = value , <EOL> output = "<STR_LIT>" ) <EOL> def errorResult ( self , code , output ) : <EOL> code_dict = dict ( geni_code = code ) <EOL> return dict ( code = code_dict , <EOL> value = "<STR_LIT>" , <EOL> output = output ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" , "<STR_LIT:GET>" ] ) <EOL> def enableVlanAssignmentAutomation ( self ) : <EOL> self . vlan_automation_on = True <EOL> return jsonify ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> @ route ( '<STR_LIT>' , methods = [ "<STR_LIT:POST>" , "<STR_LIT:GET>" ] ) <EOL> def disableVlanAssignmentAutomation ( self ) : <EOL> self . vlan_automation_on = False <EOL> return jsonify ( { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def setup ( app ) : <EOL> api = AdminAPIv1 ( app ) <EOL> return api </s>
