monocle 1.2.0

A commandline application to search, parse, and process BGP information in public sources.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Inspect handlers for unified AS and prefix information lookup
//!
//! This module provides handlers for inspect-related methods like `inspect.query`.
//!
//! The handler uses the InspectLens for unified queries and sends progress notifications
//! when data sources need to be refreshed.

use crate::database::MonocleDatabase;
use crate::lens::inspect::{
    DataRefreshSummary, InspectDataSection, InspectLens, InspectQueryOptions, InspectResult,
};
use crate::server::handler::{WsContext, WsError, WsMethod, WsRequest, WsResult};
use crate::server::op_sink::WsOpSink;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::sync::Arc;

// =============================================================================
// inspect.query
// =============================================================================

/// Parameters for inspect.query
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct InspectQueryParams {
    /// One or more queries: ASN, prefix, IP, or name
    pub queries: Vec<String>,

    /// Force query type: "asn", "prefix", or "name" (optional, auto-detect if not specified)
    #[serde(default)]
    pub query_type: Option<String>,

    /// Sections to include (default: varies by query type)
    /// Available: core, peeringdb, hegemony, population, prefixes, connectivity, roas, aspa, all
    #[serde(default)]
    pub select: Option<Vec<String>>,

    /// Maximum ROAs to return (0 = unlimited, default: 10)
    #[serde(default)]
    pub max_roas: Option<usize>,

    /// Maximum prefixes to return (0 = unlimited, default: 10)
    #[serde(default)]
    pub max_prefixes: Option<usize>,

    /// Maximum neighbors per category (0 = unlimited, default: 5)
    #[serde(default)]
    pub max_neighbors: Option<usize>,

    /// Maximum search results (0 = unlimited, default: 20)
    #[serde(default)]
    pub max_search_results: Option<usize>,

    /// Country code filter for country-based search
    #[serde(default)]
    pub country: Option<String>,
}

/// Progress notification for data refresh
#[derive(Debug, Clone, Serialize)]
pub struct InspectDataRefreshProgress {
    /// Stage of the operation
    pub stage: String,
    /// Message describing the current operation
    pub message: String,
    /// Data source being refreshed (if applicable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    /// Number of records loaded (if applicable)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub count: Option<usize>,
}

/// Response for inspect.query
#[derive(Debug, Clone, Serialize)]
pub struct InspectQueryResponse {
    /// Whether any data was refreshed before the query
    pub data_refreshed: bool,
    /// Summary of data refreshes (if any occurred)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub refresh_summary: Option<DataRefreshSummary>,
    /// Query results
    pub result: InspectResult,
}

/// Handler for inspect.query method
pub struct InspectQueryHandler;

#[async_trait]
impl WsMethod for InspectQueryHandler {
    const METHOD: &'static str = "inspect.query";
    const IS_STREAMING: bool = true; // We send progress notifications for data refresh

    type Params = InspectQueryParams;

    fn validate(params: &Self::Params) -> WsResult<()> {
        // Must have at least one query or a country filter
        if params.queries.is_empty() && params.country.is_none() {
            return Err(WsError::invalid_params(
                "At least one query or country filter is required",
            ));
        }

        // Validate query_type if provided
        if let Some(ref qt) = params.query_type {
            match qt.to_lowercase().as_str() {
                "asn" | "prefix" | "name" => {}
                _ => {
                    return Err(WsError::invalid_params(format!(
                        "Invalid query_type: {}. Use 'asn', 'prefix', or 'name'",
                        qt
                    )));
                }
            }
        }

        // Validate select sections if provided
        if let Some(ref sections) = params.select {
            for section in sections {
                let s_lower = section.to_lowercase();
                if s_lower != "all" && InspectDataSection::from_str(&s_lower).is_none() {
                    return Err(WsError::invalid_params(format!(
                        "Invalid section: {}. Available: {}",
                        section,
                        InspectDataSection::all_names().join(", ")
                    )));
                }
            }
        }

        Ok(())
    }

