<commit_before>#! /usr/bin/env python
# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-

import pprint
import os
import sys
import subprocess
import json
import argparse
import io
import time

from flask import Flask, json, Response, render_template, make_response, request

## Global Var ##
ControllerIP="127.0.0.1"
ControllerPort=8080

DEBUG=0
pp = pprint.PrettyPrinter(indent=4)

app = Flask(__name__)

## Worker Functions ##
def log_error(txt):
  print '%s' % (txt)

def debug(txt):
  if DEBUG:
    print '%s' % (txt)

# @app.route("/wm/flow/get/<flow-id>/json")
# Sample output:
# {"flowId":{"value":"0x5"},"installerId":{"value":"FOOBAR"},"dataPath":{"srcPort":{"dpid":{"value":"00:00:00:00:00:00:00:01"},"port":{"value":0}},"dstPort":{"dpid":{"value":"fc00:db20:35b:7399::5"},"port":{"value":0}},"flowEntries":[{"flowEntryId":"0x1389","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"00:00:00:00:00:00:00:01"},"inPort":{"value":0},"outPort":{"value":1},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null},{"flowEntryId":"0x138a","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"fc00:db20:35b:7399::5"},"inPort":{"value":9},"outPort":{"value":0},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null}]}}

def print_flow_path(parsedResult):
  flowId = parsedResult['flowId']['value']
  installerId = parsedResult['installerId']['value']
  srcSwitch = parsedResult['dataPath']['srcPort']['dpid']['value']
  srcPort = parsedResult['dataPath']['srcPort']['port']['value']
  dstSwitch = parsedResult['dataPath']['dstPort']['dpid']['value']
  dstPort = parsedResult['dataPath']['dstPort']['port']['value']

  print "FlowPath: (flowId = %s installerId = %s src = %s/%s dst = %s/%s)" % (flowId, installerId, srcSwitch, srcPort, dstSwitch, dstPort)

  for f in parsedResult['dataPath']['flowEntries']:
    dpid = f['dpid']['value']
    userState = f['flowEntryUserState']
    switchState = f['flowEntrySwitchState']
    match = f['flowEntryMatch'];
    actions = f['flowEntryActions']
    print "  FlowEntry: (%s, %s, %s)" % (dpid, userState, switchState)

    #
    # Print the match conditions
    #
    if match == None:
      print "   Match: %s" % (match)
    else:
      inPort = match['inPort']
      matchInPort = match['matchInPort']
      srcMac = match['srcMac']
      matchSrcMac = match['matchSrcMac']
      dstMac = match['dstMac']
      matchDstMac = match['matchDstMac']
      vlanId = match['vlanId']
      matchVlanId = match['matchVlanId']
      vlanPriority = match['vlanPriority']
      matchVlanPriority = match['matchVlanPriority']
      ethernetFrameType = match['ethernetFrameType']
      matchEthernetFrameType = match['matchEthernetFrameType']
      ipToS = match['ipToS']
      matchIpToS = match['matchIpToS']
      ipProto = match['ipProto']
      matchIpProto = match['matchIpProto']
      srcIPv4Net = match['srcIPv4Net']
      matchSrcIPv4Net = match['matchSrcIPv4Net']
      dstIPv4Net = match['dstIPv4Net']
      matchDstIPv4Net = match['matchDstIPv4Net']
      srcTcpUdpPort = match['srcTcpUdpPort']
      matchSrcTcpUdpPort = match['matchSrcTcpUdpPort']
      dstTcpUdpPort = match['dstTcpUdpPort']
      matchDstTcpUdpPort = match['matchDstTcpUdpPort']
      if matchInPort == True:
	print "    inPort: %s" % inPort['value']
      if matchSrcMac == True:
	print "    srcMac: %s" % srcMac['value']
      if matchDstMac == True:
	print "    dstMac: %s" % dstMac['value']
      if matchVlanId == True:
	print "    vlanId: %s" % vlanId
      if matchVlanPriority == True:
	print "    vlanPriority: %s" % vlanPriority
      if matchEthernetFrameType == True:
	print "    ethernetFrameType: %s" % hex(ethernetFrameType)
      if matchIpToS == True:
	print "    ipToS: %s" % ipToS
      if matchIpProto == True:
	print "    ipProto: %s" % ipProto
      if matchSrcIPv4Net == True:
	print "    srcIPv4Net: %s" % srcIPv4Net['value']
      if matchDstIPv4Net == True:
	print "    dstIPv4Net: %s" % dstIPv4Net['value']
      if matchSrcTcpUdpPort == True:
	print "    srcTcpUdpPort: %s" % srcTcpUdpPort
      if matchDstTcpUdpPort == True:
	print "    dstTcpUdpPort: %s" % dstTcpUdpPort

    #
    # Print the actions
    #
    if actions == None:
      print "   Actions: %s" % (actions)
    else:
      for a in actions:
	actionType = a['actionType']
	if actionType == "ACTION_OUTPUT":
	  port = a['actionOutput']['port']['value']
	  maxLen = a['actionOutput']['maxLen']
	  print "    actionType: %s port: %s maxLen: %s" % (actionType, port, maxLen)
	if actionType == "ACTION_SET_VLAN_VID":
	  vlanId = a['actionSetVlanId']['vlanId']
	  print "    actionType: %s vlanId: %s" % (actionType, vlanId)
	if actionType == "ACTION_SET_VLAN_PCP":
	  vlanPriority = a['actionSetVlanPriority']['vlanPriority']
	  print "    actionType: %s vlanPriority: %s" % (actionType, vlanPriority)
	if actionType == "ACTION_STRIP_VLAN":
	  stripVlan = a['actionStripVlan']['stripVlan']
	  print "    actionType: %s stripVlan: %s" % (actionType, stripVlan)
	if actionType == "ACTION_SET_DL_SRC":
	  setEthernetSrcAddr = a['actionSetEthernetSrcAddr']['addr']['value']
	  print "    actionType: %s setEthernetSrcAddr: %s" % (actionType, setEthernetSrcAddr)
	if actionType == "ACTION_SET_DL_DST":
	  setEthernetDstAddr = a['actionSetEthernetDstAddr']['addr']['value']
	  print "    actionType: %s setEthernetDstAddr: %s" % (actionType, setEthernetDstAddr)
	if actionType == "ACTION_SET_NW_SRC":
	  setIPv4SrcAddr = a['actionSetIPv4SrcAddr']['addr']['value']
	  print "    actionType: %s setIPv4SrcAddr: %s" % (actionType, setIPv4SrcAddr)
	if actionType == "ACTION_SET_NW_DST":
	  setIPv4DstAddr = a['actionSetIPv4DstAddr']['addr']['value']
	  print "    actionType: %s setIPv4DstAddr: %s" % (actionType, setIPv4DstAddr)
	if actionType == "ACTION_SET_NW_TOS":
	  setIpToS = a['actionSetIpToS']['ipToS']
	  print "    actionType: %s setIpToS: %s" % (actionType, setIpToS)
	if actionType == "ACTION_SET_TP_SRC":
	  setTcpUdpSrcPort = a['actionSetTcpUdpSrcPort']['port']
	  print "    actionType: %s setTcpUdpSrcPort: %s" % (actionType, setTcpUdpSrcPort)
	if actionType == "ACTION_SET_TP_DST":
	  setTcpUdpDstPort = a['actionSetTcpUdpDstPort']['port']
	  print "    actionType: %s setTcpUdpDstPort: %s" % (actionType, setTcpUdpDstPort)
	if actionType == "ACTION_ENQUEUE":
	  port = a['actionEnqueue']['port']['value']
	  queueId = a['actionEnqueue']['queueId']
	  print "    actionType: %s port: %s queueId: %s" % (actionType, port, queueId)

