# Path: netdevice/models.py
# class router(models.Model):
#     """
#     Router - A network device that moves packets around.
#     routing_id:         An IP address the router will use as identification and routing decisions.    [203.0.113.1]
#     hostname:           The (DNS) hostname of the router, domain included.                            [Router1.isp]
#     ibgp:               Boolean value to build an iBGP mesh with every router in the database.        [True]
#     network_os:         The network operating system and subsequent template this router is tied too.
#     service_ssh:        Boolean value to enable SSHv2 on the router.                                  [True]
#     service_netconf:    Boolean value to enable NETCONF on the router.                                [True]
#     local_aut_num:      Local autonomous system number for the router.
#     Example string: Router1
#     """
# 
#     routing_id         = models.GenericIPAddressField(unpack_ipv4=True)
#     hostname           = models.CharField(max_length=255)
#     ibgp               = models.BooleanField()
#     network_os         = models.ForeignKey('network_os', on_delete=models.CASCADE)
#     service_ssh        = models.BooleanField(default=True)
#     service_netconf    = models.BooleanField(default=True)
#     local_aut_num      = models.ForeignKey('bgp.aut_num', on_delete=models.CASCADE)
# 
#     def ldp_exists(self):
#         return self.interface_set.filter(logical_interface__ldp=True).exists()
# 
#     def __str__(self):
#         return self.hostname
# 
# Path: bgp/models.py
# class aut_num(models.Model):
#     """
#     Autonomous System Number - Used in BGPv4 to represent an
#                                administrative domain where internet prefixes
#                                are exchanged.
# 
#     asn:        The actual autonomous system number of someone we neighbor.
#                 [65000]
#     name:       A specific company or personal name for the peer.
#                 [123 ISP Inc.]
#     contact:    An email address to go along with the name.
#                 [john.smith@example.com]
# 
#     Example string: 65000: 123 ISP Inc. (john.smith@example.com)
#     """
# 
#     asn        = models.BigIntegerField(unique=False)
#     name       = models.CharField(max_length=255)
#     contact    = models.EmailField(blank=True)
#     vrf        = models.ForeignKey(
#                                    'netdevice.vrf',
#                                    on_delete=models.CASCADE,
#                                    null=True,
#                                    blank=True
#                                   )
#     class Meta:
#         unique_together = ("asn", "vrf")
# 
#     def __str__(self):
#         return str(self.asn) + ": " + self.name + " (" + self.contact + ")"
# 
# class neighbor(models.Model):
#     """
#     BGP Neighbor - Peering sesssions with our neighbors over BGP.
# 
#     router:          Router that the peer session is tied to.
#     aut_num:         ASN that the peer session is tied to.
#     peer_ip:         IP Address of BGP neighbor.
#                      [203.0.113.1]
#     soft_inbound:    Boolean value for if soft-reconfiguration inbound is
#                      enabled on the peer session.
#                      [True]
# 
#     Example string: PeeringBuddy - Router1 203.0.113.1
#     """
# 
#     router        = models.ForeignKey(
#                                       'netdevice.router',
#                                       on_delete=models.CASCADE
#                                      )
#     vrf           = models.ForeignKey(
#                                       'netdevice.vrf',
#                                       on_delete=models.CASCADE,
#                                       null=True,
#                                       blank=True
#                                      )
#     aut_num       = models.ForeignKey('aut_num', on_delete=models.CASCADE)
#     peer_ip       = models.GenericIPAddressField(unpack_ipv4=True)
#     soft_inbound  = models.BooleanField(default=True)
# 
#     def __str__(self):
#         asn_router = self.aut_num.name + " - " + self.router.hostname + " "
#         return asn_router + self.peer_ip
# 
# Path: bgp/forms.py
# class ASNForm(forms.ModelForm):
# 
#     class Meta:
#         model  = aut_num
#         fields = ('__all__')
# 
# class NeighborForm(forms.ModelForm):
# 
#     class Meta:
#         model  = neighbor
#         fields = ('__all__')


from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import get_object_or_404, render, redirect
from netdevice.models import router
from bgp.models import aut_num, neighbor
from bgp.forms import ASNForm, NeighborForm
# file: bgp/views.py




def asn_list(request):
    asns      = aut_num.objects.order_by('asn')
    asn_count = asns.count()
    paginator = Paginator(asns, 20)
    page      = request.GET.get('page')

    try:
        asn_list = paginator.page(page)
    except PageNotAnInteger:
        asn_list = paginator.page(1)
    except EmptyPage:
        asn_list = paginator.page(paginator.num_pages)

    context = {
              'asn_list':  asn_list,
              'asn_count': asn_count,
              }

    return render(request, 'bgp/asn_list.html', context)

def aut_num_detail(request, aut_num_id):

