<s> import xmlrpclib <EOL> import jsonrpc <EOL> import logging <EOL> import datetime <EOL> import time <EOL> from foam . core . log import KeyAdapter <EOL> from foam . openflow . types import Port <EOL> import foam . core . allocation <EOL> MAXAGE = datetime . timedelta ( hours = <NUM_LIT:6> ) <EOL> class CachedSlice ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> self . _time = datetime . datetime . now ( ) <EOL> self . _info = Connection . getSliceInfo ( self . name ) <EOL> def exists ( self ) : <EOL> now = datetime . datetime . now ( ) <EOL> if ( now - self . _time ) > MAXAGE : <EOL> self . _time = now <EOL> self . _info = Connection . getSliceInfo ( self . name ) <EOL> if self . _info is not None : <EOL> return True <EOL> else : <EOL> return False <EOL> class SliceCache ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _cache = { } <EOL> def exists ( self , slice_name ) : <EOL> try : <EOL> return self . _cache [ slice_name ] . exists ( ) <EOL> except KeyError , e : <EOL> sl = CachedSlice ( slice_name ) <EOL> self . _cache [ slice_name ] = sl <EOL> return sl . exists ( ) <EOL> def delete ( self , slice_name ) : <EOL> try : <EOL> del self . _cache [ slice_name ] <EOL> except : <EOL> return <EOL> def add ( self , slice_name ) : <EOL> self . _cache [ slice_name ] = CachedSlice ( slice_name ) <EOL> class _Connection ( object ) : <EOL> def __init__ ( self ) : <EOL> self . xmlcall = self . _xmlcall_time <EOL> self . log = KeyAdapter ( "<STR_LIT>" , logging . getLogger ( '<STR_LIT>' ) ) <EOL> self . plog = logging . getLogger ( "<STR_LIT>" ) <EOL> self . __host = ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) <EOL> self . __passwd = ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) <EOL> self . __jsonport = ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) <EOL> self . __xmlport = ConfigDB . getConfigItemByKey ( "<STR_LIT>" ) . getValue ( ) <EOL> self . __sliceCache = SliceCache ( ) <EOL> self . buildConnections ( ) <EOL> def updateConfig ( self , info ) : <EOL> self . info = info <EOL> self . buildConnections ( ) <EOL> self . rebuildCache ( ) <EOL> def buildConnections ( self ) : <EOL> self . xmlconn = xmlrpclib . ServerProxy ( "<STR_LIT>" % ( <EOL> self . __passwd , self . __host , self . __xmlport ) ) <EOL> self . jsonconn = jsonrpc . ServiceProxy ( "<STR_LIT>" % ( <EOL> self . __passwd , self . __host , self . __jsonport ) ) <EOL> def _xmlcall_time ( self , method , * args ) : <EOL> m = getattr ( self . xmlconn , "<STR_LIT>" % ( method ) ) <EOL> t1 = time . time ( ) <EOL> ret = m ( * args ) <EOL> dur = time . time ( ) - t1 <EOL> self . plog . info ( "<STR_LIT>" % ( method , dur * <NUM_LIT:1000> ) ) <EOL> return ret <EOL> def _xmlcall_fast ( self , method , * args ) : <EOL> m = getattr ( self . xmlconn , "<STR_LIT>" % ( method ) ) <EOL> return m ( * args ) <EOL> def rebuildCache ( self ) : <EOL> self . __sliceCache = SliceCache ( ) <EOL> def getDeviceList ( self ) : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> dl = self . xmlcall ( "<STR_LIT>" ) <EOL> dl . sort ( ) <EOL> return dl <EOL> def getLinkList ( self ) : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> return self . xmlcall ( "<STR_LIT>" ) <EOL> def getFVVersion ( self ) : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> self . fvversion = self . xmlcall ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return self . fvversion <EOL> def getDevicePorts ( self , dpid ) : <EOL> self . log . debug ( "<STR_LIT>" % ( dpid ) ) <EOL> pinfoall = [ ] <EOL> if "<STR_LIT>" in self . getFVVersion ( ) : <EOL> pinfoall = self . xmlcall ( "<STR_LIT>" , dpid , "<STR_LIT:all>" ) <EOL> portlist = [ ] <EOL> dinfo = self . xmlcall ( "<STR_LIT>" , dpid ) <EOL> for portstr in dinfo [ "<STR_LIT>" ] . split ( "<STR_LIT:U+002C>" ) : <EOL> p = Port ( ) <EOL> elems = portstr . split ( "<STR_LIT:(>" ) <EOL> p . name = elems [ <NUM_LIT:0> ] <EOL> p . num = int ( elems [ <NUM_LIT:1> ] [ : - <NUM_LIT:1> ] ) <EOL> for pinfo in pinfoall : <EOL> pelems = pinfo . split ( "<STR_LIT:U+002C>" ) <EOL> if int ( pelems [ <NUM_LIT:0> ] ) == p . num : <EOL> p . features = pelems [ <NUM_LIT:2> ] <EOL> break <EOL> p . dpid = dpid <EOL> portlist . append ( p ) <EOL> return portlist <EOL> def getCombinedStats ( self , slice_name ) : <EOL> return None <EOL> def deleteSlice ( self , slice_name ) : <EOL> self . log . debug ( "<STR_LIT>" % ( slice_name ) ) <EOL> self . xmlcall ( "<STR_LIT>" , slice_name ) <EOL> self . __sliceCache . delete ( slice_name ) <EOL> def sliceExists ( self , slice_name ) : <EOL> return self . __sliceCache . exists ( slice_name ) <EOL> def getSliceInfo ( self , slice_name ) : <EOL> self . log . debug ( "<STR_LIT>" % ( slice_name ) ) <EOL> try : <EOL> sinfo = self . xmlcall ( "<STR_LIT>" , slice_name ) <EOL> return sinfo <EOL> except : <EOL> return None <EOL> def createSlice ( self , slice_name , controller , email ) : <EOL> self . log . debug ( "<STR_LIT>" % ( slice_name , controller , email ) ) <EOL> self . xmlcall ( "<STR_LIT>" , slice_name , self . __passwd , controller , email ) <EOL> self . __sliceCache . add ( slice_name ) <EOL> def changeFlowspace ( self , opslist ) : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> self . xmlcall ( "<STR_LIT>" , opslist ) <EOL> def addVirtualLink ( self , slice_name , action ) : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> self . xmlcall ( "<STR_LIT>" , slice_name , action ) <EOL> def updateInfo ( self , key , value ) : <EOL> if key == "<STR_LIT>" : <EOL> self . __host = value <EOL> elif key == "<STR_LIT>" : <EOL> self . __passwd = value <EOL> elif key == "<STR_LIT>" : <EOL> self . __jsonport = value <EOL> elif key == "<STR_LIT>" : <EOL> self . __xmlport = value <EOL> elif key == "<STR_LIT>" : <EOL> v = coerceBool ( key , value ) <EOL> if v : <EOL> self . xmlcall = self . _xmlcall_time <EOL> else : <EOL> self . xmlcall = self . _xmlcall_fast <EOL> self . buildConnections ( ) <EOL> return value <EOL> def updateInfo ( key , value ) : <EOL> return Connection . updateInfo ( key , value ) <EOL> class FSAllocation ( foam . core . allocation . Allocation ) : <EOL> def __init__ ( self ) : <EOL> super ( FSAllocation , self ) . __init__ ( ) <EOL> self . _groups = { } <EOL> self . _flowspecs = [ ] <EOL> self . _virtuallinks = [ ] <EOL> self . _controllers = [ ] <EOL> def __str__ ( self ) : <EOL> x = super ( FSAllocation , self ) . __str__ ( ) <EOL> return "<STR_LIT>" % ( <EOL> "<STR_LIT>" . join ( [ str ( x ) for x in self . _controllers ] ) , <EOL> "<STR_LIT>" . join ( [ "<STR_LIT>" % ( k , "<STR_LIT>" . join ( str ( x ) for x in v ) ) for k , v in self . _groups . iteritems ( ) ] ) , <EOL> "<STR_LIT>" . join ( [ str ( x ) for x in self . _flowspecs ] ) , <EOL> "<STR_LIT>" . join ( [ str ( x ) for x in self . _virtuallinks ] ) ) <EOL> def getDataDict ( self , detail = True ) : <EOL> obj = super ( FSAllocation , self ) . getDataDict ( detail ) <EOL> if detail : <EOL> dobj = { "<STR_LIT>" : [ x . __json__ ( ) for x in self . _controllers ] , <EOL> "<STR_LIT>" : len ( self . generateFlowEntries ( ) ) } <EOL> obj . update ( dobj ) <EOL> return obj <EOL> def getGroups ( self ) : <EOL> return self . _groups <EOL> def getFlowspecs ( self ) : <EOL> return self . _flowspecs <EOL> def getVirtualLinks ( self ) : <EOL> return self . _virtuallinks <EOL> def getControllers ( self ) : <EOL> return self . _controllers <EOL> def addGroup ( self , name , dplist = [ ] ) : <EOL> if name is None : <EOL> raise NoGroupName ( ) <EOL> if self . _groups . has_key ( name ) : <EOL> raise GroupNameAlreadyUsed ( name ) <EOL> self . _groups [ name ] = dplist <EOL> def addDatapathToGroup ( self , gname , dp ) : <EOL> self . _groups . setdefault ( gname , [ ] ) . append ( dp ) <EOL> def addController ( self , controller ) : <EOL> self . _controllers . append ( controller ) <EOL> def addFlowSpec ( self , fs ) : <EOL> self . _flowspecs . append ( fs ) <EOL> def addVirtualLink ( self , vl ) : <EOL> self . _virtuallinks . append ( vl ) <EOL> def getGroupDatapaths ( self , gname ) : <EOL> return self . _groups [ gname ] <EOL> def validate ( self ) : <EOL> cs = [ x for x in self . _controllers if x . type == "<STR_LIT>" ] <EOL> if len ( cs ) == <NUM_LIT:0> : <EOL> raise NoPrimaryController ( ) <EOL> elif len ( cs ) > <NUM_LIT:1> : <EOL> raise TooManyPrimaryControllers ( ) <EOL> def createSlice ( self ) : <EOL> cs = [ x for x in self . _controllers if x . type == "<STR_LIT>" ] <EOL> if len ( cs ) == <NUM_LIT:0> : <EOL> raise NoPrimaryController ( ) <EOL> Connection . createSlice ( str ( self . getUUID ( ) ) , cs [ <NUM_LIT:0> ] . url , self . getEmail ( ) ) <EOL> def generateFlowEntries ( self , priority = <NUM_LIT:100> ) : <EOL> entries = [ ] <EOL> for fs in self . _flowspecs : <EOL> entries . extend ( fs . generateFlowEntries ( priority , self . _groups . get ( None , [ ] ) ) ) <EOL> return entries <EOL> def insertFlowspace ( self , priority ) : <EOL> flowspace = self . generateFlowEntries ( priority ) <EOL> action = "<STR_LIT>" % ( self . getUUID ( ) ) <EOL> ops = [ ] <EOL> for entry in flowspace : <EOL> match = entry [ <NUM_LIT:2> ] <EOL> if match == "<STR_LIT>" : <EOL> match = "<STR_LIT>" <EOL> ops . append ( { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : entry [ <NUM_LIT:0> ] , "<STR_LIT>" : str ( entry [ <NUM_LIT:1> ] ) , <EOL> "<STR_LIT>" : match , "<STR_LIT>" : action } ) <EOL> Connection . changeFlowspace ( ops ) <EOL> def generateVLinkEntries ( self ) : <EOL> entries = [ ] <EOL> for fs in self . _virtuallinks : <EOL> entries . extend ( fs . generateVLinkEntries ( ) ) <EOL> return entries <EOL> def insertVirtualLink ( self ) : <EOL> vlinks = self . generateVLinkEntries ( ) <EOL> slicename = "<STR_LIT:%s>" % ( self . getUUID ( ) ) <EOL> for action in vlinks : <EOL> Connection . addVirtualLink ( slicename , action ) <EOL> from foam . core . configdb import ConfigDB , ConfigItem , coerceBool <EOL> citems = [ ] <EOL> citems . append ( ConfigItem ( ) . setKey ( "<STR_LIT>" ) . setValue ( None ) <EOL> . setDesc ( "<STR_LIT>" ) <EOL> . setUpdateFuncName ( "<STR_LIT>" ) ) <EOL> citems . append ( ConfigItem ( ) . setKey ( "<STR_LIT>" ) . setValue ( <NUM_LIT> ) <EOL> . setDesc ( "<STR_LIT>" ) <EOL> . setUpdateFuncName ( "<STR_LIT>" ) ) <EOL> citems . append ( ConfigItem ( ) . setKey ( "<STR_LIT>" ) . setValue ( <NUM_LIT> ) <EOL> . setDesc ( "<STR_LIT>" ) <EOL> . setUpdateFuncName ( "<STR_LIT>" ) ) <EOL> citems . append ( ConfigItem ( ) . setKey ( "<STR_LIT>" ) . setValue ( None ) <EOL> . setDesc ( "<STR_LIT>" ) <EOL> . setUpdateFuncName ( "<STR_LIT>" ) ) <EOL> citems . append ( ConfigItem ( ) . setKey ( "<STR_LIT>" ) . setValue ( True ) <EOL> . setDesc ( "<STR_LIT>" ) <EOL> . setUpdateFuncName ( "<STR_LIT>" ) ) <EOL> [ ConfigDB . installConfigItem ( x ) for x in citems ] <EOL> del citems <EOL> Connection = _Connection ( ) </s>
