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
//! Types for creating and serializing an activity map query request.
//!
//! # Serialization & Deserialization
//! All types in this module support serialization and deserialization via `serde`.
//! Types generally try to only serialize properties that differ from the backend
//! defaults; this should reduce the size of the serialized object and improve
//! readability.

use std::ops::Index;

use serde::{Serialize, Serializer};
use serde::ser::SerializeSeq;

use ::{Builder, Oid, QueryTime};
use activitymap::rsp::Appearance;

/// Envelope for an ad-hoc activity map query.
///
/// # Construction
/// If constructed with struct literal syntax, `Query::default()` _must_
/// be used to ensure source compatibility with future library updates.
#[derive(Debug, Clone, Serialize, Deserialize, Default, Builder)]
#[builder(default, setter(into))]
#[serde(default)]
pub struct Query {
    /// The absolute or relative timestamp at which the query should start.
    pub from: QueryTime,

    /// The absolute or relative timestmap at which the query should end. If not set,
    /// defaults to the current packet time of the appliance.
    pub until: QueryTime,

    /// The traversals that should be performed across the topology. Results from all
    /// walks will be merged into a single set of edges in the response.
    pub walks: Vec<Walk>,

    /// The set of metrics should drive the weight of an edge.
    pub weighting: Weighting,

    /// The additional data to return for each edge.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub edge_annotations: Vec<EdgeAnnotation>,
}

/// Find a step configuration for an `rsp::Appearance` from the edge list in
/// a query response.
impl Index<Appearance> for Query {
    type Output = Step;

    fn index(&self, idx: Appearance) -> &Self::Output {
        &self.walks[idx.walk as usize].steps[idx.step as usize]
    }
}

impl From<Walk> for Query {
    fn from(walk: Walk) -> Self {
        Query {
            walks: vec![walk],
            ..Default::default()
        }
    }
}

impl Builder for Query {
    type Builder = QueryBuilder;
}

/// The type of metrics that should be used to compute edge weight.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Weighting {
    /// The number of bytes transferred in both directions between the two peers.
    /// This is the default strategy.
    Bytes,

    /// The number of connections *established* during the time interval.
    ///
    /// This does not include connections opened before the query interval,
    /// so results may be lower than expected, especially for protocols with
    /// long-lived connections.
    Connections,
    Turns,
}

impl Default for Weighting {
    fn default() -> Self {
        Weighting::Bytes
    }
}

/// Flags to opt into additional data about the topology from the appliance.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EdgeAnnotation {
    /// Causes the response to include an array for each edge which lists each
    /// walk and step index which traversed the selected edge.
    Appearances,

    /// Causes the response to include an array for each edge breaking down the
    /// total edge weight by protocol.
    Protocols,
}

/// A set of steps from one or more starting points which build
/// a directed graph topology.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
#[builder(setter(into))]
pub struct Walk {
    /// The starting device(s) for the walk.
    pub origins: WalkOrigin,

    /// The ordered set of steps to take away from the walk origins.
    pub steps: Vec<Step>,
}

impl Default for Walk {
    fn default() -> Self {
        Walk {
            origins: WalkOrigin::All,
            steps: vec![Step::default()],
        }
    }
}

impl Builder for Walk {
    type Builder = WalkBuilder;
}

/// Sets the origins for a walk.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(untagged)]
pub enum WalkOrigin {
    /// Starts the walk at every compatible device.
    All,
    /// Starts the walk from the specified devices or compatible members of the
    /// specified groups.
    Specific(Vec<Source>),
}

impl Serialize for WalkOrigin {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        match *self {
            WalkOrigin::All => {
                /// The backend API overloads the "object_type" field to accept
                /// "all_devices" in walk origins. Since "all_devices" isn't a
                /// valid type in other contexts, we hide that knowledge here.
                #[derive(Serialize)]
                struct AllDevices {
                    object_type: &'static str
                };

                (vec![AllDevices {
                    object_type: "all_devices"
                }]).serialize(s)
            },
            WalkOrigin::Specific(ref sources) => {
                let mut seq = s.serialize_seq(Some(sources.len()))?;
                for source in sources {
                    seq.serialize_element(source)?;
                }

                seq.end()
            }
        }
    }
}

impl From<Vec<Source>> for WalkOrigin {
    fn from(val: Vec<Source>) -> Self {
        WalkOrigin::Specific(val)
    }
}

