bsc: add active lchan matching via vty

Will be used in upcoming handover_2G test suite in
I0b2671304165a1aaae2b386af46fbd8b098e3bd8, which needs to verify that a
handover actually ended up on the expected lchan.

Change-Id: I03df8f3ae2ee47930eee311c7ce104c36dbb3154
diff --git a/src/osmo_gsm_tester/obj/bsc_osmo.py b/src/osmo_gsm_tester/obj/bsc_osmo.py
index bd8649b..158bf93 100644
--- a/src/osmo_gsm_tester/obj/bsc_osmo.py
+++ b/src/osmo_gsm_tester/obj/bsc_osmo.py
@@ -192,4 +192,53 @@
         self.bsc = bsc
         super().__init__(self.bsc.addr(), port)
 
+    def get_active_lchans(self):
+        lchan_summary = self.cmd('show lchan summary')
+
+        re_lchan_summary = re.compile('BTS ([0-9]+), TRX ([0-9]+), Timeslot ([0-9]+) *([^,]*), Lchan ([0-9]+),.* State ([A-Za-z_]+).*')
+        active_lchans = set()
+        for line in lchan_summary:
+            m = re_lchan_summary.match(line)
+            if m:
+                bts, trx, ts, lchan_type, subslot, state = m.groups()
+                active_lchans.add('%s-%s-%s-%s %s %s' % (bts, trx, ts, subslot, lchan_type, state))
+        if not active_lchans:
+            self.dbg('No active lchans')
+        else:
+            self.dbg('Active lchans:\n|', '\n| '.join(active_lchans), '\n');
+        return active_lchans
+
+    def active_lchans_match(self, expected=[], not_expected=[]):
+        active_lchans = self.get_active_lchans()
+        matches = []
+        mismatches = []
+
+        for expected_lchan in expected:
+            found = False
+            for active_lchan in active_lchans:
+                if active_lchan.startswith(expected_lchan):
+                    found = True
+                    break
+            if found:
+                matches.append(expected_lchan)
+            else:
+                mismatches.append('missing: ' + expected_lchan)
+
+        for not_expected_lchan in not_expected:
+            found = False
+            for active_lchan in active_lchans:
+                if active_lchan.startswith(not_expected_lchan):
+                    found = True
+                    break
+            if not found:
+                matches.append('not: ' + not_expected_lchan)
+            else:
+                mismatches.append('unexpected: ' + not_expected_lchan)
+
+        if matches:
+            self.log('Found matching lchan activity (%d of %d requirements):' % (len(matches), len(expected) + len(not_expected)), matches)
+        if mismatches:
+            self.err('Found unexpected lchan activity (%d of %d requirements):' % (len(mismatches), len(expected) + len(not_expected)), mismatches)
+        return not mismatches
+
 # vim: expandtab tabstop=4 shiftwidth=4