OsmoCtrl cleanup: get_var(), set_var(), get_trap()

CTRL interface interaction was mostly inherited from the first legacy
implementation of osmo-gsm-tester, and it was a pain to look at from the
start. Now, while I'm close to the topic, I want this to improve:

Properly match a GET_REPLY/SET_REPLY to a sent GET/SET by the message
ID.

Completely drop the do_get() and do_set(), which were not useful for
correct handling of the CTRL request and response messaging. The API to
use by callers is set_var(), get_var()/get_int_var() and get_trap().
These call the internal _sendrecv() (or for TRAP only _recv())
functions. Make it so that tese work both on an already connected
OsmoCtrl, as well as one that needs to establish a (short) connection,
so that both are trivially possible:

    # one CTRL connection stays open
    with OsmoCtrl(...) as ctrl:
  	ctrl.get_var('var1')
  	ctrl.get_var('var2')
  	ctrl.get_var('var3')

and

  # get_var() opens a connection, does the GET and closes again
  OsmoCtrl(...).get_var('var1')

Do away with doubling the instances OsmoCtrl and e.g. OsmoBscCtrl.
Rather make OsmoBscCtrl a child class of OsmoCtrl, which means that we
no longer have bsc.ctrl().ctrl(), just bsc.ctrl().

Have VERB_* constants instead of dup'd strings.

Apply to / simplify all callers of OsmoCtrl.

Some of these changes are similar to recently added OsmoVty.

Change-Id: Id561e5a55d8057a997a8ec9e7fa6f94840194df1
diff --git a/src/osmo_gsm_tester/obj/bsc_osmo.py b/src/osmo_gsm_tester/obj/bsc_osmo.py
index 158bf93..510063a 100644
--- a/src/osmo_gsm_tester/obj/bsc_osmo.py
+++ b/src/osmo_gsm_tester/obj/bsc_osmo.py
@@ -149,8 +149,10 @@
         # over this list, we have a 1:1 match in indexes.
         return self.bts.index(bts)
 
-    def bts_is_connected(self, bts):
-        return OsmoBscCtrl(self).bts_is_connected(self.bts_num(bts))
+    def bts_is_connected(self, bts, use_ctrl=None):
+        if use_ctrl is None:
+            use_ctrl = self.ctrl()
+        return use_ctrl.bts_is_connected(self.bts_num(bts))
 
     def running(self):
         return not self.process.terminated()
@@ -160,32 +162,16 @@
             self.vty.disconnect()
             self.vty = None
 
-class OsmoBscCtrl(log.Origin):
-    PORT = 4249
-    BTS_OML_STATE_VAR = "bts.%d.oml-connection-state"
-    BTS_OML_STATE_RE = re.compile("GET_REPLY (\d+) bts.\d+.oml-connection-state (?P<oml_state>\w+)")
-
-    def __init__(self, bsc):
-        self.bsc = bsc
-        super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.bsc.addr(), OsmoBscCtrl.PORT))
-
     def ctrl(self):
-        return osmo_ctrl.OsmoCtrl(self.bsc.addr(), OsmoBscCtrl.PORT)
+        return OsmoBscCtrl(self)
+
+class OsmoBscCtrl(osmo_ctrl.OsmoCtrl):
+    def __init__(self, bsc, port=4249):
+        self.bsc = bsc
+        super().__init__(bsc.addr(), port)
 
     def bts_is_connected(self, bts_num):
-        with self.ctrl() as ctrl:
-            ctrl.do_get(OsmoBscCtrl.BTS_OML_STATE_VAR % bts_num)
-            data = ctrl.receive()
-            while (len(data) > 0):
-                (answer, data) = ctrl.remove_ipa_ctrl_header(data)
-                answer_str = answer.decode('utf-8')
-                answer_str = answer_str.replace('\n', ' ')
-                res = OsmoBscCtrl.BTS_OML_STATE_RE.match(answer_str)
-                if res:
-                    oml_state = str(res.group('oml_state'))
-                    if oml_state == 'connected':
-                        return True
-        return False
+        return self.get_var('bts.%d.oml-connection-state' % bts_num) == 'connected'
 
 class OsmoBscVty(osmo_vty.OsmoVty):
     def __init__(self, bsc, port=4242):