Statistics
| Branch: | Tag: | Revision:

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

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.settings import MAC_POOL_BASE
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
        bridge_pool = BridgePoolTable.get_pool()
53
        bridges = []
54
        for i in xrange(0, bridge_pool.size()):
55
            if not bridge_pool.is_available(i, index=True) and \
56
               not bridge_pool.is_reserved(i, index=True):
57
                bridges.append(bridge_pool.index_to_value(i))
58

    
59
        write("Used bridges from Pool: %d\n" % len(bridges))
60

    
61
        network_bridges = Network.objects.filter(type='PRIVATE_PHYSICAL_VLAN',
62
                                                deleted=False)\
63
                                         .values_list('link', flat=True)
64

    
65
        write("Used bridges from Networks: %d\n" %
66
                          len(network_bridges))
67

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

    
81
    def detect_mac_prefixes(self):
82
        write = self.stdout.write
83

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

    
88
        macp_pool = MacPrefixPoolTable.get_pool()
89
        macs = []
90
        for i in xrange(0, macp_pool.size()):
91
            if not macp_pool.is_available(i, index=True) and \
92
               not macp_pool.is_reserved(i, index=True):
93
                value = macp_pool.index_to_value(i)
94
                if value != MAC_POOL_BASE:
95
                    macs.append(macp_pool.index_to_value(i))
96

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

    
99
        network_mac_prefixes = \
100
            Network.objects.filter(deleted=False)\
101
                            .exclude(mac_prefix=MAC_POOL_BASE) \
102
                            .values_list('mac_prefix', flat=True)
103
        write("Used MAC prefixes from Networks: %d\n" %
104
                          len(network_mac_prefixes))
105

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

    
120
    def detect_unique_mac_prefixes(self):
121
        write = self.stdout.write
122

    
123
        write("---------------------------------------\n")
124
        write("Checking uniqueness of BackendNetwork prefixes.\n")
125
        write("---------------------------------------\n")
126

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