#!/usr/bin/env python3
# author: @netmanchris



# This section imports required libraries
import requests
import json
from pyhpeimc.auth import IMCAuth

HEADERS = {'Accept': 'application/json', 'Content-Type':
    'application/json', 'Accept-encoding': 'application/json'}

#auth = IMCAuth('http://','10.101.0.201','8080', 'admin','admin')

headers = {'Accept': 'application/json', 'Content-Type':
    'application/json', 'Accept-encoding': 'application/json'}

def get_dev_alarms(devId, auth, url):
    """
    function takes the devId of a specific device and issues a RESTFUL call to get the current alarms for the target
    device.

    :param devId: int or str value of the target device

    :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class

    :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass

    :return:list of dictionaries containing the alarms for this device

    :rtype: list

    >>> from pyhpeimc.auth import *

    >>> from pyhpeimc.plat.alarms import *

    >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin")

    >>> get_dev_alarms('10', auth.creds.auth.url)
    [{'OID': '1.3.6.1.6.3.1.1.5.3.0',
  'ackStatus': '1',
  'ackStatusDesc': 'Acknowledged',
  'ackTime': '1467854226',
  'ackTimeDesc': '2016-07-06 21:17:06',
  'ackUserName': 'admin',
  'alarmCategory': '3',
  'alarmCategoryDesc': 'Interface/Link Status Alarm',
  'alarmDesc': 'The interface GigabitEthernet2/0/23 is UP.',
  'alarmDetail': 'http://kontrolissues.thruhere.net:8086/imcrs/fault/alarm/160612',
  'alarmLevel': '5',
  'alarmLevelDesc': 'Info',
  'deviceId': '10',
  'deviceIp': '192.168.1.221',
  'deviceName': 'HP_5500EI',
  'faultTime': '1467765645',
  'faultTimeDesc': '2016-07-05 20:40:45',
  'holdInfo': '',
  'id': '160612',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Interface Index=85;Interface Description=GigabitEthernet2/0/23;Interface Admin Status=1;Interface Operate Status=1',
  'parentId': '0',
  'recStatus': '1',
  'recStatusDesc': 'Recovered',
  'recTime': '1467765645',
  'recTimeDesc': '2016-07-05 20:40:45',
  'recUserName': '$SYSTEM',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.6.3.1.1.5.2.0',
  'ackStatus': '1',
  'ackStatusDesc': 'Acknowledged',
  'ackTime': '1467854226',
  'ackTimeDesc': '2016-07-06 21:17:06',
  'ackUserName': 'admin',
  'alarmCategory': '3',
  'alarmCategoryDesc': 'Interface/Link Status Alarm',
  'alarmDesc': 'The interface GigabitEthernet2/0/23 is DOWN.',
  'alarmDetail': 'http://kontrolissues.thruhere.net:8086/imcrs/fault/alarm/160539',
  'alarmLevel': '2',
  'alarmLevelDesc': 'Major',
  'deviceId': '10',
  'deviceIp': '192.168.1.221',
  'deviceName': 'HP_5500EI',
  'faultTime': '1467747889',
  'faultTimeDesc': '2016-07-05 15:44:49',
  'holdInfo': '',
  'id': '160539',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Interface Index=85;Interface Description=GigabitEthernet2/0/23;Interface Admin Status=1;Interface Operate Status=2',
  'parentId': '0',
  'recStatus': '1',
  'recStatusDesc': 'Recovered',
  'recTime': '1467765645',
  'recTimeDesc': '2016-07-05 20:40:45',
  'recUserName': '$SYSTEM',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.6.3.1.1.5.2.0',
  'ackStatus': '1',
  'ackStatusDesc': 'Acknowledged',
  'ackTime': '1467854226',
  'ackTimeDesc': '2016-07-06 21:17:06',
  'ackUserName': 'admin',
  'alarmCategory': '3',
  'alarmCategoryDesc': 'Interface/Link Status Alarm',
  'alarmDesc': 'The interface GigabitEthernet2/0/20 is DOWN.',
  'alarmDetail': 'http://kontrolissues.thruhere.net:8086/imcrs/fault/alarm/160524',
  'alarmLevel': '2',
  'alarmLevelDesc': 'Major',
  'deviceId': '10',
  'deviceIp': '192.168.1.221',
  'deviceName': 'HP_5500EI',
  'faultTime': '1467747461',
  'faultTimeDesc': '2016-07-05 15:37:41',
  'holdInfo': '',
  'id': '160524',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Interface Index=82;Interface Description=GigabitEthernet2/0/20;Interface Admin Status=1;Interface Operate Status=2',
  'parentId': '0',
  'recStatus': '1',
  'recStatusDesc': 'Recovered',
  'recTime': '1467854218',
  'recTimeDesc': '2016-07-06 21:16:58',
  'recUserName': 'admin',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.6.3.1.1.5.2.0',
  'ackStatus': '1',
  'ackStatusDesc': 'Acknowledged',
  'ackTime': '1467854226',
  'ackTimeDesc': '2016-07-06 21:17:06',
  'ackUserName': 'admin',
  'alarmCategory': '3',
  'alarmCategoryDesc': 'Interface/Link Status Alarm',
  'alarmDesc': 'The interface GigabitEthernet1/0/21 is DOWN.',
  'alarmDetail': 'http://kontrolissues.thruhere.net:8086/imcrs/fault/alarm/160496',
  'alarmLevel': '2',
  'alarmLevelDesc': 'Major',
  'deviceId': '10',
  'deviceIp': '192.168.1.221',
  'deviceName': 'HP_5500EI',
  'faultTime': '1467745458',
  'faultTimeDesc': '2016-07-05 15:04:18',
  'holdInfo': '',
  'id': '160496',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Interface Index=21;Interface Description=GigabitEthernet1/0/21;Interface Admin Status=1;Interface Operate Status=2',
  'parentId': '0',
  'recStatus': '1',
  'recStatusDesc': 'Recovered',
  'recTime': '1467854218',
  'recTimeDesc': '2016-07-06 21:16:58',
  'recUserName': 'admin',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.6.3.1.1.5.3.0',
  'ackStatus': '1',
  'ackStatusDesc': 'Acknowledged',
  'ackTime': '1467854226',
  'ackTimeDesc': '2016-07-06 21:17:06',
  'ackUserName': 'admin',
  'alarmCategory': '3',
  'alarmCategoryDesc': 'Interface/Link Status Alarm',
  'alarmDesc': 'The interface GigabitEthernet2/0/20 is UP.',
  'alarmDetail': 'http://kontrolissues.thruhere.net:8086/imcrs/fault/alarm/160487',
  'alarmLevel': '5',
  'alarmLevelDesc': 'Info',
  'deviceId': '10',
  'deviceIp': '192.168.1.221',
  'deviceName': 'HP_5500EI',
  'faultTime': '1467742807',
  'faultTimeDesc': '2016-07-05 14:20:07',
  'holdInfo': '',
  'id': '160487',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Interface Index=82;Interface Description=GigabitEthernet2/0/20;Interface Admin Status=1;Interface Operate Status=1',
  'parentId': '0',
  'recStatus': '1',
  'recStatusDesc': 'Recovered',
  'recTime': '1467742807',
  'recTimeDesc': '2016-07-05 14:20:07',
  'recUserName': '$SYSTEM',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.6.3.1.1.5.2.0',
  'ackStatus': '1',
  'ackStatusDesc': 'Acknowledged',
  'ackTime': '1467854226',
  'ackTimeDesc': '2016-07-06 21:17:06',
  'ackUserName': 'admin',
  'alarmCategory': '3',
  'alarmCategoryDesc': 'Interface/Link Status Alarm',
  'alarmDesc': 'The interface GigabitEthernet2/0/19 is DOWN.',
  'alarmDetail': 'http://kontrolissues.thruhere.net:8086/imcrs/fault/alarm/160485',
  'alarmLevel': '2',
  'alarmLevelDesc': 'Major',
  'deviceId': '10',
  'deviceIp': '192.168.1.221',
  'deviceName': 'HP_5500EI',
  'faultTime': '1467742775',
  'faultTimeDesc': '2016-07-05 14:19:35',
  'holdInfo': '',
  'id': '160485',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Interface Index=81;Interface Description=GigabitEthernet2/0/19;Interface Admin Status=1;Interface Operate Status=2',
  'parentId': '0',
  'recStatus': '1',
  'recStatusDesc': 'Recovered',
  'recTime': '1467854218',
  'recTimeDesc': '2016-07-06 21:16:58',
  'recUserName': 'admin',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.6.3.1.1.5.3.0',
  'ackStatus': '1',
  'ackStatusDesc': 'Acknowledged',
  'ackTime': '1467854226',
  'ackTimeDesc': '2016-07-06 21:17:06',
  'ackUserName': 'admin',
  'alarmCategory': '3',
  'alarmCategoryDesc': 'Interface/Link Status Alarm',
  'alarmDesc': 'The interface GigabitEthernet2/0/19 is UP.',
  'alarmDetail': 'http://kontrolissues.thruhere.net:8086/imcrs/fault/alarm/160484',
  'alarmLevel': '5',
  'alarmLevelDesc': 'Info',
  'deviceId': '10',
  'deviceIp': '192.168.1.221',
  'deviceName': 'HP_5500EI',
  'faultTime': '1467742742',
  'faultTimeDesc': '2016-07-05 14:19:02',
  'holdInfo': '',
  'id': '160484',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Interface Index=81;Interface Description=GigabitEthernet2/0/19;Interface Admin Status=1;Interface Operate Status=1',
  'parentId': '0',
  'recStatus': '1',
  'recStatusDesc': 'Recovered',
  'recTime': '1467742742',
  'recTimeDesc': '2016-07-05 14:19:02',
  'recUserName': '$SYSTEM',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.6.3.1.1.5.3.0',
  'ackStatus': '1',
  'ackStatusDesc': 'Acknowledged',
  'ackTime': '1467854226',
  'ackTimeDesc': '2016-07-06 21:17:06',
  'ackUserName': 'admin',
  'alarmCategory': '3',
  'alarmCategoryDesc': 'Interface/Link Status Alarm',
  'alarmDesc': 'The interface GigabitEthernet1/0/21 is UP.',
  'alarmDetail': 'http://kontrolissues.thruhere.net:8086/imcrs/fault/alarm/160447',
  'alarmLevel': '5',
  'alarmLevelDesc': 'Info',
  'deviceId': '10',
  'deviceIp': '192.168.1.221',
  'deviceName': 'HP_5500EI',
  'faultTime': '1467736157',
  'faultTimeDesc': '2016-07-05 12:29:17',
  'holdInfo': '',
  'id': '160447',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Interface Index=21;Interface Description=GigabitEthernet1/0/21;Interface Admin Status=1;Interface Operate Status=1',
  'parentId': '0',
  'recStatus': '1',
  'recStatusDesc': 'Recovered',
  'recTime': '1467736157',
  'recTimeDesc': '2016-07-05 12:29:17',
  'recUserName': '$SYSTEM',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.6.3.1.1.5.2.0',
  'ackStatus': '1',
  'ackStatusDesc': 'Acknowledged',
  'ackTime': '1467854226',
  'ackTimeDesc': '2016-07-06 21:17:06',
  'ackUserName': 'admin',
  'alarmCategory': '3',
  'alarmCategoryDesc': 'Interface/Link Status Alarm',
  'alarmDesc': 'The interface GigabitEthernet1/0/21 is DOWN.',
  'alarmDetail': 'http://kontrolissues.thruhere.net:8086/imcrs/fault/alarm/160446',
  'alarmLevel': '2',
  'alarmLevelDesc': 'Major',
  'deviceId': '10',
  'deviceIp': '192.168.1.221',
  'deviceName': 'HP_5500EI',
  'faultTime': '1467736125',
  'faultTimeDesc': '2016-07-05 12:28:45',
  'holdInfo': '',
  'id': '160446',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Interface Index=21;Interface Description=GigabitEthernet1/0/21;Interface Admin Status=1;Interface Operate Status=2',
  'parentId': '160438',
  'recStatus': '1',
  'recStatusDesc': 'Recovered',
  'recTime': '1467736157',
  'recTimeDesc': '2016-07-05 12:29:17',
  'recUserName': '$SYSTEM',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.6.3.1.1.5.3.0',
  'ackStatus': '1',
  'ackStatusDesc': 'Acknowledged',
  'ackTime': '1467854226',
  'ackTimeDesc': '2016-07-06 21:17:06',
  'ackUserName': 'admin',
  'alarmCategory': '3',
  'alarmCategoryDesc': 'Interface/Link Status Alarm',
  'alarmDesc': 'The interface GigabitEthernet1/0/21 is UP.',
  'alarmDetail': 'http://kontrolissues.thruhere.net:8086/imcrs/fault/alarm/160444',
  'alarmLevel': '5',
  'alarmLevelDesc': 'Info',
  'deviceId': '10',
  'deviceIp': '192.168.1.221',
  'deviceName': 'HP_5500EI',
  'faultTime': '1467736092',
  'faultTimeDesc': '2016-07-05 12:28:12',
  'holdInfo': '',
  'id': '160444',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Interface Index=21;Interface Description=GigabitEthernet1/0/21;Interface Admin Status=1;Interface Operate Status=1',
  'parentId': '0',
  'recStatus': '1',
  'recStatusDesc': 'Recovered',
  'recTime': '1467736092',
  'recTimeDesc': '2016-07-05 12:28:12',
  'recUserName': '$SYSTEM',
  'remark': '',
  'somState': '0'}]
    """
    # checks to see if the imc credentials are already available
    get_dev_alarm_url = "/imcrs/fault/alarm?operatorName=admin&deviceId=" + \
                        str(devId) + "&desc=false"
    f_url = url + get_dev_alarm_url
    # creates the URL using the payload variable as the contents
    r = requests.get(f_url, auth=auth, headers=headers)
    try:
        if r.status_code == 200:
            dev_alarm = (json.loads(r.text))
            if 'alarm' in dev_alarm:
                return dev_alarm['alarm']
            else:
                return "Device has no alarms"
    except requests.exceptions.RequestException as e:
            return "Error:\n" + str(e) + ' get_dev_alarms: An Error has occured'
