setfilters26.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 #**********************************************************************
4 # Copyright (c) 2013 InterWorking Labs, All Rights Reserved *
5 # *
6 # RESTRICTED RIGHTS LEGEND *
7 # *
8 # This software is provided with RESTRICTED RIGHTS. *
9 # *
10 # Use, duplication, or disclosure by the U.S. Government is subject *
11 # to restrictions set forth in subparagraph (c)(1)(ii) of the Rights *
12 # in Technical Data and Computer Software clause at DFARS *
13 # 252.227-7013 or subparagraphs (c)(1) and (2) of the Commercial *
14 # Computer Software - Restricted Rights at 48 CFR 52.227-19, as *
15 # applicable. The "Manufacturer" for purposes of these regulations *
16 # is InterWorking Labs, PO Box 66190, Scotts Valley, California 95060 *
17 # U.S.A. *
18 #**********************************************************************
19 
20 #**********************************************************************
21 # $Id: setfilters26.py 464 2013-12-03 00:43:56Z karl $
22 #**********************************************************************
23 
24 have_argparse = False
25 try:
26  import argparse
27  have_argparse = True
28 except:
29  pass
30 
31 import httplib
32 import json
33 import os
34 import sys
35 import urllib
36 
37 #**********************************************************************
38 # GetAllFilterNames()
39 #**********************************************************************
40 def GetAllFilterNames(mm_hostname):
41  mm2_config_in_json = GetMM2ConfigInJson(mm_hostname)
42  jdata = json.loads(mm2_config_in_json)
43  full_config = jdata[2]
44  return set([str(filt['name']) for filt in full_config["filters"]])
45 
46 #**********************************************************************
47 # GetMM2ConfigInJson()
48 #**********************************************************************
49 def GetMM2ConfigInJson(mm2name):
50  conn = httplib.HTTPConnection(mm2name)
51  conn.request("GET", "/mm2/mpc/mm2.py/get_config_json")
52  resp = conn.getresponse()
53 
54  if (resp.status/100) != 2:
55  raise Exception,resp.reason
56  data = resp.read()
57  conn.close()
58  return data
59 
60 #**********************************************************************
61 # class FiltSetting
62 #**********************************************************************
63 class FiltSetting(object):
64 
65  def __init__(self, filtname, toband):
66  bnum = int(toband)
67  if (bnum <= 0) or (bnum > 5):
68  raise ValueError, "Band number for \"%s\" must be in range 1..5" % filtname
69  self.FiltName = filtname
70  self.ToBand = bnum
71 
72 #**********************************************************************
73 # SetFiltMap()
74 #**********************************************************************
75 def SetFiltMap(mm2host, a2bfilt_vals, b2afilt_vals, all_filter_names=None):
76 
77  if all_filter_names is None:
78  all_filter_names = GetAllFilterNames(mm2host)
79 
80  # Survive a None for either of the lists of filter settings
81  if (a2bfilt_vals is None):
82  a2bfilt_vals = []
83  if (b2afilt_vals is None):
84  b2afilt_vals = []
85 
86  # Make sure we do not have any stray filter names
87  for fvals in (a2bfilt_vals, b2afilt_vals):
88  for filt in fvals:
89  if filt.FiltName not in all_filter_names:
90  raise ValueError, "Filter name \"%s\" is not present" % filt.FiltName
91 
92  offa2b = all_filter_names.difference(set(map(lambda x: x.FiltName, a2bfilt_vals)))
93  offb2a = all_filter_names.difference(set(map(lambda x: x.FiltName, b2afilt_vals)))
94 
95  fdict = {}
96  for fn in offa2b:
97  fdict["DBNDS_%s" % fn] = "down-band-none"
98  for fn in offb2a:
99  fdict["UBNDS_%s" % fn] = "up-band-none"
100 
101  for fs in a2bfilt_vals:
102  fdict["DBNDS_%s" % fs.FiltName] = "down-band-%d" % fs.ToBand
103  for fs in b2afilt_vals:
104  fdict["UBNDS_%s" % fs.FiltName] = "up-band-%d" % fs.ToBand
105 
106  fdict["SUBMIT_FILTERMAP_BUTTON"] = "Submit"
107 
108  conn = httplib.HTTPConnection(mm2host)
109  params = urllib.urlencode(fdict)
110  headers = {"Content-type": "application/x-www-form-urlencoded",
111  "Accept": "text/plain"}
112  conn.request("POST", "/mm2/mpc/mm2.py/process_filtmap", params, headers)
113  resp = conn.getresponse()
114 
115  if (resp.status/100) != 2:
116  raise Exception,resp.reason
117  data = resp.read()
118  conn.close()
119 
120 #**********************************************************************
121 # Entry
122 #**********************************************************************
123 if __name__ == "__main__":
124  a2bfilts = []
125  b2afilts = []
126 
127  parser = argparse.ArgumentParser(description="Set classification filter map.")
128 
129  parser.add_argument("-A", action="append", nargs=2, dest="a2b",
130  help="Specify a filter name and band number for packats flowing LanA to Lan B.")
131 
132  parser.add_argument("-B", action="append", nargs=2, dest="b2a",
133  help="Specify a filter name and band number for packats flowing LanB to Lan A.")
134 
135  parser.add_argument("-d", "--defaults", action="store_true",
136  help="Turn all filters off (default state).")
137 
138  parser.add_argument("-f", "--filters", action="store_true",
139  help="List all the flters, do nothing else.")
140 
141  parser.add_argument("mm_hostname",
142  help='The host name or IP address of the Mini Maxwell/Maxwell G.')
143 
144  pargs = parser.parse_args()
145 
146  mm_hostname = pargs.mm_hostname
147  a2bargs = pargs.a2b
148  b2aargs = pargs.b2a
149 
150  if pargs.filters:
151  try:
152  all_filter_names = GetAllFilterNames(mm_hostname)
153  except Exception, err:
154  print "HTTP Exception occurred:", str(err)
155  sys.exit(1)
156 
157  for filt in all_filter_names:
158  print filt
159  sys.exit(0)
160 
161  if pargs.defaults:
162  SetFiltMap(mm_hostname, a2bfilts, b2afilts)
163  sys.exit(0)
164 
165  if a2bargs is not None:
166  for farg in a2bargs:
167  try:
168  a2bfilts.append(FiltSetting(farg[0], farg[1]))
169  except Exception, err:
170  print "Parameter error: %s - Aborting." % str(err)
171  sys.exit(1)
172 
173  if b2aargs is not None:
174  for farg in b2aargs:
175  try:
176  b2afilts.append(FiltSetting(farg[0], farg[1]))
177  except Exception, err:
178  print "Parameter error: %s - Aborting." % str(err)
179  sys.exit(1)
180 
181  SetFiltMap(mm_hostname, a2bfilts, b2afilts)
def __init__(self, filtname, toband)
Definition: setfilters26.py:65
def GetMM2ConfigInJson(mm2name)
Definition: setfilters26.py:49
def SetFiltMap(mm2host, a2bfilt_vals, b2afilt_vals, all_filter_names=None)
Definition: setfilters26.py:75
def GetAllFilterNames(mm_hostname)
Definition: setfilters26.py:40


mini_maxwell
Author(s): Yusuke Furuta
autogenerated on Wed Jul 10 2019 03:47:09