<commit_before># cli commands

from twisted.python import log
from twisted.internet import defer

from opennsa import constants as cnt, nsa, error


LABEL_LOOKUP = {
    'vlan' : cnt.ETHERNET_VLAN
}

# this parser should perhaps be somewhere else
def _createSTP(stp_desc):

    if '%' in stp_desc:
        network, port = stp_desc.rsplit('%',1)
    else:
        network, local_part = stp_desc.rsplit(':',1)
        port = network + ':' + local_part

#    if '#' in local_part:
#        port, label_part = local_part.split('#',1)
#        labels = []
#        for tvl in label_part.split(';'):
#            if not '=' in tvl:
#                raise ValueError('Invalid label type-value: %s' % tvl)
#            type_, values = tvl.split('=')
#            labels.append( nsa.Label( LABEL_LOOKUP.get(type_, type_), values ) )
#    else:
#        port = local_part
#        labels = None

    labels = None
    return nsa.STP(network, port, labels)


def _createEVTS(src, dst, capacity, mtu=1500, burst_size=5000000):

    src_np, src_vlan = src.split('#')
    dst_np, dst_vlan = dst.split('#')

    src_stp = _createSTP(src_np)
    dst_stp = _createSTP(dst_np)

    src_stp.labels = [ nsa.Label(cnt.ETHERNET_VLAN, src_vlan) ]
    dst_stp.labels = [ nsa.Label(cnt.ETHERNET_VLAN, dst_vlan) ]

    return nsa.EthernetVLANService(src_stp, dst_stp, capacity, mtu, burst_size)


def _handleEvent(event):

    notification_type, header, entry = event

    if notification_type == 'errorEvent':
        log.msg('Error event: %s' % str(entry))
        return True
    elif notification_type == 'dataPlaneStateChange':
        cid, nid, timestamp, dps = entry
        active, version, consistent = dps
        if active:
            log.msg('Connection %s Data plane active, version %i, consistent: %s' % (cid, version, consistent))
            return False
        else:
            log.msg('Connection %s Data plane down, version %i, consistent: %s' % (cid, version, consistent))
            return consistent # this means we don't exit on initial partially down, where we are not consistent

    else:
        log.msg('Unrecognized event %s ' % notification_type)
        return False


@defer.inlineCallbacks
def discover(client, service_url):

    res = yield client.queryNSA(service_url)
    print "-- COMMAND RESULT --"
    print res
    print "--"


@defer.inlineCallbacks
def reserve(client, client_nsa, provider_nsa, src, dst, start_time, end_time, capacity, connection_id, global_id):

    nsi_header = nsa.NSIHeader(client_nsa.urn(), provider_nsa.urn(), reply_to=provider_nsa.endpoint)
    schedule = nsa.Schedule(start_time, end_time)
    service_def = _createEVTS(src, dst, capacity)
    crt = nsa.Criteria(0, schedule, service_def)

    if connection_id or global_id:
        log.msg("Connection id: %s  Global id: %s" % (connection_id, global_id))


    try:
        assigned_connection_id = yield client.reserve(nsi_header, connection_id, global_id, 'Test Connection', crt)
        log.msg("Connection created and held. Id %s at %s" % (assigned_connection_id, provider_nsa))
        yield client.reserveCommit(nsi_header, assigned_connection_id)
        log.msg("Reservation committed at %s" % provider_nsa)

    except error.NSIError, e:
        log.msg('Error reserving %s, %s : %s' % (connection_id, e.__class__.__name__, str(e)))