    async fn handle(
        ctx: Arc<WsContext>,
        _req: WsRequest,
        params: Self::Params,
        sink: WsOpSink,
    ) -> WsResult<()> {
        // Build query options first (no DB needed)
        let options = build_query_options(&params);

        // Do all DB work in a block before any awaits to avoid Send issues
        let (refresh_summary, result): (DataRefreshSummary, InspectResult) = {
            let db = MonocleDatabase::open_in_dir(ctx.data_dir())
                .map_err(|e| WsError::internal(format!("Failed to open database: {}", e)))?;

            let lens = InspectLens::new(&db, &ctx.config);

            // Ensure data is available, refreshing if needed
            let refresh_summary = lens
                .ensure_data_available()
                .map_err(|e| WsError::operation_failed(format!("Failed to ensure data: {}", e)))?;

            // Execute query
            let result = if let Some(ref country) = params.country {
                lens.query_by_country(country, &options)
                    .map_err(|e| WsError::operation_failed(e.to_string()))?
            } else if let Some(ref query_type) = params.query_type {
                match query_type.to_lowercase().as_str() {
                    "asn" => lens
                        .query_as_asn(&params.queries, &options)
                        .map_err(|e| WsError::operation_failed(e.to_string()))?,
                    "prefix" => lens
                        .query_as_prefix(&params.queries, &options)
                        .map_err(|e| WsError::operation_failed(e.to_string()))?,
                    "name" => lens
                        .query_as_name(&params.queries, &options)
                        .map_err(|e| WsError::operation_failed(e.to_string()))?,
                    _ => {
                        return Err(WsError::invalid_params(format!(
                            "Invalid query_type: {}",
                            query_type
                        )));
                    }
                }
            } else {
                // Auto-detect query types
                lens.query(&params.queries, &options)
                    .map_err(|e| WsError::operation_failed(e.to_string()))?
            };

            (refresh_summary, result)
        };

        // Now we can safely do awaits - send progress notifications for any refreshes
        if refresh_summary.any_refreshed {
            for refresh in &refresh_summary.sources {
                if refresh.refreshed {
                    sink.send_progress(InspectDataRefreshProgress {
                        stage: "refreshed".to_string(),
                        message: refresh.message.clone(),
                        source: Some(refresh.source.clone()),
                        count: refresh.count,
                    })
                    .await
                    .map_err(|e| WsError::internal(e.to_string()))?;
                }
            }
        }

        // Build response
        let response = InspectQueryResponse {
            data_refreshed: refresh_summary.any_refreshed,
            refresh_summary: if refresh_summary.any_refreshed {
                Some(refresh_summary)
            } else {
                None
            },
            result,
        };

        // Send final result
        sink.send_result(response)
            .await
            .map_err(|e| WsError::internal(e.to_string()))?;

        Ok(())
    }
}

/// Build query options from parameters
fn build_query_options(params: &InspectQueryParams) -> InspectQueryOptions {
    let mut options = InspectQueryOptions::default();

    // Parse select sections
    if let Some(ref sections) = params.select {
        let mut selected = HashSet::new();

        for s in sections {
            let s_lower = s.to_lowercase();
            if s_lower == "all" {
                selected.extend(InspectDataSection::all());
            } else if let Some(section) = InspectDataSection::from_str(&s_lower) {
                selected.insert(section);
            }
        }

        if !selected.is_empty() {
            options.select = Some(selected);
        }
    }

    // Apply limits
    if let Some(max_roas) = params.max_roas {
        options.max_roas = max_roas;
    }

    if let Some(max_prefixes) = params.max_prefixes {
        options.max_prefixes = max_prefixes;
    }

    if let Some(max_neighbors) = params.max_neighbors {
        options.max_neighbors = max_neighbors;
    }

    if let Some(max_search_results) = params.max_search_results {
        options.max_search_results = max_search_results;
    }

    options
}

// =============================================================================
// inspect.refresh
// =============================================================================

/// Parameters for inspect.refresh
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct InspectRefreshParams {
    /// Force refresh even if data is not stale
    #[serde(default)]
    pub force: bool,
}

/// Response for inspect.refresh
#[derive(Debug, Clone, Serialize)]
pub struct InspectRefreshResponse {
    /// Summary of refresh operations
    pub summary: DataRefreshSummary,
}

/// Handler for inspect.refresh method
pub struct InspectRefreshHandler;

#[async_trait]
impl WsMethod for InspectRefreshHandler {
    const METHOD: &'static str = "inspect.refresh";
    const IS_STREAMING: bool = true; // We send progress notifications

    type Params = InspectRefreshParams;

