This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
Max gerrit-no-reply at lists.osmocom.orgHello Harald Welte, Jenkins Builder,
I'd like you to reexamine a change. Please visit
https://gerrit.osmocom.org/1875
to look at the new patch set (#4).
Add simple CTRL2SOAP proxy
Add python client which converts TRAP messages into SOAP requests and
perform corresponding actions.
It can be used as follows
./soap.py -d -w http://example.com/soapservice/htdocs/wsdl/test.wsdl
See ./soap.py -h for additional options.
Change-Id: I82844ec7a302bac30d6daee9ebca2188fd48ca46
Related: SYS#3028
---
A openbsc/contrib/soap.py
1 file changed, 172 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/openbsc refs/changes/75/1875/4
diff --git a/openbsc/contrib/soap.py b/openbsc/contrib/soap.py
new file mode 100755
index 0000000..96c22c4
--- /dev/null
+++ b/openbsc/contrib/soap.py
@@ -0,0 +1,172 @@
+#!/usr/bin/python3
+# -*- mode: python-mode; py-indent-tabs-mode: nil -*-
+"""
+/*
+ * Copyright (C) 2016 sysmocom s.f.m.c. GmbH
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+"""
+
+from twisted.internet import reactor
+from twisted_ipa import CTRL, IPAFactory
+from ipa import Ctrl
+from treq import post, collect
+from suds.client import Client
+from functools import partial
+import argparse, datetime, signal, sys, os
+
+
+version = "v0.3" # bump this on every non-trivial change
+
+# keys from OpenBSC openbsc/src/libbsc/bsc_rf_ctrl.c, values SOAP-specific
+oper = { 'inoperational' : 0, 'operational' : 1 }
+admin = { 'locked' : 0, 'unlocked' : 1 }
+policy = { 'off' : 0, 'on' : 1, 'grace' : 2, 'unknown' : 3 }
+
+# keys from OpenBSC openbsc/src/libbsc/bsc_vty.c
+fix = { 'invalid' : 0, 'fix2d' : 1, 'fix3d' : 3 }
+
+
+class Trap(CTRL):
+ """
+ TRAP handler (agnostic to factory's client object)
+ """
+ def ctrl_TRAP(self, data, op_id, v):
+ """
+ Parse CTRL TRAP and dispatch to appropriate handler after normalization
+ """
+ (l, r) = v.split()
+ loc = l.split('.')
+ t_type = loc[-1]
+ p = partial(lambda a, i: a[i] if len(a) > i else None, loc) # parse helper
+ method = getattr(self, 'handle_' + t_type.replace('-', ''), lambda: "Unhandled %s trap" % t_type)
+ method(p(1), p(3), p(5), p(7), r) # we expect net.0.bsc.666.bts.2.trx.1 format for trap prefix
+
+ def ctrl_SET_REPLY(self, data, _, v):
+ """
+ Debug log for replies to our commands
+ """
+ if self.factory.debug:
+ print('SET REPLY %s' % v)
+
+ def ctrl_ERROR(self, data, op_id, v):
+ """
+ We want to know if smth went wrong
+ """
+ if self.factory.debug:
+ print('CTRL ERROR [%s] %s' % (op_id, v))
+
+ def dbg(*_):
+ """
+ Turn off debug print from lower layers as it's too verbose
+ """
+ pass
+
+ def connectionMade(self):
+ """
+ Logging wrapper, calling super() is necessary not to break reconnection logic
+ """
+ print("Connected to CTRL@%s:%d" % (self.factory.host, self.factory.port))
+ super(CTRL, self).connectionMade()
+
+ def handle_locationstate(self, net, bsc, bts, trx, data):
+ """
+ Handle location-state TRAP: parse trap content, build SOAP context and use treq's routines to post it while setting up async handlers
+ """
+ def cback(c, r):
+ """
+ Callback function: takes c (SOAP client's RequestContext) and server's reply, sends set of parsed ctrl commands (in a 'fire and forget' way)
+ """
+ repl = c.process_reply(r)
+ bsc_id = repl.commands[0].split()[0].split('.')[3] # we expect 1st command to have net.0.bsc.666.bts.2.trx.1 location prefix format
+ print("Received SOAP response for BSC %s, error status: %s" % (bsc_id, repl.error))
+ if self.factory.debug:
+ print("BSC %s commands: %s" % (bsc_id, repl.commands))
+ for t in repl.commands: # Process OpenBscCommands format from .wsdl
+ (_, m) = Ctrl().cmd(*t.split())
+ self.transport.write(m)
+ (ts, fx, lat, lon, height, opr, adm, pol, mcc, mnc) = data.split(',')
+ tstamp = datetime.datetime.fromtimestamp(float(ts)).isoformat()
+ if self.factory.debug:
+ print('location-state@%s.%s.%s.%s (%s) [%s/%s] => %s' % (net, bsc, bts, trx, tstamp, mcc, mnc, data))
+ ctx = self.factory.client.registerSiteLocation(bsc, float(lon), float(lat), fix.get(fx, 0), tstamp, oper.get(opr, 2), admin.get(adm, 2), policy.get(pol, 3))
+ post(self.factory.location, ctx.envelope).addCallback(collect, partial(cback, ctx)) # treq's collect helper is handy to get all reply content at once using closure on ctx
+
+ def handle_notificationrejectionv1(self, net, bsc, bts, trx, data):
+ """
+ Handle notification-rejection-v1 TRAP: just an example to show how more message types can be handled
+ """
+ if self.factory.debug:
+ print('notification-rejection-v1 at bsc-id %s => %s' % (bsc, data))
+
+
+class TrapFactory(IPAFactory):
+ """
+ Store SOAP client object so TRAP handler can use it for requests
+ """
+ location = None
+ client = None
+ host = None
+ port = None
+ debug = None
+ def __init__(self, host, port, proto=None, debug=False, wsdl=None, location=None):
+ self.host = host # for logging only,
+ self.port = port # seems to be no way to get it from ReconnectingClientFactory
+ self.debug = debug
+ soap = Client(wsdl, location=location, nosend=True) # make async SOAP client
+ self.location = location.encode() if location else soap.wsdl.services[0].ports[0].location # necessary for dispatching HTTP POST via treq
+ self.client = soap.service
+ if debug:
+ print("Using IPA %s, SUDS client: %s" % (Ctrl.version, soap))
+ super(TrapFactory, self).__init__(proto, debug)
+
+
+def reloader(path, script, dbg, signum, _):
+ """
+ Signal handler: we have to use execl() because twisted's reactor is not restartable due to some bug in twisted implementation
+ """
+ print("Received Signal %s - restarting..." % signal.Signals(signum).name)
+ if signum == signal.SIGUSR1 and dbg not in sys.argv:
+ sys.argv.append(dbg) # enforce debug
+ if signum == signal.SIGUSR2 and dbg in sys.argv:
+ sys.argv.remove(dbg) # disable debug
+ os.execl(path, script, *sys.argv[1:])
+
+
+if __name__ == '__main__':
+ p = argparse.ArgumentParser()
+ p.add_argument('-v', '--version', action='version', version=("%(prog)s " + version))
+ p.add_argument('-p', '--port', type=int, default=4250, help="Port to use for CTRL interface, defaults to 4250")
+ p.add_argument('-c', '--ctrl', default='localhost', help="Adress to use for CTRL interface, defaults to localhost")
+ p.add_argument('-w', '--wsdl', required=True, help="WSDL URL for SOAP")
+ p.add_argument('-s', '--size', type=int, default=5, help="Size of thread pool")
+ p.add_argument('-d', '--debug', action='store_true', help="Enable debug log")
+ p.add_argument('-l', '--location', help="Override location found in WSDL file (don't use unless you know what you're doing)")
+ args = p.parse_args()
+ fl = os.path.basename(__file__)
+
+ reboot = partial(reloader, os.getcwd() + '/' + fl, fl, '-d')
+ signal.signal(signal.SIGHUP, reboot)
+ signal.signal(signal.SIGQUIT, reboot)
+ signal.signal(signal.SIGUSR1, reboot) # restart and enabled debug output
+ signal.signal(signal.SIGUSR2, reboot) # restart and disable debug output
+
+ print("SOAP proxy %s starting with PID %d ..." % (version, os.getpid()))
+ reactor.suggestThreadPoolSize(args.size)
+ reactor.connectTCP(args.ctrl, args.port, TrapFactory(args.ctrl, args.port, Trap, args.debug, args.wsdl, args.location))
+ reactor.run()
--
To view, visit https://gerrit.osmocom.org/1875
To unsubscribe, visit https://gerrit.osmocom.org/settings
Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I82844ec7a302bac30d6daee9ebca2188fd48ca46
Gerrit-PatchSet: 4
Gerrit-Project: openbsc
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Max <msuraev at sysmocom.de>