# 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: static/models.py
# class ipv6_static(models.Model):
#     """
#     IPv6 Static Routes - Routes set by the user in a given router.
# 
#     router:       The router to place this static route.
#     network:      The network address of the route.                                    [2001:db8::]
#     cidr:         The CIDR length of this static route.                                [64]
#     next-hop:     Next-hop device of this static route.                                [2001:db8:1:1::1]
# 
#     Example string: Router1 2001:db8:: next-hop 2001:db8:1:1::1
#     """
# 
#     router      = models.ForeignKey('netdevice.router', on_delete=models.CASCADE, null=True, blank=True)
#     vrf         = models.ForeignKey(
#                                     'netdevice.vrf',
#                                     on_delete=models.CASCADE,
#                                     null=True,
#                                     blank=True
#                                    )
#     network     = models.GenericIPAddressField(unpack_ipv4=False)
#     cidr        = models.PositiveSmallIntegerField(default=64)
#     next_hop    = models.GenericIPAddressField(unpack_ipv4=True)
# 
#     def __str__(self):
#         return self.router.hostname + " " + str(self.network) + "/" + str(self.cidr) + " next-hop " + str(self.next_hop)
# 
# class ipv4_static(models.Model):
#     """
#     IPv4 Static Routes - Routes set by the user in a given router
# 
#     router:       The router to place this static route.
#     network:      The network address of the route.                                    [198.51.100.0]
#     cidr:         The CIDR length of this static route.                                [24]
#     next-hop:     Next-hop device of this static route.                                [198.51.100.1]
# 
#     Example string: Router1 198.51.100.0/24 next-hop 198.51.100.1
#     """
# 
#     router      = models.ForeignKey('netdevice.router', on_delete=models.CASCADE, null=True, blank=True)
#     vrf         = models.ForeignKey(
#                                     'netdevice.vrf',
#                                     on_delete=models.CASCADE,
#                                     null=True,
#                                     blank=True
#                                    )
#     network     = models.GenericIPAddressField(unpack_ipv4=True)
#     cidr        = models.PositiveSmallIntegerField(default=24)
#     next_hop    = models.GenericIPAddressField(unpack_ipv4=True)
# 
#     def subnet_mask(self):
#         host_bits = 32 - int(self.cidr)
#         netmask = socket.inet_ntoa(struct.pack('!I', (1 << 32) - (1 << host_bits)))
#         return netmask
# 
#     def __str__(self):
#         return self.router.hostname + " " + str(self.network) + "/" + str(self.cidr) + " next-hop " + str(self.next_hop)
# 
# Path: static/forms.py
# class IPv6StaticForm(forms.ModelForm):
# 
#     class Meta:
#         model  = ipv6_static
#         fields = ('__all__')
# 
# class IPv4StaticForm(forms.ModelForm):
# 
#     class Meta:
#         model  = ipv4_static
#         fields = ('__all__')


from django.shortcuts import get_object_or_404, render, redirect
from netdevice.models import router
from static.models import ipv6_static, ipv4_static
from static.forms import IPv6StaticForm, IPv4StaticForm


def ipv6_static_create(request, router_id):
    router_obj = get_object_or_404(router, pk=router_id)
    if request.method == "POST":
        form = IPv6StaticForm(request.POST)
        if form.is_valid():
            ipv6_static_obj = form.save()
            return redirect('netdevice:router_detail', router_id=ipv6_static_obj.router.pk)
    else:
        form = IPv6StaticForm(initial={'router': router_obj})
    return render(request, 'op_webgui/generic_edit.html', {'form': form})

def ipv6_static_edit(request, ipv6_static_id):
    ipv6_static_obj = get_object_or_404(ipv6_static, pk=ipv6_static_id)
    if request.method == "POST":
        form = IPv6StaticForm(request.POST, instance=ipv6_static_obj)
        if form.is_valid():
            ipv6_static_obj = form.save()
            return redirect('netdevice:router_detail', router_id=ipv6_static_obj.router.pk)
    else:
        form = IPv6StaticForm(instance=ipv6_static_obj)
    return render(request, 'op_webgui/generic_edit.html', {'form': form})

def ipv4_static_create(request, router_id):
    router_obj = get_object_or_404(router, pk=router_id)
    if request.method == "POST":
        form = IPv4StaticForm(request.POST)
        if form.is_valid():
            ipv4_static_obj = form.save()