def get_flow_path(flow_id):
  try:
    command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
    debug("get_flow_path %s" % command)

    result = os.popen(command).read()
    debug("result %s" % result)
    if len(result) == 0:
	print "No Flow found"
	return;

    parsedResult = json.loads(result)
    debug("parsed %s" % parsedResult)
  except:
    log_error("Controller IF has issue")
    exit(1)

  print_flow_path(parsedResult)


def get_installer_flow_paths(installer_id, v1, p1, v2, p2):
  try:
    command = "curl -s \"http://%s:%s/wm/flow/getall-by-installer-id/%s/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, installer_id, v1, p1, v2, p2)
    debug("get_installer_flow_paths %s" % command)

    result = os.popen(command).read()
    debug("result %s" % result)
    if len(result) == 0:
	print "No Flows found"
	return;

    parsedResult = json.loads(result)
    debug("parsed %s" % parsedResult)
  except:
    log_error("Controller IF has issue")<commit_msg>Minor tweak in the formatting.<commit_after>#! /usr/bin/env python
# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-

import pprint
import os
import sys
import subprocess
import json
import argparse
import io
import time

from flask import Flask, json, Response, render_template, make_response, request

## Global Var ##
ControllerIP="127.0.0.1"
ControllerPort=8080

DEBUG=0
pp = pprint.PrettyPrinter(indent=4)

