def do_get_events(request, cluster, node_id):
    # FIXME: Hack to handle the old hard-coded controller id value
    if node_id == 'localhost':
        node_id = get_local_controller_id()

    # Get the time range over which we're getting the events
    start_time, end_time = get_time_range_from_request(request)

    init_db_connection()

    if request.method == 'GET':
        include_pk_tag_param = request.GET.get(INCLUDE_PK_TAG_QUERY_PARAM,
                                               'false')
        include_pk_tag = include_pk_tag_param.lower() == 'true'
        events_list = get_log_event_data(cluster, node_id, start_time,
                                         end_time, include_pk_tag)
        response_data = simplejson.dumps(events_list)
        response = HttpResponse(response_data, JSON_CONTENT_TYPE)
    elif request.method == 'DELETE':
        delete_log_event_data(cluster, node_id, start_time, end_time)
        response = get_successful_response()
    else:
        raise RestInvalidMethodException()

    return response
    def test_stats_target_types(self):
        
        local_controller_id = get_local_controller_id()
        
        # Check getting the list of all target types
        response = test_get_rest_data('v1/stats/target/local/')
        self.assertEqual(response.status_code, 200)
        results = simplejson.loads(response.content)
        self.assertEqual(len(results.keys()), 2)
        self.assertTrue('controller' in results)
        self.assertTrue('switch' in results)
        
        # Check getting the info for the controller target type
        response = test_get_rest_data('v1/stats/target/local/controller')
        self.assertEqual(response.status_code, 200)
        results = simplejson.loads(response.content)
        self.assertEqual(len(results.keys()), 1)
        self.assertTrue('192.168.1.1' in results)
        controller_info = results['192.168.1.1']
        #self.assertEqual(controller_info['controller'], local_controller_id)
        self.assertEqual(controller_info['last-updated'], make_timestamp(10,11))

        # Check getting the info for the switch target type
        response = test_get_rest_data('v1/stats/target/local/switch')
        self.assertEqual(response.status_code, 200)
        results = simplejson.loads(response.content)
        self.assertEqual(len(results.keys()), 1)
        self.assertTrue('00:01:02:03:04:05' in results)
        switch_info = results['00:01:02:03:04:05']
        self.assertEqual(switch_info['controller'], local_controller_id)
        self.assertEqual(switch_info['last-updated'], make_timestamp(1,4))
    def test_stats_target_types(self):

        local_controller_id = get_local_controller_id()

        # Check getting the list of all target types
        response = test_get_rest_data('v1/stats/target/local/')
        self.assertEqual(response.status_code, 200)
        results = simplejson.loads(response.content)
        self.assertEqual(len(results.keys()), 2)
        self.assertTrue('controller' in results)
        self.assertTrue('switch' in results)

        # Check getting the info for the controller target type
        response = test_get_rest_data('v1/stats/target/local/controller')
        self.assertEqual(response.status_code, 200)
        results = simplejson.loads(response.content)
        self.assertEqual(len(results.keys()), 1)
        self.assertTrue('192.168.1.1' in results)
        controller_info = results['192.168.1.1']
        #self.assertEqual(controller_info['controller'], local_controller_id)
        self.assertEqual(controller_info['last-updated'],
                         make_timestamp(10, 11))

        # Check getting the info for the switch target type
        response = test_get_rest_data('v1/stats/target/local/switch')
        self.assertEqual(response.status_code, 200)
        results = simplejson.loads(response.content)
        self.assertEqual(len(results.keys()), 1)
        self.assertTrue('00:01:02:03:04:05' in results)
        switch_info = results['00:01:02:03:04:05']
        self.assertEqual(switch_info['controller'], local_controller_id)
        self.assertEqual(switch_info['last-updated'], make_timestamp(1, 4))
def do_get_stats_type_index(request, cluster, target_type, target_id, stats_type=None):
    # FIXME: Hack to handle the old hard-coded controller id value
    if target_type == 'controller' and target_id == 'localhost':
        target_id = get_local_controller_id()
    init_db_connection()
    index_data = get_stats_type_index(cluster, target_type, target_id, stats_type)
    response_data = simplejson.dumps(index_data)
    return HttpResponse(response_data, JSON_CONTENT_TYPE)