# 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: static/models.py
# class ipv6_static(models.Model):
#     """
#     IPv6 Static Routes - Routes set by the user in a given router.
# 
#     router:       The router to place this static route.
#     network:      The network address of the route.                                    [2001:db8::]
#     cidr:         The CIDR length of this static route.                                [64]
#     next-hop:     Next-hop device of this static route.                                [2001:db8:1:1::1]
# 
#     Example string: Router1 2001:db8:: next-hop 2001:db8:1:1::1
#     """
# 
#     router      = models.ForeignKey('netdevice.router', on_delete=models.CASCADE, null=True, blank=True)
#     vrf         = models.ForeignKey(
#                                     'netdevice.vrf',
#                                     on_delete=models.CASCADE,
#                                     null=True,
#                                     blank=True
#                                    )
#     network     = models.GenericIPAddressField(unpack_ipv4=False)
#     cidr        = models.PositiveSmallIntegerField(default=64)
#     next_hop    = models.GenericIPAddressField(unpack_ipv4=True)
# 
#     def __str__(self):
#         return self.router.hostname + " " + str(self.network) + "/" + str(self.cidr) + " next-hop " + str(self.next_hop)
# 
# class ipv4_static(models.Model):
#     """
#     IPv4 Static Routes - Routes set by the user in a given router
# 
#     router:       The router to place this static route.
#     network:      The network address of the route.                                    [198.51.100.0]
#     cidr:         The CIDR length of this static route.                                [24]
#     next-hop:     Next-hop device of this static route.                                [198.51.100.1]
# 
#     Example string: Router1 198.51.100.0/24 next-hop 198.51.100.1
#     """
# 
#     router      = models.ForeignKey('netdevice.router', on_delete=models.CASCADE, null=True, blank=True)
#     vrf         = models.ForeignKey(
#                                     'netdevice.vrf',
#                                     on_delete=models.CASCADE,
#                                     null=True,
#                                     blank=True
#                                    )
#     network     = models.GenericIPAddressField(unpack_ipv4=True)
#     cidr        = models.PositiveSmallIntegerField(default=24)
#     next_hop    = models.GenericIPAddressField(unpack_ipv4=True)
# 
#     def subnet_mask(self):
#         host_bits = 32 - int(self.cidr)
#         netmask = socket.inet_ntoa(struct.pack('!I', (1 << 32) - (1 << host_bits)))
#         return netmask
# 
#     def __str__(self):
#         return self.router.hostname + " " + str(self.network) + "/" + str(self.cidr) + " next-hop " + str(self.next_hop)
# 
# Path: static/forms.py
# class IPv6StaticForm(forms.ModelForm):
# 
#     class Meta:
#         model  = ipv6_static
#         fields = ('__all__')
# 
# class IPv4StaticForm(forms.ModelForm):
# 
#     class Meta:
#         model  = ipv4_static
#         fields = ('__all__')


# Path: static/views.py
from django.shortcuts import get_object_or_404, render, redirect
from netdevice.models import router
from static.models import ipv6_static, ipv4_static
from static.forms import IPv6StaticForm, IPv4StaticForm



def ipv6_static_create(request, router_id):
    router_obj = get_object_or_404(router, pk=router_id)
    if request.method == "POST":
        form = IPv6StaticForm(request.POST)
        if form.is_valid():
            ipv6_static_obj = form.save()
            return redirect('netdevice:router_detail', router_id=ipv6_static_obj.router.pk)
    else:
        form = IPv6StaticForm(initial={'router': router_obj})
    return render(request, 'op_webgui/generic_edit.html', {'form': form})

def ipv6_static_edit(request, ipv6_static_id):
    ipv6_static_obj = get_object_or_404(ipv6_static, pk=ipv6_static_id)
    if request.method == "POST":
        form = IPv6StaticForm(request.POST, instance=ipv6_static_obj)
        if form.is_valid():
            ipv6_static_obj = form.save()
            return redirect('netdevice:router_detail', router_id=ipv6_static_obj.router.pk)
    else:
        form = IPv6StaticForm(instance=ipv6_static_obj)
    return render(request, 'op_webgui/generic_edit.html', {'form': form})

def ipv4_static_create(request, router_id):
    router_obj = get_object_or_404(router, pk=router_id)
    if request.method == "POST":
        form = IPv4StaticForm(request.POST)
        if form.is_valid():
            ipv4_static_obj = form.save()

            return redirect('netdevice:router_detail', router_id=ipv4_static_obj.router.pk)