openbsc[master]: Add simple CTRL2SOAP proxy

2017-03-23 Thread Harald Welte

Patch Set 9: Code-Review+2

-- 
To view, visit https://gerrit.osmocom.org/1875
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I82844ec7a302bac30d6daee9ebca2188fd48ca46
Gerrit-PatchSet: 9
Gerrit-Project: openbsc
Gerrit-Branch: master
Gerrit-Owner: Max 
Gerrit-Reviewer: Harald Welte 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Max 
Gerrit-HasComments: No


[PATCH] openbsc[master]: Add simple CTRL2SOAP proxy

2017-03-22 Thread Max
Hello 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 (#9).

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, 188 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/openbsc refs/changes/75/1875/9

diff --git a/openbsc/contrib/soap.py b/openbsc/contrib/soap.py
new file mode 100755
index 000..4d0a023
--- /dev/null
+++ b/openbsc/contrib/soap.py
@@ -0,0 +1,188 @@
+#!/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.
+ */
+"""
+
+__version__ = "v0.7" # bump this on every non-trivial change
+
+from twisted.internet import defer, reactor
+from twisted_ipa import CTRL, IPAFactory, __version__ as twisted_ipa_version
+from ipa import Ctrl
+from treq import post, collect
+from suds.client import Client
+from functools import partial
+from distutils.version import StrictVersion as V # FIXME: use 
NormalizedVersion from PEP-386 when available
+import argparse, datetime, signal, sys, os, logging, logging.handlers
+
+# we don't support older versions of TwistedIPA module
+assert V(twisted_ipa_version) > V('0.4')
+
+# 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' : 1 } # SOAP server treats it as 
boolean but expects int
+
+
+def handle_reply(p, f, log, r):
+"""
+Reply handler: takes function p to process raw SOAP server reply r, 
function f to run for each command and verbosity flag v
+"""
+repl = p(r) # result is expected to have both commands[] array and error 
string (could be None)
+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
+log.info("Received SOAP response for BSC %s with %d commands, error 
status: %s" % (bsc_id, len(repl.commands), repl.error))
+log.debug("BSC %s commands: %s" % (bsc_id, repl.commands))
+for t in repl.commands: # Process OpenBscCommands format from .wsdl
+(_, m) = Ctrl().cmd(*t.split())
+f(m)
+
+
+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
+"""
+self.factory.log.debug('SET REPLY %s' % v)
+
+def ctrl_ERROR(self, data, op_id, v):
+"""
+We want to know if smth went wrong
+"""
+self.factory.log.debug('CTRL ERROR [%s] %s' % (op_id, v))
+
+def connectionMade(self):
+"""
+Logging wrapper, calling super() is necessary not to break 
reconnection logic
+"""
+self.factory.log.info("Connected to CTRL@%s:%d" % (self.factory.host, 
self.factory.port))
+super(CTRL, self).connectionMade()
+
+@defer.inlineCallbacks
+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
+"""
+(ts, 

openbsc[master]: Add simple CTRL2SOAP proxy

2017-03-15 Thread Harald Welte

Patch Set 6: Code-Review+2

-- 
To view, visit https://gerrit.osmocom.org/1875
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I82844ec7a302bac30d6daee9ebca2188fd48ca46
Gerrit-PatchSet: 6
Gerrit-Project: openbsc
Gerrit-Branch: master
Gerrit-Owner: Max 
Gerrit-Reviewer: Harald Welte 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Max 
Gerrit-HasComments: No