Statistics
| Branch: | Tag: | Revision:

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

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
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
            used_bridge = not (bridge_pool.is_available(i, index=True) or
60
                               bridge_pool.is_reserved(i, index=True))
61
            if used_bridge:
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" % len(network_bridges))
71

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

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

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

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

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

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

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

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

    
126
    def detect_unique_mac_prefixes(self):
127
        write = self.stdout.write
128

    
129
        write("---------------------------------------\n")
130
        write("Checking uniqueness of BackendNetwork prefixes.\n")
131
        write("---------------------------------------\n")
132

    
133
        back_networks = BackendNetwork.objects
134
        mac_prefixes = back_networks.filter(deleted=False,
135
                                            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]) + "\n")