def setUp(self):
     self.ASB = AgentStatusBuilder(Mock())
class TestAgentStatusBuilder(PyonTestCase):

    def setUp(self):
        self.ASB = AgentStatusBuilder(Mock())


    def test_crush_list(self):

        some_values = []
        self.assertEqual(DeviceStatusType.STATUS_UNKNOWN, self.ASB._crush_status_list(some_values))
        def add_and_check(onestatus):
            oldsize = len(some_values)
            some_values.append(onestatus)
            self.assertEqual(oldsize + 1, len(some_values))
            self.assertEqual(onestatus, self.ASB._crush_status_list(some_values))

        # each successive addition here should become the returned status -- they are done in increasing importance
        add_and_check(DeviceStatusType.STATUS_UNKNOWN)
        add_and_check(DeviceStatusType.STATUS_OK)
        add_and_check(DeviceStatusType.STATUS_WARNING)
        add_and_check(DeviceStatusType.STATUS_CRITICAL)

    def test_crush_dict(self):

        statuses = {AggregateStatusType.AGGREGATE_COMMS    : DeviceStatusType.STATUS_CRITICAL,
                    AggregateStatusType.AGGREGATE_POWER    : DeviceStatusType.STATUS_WARNING,
                    AggregateStatusType.AGGREGATE_DATA     : DeviceStatusType.STATUS_OK,
                    AggregateStatusType.AGGREGATE_LOCATION : DeviceStatusType.STATUS_UNKNOWN}

        ret = self.ASB._crush_status_dict(statuses)
        self.assertEqual(DeviceStatusType.STATUS_CRITICAL, ret)


    def test_set_status_computed_attributes(self):
        # set_status_computed_attributes(self, computed_attrs, values_dict=None, availability=None, reason=None)

        container = DotDict()

        log.debug("check null values")
        self.ASB.set_status_computed_attributes_notavailable(container,"this is the reason")

        for attr, enumval in reverse_mapping.iteritems():
            self.assertTrue(hasattr(container, attr))
            attrval = getattr(container, attr)
            self.assertIsInstance(attrval, ComputedIntValue)
            self.assertEqual(ComputedValueAvailability.NOTAVAILABLE, attrval.status)
            self.assertEqual("this is the reason", attrval.reason)

        log.debug("check provided values")

        statuses = {AggregateStatusType.AGGREGATE_COMMS    : DeviceStatusType.STATUS_CRITICAL,
                    AggregateStatusType.AGGREGATE_POWER    : DeviceStatusType.STATUS_WARNING,
                    AggregateStatusType.AGGREGATE_DATA     : DeviceStatusType.STATUS_OK,
                    AggregateStatusType.AGGREGATE_LOCATION : DeviceStatusType.STATUS_UNKNOWN}

        self.ASB.set_status_computed_attributes(container, statuses, ComputedValueAvailability.PROVIDED, "na")



        for attr, enumval in reverse_mapping.iteritems():
            self.assertTrue(hasattr(container, attr))
            attrval = getattr(container, attr)
            self.assertIsInstance(attrval, ComputedIntValue)
            self.assertEqual(ComputedValueAvailability.PROVIDED, attrval.status)
            self.assertEqual(statuses[enumval], attrval.value)


    def test_get_device_agent(self):
        # we expect to fail
        for bad in [None, "0"]:
            handle, reason = self.ASB.get_device_agent(bad)
            self.assertIsNone(handle)
            self.assertEqual(type(""), type("reason"))

        for exn in [Unauthorized, NotFound, AttributeError]:
            def make_exception(*args, **kwargs):
                raise exn()
            self.ASB._get_agent_client = make_exception
            handle, reason = self.ASB.get_device_agent("anything")
            self.assertIsNone(handle)
            self.assertNotEqual("", reason)

        # override resource agent client and try again
        self.ASB._get_agent_client = lambda *args, **kwargs : "happy"

        handle, reason = self.ASB.get_device_agent("anything")
        self.assertEqual("happy", handle)


    def test_compute_status_list(self):

        bad = self.ASB.compute_status_list(None, [1,2,3])
        self.assertIsInstance(bad, ComputedListValue)

        statusd1 = {AggregateStatusType.AGGREGATE_COMMS    : DeviceStatusType.STATUS_CRITICAL,
                    AggregateStatusType.AGGREGATE_POWER    : DeviceStatusType.STATUS_WARNING,
                    AggregateStatusType.AGGREGATE_DATA     : DeviceStatusType.STATUS_OK,
                    AggregateStatusType.AGGREGATE_LOCATION : DeviceStatusType.STATUS_UNKNOWN}

        statusd2 = {AggregateStatusType.AGGREGATE_POWER    : DeviceStatusType.STATUS_WARNING,
                    AggregateStatusType.AGGREGATE_DATA     : DeviceStatusType.STATUS_OK,
                    AggregateStatusType.AGGREGATE_LOCATION : DeviceStatusType.STATUS_UNKNOWN}

        childstatus = {"d1": statusd1, "d2": statusd2}

        ret = self.ASB.compute_status_list(childstatus, ["d1", "d2"])
        self.assertIsInstance(ret, ComputedListValue)
        self.assertEqual(ComputedValueAvailability.PROVIDED, ret.status)
        self.assertEqual([DeviceStatusType.STATUS_CRITICAL, DeviceStatusType.STATUS_WARNING], ret.value)
 def setUp(self):
     self.ASB = AgentStatusBuilder(Mock())
    def _get_site_extension(self, site_id='', ext_associations=None, ext_exclude=None, user_id=''):
        """Returns an InstrumentDeviceExtension object containing additional related information

        @param site_id    str
        @param ext_associations    dict
        @param ext_exclude    list
        @retval observatory    ObservatoryExtension
        @throws BadRequest    A parameter is missing
        @throws NotFound    An object with the specified observatory_id does not exist
        """

        if not site_id:
            raise BadRequest("The site_id parameter is empty")

        extended_resource_handler = ExtendedResourceContainer(self)

        extended_site = extended_resource_handler.create_extended_resource_container(
            extended_resource_type=OT.SiteExtension,
            resource_id=site_id,
            computed_resource_type=OT.SiteComputedAttributes,
            ext_associations=ext_associations,
            ext_exclude=ext_exclude,
            user_id=user_id)

        RR2 = EnhancedResourceRegistryClient(self.RR)
        RR2.cache_predicate(PRED.hasModel)

        # Get status of Site instruments.
        a, b =  self._get_instrument_states(extended_site.instrument_devices)
        extended_site.instruments_operational, extended_site.instruments_not_operational = a, b

        # lookup all hasModel predicates
        # lookup is a 2d associative array of [subject type][subject id] -> object id
        lookup = dict([(rt, {}) for rt in [RT.InstrumentDevice, RT.PlatformDevice]])
        for a in RR2.filter_cached_associations(PRED.hasModel, lambda assn: assn.st in lookup):
            lookup[a.st][a.s] = a.o

        def retrieve_model_objs(rsrc_list, object_type):
        # rsrc_list is devices that need models looked up.  object_type is the resource type (a device)
        # not all devices have models (represented as None), which kills read_mult.  so, extract the models ids,
        #  look up all the model ids, then create the proper output
            model_list = [lookup[object_type].get(r._id) for r in rsrc_list]
            model_uniq = list(set([m for m in model_list if m is not None]))
            model_objs = self.RR2.read_mult(model_uniq)
            model_dict = dict(zip(model_uniq, model_objs))
            return [model_dict.get(m) for m in model_list]

        extended_site.instrument_models = retrieve_model_objs(extended_site.instrument_devices, RT.InstrumentDevice)
        extended_site.platform_models   = retrieve_model_objs(extended_site.platform_devices, RT.PlatformDevice)


        # Status computation
        extended_site.computed.instrument_status = [AgentStatusBuilder.get_aggregate_status_of_device(idev._id, "aggstatus")
                                                    for idev in extended_site.instrument_devices]
        extended_site.computed.platform_status   = [AgentStatusBuilder.get_aggregate_status_of_device(pdev._id, "aggstatus")
                                                    for pdev in extended_site.platform_devices]