/// Represents a metric source, such as a device or device group.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Source {
    pub object_type: ObjectType,
    pub object_id: Oid,
}

impl Source {
    /// Create a new `Source` instance.
    pub fn new<I: Into<Oid>>(object_type: ObjectType, id: I) -> Self {
        Source {
            object_type: object_type,
            object_id: id.into(),
        }
    }

    /// Create a new `Source` instance for a device.
    pub fn device<I: Into<Oid>>(id: I) -> Self {
        Source::new(ObjectType::Device, id.into())
    }

    /// Create a new `Source` instance for a device group.
    pub fn device_group<I: Into<Oid>>(id: I) -> Self {
        Source::new(ObjectType::DeviceGroup, id.into())
    }
}

/// Type of a metric source object which is compatible with the topology API.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ObjectType {
    /// An individual endpoint.
    Device,
    /// A user-defined set of devices.
    DeviceGroup,
    /// A system-defined set of all devices speaking a given protocol
    /// during the queried window.
    ActivityGroup,
}

/// A traversal instruction which can find new edges or protocols to include in
/// an activity map.
///
/// Each step moves from all the devices found in the previous step along the
/// specified relationships, and then prunes the found edges based on additional
/// filters such as `peer_in` and `peer_not_in`.
///
/// # Notes
/// * If `relationships` is set to a single protocol and role pair, such as "http server",
///   it is not necessary to also apply a `peer_in` filter for the HTTP Servers activity
///   group.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
#[serde(default)]
#[builder(default, setter(into))]
pub struct Step {
    /// If non-empty, limits the protocol and peer role of edges found
    /// during this step.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub relationships: Vec<Relationship>,

    /// If non-empty, limits the edges found during this step to those whose
    /// devices are in the specified groups.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub peer_in: Vec<Source>,

    /// If non-empty, limits the edges found during this step to those whose
    /// devices are not in the specified groups. If both this property and
    /// `member_of` are present in a step, then an edge must satisfy both
    /// checks to be included in the response.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub peer_not_in: Vec<Source>,
}

impl Default for Step {
    fn default() -> Self {
        Step {
            relationships: vec![],
            peer_in: vec![],
            peer_not_in: vec![],
        }
    }
}

impl Builder for Step {
    type Builder = StepBuilder;
}

/// A combination of protocol and peer role which can match a connection between devices.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct Relationship {
    /// The role of the peer device in the relationship to be discovered.
    #[serde(skip_serializing_if = "Role::is_default")]
    pub role: Role,
    /// The protocol that must be spoken between the start device and peer to include.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub protocol: Option<Protocol>,
}

impl Relationship {
    /// Create a new `Relationship` for the specified protocol and role.
    pub fn new<P: Into<Protocol>>(protocol: P, role: Role) -> Self {
        Self {
            role: role,
            protocol: Some(protocol.into()),
        }
    }
}

/// Create a relationship matching all peers over the specified protocol, regardless
/// of which device fulfilled which role.
impl From<Protocol> for Relationship {
    fn from(val: Protocol) -> Self {
        Relationship::new(val, Role::Any)
    }
}

/// Create a new relationship matching any protocol with the specified peer role.
impl From<Role> for Relationship {
    fn from(role: Role) -> Self {
        Relationship {
            role: role,
            protocol: None,
        }
    }
}

/// The role an endpoint is able to fill in a network transaction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Role {
    Client,
    Server,
    Any,
}

impl Role {
    fn is_default(&self) -> bool {
        *self == Role::Any
    }
}

impl Default for Role {
    fn default() -> Self {
        Role::Any
    }
}

/// A protocol name that will be used to filter the edges traversed during the walk.
///
/// Unlike `rsp::ProtocolStack`, this is a single string and not a full stack.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Protocol(String);

impl<'a> From<&'a str> for Protocol {
    fn from(val: &str) -> Self {
        Protocol(String::from(val))
    }
}

impl From<String> for Protocol {
    fn from(val: String) -> Self {
        Protocol(val)
    }
}

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

    #[test]
    fn source_list_serialize_all_devices() {
        assert_eq!(
            r#"[{"object_type":"all_devices"}]"#,
            serde_json::to_string(&WalkOrigin::All).unwrap()
        );
    }
}