Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.5 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

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

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

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

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

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

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

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

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

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

    
86
    def detect_mac_prefixes(self):
87
        write = self.stdout.write
88

    
89
        write("---------------------------------------\n")
90
        write("Checking consistency of the MAC Prefix Pool\n")
91
        write("---------------------------------------\n")
92

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

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

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

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

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

    
128
    def detect_unique_mac_prefixes(self):
129
        write = self.stdout.write
130

    
131
        write("---------------------------------------\n")
132
        write("Checking uniqueness of BackendNetwork prefixes.\n")
133
        write("---------------------------------------\n")
134

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