# -*- coding: utf-8 -*-
'''
NAPALM Route
============

Retrieves route details from network devices.

:codeauthor: Mircea Ulinic <mircea@cloudflare.com>
:maturity:   new
:depends:    napalm
:platform:   unix

Dependencies
------------
- :mod:`NAPALM proxy minion <salt.proxy.napalm>`

.. versionadded:: 2016.11.0
'''

from __future__ import absolute_import

import logging
log = logging.getLogger(__file__)


try:
    # will try to import NAPALM
    # https://github.com/napalm-automation/napalm
    # pylint: disable=W0611
    from napalm_base import get_network_driver
    # pylint: enable=W0611
    HAS_NAPALM = True
except ImportError:
    HAS_NAPALM = False

# ----------------------------------------------------------------------------------------------------------------------
# module properties
# ----------------------------------------------------------------------------------------------------------------------

__virtualname__ = 'route'
__proxyenabled__ = ['napalm']
# uses NAPALM-based proxy to interact with network devices

# ----------------------------------------------------------------------------------------------------------------------
# property functions
# ----------------------------------------------------------------------------------------------------------------------


def __virtual__():

    '''
    NAPALM library must be installed for this module to work.
    Also, the key proxymodule must be set in the __opts___ dictionary.
    '''

    if HAS_NAPALM and 'proxy' in __opts__:
        return __virtualname__
    else:
        return (False, 'The module napalm_route cannot be loaded: \
                napalm or proxy could not be loaded.')

# ----------------------------------------------------------------------------------------------------------------------
# helper functions -- will not be exported
# ----------------------------------------------------------------------------------------------------------------------

# ----------------------------------------------------------------------------------------------------------------------
# callable functions
# ----------------------------------------------------------------------------------------------------------------------


def show(destination, protocol=None):

    '''
    Displays all details for a certain route learned via a specific protocol.

    :param destination: destination prefix.
    :param protocol: protocol used to learn the routes to the destination.

    CLI Example:

    .. code-block:: bash

        salt 'my_router' route.show 172.16.0.0/25 bgp

    Output example:

    .. code-block:: python

        {
            '172.16.0.0/25': [
                {
                    'protocol': 'BGP',
                    'last_active': True,
                    'current_active': True,
                    'age': 1178693,
                    'routing_table': 'inet.0',
                    'next_hop': '192.168.0.11',
                    'outgoing_interface': 'xe-1/1/1.100',
                    'preference': 170,
                    'selected_next_hop': False,
                    'protocol_attributes': {
                        'remote_as': 65001,
                        'metric': 5,
                        'local_as': 13335,
                        'as_path': '',
                        'remote_address': '192.168.0.11',
                        'metric2': 0,
                        'local_preference': 0,
                        'communities': [
                            '0:2',
                            'no-export'
                        ],
                        'preference2': -1
                    },
                    'inactive_reason': ''
                },
                {
                    'protocol': 'BGP',
                    'last_active': False,
                    'current_active': False,
                    'age': 2359429,
                    'routing_table': 'inet.0',
                    'next_hop': '192.168.0.17',
                    'outgoing_interface': 'xe-1/1/1.100',
                    'preference': 170,
                    'selected_next_hop': True,
                    'protocol_attributes': {
                        'remote_as': 65001,
                        'metric': 5,
                        'local_as': 13335,
                        'as_path': '',
                        'remote_address': '192.168.0.17',
                        'metric2': 0,
                        'local_preference': 0,
                        'communities': [
                            '0:3',
                            'no-export'
                        ],
                        'preference2': -1
                    },
                    'inactive_reason': 'Not Best in its group - Router ID'
                }
            ]
        }
    '''

    return __proxy__['napalm.call'](
        'get_route_to',
        **{
            'destination': destination,
            'protocol': protocol
        }
    )

@@ -405,152 +405,85 @@
 _)%0A%0A
-%0Atry:%0A    # will try to import NAPALM%0A    # https://github.com/napalm-automation/napalm%0A    # pylint: disable=W0611%0A    from 
+# import NAPALM utils%0Aimport salt.utils.napalm%0Afrom salt.utils.
 napalm
-_base
  import 
 get_
@@ -482,118 +482,25 @@
 ort 
-get_network_driver%0A    # pylint: enable=W0611%0A    HAS_NAPALM = True%0Aexcept ImportError:%0A    HAS_NAPALM = False
+proxy_napalm_wrap
 %0A%0A# 
@@ -1160,17 +1160,16 @@
 al__():%0A
-%0A
     '''%0A
@@ -1232,79 +1232,36 @@
 work
-.%0A    Also, the key proxymodule must be set in the __opts___ dictionary
+ and run in a (proxy) minion
 .%0A  
@@ -1270,214 +1270,80 @@
 '''%0A
-%0A
     
-if HAS_NAPALM and 'proxy' in __opts__:%0A        return __virtualname__%0A    else:%0A        return (False, 'The module napalm_route cannot be loaded: %5C%0A                napalm or proxy could not be loaded.'
+return salt.utils.napalm.virtual(__opts__, __virtualname__, __file__
 )%0A%0A#
@@ -1888,24 +1888,43 @@
 ---------%0A%0A%0A
+@proxy_napalm_wrap%0A
 def show(des
@@ -1946,18 +1946,63 @@
 col=None
-):
+, **kwargs):  # pylint: disable=unused-argument
 %0A%0A    ''
@@ -2081,27 +2081,433 @@
 otocol.%0A
-%0A    :param
+    If the protocol is not specified, will return all possible routes.%0A%0A    .. note::%0A%0A        This function return the routes from the RIB.%0A        In case the destination prefix is too short,%0A        there may be too many routes matched.%0A        Therefore in cases of devices having a very high number of routes%0A        it may be necessary to adjust the prefix lenght and request%0A        using a longer prefix.%0A%0A   
  destina
@@ -2510,17 +2510,24 @@
 tination
-:
+%0A       
  destina
@@ -2543,27 +2543,21 @@
 ix.%0A
+%0A
     
-:param 
 protocol
 : pr
@@ -2552,17 +2552,35 @@
 protocol
-:
+ (optional)%0A       
  protoco
@@ -2627,16 +2627,50 @@
 ation.%0A%0A
+    .. versionchanged:: Nitrogen%0A%0A
     CLI 
@@ -2705,16 +2705,52 @@
 : bash%0A%0A
+        salt 'my_router' route.show%0A
         
@@ -5043,33 +5043,92 @@
 urn 
-__proxy__%5B'napalm.call'%5D(
+salt.utils.napalm.call(%0A        napalm_device,  # pylint: disable=undefined-variable
 %0A   

Migrate napalm_route