<s> """<STR_LIT>""" <EOL> import uuid <EOL> import random <EOL> import urlparse <EOL> import itertools <EOL> from opennsa import error , constants as cnt <EOL> LOG_SYSTEM = '<STR_LIT>' <EOL> URN_UUID_PREFIX = '<STR_LIT>' <EOL> BIDIRECTIONAL = '<STR_LIT>' <EOL> class NSIHeader ( object ) : <EOL> def __init__ ( self , requester_nsa , provider_nsa , correlation_id = None , reply_to = None , security_attributes = None , connection_trace = None ) : <EOL> self . requester_nsa = requester_nsa <EOL> self . provider_nsa = provider_nsa <EOL> self . correlation_id = correlation_id or self . _createCorrelationId ( ) <EOL> self . reply_to = reply_to <EOL> self . security_attributes = security_attributes or [ ] <EOL> self . connection_trace = connection_trace <EOL> def _createCorrelationId ( self ) : <EOL> return URN_UUID_PREFIX + str ( uuid . uuid1 ( ) ) <EOL> def newCorrelationId ( self ) : <EOL> self . correlation_id = self . _createCorrelationId ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . requester_nsa , self . provider_nsa , self . correlation_id , self . reply_to , self . security_attributes , self . connection_trace ) <EOL> class SecurityAttribute ( object ) : <EOL> def __init__ ( self , type_ , value ) : <EOL> assert type ( type_ ) is str , '<STR_LIT>' % type ( type_ ) <EOL> assert type ( value ) is str , '<STR_LIT>' % type ( value ) <EOL> self . type_ = type_ <EOL> self . value = value <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . type_ , self . value ) <EOL> class EmptyLabelSet ( Exception ) : <EOL> pass <EOL> class Label ( object ) : <EOL> def __init__ ( self , type_ , values = None ) : <EOL> assert type ( values ) in ( None , str , list ) , '<STR_LIT>' % type ( values ) <EOL> self . type_ = type_ <EOL> self . values = self . _parseLabelValues ( values ) if values is not None else None <EOL> def _parseLabelValues ( self , values ) : <EOL> def createValue ( value ) : <EOL> try : <EOL> if '<STR_LIT:->' in value : <EOL> v1 , v2 = value . split ( '<STR_LIT:->' , <NUM_LIT:1> ) <EOL> i1 , i2 = int ( v1 ) , int ( v2 ) <EOL> if i1 > i2 : <EOL> raise error . PayloadError ( '<STR_LIT>' % value ) <EOL> else : <EOL> i1 = int ( value ) <EOL> i2 = i1 <EOL> return i1 , i2 <EOL> except ValueError : <EOL> raise error . PayloadError ( '<STR_LIT>' % value ) <EOL> if type ( values ) is str : <EOL> values = values . split ( '<STR_LIT:U+002C>' ) <EOL> parsed_values = sorted ( [ createValue ( value ) for value in values ] ) <EOL> nv = [ ] <EOL> for v1 , v2 in parsed_values : <EOL> if len ( nv ) == <NUM_LIT:0> : <EOL> nv . append ( ( v1 , v2 ) ) <EOL> continue <EOL> l = nv [ - <NUM_LIT:1> ] <EOL> if v1 <= l [ <NUM_LIT:1> ] + <NUM_LIT:1> : <EOL> nv = nv [ : - <NUM_LIT:1> ] + [ ( l [ <NUM_LIT:0> ] , max ( l [ <NUM_LIT:1> ] , v2 ) ) ] <EOL> else : <EOL> nv . append ( ( v1 , v2 ) ) <EOL> return nv <EOL> def intersect ( self , other ) : <EOL> assert type ( other ) is Label , '<STR_LIT>' % type ( other ) <EOL> assert self . type_ == other . type_ , '<STR_LIT>' <EOL> label_values = [ ] <EOL> i = iter ( other . values ) <EOL> o1 , o2 = i . next ( ) <EOL> for v1 , v2 in self . values : <EOL> while True : <EOL> if v2 < o1 : <EOL> break <EOL> elif o2 < v1 : <EOL> try : <EOL> o1 , o2 = i . next ( ) <EOL> except StopIteration : <EOL> break <EOL> continue <EOL> label_values . append ( ( max ( v1 , o1 ) , min ( v2 , o2 ) ) ) <EOL> if v2 <= o2 : <EOL> break <EOL> elif o2 <= v2 : <EOL> try : <EOL> o1 , o2 = i . next ( ) <EOL> except StopIteration : <EOL> break <EOL> if len ( label_values ) == <NUM_LIT:0> : <EOL> raise EmptyLabelSet ( '<STR_LIT>' ) <EOL> ls = '<STR_LIT:U+002C>' . join ( [ '<STR_LIT>' % ( nv [ <NUM_LIT:0> ] , nv [ <NUM_LIT:1> ] ) for nv in label_values ] ) <EOL> return Label ( self . type_ , ls ) <EOL> def labelValue ( self ) : <EOL> vs = [ str ( v1 ) if v1 == v2 else str ( v1 ) + '<STR_LIT:->' + str ( v2 ) for v1 , v2 in self . values ] <EOL> return '<STR_LIT:U+002C>' . join ( vs ) <EOL> def singleValue ( self ) : <EOL> return len ( self . values ) == <NUM_LIT:1> and self . values [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == self . values [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> def enumerateValues ( self ) : <EOL> lv = [ range ( lr [ <NUM_LIT:0> ] , lr [ <NUM_LIT:1> ] + <NUM_LIT:1> ) for lr in self . values ] <EOL> return list ( itertools . chain . from_iterable ( lv ) ) <EOL> def randomLabel ( self ) : <EOL> label_range = random . choice ( self . values ) <EOL> return random . randint ( label_range [ <NUM_LIT:0> ] , label_range [ <NUM_LIT:1> ] + <NUM_LIT:1> ) <EOL> @ staticmethod <EOL> def canMatch ( l1 , l2 ) : <EOL> if l1 is None and l2 is None : <EOL> return True <EOL> elif l1 is None or l2 is None : <EOL> return False <EOL> try : <EOL> l1 . intersect ( l2 ) <EOL> return True <EOL> except EmptyLabelSet : <EOL> return False <EOL> def __eq__ ( self , other ) : <EOL> if not type ( other ) is Label : <EOL> return False <EOL> return self . type_ == other . type_ and sorted ( self . values ) == sorted ( other . values ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . type_ , self . labelValue ( ) ) <EOL> class STP ( object ) : <EOL> def __init__ ( self , network , port , label = None ) : <EOL> assert type ( network ) is str , '<STR_LIT>' <EOL> assert type ( port ) is str , '<STR_LIT>' <EOL> assert label is None or type ( label ) is Label , '<STR_LIT>' <EOL> self . network = network <EOL> self . port = port <EOL> self . label = label <EOL> def shortName ( self ) : <EOL> base = '<STR_LIT>' % ( self . network , self . port ) <EOL> if self . label : <EOL> base += '<STR_LIT:?>' + self . label . type_ . split ( '<STR_LIT:#>' ) [ - <NUM_LIT:1> ] + '<STR_LIT:=>' + self . label . labelValue ( ) <EOL> return base <EOL> def baseURN ( self ) : <EOL> return cnt . URN_OGF_PREFIX + self . network + '<STR_LIT::>' + self . port <EOL> def urn ( self ) : <EOL> label = '<STR_LIT>' <EOL> if self . label is not None : <EOL> label = '<STR_LIT:?>' + self . label . type_ . split ( '<STR_LIT:#>' ) [ - <NUM_LIT:1> ] + '<STR_LIT:=>' + self . label . labelValue ( ) <EOL> return self . baseURN ( ) + label <EOL> def __eq__ ( self , other ) : <EOL> if not type ( other ) is STP : <EOL> return False <EOL> return self . network == other . network and self . port == other . port and self . label == other . label <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . shortName ( ) <EOL> class Link ( object ) : <EOL> def __init__ ( self , src_stp , dst_stp ) : <EOL> if src_stp . label is None : <EOL> assert dst_stp . label is None , '<STR_LIT>' <EOL> else : <EOL> assert dst_stp . label is not None , '<STR_LIT>' <EOL> self . src_stp = src_stp <EOL> self . dst_stp = dst_stp <EOL> def sourceSTP ( self ) : <EOL> return self . src_stp <EOL> def destSTP ( self ) : <EOL> return self . dst_stp <EOL> def __eq__ ( self , other ) : <EOL> if not type ( other ) is Link : <EOL> return False <EOL> return ( self . src_stp , self . dst_stp ) == ( other . src_stp , other . dst_stp ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . src_stp , self . dst_stp ) <EOL> class Path ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , network_links ) : <EOL> self . network_links = network_links <EOL> def links ( self ) : <EOL> return self . network_links <EOL> def sourceEndpoint ( self ) : <EOL> return self . network_links [ <NUM_LIT:0> ] . sourceSTP ( ) <EOL> def destEndpoint ( self ) : <EOL> return self . network_links [ - <NUM_LIT:1> ] . destSTP ( ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' + '<STR_LIT:U+0020>' . join ( [ str ( nl ) for nl in self . network_links ] ) + '<STR_LIT:>>' <EOL> class NetworkServiceAgent ( object ) : <EOL> def __init__ ( self , identity , endpoint , service_type = None ) : <EOL> assert type ( identity ) is str , '<STR_LIT>' % ( type ( identity ) , identity ) <EOL> assert type ( endpoint ) is str , '<STR_LIT>' % ( type ( endpoint ) , endpoint ) <EOL> self . identity = identity <EOL> self . endpoint = endpoint . strip ( ) <EOL> self . service_type = service_type <EOL> def getHostPort ( self ) : <EOL> url = urlparse . urlparse ( self . endpoint ) <EOL> host , port = url . netloc . split ( '<STR_LIT::>' , <NUM_LIT:2> ) <EOL> port = int ( port ) <EOL> return host , port <EOL> def urn ( self ) : <EOL> return cnt . URN_OGF_PREFIX + self . identity <EOL> def getServiceType ( self ) : <EOL> if self . service_type is None : <EOL> raise ValueError ( '<STR_LIT>' % self . identity ) <EOL> return self . service_type <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . identity <EOL> class ConnectionInfo ( object ) : <EOL> def __init__ ( self , connection_id , global_reservation_id , description , service_type , criterias , provider_nsa , requester_nsa , states , notification_id , result_id ) : <EOL> assert type ( criterias ) is list , '<STR_LIT>' % str ( type ( criterias ) ) <EOL> for criteria in criterias : <EOL> assert type ( criteria ) is QueryCriteria , '<STR_LIT>' % str ( type ( criteria ) ) <EOL> self . connection_id = connection_id <EOL> self . global_reservation_id = global_reservation_id <EOL> self . description = description <EOL> self . service_type = service_type <EOL> self . criterias = criterias <EOL> self . provider_nsa = provider_nsa <EOL> self . requester_nsa = requester_nsa <EOL> self . states = states <EOL> self . notification_id = notification_id <EOL> self . result_id = result_id <EOL> class Criteria ( object ) : <EOL> def __init__ ( self , revision , schedule , service_def ) : <EOL> self . revision = revision <EOL> self . schedule = schedule <EOL> self . service_def = service_def <EOL> class QueryCriteria ( Criteria ) : <EOL> def __init__ ( self , revision , schedule , service_def , children = None ) : <EOL> assert children is None or type ( children ) is list , '<STR_LIT>' % str ( type ( children ) ) <EOL> for child in children or [ ] : <EOL> assert type ( child ) is ConnectionInfo , '<STR_LIT>' % str ( type ( child ) ) <EOL> Criteria . __init__ ( self , revision , schedule , service_def ) <EOL> self . children = children or [ ] <EOL> class Schedule ( object ) : <EOL> def __init__ ( self , start_time , end_time ) : <EOL> if start_time is not None : <EOL> assert start_time . tzinfo is None , '<STR_LIT>' <EOL> if end_time is not None : <EOL> assert end_time . tzinfo is None , '<STR_LIT>' <EOL> self . start_time = start_time <EOL> self . end_time = end_time <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . start_time , self . end_time ) <EOL> class Point2PointService ( object ) : <EOL> def __init__ ( self , source_stp , dest_stp , capacity , directionality = BIDIRECTIONAL , symmetric = None , ero = None , parameters = None ) : <EOL> if directionality is None : <EOL> raise error . MissingParameterError ( '<STR_LIT>' ) <EOL> self . source_stp = source_stp <EOL> self . dest_stp = dest_stp <EOL> self . capacity = capacity <EOL> self . directionality = directionality <EOL> self . symmetric = symmetric <EOL> self . ero = ero <EOL> self . parameters = parameters </s>
