Repository: napalm-automation/napalm

Issue:
IOS XR get_bgp_neighbors_detail: flap_count return a negative value
### Description of Issue/Question

*Note*: Please check https://guides.github.com/features/mastering-markdown/
to see how to properly format your request.

```
I notice that the get_bgp_neighbors_detail function returns a negative value for flap_count as seen in the output below.
Script
=====
from napalm import get_network_driver
driver = get_network_driver('iosxr')
device1.open()

info = device1.get_bgp_neighbors_detail()
print (json.dumps(info, indent=4))

Output returned:
===========
    "VRF-A": {
        "110": [
            {
                "up": true,
                "local_as": 18522,
                "remote_as": 110,
                "router_id": "198.0.0.177",
                "local_address": "10:11:b0::1",
                "routing_table": "VRF-A",
                "local_address_configured": false,
                "local_port": 179,
                "remote_address": "10:11:b0::11",
                "remote_port": 33155,
                "multihop": false,
                "multipath": false,
                "import_policy": "pass-all",
                "export_policy": "pass-all",
                "input_messages": 0,
                "output_messages": 3061,
                "input_updates": 0,
                "output_updates": 0,
                "messages_queued_out": 0,
                "connection_state": "Established",
                "previous_connection_state": "Connect",
                "last_event": "",
                "remove_private_as": false,
                "suppress_4byte_as": false,
                "local_as_prepend": true,
                "holdtime": 90,
                "configured_holdtime": 180,
                "keepalive": 30,
                "configured_keepalive": 60,
                "active_prefix_count": 101,
                "received_prefix_count": 101,
                "accepted_prefix_count": 101,
                "suppressed_prefix_count": 0,
                "advertised_prefix_count": 202,
                **"flap_count": -1**
            }
        ]
    }

I looked at the following relevant code at napalm/napalm/iosxr/iosxr.py:
 
                connection_down_count = napalm.base.helpers.convert(
                    int,
                    napalm.base.helpers.find_txt(neighbor, "ConnectionDownCount"),
                    0,
                )

               flap_count = int(connection_down_count / 2)
                if up:
                    **flap_count -= 1**

The connection-down-count field can have value in the range 0 to 4294967295. If the value is 0, the above code will result in a flap_count that is negative. This logic needs to be fixed.
```

### Did you follow the steps from https://github.com/napalm-automation/napalm#faq
(Place an ``x`` between the square brackets where applicable)

- [x] Yes
- [] No


### Setup

### napalm version
(Paste verbatim output from `pip freeze | grep napalm` between quotes below)

```
napalm==3.2.0
napalm-yang==0.1.0
```

### Network operating system version
(Paste verbatim output from `show version` - or equivalent - between quotes below)

```
Cisco IOS XR Software, Version 7.4.1.08I
```

### Steps to Reproduce the Issue

### Error Traceback
(Paste the complete traceback of the exception between quotes below)

```

```

IOS XR get_bgp_neighbors_detail: flap_count return a negative value
### Description of Issue/Question

*Note*: Please check https://guides.github.com/features/mastering-markdown/
to see how to properly format your request.

```
I notice that the get_bgp_neighbors_detail function returns a negative value for flap_count as seen in the output below.
Script
=====
from napalm import get_network_driver
driver = get_network_driver('iosxr')
device1.open()

info = device1.get_bgp_neighbors_detail()
print (json.dumps(info, indent=4))

Output returned:
===========
    "VRF-A": {
        "110": [
            {
                "up": true,
                "local_as": 18522,
                "remote_as": 110,
                "router_id": "198.0.0.177",
                "local_address": "10:11:b0::1",
                "routing_table": "VRF-A",
                "local_address_configured": false,
                "local_port": 179,
                "remote_address": "10:11:b0::11",
                "remote_port": 33155,
                "multihop": false,
                "multipath": false,
                "import_policy": "pass-all",
                "export_policy": "pass-all",
                "input_messages": 0,
                "output_messages": 3061,
                "input_updates": 0,
                "output_updates": 0,
                "messages_queued_out": 0,
                "connection_state": "Established",
                "previous_connection_state": "Connect",
                "last_event": "",
                "remove_private_as": false,
                "suppress_4byte_as": false,
                "local_as_prepend": true,
                "holdtime": 90,
                "configured_holdtime": 180,
                "keepalive": 30,
                "configured_keepalive": 60,
                "active_prefix_count": 101,
                "received_prefix_count": 101,
                "accepted_prefix_count": 101,
                "suppressed_prefix_count": 0,
                "advertised_prefix_count": 202,
                **"flap_count": -1**
            }
        ]
    }

I looked at the following relevant code at napalm/napalm/iosxr/iosxr.py:
 
                connection_down_count = napalm.base.helpers.convert(
                    int,
                    napalm.base.helpers.find_txt(neighbor, "ConnectionDownCount"),
                    0,
                )

               flap_count = int(connection_down_count / 2)
                if up:
                    **flap_count -= 1**

The connection-down-count field can have value in the range 0 to 4294967295. If the value is 0, the above code will result in a flap_count that is negative. This logic needs to be fixed.
```

