Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 import argparse
00025 import httplib
00026 import json
00027 import os
00028 import sys
00029 import urllib
00030
00031
00032
00033
00034 def GetAllFilterNames(mm_hostname):
00035 mm2_config_in_json = GetMM2ConfigInJson(mm_hostname)
00036 jdata = json.loads(mm2_config_in_json)
00037 full_config = jdata[2]
00038 return set([str(filt['name']) for filt in full_config["filters"]])
00039
00040
00041
00042
00043 def GetMM2ConfigInJson(mm2name):
00044 conn = httplib.HTTPConnection(mm2name)
00045 conn.request("GET", "/mm2/mpc/mm2.py/get_config_json")
00046 resp = conn.getresponse()
00047
00048 if (resp.status/100) != 2:
00049 raise Exception,resp.reason
00050 data = resp.read()
00051 conn.close()
00052 return data
00053
00054
00055
00056
00057 class FiltSetting(object):
00058
00059 def __init__(self, filtname, toband):
00060 bnum = int(toband)
00061 if (bnum <= 0) or (bnum > 5):
00062 raise ValueError, "Band number for \"%s\" must be in range 1..5" % filtname
00063 self.FiltName = filtname
00064 self.ToBand = bnum
00065
00066
00067
00068
00069 def SetFiltMap(mm2host, a2bfilt_vals, b2afilt_vals, all_filter_names=None):
00070
00071 if all_filter_names is None:
00072 all_filter_names = GetAllFilterNames(mm2host)
00073
00074
00075 if (a2bfilt_vals is None):
00076 a2bfilt_vals = []
00077 if (b2afilt_vals is None):
00078 b2afilt_vals = []
00079
00080
00081 for fvals in (a2bfilt_vals, b2afilt_vals):
00082 for filt in fvals:
00083 if filt.FiltName not in all_filter_names:
00084 raise ValueError, "Filter name \"%s\" is not present" % filt.FiltName
00085
00086 offa2b = all_filter_names.difference(set(map(lambda x: x.FiltName, a2bfilt_vals)))
00087 offb2a = all_filter_names.difference(set(map(lambda x: x.FiltName, b2afilt_vals)))
00088
00089 fdict = {}
00090 for fn in offa2b:
00091 fdict["DBNDS_%s" % fn] = "down-band-none"
00092 for fn in offb2a:
00093 fdict["UBNDS_%s" % fn] = "up-band-none"
00094
00095 for fs in a2bfilt_vals:
00096 fdict["DBNDS_%s" % fs.FiltName] = "down-band-%d" % fs.ToBand
00097 for fs in b2afilt_vals:
00098 fdict["UBNDS_%s" % fs.FiltName] = "up-band-%d" % fs.ToBand
00099
00100 fdict["SUBMIT_FILTERMAP_BUTTON"] = "Submit"
00101
00102 conn = httplib.HTTPConnection(mm2host)
00103 params = urllib.urlencode(fdict)
00104 headers = {"Content-type": "application/x-www-form-urlencoded",
00105 "Accept": "text/plain"}
00106 conn.request("POST", "/mm2/mpc/mm2.py/process_filtmap", params, headers)
00107 resp = conn.getresponse()
00108
00109 if (resp.status/100) != 2:
00110 raise Exception,resp.reason
00111 data = resp.read()
00112 conn.close()
00113
00114
00115
00116
00117 if __name__ == "__main__":
00118 a2bfilts = []
00119 b2afilts = []
00120
00121 parser = argparse.ArgumentParser(description="Set classification filter map.")
00122
00123 parser.add_argument("-A", action="append", nargs=2, dest="a2b",
00124 help="Specify a filter name and band number for packats flowing LanA to Lan B.")
00125
00126 parser.add_argument("-B", action="append", nargs=2, dest="b2a",
00127 help="Specify a filter name and band number for packats flowing LanB to Lan A.")
00128
00129 parser.add_argument("-d", "--defaults", action="store_true",
00130 help="Turn all filters off (default state).")
00131
00132 parser.add_argument("-f", "--filters", action="store_true",
00133 help="List all the flters, do nothing else.")
00134
00135 parser.add_argument("mm_hostname",
00136 help='The host name or IP address of the Mini Maxwell/Maxwell G.')
00137
00138 pargs = parser.parse_args()
00139
00140 mm_hostname = pargs.mm_hostname
00141 a2bargs = pargs.a2b
00142 b2aargs = pargs.b2a
00143
00144 if pargs.filters:
00145 try:
00146 all_filter_names = GetAllFilterNames(mm_hostname)
00147 except Exception, err:
00148 print "HTTP Exception occurred:", str(err)
00149 sys.exit(1)
00150
00151 for filt in all_filter_names:
00152 print filt
00153 sys.exit(0)
00154
00155 if pargs.defaults:
00156 SetFiltMap(mm_hostname, a2bfilts, b2afilts)
00157 sys.exit(0)
00158
00159 if a2bargs is not None:
00160 for farg in a2bargs:
00161 try:
00162 a2bfilts.append(FiltSetting(farg[0], farg[1]))
00163 except Exception, err:
00164 print "Parameter error: %s - Aborting." % str(err)
00165 sys.exit(1)
00166
00167 if b2aargs is not None:
00168 for farg in b2aargs:
00169 try:
00170 b2afilts.append(FiltSetting(farg[0], farg[1]))
00171 except Exception, err:
00172 print "Parameter error: %s - Aborting." % str(err)
00173 sys.exit(1)
00174
00175 SetFiltMap(mm_hostname, a2bfilts, b2afilts)