# -*- coding: utf-8 -*-
"""
web2ldap plugin classes for attributes defined for DHCP service

See http://tools.ietf.org/draft/draft-ietf-dhc-ldap-schema/
"""

import re
from w2lapp.schema.syntaxes import IA5String,SelectList,IntegerRange,BitArrayInteger,IPHostAddress,MacAddress,DynamicDNSelectList,BitArrayInteger,DNSDomain,syntax_registry


class DHCPServerDN(DynamicDNSelectList):
  oid = 'DHCPServerDN-oid'
  desc = 'DN of DHCP server entry'
  ldap_url = 'ldap:///_?cn?sub?(objectClass=dhcpServer)'

syntax_registry.registerAttrType(
  DHCPServerDN.oid,[
    '2.16.840.1.113719.1.203.4.1', # dhcpPrimaryDN
    '2.16.840.1.113719.1.203.4.2', # dhcpSecondaryDN
  ]
)


class DHCPOptionsDN(DynamicDNSelectList):
  oid = 'DHCPOptionsDN-oid'
  desc = 'DN of DHCP option object'
  ldap_url = 'ldap:///_?cn?sub?(objectClass=dhcpOptions)'

syntax_registry.registerAttrType(
  DHCPOptionsDN.oid,[
    '2.16.840.1.113719.1.203.4.9', # dhcpOptionsDN
  ]
)


class DHCPHostDN(DynamicDNSelectList):
  oid = 'DHCPHostDN-oid'
  desc = 'DN of DHCP host object'
  ldap_url = 'ldap:///_?cn?sub?(objectClass=dhcpHost)'

syntax_registry.registerAttrType(
  DHCPHostDN.oid,[
    '2.16.840.1.113719.1.203.4.10', # dhcpHostDN
    '2.16.840.1.113719.1.203.4.31', # dhcpReservedForClient
    '2.16.840.1.113719.1.203.4.32', # dhcpAssignedToClient
  ]
)


class DHCPPoolDN(DynamicDNSelectList):
  oid = 'DHCPPoolDN-oid'
  desc = 'DN of DHCP pool object'
  ldap_url = 'ldap:///_??sub?(objectClass=dhcpPool)'

syntax_registry.registerAttrType(
  DHCPPoolDN.oid,[
    '2.16.840.1.113719.1.203.4.11', # dhcpPoolDN
  ]
)


class DHCPGroupDN(DynamicDNSelectList):
  oid = 'DHCPGroupDN-oid'
  desc = 'DN of DHCP group object'
  ldap_url = 'ldap:///_?cn?sub?(objectClass=dhcpGroup)'

syntax_registry.registerAttrType(
  DHCPGroupDN.oid,[
    '2.16.840.1.113719.1.203.4.12', # dhcpGroupDN
  ]
)


class DHCPSubnetDN(DynamicDNSelectList):
  oid = 'DHCPSubnetDN-oid'
  desc = 'DN of DHCP subnet object'
  ldap_url = 'ldap:///_?cn?sub?(objectClass=dhcpSubnet)'

syntax_registry.registerAttrType(
  DHCPSubnetDN.oid,[
    '2.16.840.1.113719.1.203.4.13', # dhcpSubnetDN
  ]
)


class DHCPLeasesDN(DynamicDNSelectList):
  oid = 'DHCPLeasesDN-oid'
  desc = 'DN of DHCP leases object'
  ldap_url = 'ldap:///_?cn?sub?(objectClass=dhcpLeases)'

syntax_registry.registerAttrType(
  DHCPLeasesDN.oid,[
    '2.16.840.1.113719.1.203.4.14', # dhcpLeaseDN
    '2.16.840.1.113719.1.203.4.15', # dhcpLeasesDN
  ]
)


class DHCPClassesDN(DynamicDNSelectList):
  oid = 'DHCPClassesDN-oid'
  desc = 'DN of DHCP classes object'
  ldap_url = 'ldap:///_?cn?sub?(objectClass=dhcpClass)'

syntax_registry.registerAttrType(
  DHCPClassesDN.oid,[
    '2.16.840.1.113719.1.203.4.16', # dhcpClassesDN
  ]
)


class DHCPSubclassesDN(DynamicDNSelectList):
  oid = 'DHCPSubclassesDN-oid'
  desc = 'DN of DHCP Subclasses object'
  ldap_url = 'ldap:///_?cn?sub?(objectClass=dhcpSubclass)'

syntax_registry.registerAttrType(
  DHCPSubclassesDN.oid,[
    '2.16.840.1.113719.1.203.4.17', # dhcpSubclassesDN
  ]
)


