Repository: napalm-automation/napalm

Issue:
get_bgp_neighbors returns "is_enabled: true" for admin down peers on JunOS
### Description of Issue/Question

When running get_bgp_neighbors against a JunOS router, it returns "is_enabled: true" for peers that are actually admin down.

### 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
```
# pip freeze | grep napalm
napalm==3.3.1
```

### Network operating system version
```
> show version 
Model: mx204
Junos: 19.4R3.11
```

### Steps to Reproduce the Issue
```
On a JunOS router:

> configure
# set protocols bgp group EBGP neighbor 192.168.255.1 shutdown 
# commit
# exit
> show configuration protocols bgp group EBGP neighbor 192.168.255.1 
description "Test";
authentication-key "removed"; ## SECRET-DATA
export EBGP_OUT;
peer-as 65101;
shutdown;

> show bgp summary 
Threading mode: BGP I/O
Groups: 2 Peers: 3 Down peers: 1
Table          Tot Paths  Act Paths Suppressed    History Damp State    Pending
inet.0               
                       6          6          0          0          0          0
Peer                     AS      InPkt     OutPkt    OutQ   Flaps Last Up/Dwn State|#Active/Received/Accepted/Damped...
192.168.255.1         65101      52390      57967       0       1     1:16:08 Idle  
192.168.255.252       65001     342520     379127       0       2 16w6d 22:41:07 Establ  inet.0: 3/3/3/0
192.168.255.254       65001     342519     379126       0       2 16w6d 22:41:03 Establ  inet.0: 3/3/3/0

On a Linux box:

# napalm --vendor junos --user USER --password PASS HOST call get_bgp_neighbors
{
    "global": {
        "router_id": "",
        "peers": {
            "192.168.255.1": {
                "local_as": <removed>,
                "remote_as": 65101,
                "remote_id": "",
                "is_up": false,
                "is_enabled": true,     <---- THIS IS INCORRECT
                "uptime": 3658,
                "address_family": {
                    "ipv4": {
                        "received_prefixes": -1,
                        "accepted_prefixes": -1,
                        "sent_prefixes": -1
                    },
                    "ipv6": {
                        "received_prefixes": -1,
                        "accepted_prefixes": -1,
                        "sent_prefixes": -1
                    }
                }
            },
            "192.168.255.252": {
                "local_as": <removed>,
                "remote_as": 65001,
                "remote_id": "192.168.255.252",
                "is_up": true,
                "is_enabled": true,
                "description": "",
                "uptime": 10275957,
                "address_family": {
                    "ipv4": {
                        "received_prefixes": 3,
                        "accepted_prefixes": 3,
                        "sent_prefixes": 6
                    },
                    "ipv6": {
                        "received_prefixes": -1,
                        "accepted_prefixes": -1,
                        "sent_prefixes": -1
                    }
                }
            },
            "192.168.255.254": {
                "local_as": <removed>,
                "remote_as": 65001,
                "remote_id": "192.168.255.252",
                "is_up": true,
                "is_enabled": true,
                "description": "",
                "uptime": 10275953,
                "address_family": {
                    "ipv4": {
                        "received_prefixes": 3,
                        "accepted_prefixes": 3,
                        "sent_prefixes": 6
                    },
                    "ipv6": {
                        "received_prefixes": -1,
                        "accepted_prefixes": -1,
                        "sent_prefixes": -1
                    }
                }
            }
        }
    }
}
```
### Error Traceback
(Paste the complete traceback of the exception between quotes below)
```
No traceback, just incorrect returned value.
```



Discussion:

Resolving patch:
diff --git a/napalm/junos/junos.py b/napalm/junos/junos.py
--- a/napalm/junos/junos.py
+++ b/napalm/junos/junos.py
@@ -765,7 +765,7 @@ def get_bgp_neighbors(self):
             "remote_as": 0,
             "remote_id": "",
             "is_up": False,
-            "is_enabled": False,
+            "is_enabled": True,
             "description": "",
             "uptime": 0,
             "address_family": {},
@@ -828,6 +828,11 @@ def _get_bgp_neighbors_core(
                     for key, value in neighbor_details.items()
                     if key in keys
                 }