app = Flask(__name__)

## Worker Functions ##
def log_error(txt):
  print '%s' % (txt)

def debug(txt):
  if DEBUG:
    print '%s' % (txt)

# @app.route("/wm/flow/get/<flow-id>/json")
# Sample output:
# {"flowId":{"value":"0x5"},"installerId":{"value":"FOOBAR"},"dataPath":{"srcPort":{"dpid":{"value":"00:00:00:00:00:00:00:01"},"port":{"value":0}},"dstPort":{"dpid":{"value":"fc00:db20:35b:7399::5"},"port":{"value":0}},"flowEntries":[{"flowEntryId":"0x1389","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"00:00:00:00:00:00:00:01"},"inPort":{"value":0},"outPort":{"value":1},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null},{"flowEntryId":"0x138a","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"fc00:db20:35b:7399::5"},"inPort":{"value":9},"outPort":{"value":0},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null}]}}

def print_flow_path(parsedResult):
  flowId = parsedResult['flowId']['value']
  installerId = parsedResult['installerId']['value']
  srcSwitch = parsedResult['dataPath']['srcPort']['dpid']['value']
  srcPort = parsedResult['dataPath']['srcPort']['port']['value']
  dstSwitch = parsedResult['dataPath']['dstPort']['dpid']['value']
  dstPort = parsedResult['dataPath']['dstPort']['port']['value']

  print "FlowPath: (flowId = %s installerId = %s src = %s/%s dst = %s/%s)" % (flowId, installerId, srcSwitch, srcPort, dstSwitch, dstPort)

  for f in parsedResult['dataPath']['flowEntries']:
    dpid = f['dpid']['value']
    userState = f['flowEntryUserState']
    switchState = f['flowEntrySwitchState']
    match = f['flowEntryMatch'];
    actions = f['flowEntryActions']
    print "  FlowEntry: (%s, %s, %s)" % (dpid, userState, switchState)

    #
    # Print the match conditions
    #
    if match == None:
      print "   Match: %s" % (match)
    else:
      inPort = match['inPort']
      matchInPort = match['matchInPort']
      srcMac = match['srcMac']
      matchSrcMac = match['matchSrcMac']
      dstMac = match['dstMac']
      matchDstMac = match['matchDstMac']
      vlanId = match['vlanId']
      matchVlanId = match['matchVlanId']
      vlanPriority = match['vlanPriority']
      matchVlanPriority = match['matchVlanPriority']
      ethernetFrameType = match['ethernetFrameType']
      matchEthernetFrameType = match['matchEthernetFrameType']
      ipToS = match['ipToS']
      matchIpToS = match['matchIpToS']
      ipProto = match['ipProto']
      matchIpProto = match['matchIpProto']
      srcIPv4Net = match['srcIPv4Net']
      matchSrcIPv4Net = match['matchSrcIPv4Net']
      dstIPv4Net = match['dstIPv4Net']
      matchDstIPv4Net = match['matchDstIPv4Net']
      srcTcpUdpPort = match['srcTcpUdpPort']
      matchSrcTcpUdpPort = match['matchSrcTcpUdpPort']
      dstTcpUdpPort = match['dstTcpUdpPort']
      matchDstTcpUdpPort = match['matchDstTcpUdpPort']
      if matchInPort == True:
	print "    inPort: %s" % inPort['value']
      if matchSrcMac == True:
	print "    srcMac: %s" % srcMac['value']
      if matchDstMac == True:
	print "    dstMac: %s" % dstMac['value']
      if matchVlanId == True:
	print "    vlanId: %s" % vlanId
      if matchVlanPriority == True:
	print "    vlanPriority: %s" % vlanPriority
      if matchEthernetFrameType == True:
	print "    ethernetFrameType: %s" % hex(ethernetFrameType)
      if matchIpToS == True:
	print "    ipToS: %s" % ipToS
      if matchIpProto == True:
	print "    ipProto: %s" % ipProto
      if matchSrcIPv4Net == True:
	print "    srcIPv4Net: %s" % srcIPv4Net['value']
      if matchDstIPv4Net == True:
	print "    dstIPv4Net: %s" % dstIPv4Net['value']
      if matchSrcTcpUdpPort == True:
	print "    srcTcpUdpPort: %s" % srcTcpUdpPort
      if matchDstTcpUdpPort == True:
	print "    dstTcpUdpPort: %s" % dstTcpUdpPort

    #
    # Print the actions
    #
    if actions == None:
      print "   Actions: %s" % (actions)
    else:
      for a in actions:
	actionType = a['actionType']
	if actionType == "ACTION_OUTPUT":
	  port = a['actionOutput']['port']['value']
	  maxLen = a['actionOutput']['maxLen']
	  print "    actionType: %s port: %s maxLen: %s" % (actionType, port, maxLen)
	if actionType == "ACTION_SET_VLAN_VID":
	  vlanId = a['actionSetVlanId']['vlanId']
	  print "    actionType: %s vlanId: %s" % (actionType, vlanId)
	if actionType == "ACTION_SET_VLAN_PCP":
	  vlanPriority = a['actionSetVlanPriority']['vlanPriority']
	  print "    actionType: %s vlanPriority: %s" % (actionType, vlanPriority)
	if actionType == "ACTION_STRIP_VLAN":
	  stripVlan = a['actionStripVlan']['stripVlan']
	  print "    actionType: %s stripVlan: %s" % (actionType, stripVlan)
	if actionType == "ACTION_SET_DL_SRC":
	  setEthernetSrcAddr = a['actionSetEthernetSrcAddr']['addr']['value']
	  print "    actionType: %s setEthernetSrcAddr: %s" % (actionType, setEthernetSrcAddr)
	if actionType == "ACTION_SET_DL_DST":
	  setEthernetDstAddr = a['actionSetEthernetDstAddr']['addr']['value']
	  print "    actionType: %s setEthernetDstAddr: %s" % (actionType, setEthernetDstAddr)
	if actionType == "ACTION_SET_NW_SRC":
	  setIPv4SrcAddr = a['actionSetIPv4SrcAddr']['addr']['value']
	  print "    actionType: %s setIPv4SrcAddr: %s" % (actionType, setIPv4SrcAddr)
	if actionType == "ACTION_SET_NW_DST":
	  setIPv4DstAddr = a['actionSetIPv4DstAddr']['addr']['value']
	  print "    actionType: %s setIPv4DstAddr: %s" % (actionType, setIPv4DstAddr)
	if actionType == "ACTION_SET_NW_TOS":
	  setIpToS = a['actionSetIpToS']['ipToS']
	  print "    actionType: %s setIpToS: %s" % (actionType, setIpToS)
	if actionType == "ACTION_SET_TP_SRC":
	  setTcpUdpSrcPort = a['actionSetTcpUdpSrcPort']['port']
	  print "    actionType: %s setTcpUdpSrcPort: %s" % (actionType, setTcpUdpSrcPort)
	if actionType == "ACTION_SET_TP_DST":
	  setTcpUdpDstPort = a['actionSetTcpUdpDstPort']['port']
	  print "    actionType: %s setTcpUdpDstPort: %s" % (actionType, setTcpUdpDstPort)
	if actionType == "ACTION_ENQUEUE":
	  port = a['actionEnqueue']['port']['value']
	  queueId = a['actionEnqueue']['queueId']
	  print "    actionType: %s port: %s queueId: %s" % (actionType, port, queueId)

def get_flow_path(flow_id):
  try:
    command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
    debug("get_flow_path %s" % command)

    result = os.popen(command).read()
    debug("result %s" % result)
    if len(result) == 0:
      print "No Flow found"
      return;

    parsedResult = json.loads(result)
    debug("parsed %s" % parsedResult)
  except:
    log_error("Controller IF has issue")
    exit(1)

  print_flow_path(parsedResult)


def get_installer_flow_paths(installer_id, v1, p1, v2, p2):
  try:
    command = "curl -s \"http://%s:%s/wm/flow/getall-by-installer-id/%s/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, installer_id, v1, p1, v2, p2)
    debug("get_installer_flow_paths %s" % command)

    result = os.popen(command).read()
    debug("result %s" % result)
    if len(result) == 0:
	print "No Flows found"
	return;

    parsedResult = json.loads(result)
    debug("parsed %s" % parsedResult)
  except:
    log_error("Controller IF has issue")
{'fraction_non_alphanumeric': 0.16526171338953147, 'fraction_numerical': 0.015421812716460825, 'mean_word_length': 4.655580192165558, 'pattern_counts': {'":': 104, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6109866', 'n_tokens_mistral': 5726, 'n_tokens_neox': 5529, 'n_words': 1027}