Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / reconcile-pools.py @ 9e20fcee

History | View | Annotate | Download (6.4 kB)

1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29
#
30
from django.core.management.base import BaseCommand
31

    
32
from synnefo.db.models import (Network, BackendNetwork,
33
                               BridgePoolTable, MacPrefixPoolTable)
34
from synnefo.db.pools import EmptyPool
35

    
36

    
37
class Command(BaseCommand):
38
    help = 'Check consistency of unique resources.'
39

    
40
    def handle(self, **options):
41
        self.detect_bridges()
42
        self.detect_mac_prefixes()
43
        self.detect_unique_mac_prefixes()
44

    
45
    def detect_bridges(self):
46
        write = self.stdout.write
47

    
48
        write("---------------------------------------\n")
49
        write("Checking consistency of the Bridge Pool\n")
50
        write("---------------------------------------\n")
51

    
52
        try:
53
            bridge_pool = BridgePoolTable.get_pool()
54
        except EmptyPool:
55
            write("No Bridge Pool\n")
56
            return
57
        bridges = []
58
        for i in xrange(0, bridge_pool.size()):
59
            if not bridge_pool.is_available(i, index=True) and \
60
                not bridge_pool.is_reserved(i, index=True):
61
                    bridges.append(bridge_pool.index_to_value(i))
62

    
63
        write("Used bridges from Pool: %d\n" % len(bridges))
64

    
65
        network_bridges = Network.objects.filter(flavor='PHYSICAL_VLAN',
66
                                                 deleted=False)\
67
                                         .values_list('link', flat=True)
68

    
69
        write("Used bridges from Networks: %d\n" % len(network_bridges))
70

    
71
        set_network_bridges = set(network_bridges)
72
        if len(network_bridges) > len(set_network_bridges):
73
            write("Found duplicated bridges:\n")
74
            duplicates = list(network_bridges)
75
            for bridge in set_network_bridges:
76
                duplicates.remove(bridge)
77
            for bridge in set(duplicates):
78
                write("Duplicated bridge: %s. " % bridge)
79
                write("Used by the following Networks:\n")
80
                nets = Network.objects.filter(deleted=False, link=bridge)
81
                write("  " + "\n  ".join([str(net.id) for net in nets]) + "\n")
82

    
83
    def detect_mac_prefixes(self):
84
        write = self.stdout.write
85

    
86
        write("---------------------------------------\n")
87
        write("Checking consistency of the MAC Prefix Pool\n")
88
        write("---------------------------------------\n")
89

    
90
        try:
91
            macp_pool = MacPrefixPoolTable.get_pool()
92
        except EmptyPool:
93
            write("No mac-prefix pool\n")
94
            return
95

    
96
        macs = []
97
        for i in xrange(1, macp_pool.size()):
98
            if not macp_pool.is_available(i, index=True) and \
99
               not macp_pool.is_reserved(i, index=True):
100
                value = macp_pool.index_to_value(i)
101
                macs.append(value)
102

    
103
        write("Used MAC prefixes from Pool: %d\n" % len(macs))
104

    
105
        network_mac_prefixes = \
106
            Network.objects.filter(deleted=False, flavor='MAC_FILTERED')\
107
                           .values_list('mac_prefix', flat=True)
108
        write("Used MAC prefixes from Networks: %d\n" %
109
              len(network_mac_prefixes))
110

    
111
        set_network_mac_prefixes = set(network_mac_prefixes)
112
        if len(network_mac_prefixes) > len(set_network_mac_prefixes):
113
            write("Found duplicated mac_prefixes:\n")
114
            duplicates = list(network_mac_prefixes)
115
            for mac_prefix in set_network_mac_prefixes:
116
                duplicates.remove(mac_prefix)
117
            for mac_prefix in set(duplicates):
118
                write("Duplicated mac_prefix: %s. " % mac_prefix)
119
                write("Used by the following Networks:\n")
120
                nets = Network.objects.filter(deleted=False,
121
                                              mac_prefix=mac_prefix)
122
                write("  " + "\n  ".join([str(net.id) for net in nets]) + "\n")
123

    
124
    def detect_unique_mac_prefixes(self):
125
        write = self.stdout.write
126

    
127
        write("---------------------------------------\n")
128
        write("Checking uniqueness of BackendNetwork prefixes.\n")
129
        write("---------------------------------------\n")
130

    
131
        back_networks = BackendNetwork.objects
132
        mac_prefixes = back_networks.filter(deleted=False,
133
                                            network__flavor='MAC_FILTERED')\
134
                                    .values_list('mac_prefix', flat=True)
135
        set_mac_prefixes = set(mac_prefixes)
136
        if len(mac_prefixes) > len(set_mac_prefixes):
137
            write("Found duplicated mac_prefixes:\n")
138
            duplicates = list(mac_prefixes)
139
            for mac_prefix in set_mac_prefixes:
140
                duplicates.remove(mac_prefix)
141
            for mac_prefix in set(duplicates):
142
                write("Duplicated mac_prefix: %s. " % mac_prefix)
143
                write("Used by the following BackendNetworks:\n")
144
                nets = BackendNetwork.objects.filter(deleted=False,
145
                                                     mac_prefix=mac_prefix)
146
                write("  " + "\n  ".join([str(net.id) for net in nets]) + "\n")