<commit_before>#!/usr/bin/env python
"""
PRE-REQUISITES:
   1.  Python 2.x
   2.  Install python-requests:
        a.  sudo easy_install requests
   3.  Go to dev.elbrys.com and follow the directions there

BUG/SUGGESTIONS:
Mail bug reports and suggestion to : <EMAIL>
Bug reports may also be logged in github here:  https://github.com/Elbrys/ODL-S-Sample-Apps/issues


DESCRIPTION:
    This is an intermediate complexity application that utilizes multi-threading.

    The application is associated with the switch you configured on the command line.
     You must have configured that switch to use ODL-S as its openflow controller per 
     the instructions on dev.elbrys.com. Any endpoint that does not yet have an associated 
     policy (one you gave it using this application - see below) will be detected by ODL-S 
     and will cause ODL-S to generate an 'unmanaged endpoint' event.
    
    This application has created a subscription to ODL-S events.  It will receive events 
     from your switch.  IF ODL-S Event logging is ENABLED (see below): it will print a message to the screen 
     when it receives ODL-S events. When a new 'unmanaged endpoint' event occurs it will print a message to 
     the screen as well as add that endpoint to its list of known endpoints.  Event logging is disabled by default.
   
    All traffic for unmanaged endpoints will be blocked until you associate a policy to the endpoint.
  
    This application will allow you to view a list of all unmanaged endpoints and select 
     a target endpoint and then do any number of actions to that endpoint, including 
     associating a policy to it.
    
    While this application is running, you can go to your ODL-S dashboard (sdn-developer.elbrys.com) 
     and refresh the screen and you will see this application listed in the applications table.  In addition
     the switch you configured on the command line should indicate it is active on the ODL-S dashboard
     by being displayed in green.
    
    Enable ODL-S event logging, connect one or more user devices (laptop, tablet, phone) to ports on your network device. 
     After you connect the device, cause it to send network traffic (ping, web browse, etc) 
     You should see an ODL-S event indicating an 'unmanaged endpoint' with a MAC address 
     that matches the device you have connected to the switch (if ODL-S event logging is enabled).
"""

import sys, os, errno, signal 
import requests
import json
import time
import argparse
from requests.auth import HTTPBasicAuth
import threading
import Queue
import traceback

def interuppt_handler(signum, frame):
    sys.exit(-2) #Terminate process here as catching the signal removes the close process behaviour of Ctrl-C

signal.signal(signal.SIGINT, interuppt_handler)

 
def GetAuthToken(user, password):
    # This calls the  api to create an authorization token to make other calls
    # RETURNS: authorization token
    url = odlsBaseUrl + '/auth/token'
    print "GetAuthToken: " + odlsBaseUrl
    headers = {'content-type': 'application/json'}
    user = "name="+user
    appId = requests.get(url, headers=headers, auth=HTTPBasicAuth(user,password))
    result = appId.text
    status = appId.status_code
    if ((status >= 200) & (status <=299)):
        authToken = appId.json()
        authToken = authToken['token']
    else:
        print " "
        print "!! Error !!"  
        print "    Unable to create authorization token.  Double check that the username and password you entered."
        print "    See usage below:"
        parser.print_help()
        sys.exit()

    return authToken;

def CreateApp(authToken, switch, parser):
    # This removes any zombie apps and then calls the api to create an application
    # RETURNS: app identifier
    RemoveZombieApps(authToken, switch)

    url = odlsBaseUrl + '/applications'
    payload = {'name': 'ODL-S Demo App2 - Connected to switch: ' + switch,<commit_msg>Fix name of App3.<commit_after>#!/usr/bin/env python
"""
PRE-REQUISITES:
   1.  Python 2.x
   2.  Install python-requests:
        a.  sudo easy_install requests
   3.  Go to dev.elbrys.com and follow the directions there

BUG/SUGGESTIONS:
Mail bug reports and suggestion to : <EMAIL>
Bug reports may also be logged in github here:  https://github.com/Elbrys/ODL-S-Sample-Apps/issues


DESCRIPTION:
    This is an intermediate complexity application that utilizes multi-threading.

    The application is associated with the switch you configured on the command line.
     You must have configured that switch to use ODL-S as its openflow controller per 
     the instructions on dev.elbrys.com. Any endpoint that does not yet have an associated 
     policy (one you gave it using this application - see below) will be detected by ODL-S 
     and will cause ODL-S to generate an 'unmanaged endpoint' event.
    
    This application has created a subscription to ODL-S events.  It will receive events 
     from your switch.  IF ODL-S Event logging is ENABLED (see below): it will print a message to the screen 
     when it receives ODL-S events. When a new 'unmanaged endpoint' event occurs it will print a message to 
     the screen as well as add that endpoint to its list of known endpoints.  Event logging is disabled by default.
   
    All traffic for unmanaged endpoints will be blocked until you associate a policy to the endpoint.
  
    This application will allow you to view a list of all unmanaged endpoints and select 
     a target endpoint and then do any number of actions to that endpoint, including 
     associating a policy to it.
    
    While this application is running, you can go to your ODL-S dashboard (sdn-developer.elbrys.com) 
     and refresh the screen and you will see this application listed in the applications table.  In addition
     the switch you configured on the command line should indicate it is active on the ODL-S dashboard
     by being displayed in green.
    
    Enable ODL-S event logging, connect one or more user devices (laptop, tablet, phone) to ports on your network device. 
     After you connect the device, cause it to send network traffic (ping, web browse, etc) 
     You should see an ODL-S event indicating an 'unmanaged endpoint' with a MAC address 
     that matches the device you have connected to the switch (if ODL-S event logging is enabled).
"""

import sys, os, errno, signal 
import requests
import json
import time
import argparse
from requests.auth import HTTPBasicAuth
import threading
import Queue
import traceback

def interuppt_handler(signum, frame):
    sys.exit(-2) #Terminate process here as catching the signal removes the close process behaviour of Ctrl-C

signal.signal(signal.SIGINT, interuppt_handler)

 
def GetAuthToken(user, password):
    # This calls the  api to create an authorization token to make other calls
    # RETURNS: authorization token
    url = odlsBaseUrl + '/auth/token'
    print "GetAuthToken: " + odlsBaseUrl
    headers = {'content-type': 'application/json'}
    user = "name="+user
    appId = requests.get(url, headers=headers, auth=HTTPBasicAuth(user,password))
    result = appId.text
    status = appId.status_code
    if ((status >= 200) & (status <=299)):
        authToken = appId.json()
        authToken = authToken['token']
    else:
        print " "
        print "!! Error !!"  
        print "    Unable to create authorization token.  Double check that the username and password you entered."
        print "    See usage below:"
        parser.print_help()
        sys.exit()

    return authToken;

def CreateApp(authToken, switch, parser):
    # This removes any zombie apps and then calls the api to create an application
    # RETURNS: app identifier
    RemoveZombieApps(authToken, switch)

    url = odlsBaseUrl + '/applications'
    payload = {'name': 'ODL-S Demo App3 - Connected to switch: ' + switch,
{'fraction_non_alphanumeric': 0.060994166878011666, 'fraction_numerical': 0.00317017499365965, 'mean_word_length': 3.210891617725574, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '3640751', 'n_tokens_mistral': 2167, 'n_tokens_neox': 2074, 'n_words': 1008}