#            AgentStatusBuilder.add_device_aggregate_status_to_resource_extension(device_id,
#                                                                                    'aggstatus',
#                                                                                    extended_site)
        def status_unknown():
            return ComputedIntValue(status=ComputedValueAvailability.PROVIDED, value=StatusType.STATUS_UNKNOWN)
        extended_site.computed.communications_status_roll_up = status_unknown()
        extended_site.computed.power_status_roll_up          = status_unknown()
        extended_site.computed.data_status_roll_up           = status_unknown()
        extended_site.computed.location_status_roll_up       = status_unknown()
        extended_site.computed.aggregated_status             = status_unknown()

        extended_site.computed.site_status = [StatusType.STATUS_UNKNOWN] * len(extended_site.sites)



        return extended_site, RR2
class TestAgentStatusBuilder(PyonTestCase):
    def setUp(self):
        self.ASB = AgentStatusBuilder(Mock())

    def test_crush_list(self):

        some_values = []
        self.assertEqual(DeviceStatusType.STATUS_UNKNOWN,
                         self.ASB._crush_status_list(some_values))

        def add_and_check(onestatus):
            oldsize = len(some_values)
            some_values.append(onestatus)
            self.assertEqual(oldsize + 1, len(some_values))
            self.assertEqual(onestatus,
                             self.ASB._crush_status_list(some_values))

        # each successive addition here should become the returned status -- they are done in increasing importance
        add_and_check(DeviceStatusType.STATUS_UNKNOWN)
        add_and_check(DeviceStatusType.STATUS_OK)
        add_and_check(DeviceStatusType.STATUS_WARNING)
        add_and_check(DeviceStatusType.STATUS_CRITICAL)

    def test_crush_dict(self):

        statuses = {
            AggregateStatusType.AGGREGATE_COMMS:
            DeviceStatusType.STATUS_CRITICAL,
            AggregateStatusType.AGGREGATE_POWER:
            DeviceStatusType.STATUS_WARNING,
            AggregateStatusType.AGGREGATE_DATA: DeviceStatusType.STATUS_OK,
            AggregateStatusType.AGGREGATE_LOCATION:
            DeviceStatusType.STATUS_UNKNOWN
        }

        ret = self.ASB._crush_status_dict(statuses)
        self.assertEqual(DeviceStatusType.STATUS_CRITICAL, ret)

    def test_set_status_computed_attributes(self):
        # set_status_computed_attributes(self, computed_attrs, values_dict=None, availability=None, reason=None)

        container = DotDict()

        log.debug("check null values")
        self.ASB.set_status_computed_attributes_notavailable(
            container, "this is the reason")

        for attr, enumval in reverse_mapping.iteritems():
            self.assertTrue(hasattr(container, attr))
            attrval = getattr(container, attr)
            self.assertIsInstance(attrval, ComputedIntValue)
            self.assertEqual(ComputedValueAvailability.NOTAVAILABLE,
                             attrval.status)
            self.assertEqual("this is the reason", attrval.reason)

        log.debug("check provided values")

        statuses = {
            AggregateStatusType.AGGREGATE_COMMS:
            DeviceStatusType.STATUS_CRITICAL,
            AggregateStatusType.AGGREGATE_POWER:
            DeviceStatusType.STATUS_WARNING,
            AggregateStatusType.AGGREGATE_DATA: DeviceStatusType.STATUS_OK,
            AggregateStatusType.AGGREGATE_LOCATION:
            DeviceStatusType.STATUS_UNKNOWN
        }

        self.ASB.set_status_computed_attributes(
            container, statuses, ComputedValueAvailability.PROVIDED, "na")

        for attr, enumval in reverse_mapping.iteritems():
            self.assertTrue(hasattr(container, attr))
            attrval = getattr(container, attr)
            self.assertIsInstance(attrval, ComputedIntValue)
            self.assertEqual(ComputedValueAvailability.PROVIDED,
                             attrval.status)
            self.assertEqual(statuses[enumval], attrval.value)

    def test_get_device_agent(self):
        # we expect to fail
        for bad in [None, "0"]:
            handle, reason = self.ASB.get_device_agent(bad)
            self.assertIsNone(handle)
            self.assertEqual(type(""), type("reason"))

        for exn in [Unauthorized, NotFound, AttributeError]:

            def make_exception(*args, **kwargs):
                raise exn()

            self.ASB._get_agent_client = make_exception
            handle, reason = self.ASB.get_device_agent("anything")
            self.assertIsNone(handle)
            self.assertNotEqual("", reason)

        # override resource agent client and try again
        self.ASB._get_agent_client = lambda *args, **kwargs: "happy"

        handle, reason = self.ASB.get_device_agent("anything")
        self.assertEqual("happy", handle)

    def test_compute_status_list(self):

        bad = self.ASB.compute_status_list(None, [1, 2, 3])
        self.assertIsInstance(bad, ComputedListValue)

        statusd1 = {
            AggregateStatusType.AGGREGATE_COMMS:
            DeviceStatusType.STATUS_CRITICAL,
            AggregateStatusType.AGGREGATE_POWER:
            DeviceStatusType.STATUS_WARNING,
            AggregateStatusType.AGGREGATE_DATA: DeviceStatusType.STATUS_OK,
            AggregateStatusType.AGGREGATE_LOCATION:
            DeviceStatusType.STATUS_UNKNOWN
        }

        statusd2 = {
            AggregateStatusType.AGGREGATE_POWER:
            DeviceStatusType.STATUS_WARNING,
            AggregateStatusType.AGGREGATE_DATA: DeviceStatusType.STATUS_OK,
            AggregateStatusType.AGGREGATE_LOCATION:
            DeviceStatusType.STATUS_UNKNOWN
        }

        childstatus = {"d1": statusd1, "d2": statusd2}

        ret = self.ASB.compute_status_list(childstatus, ["d1", "d2"])
        self.assertIsInstance(ret, ComputedListValue)
        self.assertEqual(ComputedValueAvailability.PROVIDED, ret.status)
        self.assertEqual([
            DeviceStatusType.STATUS_CRITICAL, DeviceStatusType.STATUS_WARNING
        ], ret.value)