### Did you follow the steps from https://github.com/napalm-automation/napalm#faq
(Place an ``x`` between the square brackets where applicable)

- [x] Yes
- [] No


### Setup

### napalm version
(Paste verbatim output from `pip freeze | grep napalm` between quotes below)

```
napalm==3.2.0
napalm-yang==0.1.0
```

### Network operating system version
(Paste verbatim output from `show version` - or equivalent - between quotes below)

```
Cisco IOS XR Software, Version 7.4.1.08I
```

### Steps to Reproduce the Issue

### Error Traceback
(Paste the complete traceback of the exception between quotes below)

```

```



Discussion:
Hi @pkinicisco. Yes, feel free to open a PR to fix this, I'll mark it as a bug. Thanks for reporting.
Hi @pkinicisco. Yes, feel free to open a PR to fix this, I'll mark it as a bug. Thanks for reporting.

Resolving patch:
diff --git a/napalm/iosxr/iosxr.py b/napalm/iosxr/iosxr.py
--- a/napalm/iosxr/iosxr.py
+++ b/napalm/iosxr/iosxr.py
@@ -1283,7 +1283,7 @@ def get_bgp_neighbors_detail(self, neighbor_address=""):
                 output_messages = napalm.base.helpers.convert(
                     int, napalm.base.helpers.find_txt(neighbor, "MessagesSent"), 0
                 )
-                connection_down_count = napalm.base.helpers.convert(
+                flap_count = napalm.base.helpers.convert(
                     int,
                     napalm.base.helpers.find_txt(neighbor, "ConnectionDownCount"),
                     0,
@@ -1361,9 +1361,6 @@ def get_bgp_neighbors_detail(self, neighbor_address=""):
                     napalm.base.helpers.find_txt(neighbor, "ConfiguredKeepalive"),
                     0,
                 )
-                flap_count = int(connection_down_count / 2)
-                if up:
-                    flap_count -= 1
                 if remote_as not in bgp_neighbors_detail[vrf_name].keys():
                     bgp_neighbors_detail[vrf_name][remote_as] = []
                 bgp_neighbors_detail[vrf_name][remote_as].append(


Test patch:
diff --git a/test/iosxr/mocked_data/test_get_bgp_neighbors_detail/normal/expected_result.json b/test/iosxr/mocked_data/test_get_bgp_neighbors_detail/normal/expected_result.json
--- a/test/iosxr/mocked_data/test_get_bgp_neighbors_detail/normal/expected_result.json
+++ b/test/iosxr/mocked_data/test_get_bgp_neighbors_detail/normal/expected_result.json
@@ -90,7 +90,7 @@
 			"active_prefix_count": 273802,
 			"configured_holdtime": 180,
 			"routing_table": "default",
-			"flap_count": 1,
+			"flap_count": 5,
 			"suppressed_prefix_count": 9,
 			"local_address": "20.20.20.21",
 			"remote_port": 179,
@@ -127,7 +127,7 @@
 			"active_prefix_count": 0,
 			"configured_holdtime": 180,
 			"routing_table": "default",
-			"flap_count": 3,
+			"flap_count": 9,
 			"suppressed_prefix_count": 0,
 			"local_address": "8.8.8.8",
 			"remote_port": 63014,