+                bgp_opts = (
+                    neighbor_details.pop("bgp_options_extended", "").lower().split()
+                )
+                if "shutdown" in bgp_opts:
+                    peer["is_enabled"] = False
                 peer["local_as"] = napalm.base.helpers.as_number(peer["local_as"])
                 peer["remote_as"] = napalm.base.helpers.as_number(peer["remote_as"])
                 peer["address_family"] = self._parse_route_stats(


Test patch:
diff --git a/test/junos/mocked_data/test_get_bgp_neighbors/normal/expected_result.json b/test/junos/mocked_data/test_get_bgp_neighbors/normal/expected_result.json
--- a/test/junos/mocked_data/test_get_bgp_neighbors/normal/expected_result.json
+++ b/test/junos/mocked_data/test_get_bgp_neighbors/normal/expected_result.json
@@ -174,6 +174,27 @@
         "local_as": 20001,
         "is_enabled": true,
         "uptime": 696506
+      },
+      "192.169.1.2": {
+        "description": "",
+        "remote_id": "",
+        "address_family": {
+          "ipv6": {
+            "received_prefixes": -1,
+            "sent_prefixes": -1,
+            "accepted_prefixes": -1
+          },
+          "ipv4": {
+            "received_prefixes": -1,
+            "sent_prefixes": -1,
+            "accepted_prefixes": -1
+          }
+        },
+        "remote_as": 20000,
+        "is_up": false,
+        "local_as": 20001,
+        "is_enabled": false,
+        "uptime": 696506
       }
     }
   }
diff --git a/test/junos/mocked_data/test_get_bgp_neighbors/normal/get-bgp-neighbor-informationfrontend.xml b/test/junos/mocked_data/test_get_bgp_neighbors/normal/get-bgp-neighbor-informationfrontend.xml
--- a/test/junos/mocked_data/test_get_bgp_neighbors/normal/get-bgp-neighbor-informationfrontend.xml
+++ b/test/junos/mocked_data/test_get_bgp_neighbors/normal/get-bgp-neighbor-informationfrontend.xml
@@ -250,6 +250,28 @@
     </bgp-option-information>
     <flap-count>0</flap-count>
   </bgp-peer>
+  <bgp-peer style="detail">
+    <peer-address>192.169.1.2</peer-address>
+    <peer-as>20000</peer-as>
+    <local-address>192.169.1.3</local-address>
+    <local-as>20001</local-as>
+    <peer-type>External</peer-type>
+    <peer-state>Idle</peer-state>
+    <peer-flags/>
+    <last-state>Idle</last-state>
+    <last-event>Start</last-event>
+    <last-error>None</last-error>
+    <bgp-option-information>
+      <export-policy>NORMAL-FRONTEND</export-policy>
+      <bgp-options>Preference LocalAddress AddressFamily PeerAS Multipath Refresh</bgp-options>
+      <bgp-options2>VpnApplyExport PeerSpecficLoopsAllowed</bgp-options2>
+      <bgp-options-extended>GracefulShutdownRcv Shutdown</bgp-options-extended>
+      <address-families>inet-unicast inet6-unicast</address-families>
+      <holdtime>90</holdtime>
+      <preference>170</preference>
+    </bgp-option-information>
+    <flap-count>0</flap-count>
+  </bgp-peer>
   <bgp-peer style="detail">
     <peer-address>2a01:280:100::1</peer-address>
     <peer-as>20000</peer-as>
diff --git a/test/junos/mocked_data/test_get_bgp_neighbors/normal/get-bgp-summary-informationfrontend.xml b/test/junos/mocked_data/test_get_bgp_neighbors/normal/get-bgp-summary-informationfrontend.xml
--- a/test/junos/mocked_data/test_get_bgp_neighbors/normal/get-bgp-summary-informationfrontend.xml
+++ b/test/junos/mocked_data/test_get_bgp_neighbors/normal/get-bgp-summary-informationfrontend.xml
@@ -133,6 +133,16 @@
     <elapsed-time seconds="696506">1w1d1h</elapsed-time>
     <peer-state>Active</peer-state>
   </bgp-peer>
+  <bgp-peer style="terse">
+    <peer-address>192.169.1.2</peer-address>
+    <peer-as>20000</peer-as>
+    <input-messages>0</input-messages>
+    <output-messages>0</output-messages>
+    <route-queue-count>0</route-queue-count>
+    <flap-count>0</flap-count>
+    <elapsed-time seconds="696506">1w1d1h</elapsed-time>
+    <peer-state>Idle</peer-state>
+  </bgp-peer>
   <bgp-peer style="terse">
     <peer-address>2a01:280:100::1</peer-address>
     <peer-as>20000</peer-as>