setfilters.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: setfilters.py 429 2013-08-16 21:36:11Z karl $
22 #**********************************************************************
23 
24 import argparse
25 import httplib
26 import json
27 import os
28 import sys
29 import urllib
30 
31 #**********************************************************************
32 # GetAllFilterNames()
33 #**********************************************************************
34 def GetAllFilterNames(mm_hostname):
35  mm2_config_in_json = GetMM2ConfigInJson(mm_hostname)
36  jdata = json.loads(mm2_config_in_json)
37  full_config = jdata[2]
38  return set([str(filt['name']) for filt in full_config["filters"]])
39 
40 #**********************************************************************
41 # GetMM2ConfigInJson()
42 #**********************************************************************
43 def GetMM2ConfigInJson(mm2name):
44  conn = httplib.HTTPConnection(mm2name)
45  conn.request("GET", "/mm2/mpc/mm2.py/get_config_json")
46  resp = conn.getresponse()
47 
48  if (resp.status/100) != 2:
49  raise Exception(resp.reason)
50  data = resp.read()
51  conn.close()
52  return data
53 
54 #**********************************************************************
55 # class FiltSetting
56 #**********************************************************************
57 class FiltSetting(object):
58 
59  def __init__(self, filtname, toband):
60  bnum = int(toband)
61  if (bnum <= 0) or (bnum > 5):
62  raise ValueError("Band number for \"%s\" must be in range 1..5" % filtname)
63  self.FiltName = filtname
64  self.ToBand = bnum
65 
66 #**********************************************************************
67 # SetFiltMap()
68 #**********************************************************************
69 def SetFiltMap(mm2host, a2bfilt_vals, b2afilt_vals, all_filter_names=None):
70 
71  if all_filter_names is None:
72  all_filter_names = GetAllFilterNames(mm2host)
73 
74  # Survive a None for either of the lists of filter settings
75  if (a2bfilt_vals is None):
76  a2bfilt_vals = []
77  if (b2afilt_vals is None):
78  b2afilt_vals = []
79 
80  # Make sure we do not have any stray filter names
81  for fvals in (a2bfilt_vals, b2afilt_vals):
82  for filt in fvals:
83  if filt.FiltName not in all_filter_names:
84  raise ValueError("Filter name \"%s\" is not present" % filt.FiltName)
85 
86  offa2b = all_filter_names.difference(set(map(lambda x: x.FiltName, a2bfilt_vals)))
87  offb2a = all_filter_names.difference(set(map(lambda x: x.FiltName, b2afilt_vals)))
88 
89  fdict = {}
90  for fn in offa2b:
91  fdict["DBNDS_%s" % fn] = "down-band-none"
92  for fn in offb2a:
93  fdict["UBNDS_%s" % fn] = "up-band-none"
94 
95  for fs in a2bfilt_vals:
96  fdict["DBNDS_%s" % fs.FiltName] = "down-band-%d" % fs.ToBand
97  for fs in b2afilt_vals:
98  fdict["UBNDS_%s" % fs.FiltName] = "up-band-%d" % fs.ToBand
99 
100  fdict["SUBMIT_FILTERMAP_BUTTON"] = "Submit"
101 
102  conn = httplib.HTTPConnection(mm2host)
103  params = urllib.urlencode(fdict)
104  headers = {"Content-type": "application/x-www-form-urlencoded",
105  "Accept": "text/plain"}
106  conn.request("POST", "/mm2/mpc/mm2.py/process_filtmap", params, headers)
107  resp = conn.getresponse()
108 
109  if (resp.status/100) != 2:
110  raise Exception(resp.reason)
111  data = resp.read()
112  conn.close()
113 
114 #**********************************************************************
115 # Entry
116 #**********************************************************************
117 if __name__ == "__main__":
118  a2bfilts = []
119  b2afilts = []
120 
121  parser = argparse.ArgumentParser(description="Set classification filter map.")
122 
123  parser.add_argument("-A", action="append", nargs=2, dest="a2b",
124  help="Specify a filter name and band number for packats flowing LanA to Lan B.")
125 
126  parser.add_argument("-B", action="append", nargs=2, dest="b2a",
127  help="Specify a filter name and band number for packats flowing LanB to Lan A.")
128 
129  parser.add_argument("-d", "--defaults", action="store_true",
130  help="Turn all filters off (default state).")
131 
132  parser.add_argument("-f", "--filters", action="store_true",
133  help="List all the flters, do nothing else.")
134 
135  parser.add_argument("mm_hostname",
136  help='The host name or IP address of the Mini Maxwell/Maxwell G.')
137 
138  pargs = parser.parse_args()
139 
140  mm_hostname = pargs.mm_hostname
141  a2bargs = pargs.a2b
142  b2aargs = pargs.b2a
143 
144  if pargs.filters:
145  try:
146  all_filter_names = GetAllFilterNames(mm_hostname)
147  except Exception as err:
148  print("HTTP Exception occurred:", str(err))
149  sys.exit(1)
150 
151  for filt in all_filter_names:
152  print(filt)
153  sys.exit(0)
154 
155  if pargs.defaults:
156  SetFiltMap(mm_hostname, a2bfilts, b2afilts)
157  sys.exit(0)
158 
159  if a2bargs is not None:
160  for farg in a2bargs:
161  try:
162  a2bfilts.append(FiltSetting(farg[0], farg[1]))
163  except Exception as err:
164  print("Parameter error: %s - Aborting." % str(err))
165  sys.exit(1)
166 
167  if b2aargs is not None:
168  for farg in b2aargs:
169  try:
170  b2afilts.append(FiltSetting(farg[0], farg[1]))
171  except Exception as err:
172  print("Parameter error: %s - Aborting." % str(err))
173  sys.exit(1)
174 
175  SetFiltMap(mm_hostname, a2bfilts, b2afilts)
def GetAllFilterNames(mm_hostname)
Definition: setfilters.py:34
def __init__(self, filtname, toband)
Definition: setfilters.py:59
def GetMM2ConfigInJson(mm2name)
Definition: setfilters.py:43
def SetFiltMap(mm2host, a2bfilt_vals, b2afilt_vals, all_filter_names=None)
Definition: setfilters.py:69


mini_maxwell
Author(s): Yusuke Furuta
autogenerated on Tue May 11 2021 02:55:40