@defer.inlineCallbacks
def reserveprovision(client, client_nsa, provider_nsa, src, dst, start_time, end_time, capacity, connection_id, global_id, notification_wait):

    nsi_header = nsa.NSIHeader(client_nsa.urn(), provider_nsa.urn())
    schedule = nsa.Schedule(start_time, end_time)
    service_def = _createEVTS(src, dst, capacity)
    crt = nsa.Criteria(0, schedule, service_def)

    log.msg("Connection id: %s  Global id: %s" % (connection_id, global_id))

    try:
        assigned_connection_id = yield client.reserve(nsi_header, connection_id, global_id, 'Test Connection', crt)
        log.msg("Connection created and held. Id %s at %s" % (assigned_connection_id, provider_nsa))
        yield client.reserveCommit(nsi_header, assigned_connection_id)
        log.msg("Connection committed at %s" % provider_nsa)
    except error.NSIError, e:
        log.msg('Error reserving %s, %s : %s' % (connection_id, e.__class__.__name__, str(e)))
        defer.returnValue(None)

    try:
        yield client.provision(nsi_header, assigned_connection_id)
        log.msg('Connection %s provisioned' % assigned_connection_id)
    except error.NSIError, e:
        log.msg('Error provisioning %s, %s : %s' % (assigned_connection_id, e.__class__.__name__, str(e)))

    while notification_wait:
        event = yield client.notifications.get()
        exit = _handleEvent(event)
        if exit:
            break



@defer.inlineCallbacks
def rprt(client, client_nsa, provider_nsa, src, dst, start_time, end_time, capacity, connection_id, global_id):
    # reserve, provision, release,  terminate
    nsi_header = nsa.NSIHeader(client_nsa.urn(), provider_nsa.urn())
    schedule = nsa.Schedule(start_time, end_time)
    service_def = _createEVTS(src, dst, capacity)
    crt = nsa.Criteria(0, schedule, service_def)

    log.msg("Connection id: %s  Global id: %s" % (connection_id, global_id))

    try:
        assigned_connection_id = yield client.reserve(nsi_header, connection_id, global_id, 'Test Connection', crt)
        log.msg("Connection created and held. Id %s at %s" % (assigned_connection_id, provider_nsa))
        yield client.reserveCommit(nsi_header, assigned_connection_id)
        log.msg("Connection committed at %s" % provider_nsa)
    except error.NSIError, e:
        log.msg('Error reserving %s, %s : %s' % (connection_id, e.__class__.__name__, str(e)))
        defer.returnValue(None)

    try:
        yield client.provision(nsi_header, assigned_connection_id)
        log.msg('Connection %s provisioned' % assigned_connection_id)
    except error.NSIError, e:
        log.msg('Error provisioning %s, %s : %s' % (assigned_connection_id, e.__class__.__name__, str(e)))
        defer.returnValue(None)

    try:
        yield client.release(nsi_header, assigned_connection_id)
        log.msg('Connection %s released' % assigned_connection_id)
    except error.NSIError, e:
        log.msg('Error releasing %s, %s : %s' % (assigned_connection_id, e.__class__.__name__, str(e)))
        defer.returnValue(None)

    try:
        yield client.terminate(nsi_header, assigned_connection_id)
        log.msg('Connection %s terminated' % assigned_connection_id)
    except error.NSIError, e:
        log.msg('Error terminating %s, %s : %s' % (assigned_connection_id, e.__class__.__name__, str(e)))
        defer.returnValue(None)


@defer.inlineCallbacks
def provision(client, client_nsa, provider_nsa, connection_id, notification_wait):

    try:
        yield client.provision(client_nsa, provider_nsa, None, connection_id)
        log.msg('Connection %s provisioned' % connection_id)
    except error.NSIError, e:
        log.msg('Error provisioning %s, %s : %s' % (connection_id, e.__class__.__name__, str(e)))

    if notification_wait:
        log.msg("Notification wait not added to provision yet")


@defer.inlineCallbacks
def release(client, client_nsa, provider_nsa, connection_id, notification_wait):

    try:
        yield client.release(client_nsa, provider_nsa, None, connection_id)
        log.msg('Connection %s released' % connection_id)
    except error.NSIError, e:
        log.msg('Error releasing %s, %s : %s' % (connection_id, e.__class__.__name__, str(e)))
<commit_msg>have command line client generate new correlation ids when sending multiple requests in one command<commit_after># cli commands

from twisted.python import log
from twisted.internet import defer

from opennsa import constants as cnt, nsa, error


LABEL_LOOKUP = {
    'vlan' : cnt.ETHERNET_VLAN
}