# Path: netdevice/models.py
# class router(models.Model):
#     """
#     Router - A network device that moves packets around.
#     routing_id:         An IP address the router will use as identification and routing decisions.    [203.0.113.1]
#     hostname:           The (DNS) hostname of the router, domain included.                            [Router1.isp]
#     ibgp:               Boolean value to build an iBGP mesh with every router in the database.        [True]
#     network_os:         The network operating system and subsequent template this router is tied too.
#     service_ssh:        Boolean value to enable SSHv2 on the router.                                  [True]
#     service_netconf:    Boolean value to enable NETCONF on the router.                                [True]
#     local_aut_num:      Local autonomous system number for the router.
#     Example string: Router1
#     """
# 
#     routing_id         = models.GenericIPAddressField(unpack_ipv4=True)
#     hostname           = models.CharField(max_length=255)
#     ibgp               = models.BooleanField()
#     network_os         = models.ForeignKey('network_os', on_delete=models.CASCADE)
#     service_ssh        = models.BooleanField(default=True)
#     service_netconf    = models.BooleanField(default=True)
#     local_aut_num      = models.ForeignKey('bgp.aut_num', on_delete=models.CASCADE)
# 
#     def ldp_exists(self):
#         return self.interface_set.filter(logical_interface__ldp=True).exists()
# 
#     def __str__(self):
#         return self.hostname
# 
# Path: bgp/models.py
# class aut_num(models.Model):
#     """
#     Autonomous System Number - Used in BGPv4 to represent an
#                                administrative domain where internet prefixes
#                                are exchanged.
# 
#     asn:        The actual autonomous system number of someone we neighbor.
#                 [65000]
#     name:       A specific company or personal name for the peer.
#                 [123 ISP Inc.]
#     contact:    An email address to go along with the name.
#                 [john.smith@example.com]
# 
#     Example string: 65000: 123 ISP Inc. (john.smith@example.com)
#     """
# 
#     asn        = models.BigIntegerField(unique=False)
#     name       = models.CharField(max_length=255)
#     contact    = models.EmailField(blank=True)
#     vrf        = models.ForeignKey(
#                                    'netdevice.vrf',
#                                    on_delete=models.CASCADE,
#                                    null=True,
#                                    blank=True
#                                   )
#     class Meta:
#         unique_together = ("asn", "vrf")
# 
#     def __str__(self):
#         return str(self.asn) + ": " + self.name + " (" + self.contact + ")"
# 
# class neighbor(models.Model):
#     """
#     BGP Neighbor - Peering sesssions with our neighbors over BGP.
# 
#     router:          Router that the peer session is tied to.
#     aut_num:         ASN that the peer session is tied to.
#     peer_ip:         IP Address of BGP neighbor.
#                      [203.0.113.1]
#     soft_inbound:    Boolean value for if soft-reconfiguration inbound is
#                      enabled on the peer session.
#                      [True]
# 
#     Example string: PeeringBuddy - Router1 203.0.113.1
#     """
# 
#     router        = models.ForeignKey(
#                                       'netdevice.router',
#                                       on_delete=models.CASCADE
#                                      )
#     vrf           = models.ForeignKey(
#                                       'netdevice.vrf',
#                                       on_delete=models.CASCADE,
#                                       null=True,
#                                       blank=True
#                                      )
#     aut_num       = models.ForeignKey('aut_num', on_delete=models.CASCADE)
#     peer_ip       = models.GenericIPAddressField(unpack_ipv4=True)
#     soft_inbound  = models.BooleanField(default=True)
# 
#     def __str__(self):
#         asn_router = self.aut_num.name + " - " + self.router.hostname + " "
#         return asn_router + self.peer_ip
# 
# Path: bgp/forms.py
# class ASNForm(forms.ModelForm):
# 
#     class Meta:
#         model  = aut_num
#         fields = ('__all__')
# 
# class NeighborForm(forms.ModelForm):
# 
#     class Meta:
#         model  = neighbor
#         fields = ('__all__')


# Path: bgp/views.py
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import get_object_or_404, render, redirect
from netdevice.models import router
from bgp.models import aut_num, neighbor
from bgp.forms import ASNForm, NeighborForm

# file: bgp/views.py




def asn_list(request):
    asns      = aut_num.objects.order_by('asn')
    asn_count = asns.count()
    paginator = Paginator(asns, 20)
    page      = request.GET.get('page')

    try:
        asn_list = paginator.page(page)
    except PageNotAnInteger:
        asn_list = paginator.page(1)
    except EmptyPage:
        asn_list = paginator.page(paginator.num_pages)

    context = {
              'asn_list':  asn_list,
              'asn_count': asn_count,
              }

    return render(request, 'bgp/asn_list.html', context)

def aut_num_detail(request, aut_num_id):

    aut_num_obj    = get_object_or_404(aut_num, pk=aut_num_id)