.. versionadded:: 2019.2.0

    Return a dictionary structured as standardised in the
    `openconfig-interfaces <http://ops.openconfig.net/branches/master/openconfig-interfaces.html>`_
    YANG model, containing physical and configuration data available in Netbox,
    e.g., IP addresses, MTU, enabled / disabled, etc.

    device_name: ``None``
        The name of the device to query the interface data for. If not provided,
        will use the Minion ID.

    CLI Example:

    .. code-block:: bash

        salt '*' netbox.openconfig_interfaces
        salt '*' netbox.openconfig_interfaces device_name=cr1.thn.lon

def openconfig_interfaces(device_name=None):
    '''
    .. versionadded:: 2019.2.0

    Return a dictionary structured as standardised in the
    `openconfig-interfaces <http://ops.openconfig.net/branches/master/openconfig-interfaces.html>`_
    YANG model, containing physical and configuration data available in Netbox,
    e.g., IP addresses, MTU, enabled / disabled, etc.

    device_name: ``None``
        The name of the device to query the interface data for. If not provided,
        will use the Minion ID.

    CLI Example:

    .. code-block:: bash

        salt '*' netbox.openconfig_interfaces
        salt '*' netbox.openconfig_interfaces device_name=cr1.thn.lon
    '''
    oc_if = {}
    interfaces = get_interfaces(device_name=device_name)
    ipaddresses = get_ipaddresses(device_name=device_name)
    for interface in interfaces:
        if_name, if_unit = _if_name_unit(interface['name'])
        if if_name not in oc_if:
            oc_if[if_name] = {
                'config': {
                    'name': if_name
                },
                'subinterfaces': {'subinterface': {}}
             }
        if if_unit == '0':
            oc_if[if_name]['config']['enabled'] = interface['enabled']
            if interface['description']:
                if if_name == interface['name']:
                    # When that's a real unit 0 interface
                    # Otherwise it will inherit the description from the subif
                    oc_if[if_name]['config']['description'] = str(interface['description'])
                else:
                    subif_descr = {
                        'subinterfaces': {
                            'subinterface': {
                                if_unit: {
                                    'config': {
                                        'description': str(interface['description'])
                                    }
                                }
                            }
                        }
                    }
                    oc_if[if_name] = __utils__['dictupdate.update'](oc_if[if_name], subif_descr)
            if interface['mtu']:
                oc_if[if_name]['config']['mtu'] = int(interface['mtu'])
        else:
            oc_if[if_name]['subinterfaces']['subinterface'][if_unit] = {
                'config': {
                    'index': int(if_unit),
                    'enabled': interface['enabled']
                }
            }
            if interface['description']:
                oc_if[if_name]['subinterfaces']['subinterface'][if_unit]['config']['description'] =\
                    str(interface['description'])
    for ipaddress in ipaddresses:
        ip, prefix_length = ipaddress['address'].split('/')
        if_name = ipaddress['interface']['name']
        if_name, if_unit = _if_name_unit(if_name)
        ipvkey = 'ipv{}'.format(ipaddress['family'])
        if if_unit not in oc_if[if_name]['subinterfaces']['subinterface']:
            oc_if[if_name]['subinterfaces']['subinterface'][if_unit] = {
                'config': {
                    'index': int(if_unit),
                    'enabled': True
                }
            }
        if ipvkey not in oc_if[if_name]['subinterfaces']['subinterface'][if_unit]:
            oc_if[if_name]['subinterfaces']['subinterface'][if_unit][ipvkey] = {
                'addresses': {
                    'address': {}
                }
            }
        oc_if[if_name]['subinterfaces']['subinterface'][if_unit][ipvkey]['addresses']['address'][ip] = {
            'config': {
                'ip': ip,
                'prefix_length': int(prefix_length)
            }
        }
    return {
        'interfaces': {
            'interface': oc_if
        }
    }