Example #1
0
def execute_test_event_action():
    """Executes the SubmitTestEvent Action

        Return a JSON containing the EventType received.
        Logs exception of any error and return abort.

        Returns:
            JSON: JSON containing the EventType.

        Exceptions:
            OneViewRedfishError: When an invalid JSON is received.
            return abort(400)

            Exception: Unexpected error.
            return abort(500)
    """

    try:
        try:
            event_type = request.get_json()['EventType']
        except Exception:
            raise OneViewRedfishError(
                {'message': 'Invalid JSON data. Missing EventType property.'})

        if (event_type not in util.subscriptions_by_type.keys()):
            raise OneViewRedfishError(
                {'message': 'Invalid EventType value: %s' % event_type})

        # Creates a sample OneView SCMB message according to
        # the value of 'event_type'
        if (event_type == "Alert"):
            message = deepcopy(ONEVIEW_TEST_ALERT)
        else:
            message = deepcopy(ONEVIEW_TEST_TASK)
            message['changeType'] = REDFISH_TO_ONEVIEW_EVENTS[event_type]

        event = Event(message)

        util.dispatch_event(event)

        json_str = event.serialize()

        return Response(response=json_str,
                        status=status.HTTP_202_ACCEPTED,
                        mimetype='application/json')
    except OneViewRedfishError as e:
        logging.exception('Mapping error: {}'.format(e))
        abort(status.HTTP_400_BAD_REQUEST, e.msg['message'])
    except Exception as e:
        logging.exception('Unexpected error: {}'.format(e))
        abort(status.HTTP_500_INTERNAL_SERVER_ERROR)
    def consume_message(self, ch, method, properties, body):
        body = json.loads(body.decode('utf-8'))
        resource = body['resource']

        if (resource['category'] == 'alerts'):
            category = resource['associatedResource']['resourceCategory']
        else:
            category = resource['category']

        if (category in SCMB_RESOURCE_LIST):
            event = Event(body)

            util.dispatch_event(event)
        else:
            logging.debug('SCMB message received for an unmanaged resource')
    def test_submit_event_without_subscriber(self, start_mock,
                                             subscription_mock,
                                             check_ov_availability):
        """Tests SubmitTestEvent action with no subscribers"""

        config.load_config(self.config_file)

        with open('oneview_redfish_toolkit/mockups/oneview/Alert.json') as f:
            event_mockup = Event(json.loads(f.read()))

        subscription_mock['Alert'].values.return_value = []

        util.dispatch_event(event_mockup)

        self.assertFalse(start_mock.called)
    def test_submit_event_with_subscriber(self, start_mock, subscription_mock,
                                          check_ov_availability):
        """Tests SubmitTestEvent action with two subscribers"""

        config.load_config(self.config_file)

        with open('oneview_redfish_toolkit/mockups/oneview/Alert.json') as f:
            event_mockup = Event(json.loads(f.read()))

        subscription_mock['Alert'].values.return_value = [
            Subscription('1', 'destination1', [], 'context1'),
            Subscription('2', 'destination2', [], 'context2')
        ]

        util.dispatch_event(event_mockup)

        self.assertTrue(start_mock.call_count == 2)