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
use error::Error;
use uuid::Uuid;
use graph_description::ProcessInboundConnection;
use serde_json::Value;
use std::convert::TryFrom;
use node::NodeT;


pub enum ProcessInboundConnectionState {
    Bound,
    Existing,
    Closed,
}

impl From<ProcessInboundConnectionState> for u32 {
    fn from(p: ProcessInboundConnectionState) -> u32 {
        match p {
            ProcessInboundConnectionState::Bound => 1,
            ProcessInboundConnectionState::Closed => 2,
            ProcessInboundConnectionState::Existing => 3,
        }
    }
}

impl TryFrom<u32> for ProcessInboundConnectionState {
    type Error = Error;

    fn try_from(p: u32) -> Result<ProcessInboundConnectionState, Error> {
        match p {
            1 => Ok(ProcessInboundConnectionState::Bound),
            2 => Ok(ProcessInboundConnectionState::Closed),
            3 => Ok(ProcessInboundConnectionState::Existing),
            _ => Err(Error::InvalidProcessInboundConnectionState(p))
        }
    }
}

impl ProcessInboundConnection {
    pub fn new(
        asset_id: impl Into<Option<String>>,
        hostname: impl Into<Option<String>>,
        state: ProcessInboundConnectionState,
        port: u16,
        ip_address: impl Into<String>,
        protocol: impl Into<String>,
        created_timestamp: u64,
        terminated_timestamp: u64,
        last_seen_timestamp: u64,
    ) -> Self {
        let asset_id = asset_id.into();
        let hostname = hostname.into();
        let protocol = protocol.into();

        if hostname.is_none() && asset_id.is_none() {
            panic!("ProcessInboundConnection must have at least asset_id or hostname");
        }

        let ip_address = ip_address.into();

        Self {
            node_key: Uuid::new_v4().to_string(),
            ip_address,
            asset_id,
            hostname,
            protocol,
            created_timestamp,
            terminated_timestamp,
            last_seen_timestamp,
            port: port as u32,
            state: state.into(),
        }
    }

    pub fn into_json(self) -> Value {
        let mut j = json!({
            "node_key": self.node_key,
            "dgraph.type": "ProcessInboundConnection",
            "asset_id": self.asset_id.unwrap(),
            "protocol": self.protocol,
            "port": self.port,
        });

        if self.created_timestamp != 0 {
            j["created_timestamp"] = self.created_timestamp.into();
        }
        if self.terminated_timestamp != 0 {
            j["terminated_timestamp"] = self.terminated_timestamp.into();
        }
        if self.last_seen_timestamp != 0 {
            j["last_seen_timestamp"] = self.last_seen_timestamp.into();
        }

        j
    }
}

impl NodeT for ProcessInboundConnection {
    fn get_asset_id(&self) -> Option<&str> {
        None
    }

    fn set_asset_id(&mut self, asset_id: impl Into<String>) {
        self.asset_id = Some(asset_id.into());
    }

    fn get_node_key(&self) -> &str {
        &self.node_key
    }

    fn set_node_key(&mut self, node_key: impl Into<String>) {
        self.node_key = node_key.into();
    }

    fn merge(&mut self, other: &Self) -> bool {
        if self.node_key != other.node_key {
            warn!("Attempted to merge two ProcessInboundConnection Nodes with differing node_keys");
            return false
        }

        if self.ip_address != other.ip_address {
            warn!("Attempted to merge two ProcessInboundConnection Nodes with differing IPs");
            return false;
        }

        let mut merged = false;

        if self.asset_id.is_none() && other.asset_id.is_some() {
            self.asset_id = other.asset_id.clone();
        }

        if self.hostname.is_none() && other.hostname.is_some() {
            self.hostname = other.hostname.clone();
        }

        if self.created_timestamp != 0 && self.created_timestamp > other.created_timestamp {
            self.created_timestamp = other.created_timestamp;
            merged = true;
        }

        if self.terminated_timestamp != 0 && self.terminated_timestamp < other.terminated_timestamp {
            self.terminated_timestamp = other.terminated_timestamp;
            merged = true;
        }

        if self.last_seen_timestamp != 0 && self.last_seen_timestamp < other.last_seen_timestamp {
            self.last_seen_timestamp = other.last_seen_timestamp;
            merged = true;
        }

        merged
    }

    fn merge_into(&mut self, other: Self) -> bool {
        self.merge(&other)
    }
}