Initial commit of PassiveTest

Change-Id: Idcd9a0c72df5eae6b4eedc544e473ebc9763ccdb
diff --git a/xos/synchronizer/steps/roles/setup_probe/files/viv b/xos/synchronizer/steps/roles/setup_probe/files/viv
new file mode 100755
index 0000000..7cd7375
--- /dev/null
+++ b/xos/synchronizer/steps/roles/setup_probe/files/viv
@@ -0,0 +1,268 @@
+#!/usr/bin/env python
+import requests, sys, getopt, json
+ 
+COMMANDS     = [ 'show','reset', 'add','delete','deploy',"V_BUFS","NUM_BUFS", "INPUT_MODE", "TX_MODE", "QUEUE_TYPE" ]
+trafficTypes = ['TSA_CP','TSA_UP', 'TAA_CP', 'TAA_UP']
+INPUT_MODES=["mmap","pfring","pfring_zc","dpdk"]
+TX_MODES=["frp_udp","frp_tcp","viv"]
+QUEUE_TYPES=["CLFFIFO","dpdk"]
+RESOURCE_NAMES =  [ 'V_BUFS', 'NUM_BUFS', 'INPUT_MODE', 'TX_MODE', 'QUEUE_TYPE']
+
+settings = {
+             'ipAddress':'127.0.0.1',
+             'port':'8080',
+             'api_version':'v1.0',
+             'resource': 'vivs/1',
+             'operation':'GET',
+             'verbose': False,
+             'data': None
+            }
+ 
+class restClient:
+
+  def doIt( self, settings ):
+    try:
+      hdrs = { 'Accept':'application/json','content-type':'application/json' }
+      url = "http://%s:%s/%s" % ( settings['ipAddress'], settings['port'], settings['api_version'] )
+      if settings['resource']:
+        url = "%s/%s" % (url, settings['resource'])
+
+      if settings['operation'] == 'GET':
+        r = requests.get( url, headers = hdrs )
+      elif settings['operation'] == 'POST':
+        data = json.loads(settings['data'])
+        r = requests.post( url, data=json.dumps(data), headers = hdrs )
+
+      if r.status_code == requests.codes.ok:
+        if r.headers['content-type'] == 'application/json':
+          print json.dumps( r.json(), indent=4 )
+        else:
+          print "Received unexpected content type: %s" % r.headers['content-type']
+          if settings['verbose']:
+            print r.text
+
+      if settings['verbose']:
+        print "\nOperation:\n    %s"            % settings['operation']
+        print "\nURL:\n    %s"                  % r.url 
+        print "\nHeaders sent:\n    %s"         % r.request.headers
+        print "\nResponse status code:\n    %s" % r.status_code
+
+    except requests.ConnectionError as e:
+      print e
+      raise  # re-raise exception
+    except ValueError as e:
+      print "Invalid JSON: %s" % e 
+      raise # re-raise exception
+
+
+def stringFromList( p ):
+  """
+  e.g. ['a', 'b', 'c' ] becomes "[a|b|c]"
+  """
+  return str( p ).replace("'","").replace(", ","|")
+
+def usage():
+  print "Usage:"
+  print " $ %s [-i ip][-p port][-a version][-v] command" % sys.argv[0]
+  print ""
+  print "optional arguments:"
+  print "  -i <ip>          IP address of server. (Default: 127.0.0.1)"
+  print "  -p <port>        Port on server.       (Default: 8080)"
+  print "  -a <version>     API version to use.   (Default: v1.0)"
+  print "  -v               Be verbose"
+  print ""
+  print "command"
+  print "-------"
+  print " show [<path>]"
+  print " add input <device> <type> [<type> [...]]"
+  print " add output <ip_address> <port> <type>"
+  print " delete input <device> [<type> [<type> [...]]]"
+  print " delete output <ip_address>[:<port>] [<type> [<type> [...]]]"
+  print " reset"
+  print " deploy"
+  print ""
+  print " V_BUFS <value>"
+  print " NUM_BUFS <value>"
+  print " INPUT_MODE <input-mode>"
+  print " TX_MODE <tx-mode>"
+  print " QUEUE_TYPE <queue-type>"
+  print ""
+  print ""
+  print " Where:"
+  print "    <type>       = %s" % stringFromList( trafficTypes )
+  print "    <input_mode> = %s" % stringFromList(INPUT_MODES )
+  print "    <tx-mode>    = %s" % stringFromList( QUEUE_TYPES )
+  print ""
+
+
+def getTrafficTypes( args ):
+  traffic_types=[]
+  while len(args) > 0:
+    t = args.pop(0)
+    if t not in trafficTypes:
+      raise ValueError("Invalid traffic type '%s'" % t )
+    traffic_types.append( t )
+  return traffic_types
+
+def parseAddInputCommand( args ):
+  if len( args ) < 2:
+    raise ValueError("Usage: %s add input <device> <traffic_type> [<traffic_type> [...]]" % sys.argv[0] )
+
+  device = args.pop(0)
+  tt = getTrafficTypes( args )
+  data = { "inputs":[{"device":device,"traffic_types":tt}]}
+  settings['resource']='vivs/1/add'
+  settings['operation']='POST'
+  settings['data'] = json.dumps( data )
+
+def parseAddOutputCommand( args ):
+  if len( args ) != 3:
+    raise ValueError("Usage: %s add output <ip_address> <port> <traffic_type>" % sys.argv[0] )
+
+  ip_address = args.pop(0)
+  port = args.pop(0)
+  traffic_type = args.pop(0)
+  if traffic_type not in trafficTypes:
+    raise ValueError("Invalid traffic type '%s'" % traffic_type )
+
+  data = {"outputs":[{"ip_address":ip_address,"type":traffic_type,"port":port}]}
+  settings['resource']='vivs/1/add'
+  settings['operation']='POST'
+  settings['data'] = json.dumps( data )
+
+def parseDeleteInputCommand( args ):
+  if len( args ) < 1:
+    raise ValueError( "Usage: %s delete input <device> [<traffic_type> [<traffic_type> [...]]]" % sys.argv[0] )
+
+  device = args.pop(0)
+  tt = getTrafficTypes( args )
+  data = {"inputs":[{"device":device,"traffic_types":tt}]}
+  settings['resource']='vivs/1/delete'
+  settings['operation']='POST'
+  settings['data'] = json.dumps( data )
+
+def parseDeleteOutputCommand( args ):
+  if len(args) == 0:
+    raise ValueError( "Usage: %s delete output <ip_address>[:<port>] [<traffic_type> [<traffic_type> [...]]]" % sys.argv[0] )
+
+  ipp = args.pop(0).split(':')
+  ip_address = ipp[0]
+  try:
+    port = ipp[1]
+  except IndexError:
+    # No port was specified
+    # All ports with spec'd traffic types will be removed for 
+    # the IP given.
+    port = None
+
+  tt = getTrafficTypes( args )
+  data = {"outputs":[{"ip_address":ip_address,"port":port, "traffic_types":tt}]}
+  settings['resource']='vivs/1/delete'
+  settings['operation']='POST'
+  settings['data'] = json.dumps( data )
+
+def parseArgs( argv ):
+  try:
+    opts,args = getopt.getopt( argv,
+        "i:p:a:vh", ["ip=","port=","api_version="])
+  except getopt.GetoptError as e:
+    print e
+    raise    # re-raise exception
+  for opt,arg in opts:
+    if opt == '-h':
+      usage()
+      sys.exit()
+    elif opt == '-v':
+      settings['verbose'] = True
+    elif opt in ("-i","--ip"):
+      settings['ipAddress'] = arg
+    elif opt in ("-p","--port"):
+      settings['port'] = arg
+    elif opt in ("-a","--api_version"):
+      settings['api_version'] = arg
+
+  # process residual non option args
+  if len(args) == 0:
+    raise ValueError( "Expected one of: %s" % str( COMMANDS ) )
+
+  cmd = args.pop(0)
+  if cmd not in COMMANDS:
+    print 'Unknown command', cmd
+    sys.exit(1)
+
+  if cmd in ['show']:
+    if len(args) != 0:
+      settings['resource'] = args.pop(0)
+
+  elif cmd in ['reset']:
+    settings['resource']='vivs/1/reset' 
+    settings['operation'] = 'POST'
+    settings['data'] = json.dumps( {} )
+
+  elif cmd in ['add']:
+
+    if len(args) == 0:
+      raise ValueError("Expected 'input' or 'output'")
+
+    direction = args.pop(0)
+    if direction not in ['input','output']:
+      raise ValueError( "expected 'input' or 'output', found '%s'" % direction )
+ 
+    if direction == 'input':
+      parseAddInputCommand( args )
+    else:
+      parseAddOutputCommand( args )
+
+  elif cmd in ['delete']:
+    if len(args) == 0:
+      raise ValueError("Expected 'input' or 'output'" )
+
+    direction = args.pop(0)
+    if direction not in ['input','output']:
+      raise ValueError("expected 'input' or 'output', found '%s'" % direction )
+
+    if direction == 'input':
+      parseDeleteInputCommand( args )
+    else:
+      parseDeleteOutputCommand( args )
+
+  elif cmd in ['deploy']:
+    settings['resource']='vivs/1/deploy'
+    settings['operation'] = 'POST'
+    settings['data'] = '{}'
+
+  else:
+
+    if cmd in RESOURCE_NAMES:
+      if len( args ) == 0 :
+          raise ValueError( 'No value supplied' )
+
+      val = args.pop(0)
+      # The server will complain if it does not like the value.
+      settings['resource']='vivs/1/%s' % cmd
+      settings['operation'] = 'POST'
+      settings['data'] = json.dumps( { cmd : val } )
+
+
+  return settings
+
+
+def RestClient(argv):
+  settings = parseArgs( argv )
+  client = restClient()
+  client.doIt( settings )
+
+
+if __name__ == '__main__':
+  try:
+    RestClient(sys.argv[1:])
+  except ValueError as e:
+    print e
+    sys.exit(2)
+  except getopt.GetoptError as e:
+    print e
+    sys.exit(3)
+  except requests.ConnectionError as e:
+    print e
+    sys.exit(4)
+