Example #1
0
def _delete_policies(asys: UserAS, parsed_request):
    """Delete the peering policies contained in `parsed_request` and apply the remaining policies.

    If deleting any policy fails or parsed_request is invalid, the whole process is rolled back.

    :param asys: AS model instance of the AS the policies belong to.
    :param parsed_request: Nested dictionary in the same format _get_policies() returns.

    :raises django.db.DatabaseError:
    """
    for ixp_id, policies in parsed_request.items():
        try:
            ixp = IXP.objects.get(id=ixp_id)
        except IXP.DoesNotExist:
            raise InvalidPolicy("IXP %s does not exist." % ixp_id)

        if 'AS' in policies:
            for peer_as in policies['AS'].get('accept', []):
                _delete_as_policy(ixp, asys, peer_as, deny=False)
            for peer_as in policies['AS'].get('deny', []):
                _delete_as_policy(ixp, asys, peer_as, deny=True)

        if 'ISD' in policies:
            for peer_isd in policies['ISD'].get('accept', []):
                _delete_isd_policy(ixp, asys, peer_isd, deny=False)
            for peer_isd in policies['ISD'].get('deny', []):
                _delete_isd_policy(ixp, asys, peer_isd, deny=True)

        apply_peering_policy(asys, ixp)
Example #2
0
    def _add_as_policy(self, asys, peer_as, deny):
        """Add an AS peering policy and apply it.

        :returns: The newly created policy instance.
        """
        policy = ASPeerPolicy.objects.create(ixp=self.ixp,
                                             AS=asys,
                                             peer_as=peer_as,
                                             deny=deny)
        apply_peering_policy(asys,
                             self.ixp,
                             max_peering_per_br=self._MAX_PEERING_PER_BR)
        return policy
Example #3
0
    def _add_isd_policy(self, ixp, asys, peer_isd, deny):
        """Add an ISD peering policy and apply it.

        :returns: The newly created policy instance.
        """
        policy = ISDPeerPolicy.objects.create(ixp=ixp,
                                              AS=asys,
                                              peer_isd=peer_isd,
                                              deny=deny)

        apply_peering_policy(asys, self.ixp1)
        apply_peering_policy(asys, self.ixp2)

        return policy
Example #4
0
    def test_get_peers(self):
        """Test retrieveing a list of peers."""
        # No peering links yet
        resp = self.client.get(self.url_peers, **self.auth_headers)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json(), {"1": [], "2": []})

        # Invalid GET parameter
        resp = self.client.get(self.url_peers, {'ixp': 'nan'},
                               **self.auth_headers)
        self.assertEqual(resp.status_code, 400)

        # Allow 17-ffaa:1:1 -> 19-ffaa:1:2 and 19-ffaa:1:2 -> 17-ffaa:1:1
        ASPeerPolicy.objects.create(ixp=self.ixp1,
                                    AS=self.ases[0],
                                    peer_as=self.ases[1],
                                    deny=False)
        ASPeerPolicy.objects.create(ixp=self.ixp1,
                                    AS=self.ases[1],
                                    peer_as=self.ases[0],
                                    deny=False)
        apply_peering_policy(self.ases[0], self.ixp1)
        apply_peering_policy(self.ases[1], self.ixp1)

        resp = self.client.get(self.url_peers, **self.auth_headers)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json(), {
            "1": [{
                "as_id": "ffaa:1:2",
                "label": ""
            }],
            "2": []
        })

        resp = self.client.get(self.url_peers, {'ixp': '1'},
                               **self.auth_headers)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json(),
                         {"1": [{
                             "as_id": "ffaa:1:2",
                             "label": ""
                         }]})

        resp = self.client.get(self.url_peers, {'ixp': '2'},
                               **self.auth_headers)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json(), {"2": []})
Example #5
0
 def _delete_policy(self, policy):
     """Delete a peering policy and apply the changes."""
     policy.delete()
     apply_peering_policy(policy.AS,
                          self.ixp,
                          max_peering_per_br=self._MAX_PEERING_PER_BR)
Example #6
0
    def _delete_policy(self, policy):
        """Delete a peering policy and apply the changes."""
        policy.delete()

        apply_peering_policy(policy.AS, self.ixp1)
        apply_peering_policy(policy.AS, self.ixp2)
Example #7
0
 def delete_queryset(self, request, queryset):
     update = {(obj.AS, obj.ixp) for obj in queryset}
     super().delete_queryset(request, queryset)
     for asys, ixp in update:
         apply_peering_policy(asys, ixp)
Example #8
0
 def delete_model(self, request, obj):
     super().delete_model(request, obj)
     apply_peering_policy(obj.AS, obj.ixp)
Example #9
0
 def save_model(self, request, obj, form, change):
     super().save_model(request, obj, form, change)
     apply_peering_policy(obj.AS, obj.ixp)