class DHCPSharedNetworkDN(DynamicDNSelectList):
  oid = 'DHCPSharedNetworkDN-oid'
  desc = 'DN of DHCP shared network object'
  ldap_url = 'ldap:///_?cn?sub?(objectClass=dhcpSharedNetwork)'

syntax_registry.registerAttrType(
  DHCPSharedNetworkDN.oid,[
    '2.16.840.1.113719.1.203.4.18', # dhcpSharedNetworkDN
  ]
)


class DHCPServiceDN(DynamicDNSelectList):
  oid = 'DHCPServiceDN-oid'
  desc = 'DN of DHCP service object'
  ldap_url = 'ldap:///_?cn?sub?(objectClass=dhcpService)'

syntax_registry.registerAttrType(
  DHCPServiceDN.oid,[
    '2.16.840.1.113719.1.203.4.19', # dhcpServiceDN
  ]
)


class DHCPHWAddress(MacAddress):
  oid = 'DHCPHWAddress-oid'
  desc = 'Network classifier and MAC address'
  minLen = 26
  maxLen = 26
  reObj=re.compile(r'^ethernet ([0-9a-f]{2}\:){5}[0-9a-f]{2}$')

syntax_registry.registerAttrType(
  DHCPHWAddress.oid,[
    '2.16.840.1.113719.1.203.4.34', # dhcpHWAddress
  ]
)


class DHCPNetMask(IntegerRange):
  oid = 'DHCPNetMask-oid'
  desc = 'Network address mask bits'
  maxValue = 0
  maxValue = 32

syntax_registry.registerAttrType(
  DHCPNetMask.oid,[
    '2.16.840.1.113719.1.203.4.6', # dhcpNetMask
  ]
)


class DHCPRange(IPHostAddress):
  oid = 'DHCPRange-oid'
  desc = 'Network address range'

  def _validate(self,attrValue):
    try:
      l,h = attrValue.split(' ')
    except ValueError,e:
      return False
    else:
      return IPHostAddress._validate(self,l) and IPHostAddress._validate(self,h)

syntax_registry.registerAttrType(
  DHCPRange.oid,[
    '2.16.840.1.113719.1.203.4.4', # dhcpRange
  ]
)


class DHCPAddressState(SelectList):
  oid = 'DHCPAddressState-oid'
  desc = 'DHCP address state'

  attr_value_dict = {
    u'':u'',
    u'FREE':u'FREE',
    u'ACTIVE':u'ACTIVE',
    u'EXPIRED':u'EXPIRED',
    u'RELEASED':u'RELEASED',
    u'RESET':u'RESET',
    u'ABANDONED':u'ABANDONED',
    u'BACKUP':u'BACKUP',
    u'UNKNOWN':u'UNKNOWN',
    u'RESERVED':u'RESERVED (an address that is managed by DHCP that is reserved for a specific client)',
    u'RESERVED-ACTIVE':u'RESERVED-ACTIVE (same as reserved, but address is currently in use)',
    u'ASSIGNED':u'ASSIGNED (assigned manually or by some other mechanism)',
    u'UNASSIGNED':u'UNASSIGNED',
    u'NOTASSIGNABLE':u'NOTASSIGNABLE',
  }


class DHCPDnsStatus(BitArrayInteger):
  """
   0 (C): name to address (such as A RR) update successfully completed
   1 (A): Server is controlling A RR on behalf of the client
   2 (D): address to name (such as PTR RR) update successfully completed (Done)
   3 (P): Server is controlling PTR RR on behalf of the client
   4-15 : Must be zero
  """
  oid = 'DHCPDnsStatus-oid'
  flag_desc_table = (
    ('(C): name to address (such as A RR) update successfully completed',0x0001),
    ('(A): Server is controlling A RR on behalf of the client',0x0002),
    ('(D): address to name (such as PTR RR) update successfully completed (Done)',0x0004),
    ('(P): Server is controlling PTR RR on behalf of the client',0x0008),
  )

syntax_registry.registerAttrType(
  DHCPDnsStatus.oid,[
    '2.16.840.1.113719.1.203.4.28', # dhcpDnsStatus
  ]
)


syntax_registry.registerAttrType(
  DNSDomain.oid,[
    '2.16.840.1.113719.1.203.4.27', # dhcpDomainName
  ]
)


syntax_registry.registerAttrType(
  DHCPAddressState.oid,[
    '2.16.840.1.113719.1.203.4.22', # dhcpAddressState
  ]
)


# Register all syntax classes in this module
for symbol_name in dir():
  syntax_registry.registerSyntaxClass(eval(symbol_name))
