diff --git a/SAS/ResourceAssignment/RATaskSpecifiedService/lib/RATaskSpecified.py b/SAS/ResourceAssignment/RATaskSpecifiedService/lib/RATaskSpecified.py index d8ee2edfe22c987438f0baa975e20d892db692dc..9cf0ef618b0800540b15b8a85fd85539339b9a1b 100755 --- a/SAS/ResourceAssignment/RATaskSpecifiedService/lib/RATaskSpecified.py +++ b/SAS/ResourceAssignment/RATaskSpecifiedService/lib/RATaskSpecified.py @@ -26,7 +26,7 @@ posts them on the bus. """ from lofar.messaging import FromBus, ToBus, EventMessage # RPC, -from lofar.parameterset import PyParameterValue +from lofar.parameterset import parameterset from lofar.sas.otdb.OTDBBusListener import OTDBBusListener from lofar.common.util import waitForInterrupt from lofar.sas.otdb.otdbrpc import OTDBRPC @@ -78,37 +78,31 @@ OTDB_to_RADB_subtype_translation_table = { }, } -def resourceIndicatorsFromParset( parsetDict ): +def resourceIndicatorsFromParset( parset ): """ Extract the parset keys that are required for resource assignment. """ subset = {} - def get(key, default=None): - """ Return the value of parset key `key', or `default' if the key - is not defined. """ - return parsetDict.get(PARSET_PREFIX + key, default) - - def add(key, conversion=lambda x: x): + def add(key, conversion=parameterset.getString, prefix=PARSET_PREFIX): """ Add the given key to our subset selection, using an optional conversion. """ - value = get(key) - if value is not None: - subset[key] = conversion(value) + if parset.isDefined(prefix + key): + subset[key] = conversion(parset, prefix + key) """ Some conversion functions for common parameter-value types.""" - def strvector(value): - return PyParameterValue(str(value), True).getStringVector() + def strvector(parset, key): + return parset.getStringVector(key, True) - def intvector(value): - return PyParameterValue(str(value), True)._expand().getIntVector() + def intvector(parset, key): + return parset.getIntVector(key, True) - def bool(value): - return PyParameterValue(str(value), True).getBool() + def bool(parset, key): + return parset.getBool(key) # ===================================== # Parset meta info # ===================================== - subset["Version.number"] = parsetDict.get("Version.number") + add("Version.number", prefix="") # ===================================== # Observation settings @@ -123,7 +117,7 @@ def resourceIndicatorsFromParset( parsetDict ): add("Observation.Scheduler.taskDuration") add("Observation.nrBeams") - nrSAPs = int(get("Observation.nrBeams", 0)) + nrSAPs = parset.getInt(PARSET_PREFIX + "Observation.nrBeams", 0) for sap in xrange(0, nrSAPs): add("Observation.Beam[%d].subbandList" % (sap,), intvector) @@ -163,7 +157,7 @@ def resourceIndicatorsFromParset( parsetDict ): add("Observation.Beam[%d].nrTabRings" % (sap,)) add("Observation.Beam[%d].nrTiedArrayBeams" % (sap,)) - nrTABs = int(get("Observation.Beam[%d].nrTiedArrayBeams" % (sap,), 0)) + nrTABs = parset.getInt(PARSET_PREFIX + "Observation.Beam[%d].nrTiedArrayBeams" % (sap,), 0) for tab in xrange(0, nrTABs): add("Observation.Beam[%d].TiedArrayBeam[%d].coherent" % (sap,tab), bool) @@ -238,26 +232,25 @@ class RATaskSpecified(OTDBBusListener): def get_predecessors(parset): """ Extract the list of predecessor obs IDs from the given parset. """ - key = PARSET_PREFIX + "Observation.Scheduler.predecessors" - stringlist = PyParameterValue(str(parset[key]), True).getStringVector() + predecessors = parset.getStringVector(PARSET_PREFIX + "Observation.Scheduler.predecessors", True) # Key contains values starting with 'S' = Scheduler, 'L'/'T' = OTDB, 'M' = MoM # 'S' we can probably ignore? Might be only internal in the Scheduler? result = [] - for s in stringlist: + for p in predecessors: try: # Made the source a string for readability, but it's not efficient - if s.startswith('M'): - result.append({'source': 'mom', 'id': int(s[1:])}) - elif s.startswith('L') or s.startswith('T'): - result.append({'source': 'otdb', 'id': int(s[1:])}) + if p.startswith('M'): + result.append({'source': 'mom', 'id': int(p[1:])}) + elif p.startswith('L') or s.startswith('T'): + result.append({'source': 'otdb', 'id': int(p[1:])}) else: # 'S' - logger.info("found a predecessor ID I can't handle: %s" % s) - result.append({'source': 'other', 'id': int(s[1:])}) + logger.info("found a predecessor ID I can't handle: %s", p) + result.append({'source': 'other', 'id': int(p[1:])}) except ValueError: - logger.warning("found a predecessor ID that I can't parse %s" % s) + logger.warning("found a predecessor ID that I can't parse %s", p) return result - def get_specification_with_predecessors(self, id, id_source, state, found_parsets={}): + def get_specification_with_predecessors(self, id, id_source, state, found_parsets): logger.info("Processing ID %s from %s" % (id, id_source)) if id_source == "other": return None @@ -278,14 +271,16 @@ class RATaskSpecified(OTDBBusListener): if otdb_id in found_parsets: parset = found_parsets[otdb_id] else: - parset = self.otdbrpc.taskGetSpecification( otdb_id=otdb_id )['specification'] + # obtain parset & convert dict to parameterset object + parset = parameterset(self.otdbrpc.taskGetSpecification( otdb_id=otdb_id )['specification']) + # add it to our cache found_parsets[otdb_id] = parset logger.info("parset [%s]: %s" % (otdb_id, parset)) # parse & marshall task type information - otdb_task_type = parset[PARSET_PREFIX + "Observation.processType"] - otdb_task_subtype = parset[PARSET_PREFIX + "Observation.processSubType"] + otdb_task_type = parset[PARSET_PREFIX + "Observation.processType"].getString() + otdb_task_subtype = parset[PARSET_PREFIX + "Observation.processSubtype"].getString() result['task_type'] = OTDB_to_RADB_type_translation_table[otdb_task_type] result['task_subtype'] = OTDB_to_RADB_subtype_translation_table[otdb_task_type][otdb_task_subtype] @@ -309,7 +304,7 @@ class RATaskSpecified(OTDBBusListener): def createAndSendSpecifiedTask(self, main_id, status): # Construct root node of tree - resultTree = self.get_specification_with_predecessors(main_id, "otdb", status) + resultTree = self.get_specification_with_predecessors(main_id, "otdb", status, {}) logger.info("Sending result: %s" % resultTree) # Put result on bus diff --git a/SAS/ResourceAssignment/RATaskSpecifiedService/test/tRATaskSpecified.py b/SAS/ResourceAssignment/RATaskSpecifiedService/test/tRATaskSpecified.py index 98de46a6de95a72f84f7c08d86ef2b38423f5a3c..cb500884dec5df12b601d956558c24e45a31c0b9 100644 --- a/SAS/ResourceAssignment/RATaskSpecifiedService/test/tRATaskSpecified.py +++ b/SAS/ResourceAssignment/RATaskSpecifiedService/test/tRATaskSpecified.py @@ -35,17 +35,17 @@ class TestGetPredecessors(unittest.TestCase): """ Class containing tests to verify whether the uut properly obtains the predecessors from a parset """ def test_0_predecessors(self): """ Verify proper functioning in case of no predecessors """ - parset = { PARSET_PREFIX + "Observation.Scheduler.predecessors": "[]" } + parset = parameterset({ PARSET_PREFIX + "Observation.Scheduler.predecessors": "[]" }) self.assertEqual(RATaskSpecified.get_predecessors(parset), []) def test_1_predecessor(self): """ Verify proper functioning in case of a single predecessor """ - parset = { PARSET_PREFIX + "Observation.Scheduler.predecessors": "[L426528]" } + parset = parameterset({ PARSET_PREFIX + "Observation.Scheduler.predecessors": "[L426528]" }) self.assertEqual(RATaskSpecified.get_predecessors(parset), [{'id': 426528, 'source': 'otdb'}]) def test_2_predecessors(self): """ Verify proper functioning in case of a two predecessors """ - parset = { PARSET_PREFIX + "Observation.Scheduler.predecessors": "[L426528,L1]" } + parset = parameterset({ PARSET_PREFIX + "Observation.Scheduler.predecessors": "[L426528,L1]" }) self.assertEqual(sorted(RATaskSpecified.get_predecessors(parset)), [{'id': 1, 'source': 'otdb'},{'id': 426528, 'source': 'otdb'}]) @@ -281,7 +281,7 @@ class TestRASubset(unittest.TestCase): (stringified); and, the actual RA parset subset as output by the uut. """ input_parset, expected_keys = self.get_test_inputs(input_parset_file) - ra_subset = resourceIndicatorsFromParset(input_parset) + ra_subset = resourceIndicatorsFromParset(parameterset(input_parset)) actual_keys = sorted(ra_subset.keys()) actual_ra_subset_str = self.get_ra_subset_as_string(ra_subset) @@ -498,6 +498,8 @@ class TestService(unittest.TestCase): return { "TaskSpecification": { "Version.number": "1", + PARSET_PREFIX + "Observation.processType": "Observation", + PARSET_PREFIX + "Observation.processSubtype": "Beam Observation", PARSET_PREFIX + "Observation.ObsID": str(OtdbID), PARSET_PREFIX + "Observation.Scheduler.predecessors": self.fully_qualified_otdb_id_as_string(predecessors), } } @@ -614,4 +616,4 @@ class TestService(unittest.TestCase): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()