|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright Open Network Fabric Authors |
| 3 | + |
| 4 | +use std::collections::BTreeMap; |
| 5 | + |
| 6 | +use k8s_intf::gateway_agent_crd::{ |
| 7 | + GatewayAgentStatusStateBgp, GatewayAgentStatusStateBgpVrfs, |
| 8 | + GatewayAgentStatusStateBgpVrfsNeighbors, |
| 9 | + GatewayAgentStatusStateBgpVrfsNeighborsIpv4UnicastPrefixes, |
| 10 | + GatewayAgentStatusStateBgpVrfsNeighborsIpv6UnicastPrefixes, |
| 11 | + GatewayAgentStatusStateBgpVrfsNeighborsL2VpnevpnPrefixes, |
| 12 | + GatewayAgentStatusStateBgpVrfsNeighborsMessages, |
| 13 | + GatewayAgentStatusStateBgpVrfsNeighborsMessagesReceived, |
| 14 | + GatewayAgentStatusStateBgpVrfsNeighborsMessagesSent, |
| 15 | + GatewayAgentStatusStateBgpVrfsNeighborsSessionState, |
| 16 | +}; |
| 17 | + |
| 18 | +use crate::converters::k8s::ToK8sConversionError; |
| 19 | +use crate::internal::status::{ |
| 20 | + BgpMessageCounters, BgpMessages, BgpNeighborPrefixes, BgpNeighborSessionState, |
| 21 | + BgpNeighborStatus, BgpStatus, BgpVrfStatus, |
| 22 | +}; |
| 23 | + |
| 24 | +fn u64_to_i64_sat(v: u64) -> i64 { |
| 25 | + i64::try_from(v).unwrap_or(i64::MAX) |
| 26 | +} |
| 27 | + |
| 28 | +fn u32_to_i32_sat(v: u32) -> i32 { |
| 29 | + i32::try_from(v).unwrap_or(i32::MAX) |
| 30 | +} |
| 31 | + |
| 32 | +impl TryFrom<&BgpStatus> for GatewayAgentStatusStateBgp { |
| 33 | + type Error = ToK8sConversionError; |
| 34 | + |
| 35 | + fn try_from(status: &BgpStatus) -> Result<Self, Self::Error> { |
| 36 | + let vrfs = status |
| 37 | + .vrfs |
| 38 | + .iter() |
| 39 | + .map(|(k, v)| Ok((k.clone(), GatewayAgentStatusStateBgpVrfs::try_from(v)?))) |
| 40 | + .collect::<Result<BTreeMap<_, _>, _>>()?; |
| 41 | + |
| 42 | + Ok(GatewayAgentStatusStateBgp { |
| 43 | + vrfs: Some(vrfs).filter(|m| !m.is_empty()), |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl TryFrom<&BgpVrfStatus> for GatewayAgentStatusStateBgpVrfs { |
| 49 | + type Error = ToK8sConversionError; |
| 50 | + |
| 51 | + fn try_from(status: &BgpVrfStatus) -> Result<Self, Self::Error> { |
| 52 | + let neighbors = status |
| 53 | + .neighbors |
| 54 | + .iter() |
| 55 | + .map(|(k, v)| { |
| 56 | + Ok(( |
| 57 | + k.clone(), |
| 58 | + GatewayAgentStatusStateBgpVrfsNeighbors::try_from(v)?, |
| 59 | + )) |
| 60 | + }) |
| 61 | + .collect::<Result<BTreeMap<_, _>, _>>()?; |
| 62 | + |
| 63 | + Ok(GatewayAgentStatusStateBgpVrfs { |
| 64 | + neighbors: Some(neighbors).filter(|m| !m.is_empty()), |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl TryFrom<&BgpNeighborStatus> for GatewayAgentStatusStateBgpVrfsNeighbors { |
| 70 | + type Error = ToK8sConversionError; |
| 71 | + |
| 72 | + fn try_from(status: &BgpNeighborStatus) -> Result<Self, Self::Error> { |
| 73 | + let session_state = match status.session_state { |
| 74 | + BgpNeighborSessionState::Unset => { |
| 75 | + GatewayAgentStatusStateBgpVrfsNeighborsSessionState::Unset |
| 76 | + } |
| 77 | + BgpNeighborSessionState::Idle => { |
| 78 | + GatewayAgentStatusStateBgpVrfsNeighborsSessionState::Idle |
| 79 | + } |
| 80 | + BgpNeighborSessionState::Connect => { |
| 81 | + GatewayAgentStatusStateBgpVrfsNeighborsSessionState::Connect |
| 82 | + } |
| 83 | + BgpNeighborSessionState::Active => { |
| 84 | + GatewayAgentStatusStateBgpVrfsNeighborsSessionState::Active |
| 85 | + } |
| 86 | + BgpNeighborSessionState::Open => { |
| 87 | + GatewayAgentStatusStateBgpVrfsNeighborsSessionState::Open |
| 88 | + } |
| 89 | + BgpNeighborSessionState::Established => { |
| 90 | + GatewayAgentStatusStateBgpVrfsNeighborsSessionState::Established |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + Ok(GatewayAgentStatusStateBgpVrfsNeighbors { |
| 95 | + enabled: Some(status.enabled), |
| 96 | + local_as: Some(u32_to_i32_sat(status.local_as)), |
| 97 | + peer_as: Some(u32_to_i32_sat(status.peer_as)), |
| 98 | + remote_router_id: Some(status.remote_router_id.clone()), |
| 99 | + session_state: Some(session_state), |
| 100 | + |
| 101 | + connections_dropped: Some(status.connections_dropped), |
| 102 | + // NOTE: generated CRD uses i64 here |
| 103 | + established_transitions: Some(u64_to_i64_sat(status.established_transitions)), |
| 104 | + last_reset_reason: Some(status.last_reset_reason.clone()), |
| 105 | + |
| 106 | + messages: status |
| 107 | + .messages |
| 108 | + .as_ref() |
| 109 | + .map(GatewayAgentStatusStateBgpVrfsNeighborsMessages::try_from) |
| 110 | + .transpose()?, |
| 111 | + |
| 112 | + ipv4_unicast_prefixes: status |
| 113 | + .ipv4_unicast_prefixes |
| 114 | + .as_ref() |
| 115 | + .map(GatewayAgentStatusStateBgpVrfsNeighborsIpv4UnicastPrefixes::try_from) |
| 116 | + .transpose()?, |
| 117 | + |
| 118 | + ipv6_unicast_prefixes: status |
| 119 | + .ipv6_unicast_prefixes |
| 120 | + .as_ref() |
| 121 | + .map(GatewayAgentStatusStateBgpVrfsNeighborsIpv6UnicastPrefixes::try_from) |
| 122 | + .transpose()?, |
| 123 | + |
| 124 | + l2_vpnevpn_prefixes: status |
| 125 | + .l2vpn_evpn_prefixes |
| 126 | + .as_ref() |
| 127 | + .map(GatewayAgentStatusStateBgpVrfsNeighborsL2VpnevpnPrefixes::try_from) |
| 128 | + .transpose()?, |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl TryFrom<&BgpNeighborPrefixes> for GatewayAgentStatusStateBgpVrfsNeighborsIpv4UnicastPrefixes { |
| 134 | + type Error = ToK8sConversionError; |
| 135 | + |
| 136 | + fn try_from(p: &BgpNeighborPrefixes) -> Result<Self, Self::Error> { |
| 137 | + Ok(GatewayAgentStatusStateBgpVrfsNeighborsIpv4UnicastPrefixes { |
| 138 | + received: Some(u32_to_i32_sat(p.received)), |
| 139 | + received_pre_policy: Some(u32_to_i32_sat(p.received_pre_policy)), |
| 140 | + sent: Some(u32_to_i32_sat(p.sent)), |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +impl TryFrom<&BgpNeighborPrefixes> for GatewayAgentStatusStateBgpVrfsNeighborsIpv6UnicastPrefixes { |
| 146 | + type Error = ToK8sConversionError; |
| 147 | + |
| 148 | + fn try_from(p: &BgpNeighborPrefixes) -> Result<Self, Self::Error> { |
| 149 | + Ok(GatewayAgentStatusStateBgpVrfsNeighborsIpv6UnicastPrefixes { |
| 150 | + received: Some(u32_to_i32_sat(p.received)), |
| 151 | + received_pre_policy: Some(u32_to_i32_sat(p.received_pre_policy)), |
| 152 | + sent: Some(u32_to_i32_sat(p.sent)), |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +impl TryFrom<&BgpNeighborPrefixes> for GatewayAgentStatusStateBgpVrfsNeighborsL2VpnevpnPrefixes { |
| 158 | + type Error = ToK8sConversionError; |
| 159 | + |
| 160 | + fn try_from(p: &BgpNeighborPrefixes) -> Result<Self, Self::Error> { |
| 161 | + Ok(GatewayAgentStatusStateBgpVrfsNeighborsL2VpnevpnPrefixes { |
| 162 | + received: Some(u32_to_i32_sat(p.received)), |
| 163 | + received_pre_policy: Some(u32_to_i32_sat(p.received_pre_policy)), |
| 164 | + sent: Some(u32_to_i32_sat(p.sent)), |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +impl TryFrom<&BgpMessages> for GatewayAgentStatusStateBgpVrfsNeighborsMessages { |
| 170 | + type Error = ToK8sConversionError; |
| 171 | + |
| 172 | + fn try_from(m: &BgpMessages) -> Result<Self, Self::Error> { |
| 173 | + Ok(GatewayAgentStatusStateBgpVrfsNeighborsMessages { |
| 174 | + received: m |
| 175 | + .received |
| 176 | + .as_ref() |
| 177 | + .map(GatewayAgentStatusStateBgpVrfsNeighborsMessagesReceived::try_from) |
| 178 | + .transpose()?, |
| 179 | + sent: m |
| 180 | + .sent |
| 181 | + .as_ref() |
| 182 | + .map(GatewayAgentStatusStateBgpVrfsNeighborsMessagesSent::try_from) |
| 183 | + .transpose()?, |
| 184 | + }) |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +impl TryFrom<&BgpMessageCounters> for GatewayAgentStatusStateBgpVrfsNeighborsMessagesReceived { |
| 189 | + type Error = ToK8sConversionError; |
| 190 | + |
| 191 | + fn try_from(c: &BgpMessageCounters) -> Result<Self, Self::Error> { |
| 192 | + Ok(GatewayAgentStatusStateBgpVrfsNeighborsMessagesReceived { |
| 193 | + capability: Some(u64_to_i64_sat(c.capability)), |
| 194 | + keepalive: Some(u64_to_i64_sat(c.keepalive)), |
| 195 | + notification: Some(u64_to_i64_sat(c.notification)), |
| 196 | + open: Some(u64_to_i64_sat(c.open)), |
| 197 | + route_refresh: Some(u64_to_i64_sat(c.route_refresh)), |
| 198 | + update: Some(u64_to_i64_sat(c.update)), |
| 199 | + }) |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +impl TryFrom<&BgpMessageCounters> for GatewayAgentStatusStateBgpVrfsNeighborsMessagesSent { |
| 204 | + type Error = ToK8sConversionError; |
| 205 | + |
| 206 | + fn try_from(c: &BgpMessageCounters) -> Result<Self, Self::Error> { |
| 207 | + Ok(GatewayAgentStatusStateBgpVrfsNeighborsMessagesSent { |
| 208 | + capability: Some(u64_to_i64_sat(c.capability)), |
| 209 | + keepalive: Some(u64_to_i64_sat(c.keepalive)), |
| 210 | + notification: Some(u64_to_i64_sat(c.notification)), |
| 211 | + open: Some(u64_to_i64_sat(c.open)), |
| 212 | + route_refresh: Some(u64_to_i64_sat(c.route_refresh)), |
| 213 | + update: Some(u64_to_i64_sat(c.update)), |
| 214 | + }) |
| 215 | + } |
| 216 | +} |
0 commit comments