# this parser should perhaps be somewhere else
def _createSTP(stp_desc):

    if '%' in stp_desc:
        network, port = stp_desc.rsplit('%',1)
    else:
        network, local_part = stp_desc.rsplit(':',1)
        port = network + ':' + local_part

#    if '#' in local_part:
#        port, label_part = local_part.split('#',1)
#        labels = []
#        for tvl in label_part.split(';'):
#            if not '=' in tvl:
#                raise ValueError('Invalid label type-value: %s' % tvl)
#            type_, values = tvl.split('=')
#            labels.append( nsa.Label( LABEL_LOOKUP.get(type_, type_), values ) )
#    else:
#        port = local_part
#        labels = None

    labels = None
    return nsa.STP(network, port, labels)


def _createEVTS(src, dst, capacity, mtu=1500, burst_size=5000000):

    src_np, src_vlan = src.split('#')
    dst_np, dst_vlan = dst.split('#')

    src_stp = _createSTP(src_np)
    dst_stp = _createSTP(dst_np)

    src_stp.labels = [ nsa.Label(cnt.ETHERNET_VLAN, src_vlan) ]
    dst_stp.labels = [ nsa.Label(cnt.ETHERNET_VLAN, dst_vlan) ]

    return nsa.EthernetVLANService(src_stp, dst_stp, capacity, mtu, burst_size)


def _handleEvent(event):

    notification_type, header, entry = event

    if notification_type == 'errorEvent':
        log.msg('Error event: %s' % str(entry))
        return True
    elif notification_type == 'dataPlaneStateChange':
        cid, nid, timestamp, dps = entry
        active, version, consistent = dps
        if active:
            log.msg('Connection %s Data plane active, version %i, consistent: %s' % (cid, version, consistent))
            return False
        else:
            log.msg('Connection %s Data plane down, version %i, consistent: %s' % (cid, version, consistent))
            return consistent # this means we don't exit on initial partially down, where we are not consistent

    else:
        log.msg('Unrecognized event %s ' % notification_type)
        return False


@defer.inlineCallbacks
def discover(client, service_url):

    res = yield client.queryNSA(service_url)
    print "-- COMMAND RESULT --"
    print res
    print "--"


@defer.inlineCallbacks
def reserve(client, client_nsa, provider_nsa, src, dst, start_time, end_time, capacity, connection_id, global_id):

    nsi_header = nsa.NSIHeader(client_nsa.urn(), provider_nsa.urn(), reply_to=provider_nsa.endpoint)
    schedule = nsa.Schedule(start_time, end_time)
    service_def = _createEVTS(src, dst, capacity)
    crt = nsa.Criteria(0, schedule, service_def)

    if connection_id or global_id:
        log.msg("Connection id: %s  Global id: %s" % (connection_id, global_id))


    try:
        assigned_connection_id = yield client.reserve(nsi_header, connection_id, global_id, 'Test Connection', crt)
        log.msg("Connection created and held. Id %s at %s" % (assigned_connection_id, provider_nsa))
        yield client.reserveCommit(nsi_header, assigned_connection_id)
        log.msg("Reservation committed at %s" % provider_nsa)

    except error.NSIError, e:
        log.msg('Error reserving %s, %s : %s' % (connection_id, e.__class__.__name__, str(e)))



@defer.inlineCallbacks
def reserveprovision(client, client_nsa, provider_nsa, src, dst, start_time, end_time, capacity, connection_id, global_id, notification_wait):

    nsi_header = nsa.NSIHeader(client_nsa.urn(), provider_nsa.urn())
    schedule = nsa.Schedule(start_time, end_time)
    service_def = _createEVTS(src, dst, capacity)
    crt = nsa.Criteria(0, schedule, service_def)

    log.msg("Connection id: %s  Global id: %s" % (connection_id, global_id))

    try:
        assigned_connection_id = yield client.reserve(nsi_header, connection_id, global_id, 'Test Connection', crt)
        log.msg("Connection created and held. Id %s at %s" % (assigned_connection_id, provider_nsa))
        nsi_header.newCorrelationId()
        yield client.reserveCommit(nsi_header, assigned_connection_id)
        log.msg("Connection committed at %s" % provider_nsa)
    except error.NSIError, e:
        log.msg('Error reserving %s, %s : %s' % (connection_id, e.__class__.__name__, str(e)))
        defer.returnValue(None)

    try:
        nsi_header.newCorrelationId()
        yield client.provision(nsi_header, assigned_connection_id)
        log.msg('Connection %s provisioned' % assigned_connection_id)
    except error.NSIError, e:
        log.msg('Error provisioning %s, %s : %s' % (assigned_connection_id, e.__class__.__name__, str(e)))

    while notification_wait:
        event = yield client.notifications.get()
        exit = _handleEvent(event)
        if exit:
            break