def do_get_stats(request, cluster, target_type, target_id, stats_type):
    
    # FIXME: Hack to handle the old hard-coded controller id value
    if target_type == 'controller' and target_id == 'localhost':
        target_id = get_local_controller_id()
    
    # Get the time range over which we're getting the stats
    start_time, end_time = get_time_range_from_request(request)
    
    init_db_connection()
    
    if request.method == 'GET':
        window = request.GET.get(SAMPLE_WINDOW_QUERY_PARAM, 0)
        if window:
            window = convert_time_duration(window)
        if window != 0:
            window = get_closest_window_interval(int(window))
        # FIXME: Error checking on window value
                    
        data_format = request.GET.get(DATA_FORMAT_QUERY_PARAM, VALUE_DATA_FORMAT)
        # FIXME: Error checking on data_format value
        
        limit = request.GET.get(LIMIT_QUERY_PARAM)
        if limit:
            limit = int(limit)
        # FIXME: Error checking on limit value
    
        if start_time is not None and end_time is not None:
            # FIXME: Error checking on start_time and end_time values
            sample_interval = request.GET.get(SAMPLE_INTERVAL_QUERY_PARAM)
            if not sample_interval:
                # FIXME: Error checking on sample_period value
                sample_count = request.GET.get(SAMPLE_COUNT_QUERY_PARAM, DEFAULT_SAMPLE_COUNT)
                # FIXME: Error checking on sample_count value
                    
                sample_interval = (end_time - start_time) / int(sample_count)
            else:
                sample_interval = convert_time_duration(sample_interval)
            
            if sample_interval != 0:
                sample_interval = get_closest_sample_interval(sample_interval)
            
            stats_data = get_stats_data(cluster, target_type, target_id,
                stats_type, start_time, end_time, sample_interval, window, data_format, limit)
        else:
            stats_data = get_latest_stat_data(cluster, target_type, target_id, stats_type, window, data_format)
            
        response_data = simplejson.dumps(stats_data)
        response = HttpResponse(response_data, JSON_CONTENT_TYPE)
        
    elif request.method == 'DELETE':
        delete_stats_data(cluster, target_type, target_id, stats_type,
                      start_time, end_time)
        response = get_successful_response()
    else:
        raise RestInvalidMethodException()
        
    return response
def do_get_stats_type_index(request,
                            cluster,
                            target_type,
                            target_id,
                            stats_type=None):
    # FIXME: Hack to handle the old hard-coded controller id value
    if target_type == 'controller' and target_id == 'localhost':
        target_id = get_local_controller_id()
    init_db_connection()
    index_data = get_stats_type_index(cluster, target_type, target_id,
                                      stats_type)
    response_data = simplejson.dumps(index_data)
    return HttpResponse(response_data, JSON_CONTENT_TYPE)
def do_get_events(request, cluster, node_id):
    # FIXME: Hack to handle the old hard-coded controller id value
    if node_id == 'localhost':
        node_id = get_local_controller_id()
    
    # Get the time range over which we're getting the events
    start_time, end_time = get_time_range_from_request(request)
    
    init_db_connection()
    
    if request.method == 'GET':
        include_pk_tag_param = request.GET.get(INCLUDE_PK_TAG_QUERY_PARAM, 'false')
        include_pk_tag = include_pk_tag_param.lower() == 'true'
        events_list = get_log_event_data(cluster, node_id, start_time, end_time, include_pk_tag)
        response_data = simplejson.dumps(events_list)
        response = HttpResponse(response_data, JSON_CONTENT_TYPE)
    elif request.method == 'DELETE':
        delete_log_event_data(cluster, node_id, start_time, end_time)
        response = get_successful_response()
    else:
        raise RestInvalidMethodException()
        
    return response
def do_get_stats(request, cluster, target_type, target_id, stats_type):

    # FIXME: Hack to handle the old hard-coded controller id value
    if target_type == 'controller' and target_id == 'localhost':
        target_id = get_local_controller_id()

    # Get the time range over which we're getting the stats
    start_time, end_time = get_time_range_from_request(request)

    init_db_connection()

    if request.method == 'GET':
        window = request.GET.get(SAMPLE_WINDOW_QUERY_PARAM, 0)
        if window:
            window = convert_time_duration(window)
        if window != 0:
            window = get_closest_window_interval(int(window))
        # FIXME: Error checking on window value

        data_format = request.GET.get(DATA_FORMAT_QUERY_PARAM,
                                      VALUE_DATA_FORMAT)
        # FIXME: Error checking on data_format value

        limit = request.GET.get(LIMIT_QUERY_PARAM)
        if limit:
            limit = int(limit)
        # FIXME: Error checking on limit value

        if start_time is not None and end_time is not None:
            # FIXME: Error checking on start_time and end_time values
            sample_interval = request.GET.get(SAMPLE_INTERVAL_QUERY_PARAM)
            if not sample_interval:
                # FIXME: Error checking on sample_period value
                sample_count = request.GET.get(SAMPLE_COUNT_QUERY_PARAM,
                                               DEFAULT_SAMPLE_COUNT)
                # FIXME: Error checking on sample_count value

                sample_interval = (end_time - start_time) / int(sample_count)
            else:
                sample_interval = convert_time_duration(sample_interval)

            if sample_interval != 0:
                sample_interval = get_closest_sample_interval(sample_interval)

            stats_data = get_stats_data(cluster, target_type, target_id,
                                        stats_type, start_time, end_time,
                                        sample_interval, window, data_format,
                                        limit)
        else:
            stats_data = get_latest_stat_data(cluster, target_type, target_id,
                                              stats_type, window, data_format)

        response_data = simplejson.dumps(stats_data)
        response = HttpResponse(response_data, JSON_CONTENT_TYPE)

    elif request.method == 'DELETE':
        delete_stats_data(cluster, target_type, target_id, stats_type,
                          start_time, end_time)
        response = get_successful_response()
    else:
        raise RestInvalidMethodException()

    return response