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