@defer.inlineCallbacks
def rprt(client, client_nsa, provider_nsa, src, dst, start_time, end_time, capacity, connection_id, global_id):
    # reserve, provision, release,  terminate
    nsi_header = nsa.NSIHeader(client_nsa.urn(), provider_nsa.urn())
    schedule = nsa.Schedule(start_time, end_time)
    service_def = _createEVTS(src, dst, capacity)
    crt = nsa.Criteria(0, schedule, service_def)

    log.msg("Connection id: %s  Global id: %s" % (connection_id, global_id))

    try:
        assigned_connection_id = yield client.reserve(nsi_header, connection_id, global_id, 'Test Connection', crt)
        log.msg("Connection created and held. Id %s at %s" % (assigned_connection_id, provider_nsa))
        nsi_header.newCorrelationId()
        yield client.reserveCommit(nsi_header, assigned_connection_id)
        log.msg("Connection committed at %s" % provider_nsa)
    except error.NSIError, e:
        log.msg('Error reserving %s, %s : %s' % (connection_id, e.__class__.__name__, str(e)))
        defer.returnValue(None)

    try:
        nsi_header.newCorrelationId()
        yield client.provision(nsi_header, assigned_connection_id)
        log.msg('Connection %s provisioned' % assigned_connection_id)
    except error.NSIError, e:
        log.msg('Error provisioning %s, %s : %s' % (assigned_connection_id, e.__class__.__name__, str(e)))
        defer.returnValue(None)

    try:
        nsi_header.newCorrelationId()
        yield client.release(nsi_header, assigned_connection_id)
        log.msg('Connection %s released' % assigned_connection_id)
    except error.NSIError, e:
        log.msg('Error releasing %s, %s : %s' % (assigned_connection_id, e.__class__.__name__, str(e)))
        defer.returnValue(None)

    try:
        nsi_header.newCorrelationId()
        yield client.terminate(nsi_header, assigned_connection_id)
        log.msg('Connection %s terminated' % assigned_connection_id)
    except error.NSIError, e:
        log.msg('Error terminating %s, %s : %s' % (assigned_connection_id, e.__class__.__name__, str(e)))
        defer.returnValue(None)


@defer.inlineCallbacks
def provision(client, client_nsa, provider_nsa, connection_id, notification_wait):

    try:
        yield client.provision(client_nsa, provider_nsa, None, connection_id)
        log.msg('Connection %s provisioned' % connection_id)
    except error.NSIError, e:
        log.msg('Error provisioning %s, %s : %s' % (connection_id, e.__class__.__name__, str(e)))

    if notification_wait:
        log.msg("Notification wait not added to provision yet")


@defer.inlineCallbacks
def release(client, client_nsa, provider_nsa, connection_id, notification_wait):

    try:
        yield client.release(client_nsa, provider_nsa, None, connection_id)
        log.msg('Connection %s released' % connection_id)
    except error.NSIError, e:
        log.msg('Error releasing %s, %s : %s' % (connection_id, e.__class__.__name__, str(e)))

{'fraction_non_alphanumeric': 0.11429683445770628, 'fraction_numerical': 0.0022055007784120392, 'mean_word_length': 3.4699913018266164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1515623', 'n_tokens_mistral': 5077, 'n_tokens_neox': 4974, 'n_words': 1192}