    async fn handle(
        ctx: Arc<WsContext>,
        _req: WsRequest,
        _params: Self::Params,
        sink: WsOpSink,
    ) -> WsResult<()> {
        // Do all DB work in a block before any awaits
        let summary: DataRefreshSummary = {
            let db = MonocleDatabase::open_in_dir(ctx.data_dir())
                .map_err(|e| WsError::internal(format!("Failed to open database: {}", e)))?;

            let lens = InspectLens::new(&db, &ctx.config);

            // Perform refresh
            lens.ensure_data_available()
                .map_err(|e| WsError::operation_failed(format!("Failed to refresh data: {}", e)))?
        };

        // Now we can safely do awaits - send progress for each refresh
        for refresh in &summary.sources {
            sink.send_progress(InspectDataRefreshProgress {
                stage: if refresh.refreshed {
                    "refreshed"
                } else {
                    "skipped"
                }
                .to_string(),
                message: refresh.message.clone(),
                source: Some(refresh.source.clone()),
                count: refresh.count,
            })
            .await
            .map_err(|e| WsError::internal(e.to_string()))?;
        }

        // Send final result
        let response = InspectRefreshResponse { summary };

        sink.send_result(response)
            .await
            .map_err(|e| WsError::internal(e.to_string()))?;

        Ok(())
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_inspect_query_params_default() {
        let params: InspectQueryParams = serde_json::from_str(r#"{"queries": ["13335"]}"#).unwrap();
        assert_eq!(params.queries, vec!["13335"]);
        assert!(params.query_type.is_none());
        assert!(params.select.is_none());
        assert!(params.max_roas.is_none());
    }

    #[test]
    fn test_inspect_query_params_full() {
        let params: InspectQueryParams = serde_json::from_str(
            r#"{
                "queries": ["13335", "1.1.1.0/24"],
                "query_type": "asn",
                "select": ["core", "connectivity"],
                "max_roas": 5,
                "max_neighbors": 10
            }"#,
        )
        .unwrap();

        assert_eq!(params.queries.len(), 2);
        assert_eq!(params.query_type, Some("asn".to_string()));
        assert_eq!(
            params.select,
            Some(vec!["core".to_string(), "connectivity".to_string()])
        );
        assert_eq!(params.max_roas, Some(5));
        assert_eq!(params.max_neighbors, Some(10));
    }

    #[test]
    fn test_inspect_query_validation_empty_queries() {
        let params = InspectQueryParams {
            queries: vec![],
            query_type: None,
            select: None,
            max_roas: None,
            max_prefixes: None,
            max_neighbors: None,
            max_search_results: None,
            country: None,
        };

        let result = InspectQueryHandler::validate(&params);
        assert!(result.is_err());
    }

    #[test]
    fn test_inspect_query_validation_with_country() {
        let params = InspectQueryParams {
            queries: vec![],
            query_type: None,
            select: None,
            max_roas: None,
            max_prefixes: None,
            max_neighbors: None,
            max_search_results: None,
            country: Some("US".to_string()),
        };

        let result = InspectQueryHandler::validate(&params);
        assert!(result.is_ok());
    }

    #[test]
    fn test_inspect_query_validation_invalid_query_type() {
        let params = InspectQueryParams {
            queries: vec!["13335".to_string()],
            query_type: Some("invalid".to_string()),
            select: None,
            max_roas: None,
            max_prefixes: None,
            max_neighbors: None,
            max_search_results: None,
            country: None,
        };

        let result = InspectQueryHandler::validate(&params);
        assert!(result.is_err());
    }

    #[test]
    fn test_inspect_query_validation_invalid_section() {
        let params = InspectQueryParams {
            queries: vec!["13335".to_string()],
            query_type: None,
            select: Some(vec!["invalid_section".to_string()]),
            max_roas: None,
            max_prefixes: None,
            max_neighbors: None,
            max_search_results: None,
            country: None,
        };

        let result = InspectQueryHandler::validate(&params);
        assert!(result.is_err());
    }

    #[test]
    fn test_build_query_options_defaults() {
        let params = InspectQueryParams {
            queries: vec!["13335".to_string()],
            query_type: None,
            select: None,
            max_roas: None,
            max_prefixes: None,
            max_neighbors: None,
            max_search_results: None,
            country: None,
        };

        let options = build_query_options(&params);
        assert!(options.select.is_none());
        assert_eq!(options.max_roas, 10); // default
        assert_eq!(options.max_prefixes, 10); // default
        assert_eq!(options.max_neighbors, 5); // default
        assert_eq!(options.max_search_results, 20); // default
    }

    #[test]
    fn test_build_query_options_with_select() {
        let params = InspectQueryParams {
            queries: vec!["13335".to_string()],
            query_type: None,
            select: Some(vec!["basic".to_string(), "rpki".to_string()]),
            max_roas: Some(100),
            max_prefixes: None,
            max_neighbors: None,
            max_search_results: None,
            country: None,
        };

        let options = build_query_options(&params);
        assert!(options.select.is_some());
        let select = options.select.unwrap();
        assert!(select.contains(&InspectDataSection::Basic));
        assert!(select.contains(&InspectDataSection::Rpki));
        assert_eq!(options.max_roas, 100);
    }

    #[test]
    fn test_inspect_refresh_params_default() {
        let params: InspectRefreshParams = serde_json::from_str(r#"{}"#).unwrap();
        assert!(!params.force);
    }

    #[test]
    fn test_data_refresh_progress_serialization() {
        let progress = InspectDataRefreshProgress {
            stage: "refreshing".to_string(),
            message: "Refreshing RPKI data...".to_string(),
            source: Some("rpki".to_string()),
            count: Some(1000),
        };

        let json = serde_json::to_string(&progress).unwrap();
        assert!(json.contains("\"stage\":\"refreshing\""));
        assert!(json.contains("\"source\":\"rpki\""));
        assert!(json.contains("\"count\":1000"));
    }
}