Add ovsdbmonitor GUI tool by Andy Southgate, contributed by Citrix.
[sliver-openvswitch.git] / ovsdb / ovsdbmonitor / OVEConfig.py
1 # Copyright (c) 2010 Citrix Systems, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from OVEStandard import *
16 from OVELogger import *
17
18 class OVEConfig(QtCore.QObject):
19     instance = None
20     def __init__(self):
21         QtCore.QObject.__init__(self)
22         self.hosts = []
23         self.logTraffic = True
24         self.truncateUuids = True
25         self.ssgList = []
26         
27     @classmethod
28     def Inst(cls):
29         if cls.instance is None:
30             cls.instance = OVEConfig()
31             cls.instance.loadConfig()
32         return cls.instance
33
34     def hostFromUuid(self, uuid):
35         for host in self.hosts:
36             if host['uuid'] == uuid:
37                 return host
38         OVELog("+++ Couldn't find host '"+str(uuid)+"' in "+str([x['uuid'] for x in self.hosts]))
39         return None
40
41     def saveConfig(self):
42         settings = QtCore.QSettings()
43         settings.setValue('config/hosts', QVariant(json.JsonWriter().write(self.hosts)))
44         settings.setValue('config/logTraffic', QVariant(self.logTraffic))
45         settings.setValue('config/truncateUuids', QVariant(self.truncateUuids))
46         settings.setValue('config/ssgList', QVariant(json.JsonWriter().write(self.ssgList)))
47         settings.sync()
48         self.emitUpdated()
49         
50     def loadConfig(self):
51         settings = QtCore.QSettings()
52         jsonText = unicode(settings.value('config/hosts', QVariant('[]')).toString())
53         self.hosts = json.JsonReader().read(str(jsonText))
54         self.logTraffic = settings.value('config/logTraffic', QVariant(False)).toBool()
55         self.truncateUuids = settings.value('config/truncateUuids', QVariant(False)).toBool()
56         jsonText = unicode(settings.value('config/ssgList', QVariant('[]')).toString())
57         self.ssgList = json.JsonReader().read(str(jsonText))
58         if len(self.ssgList) == 0:
59             self.ssgList = [
60                 r'in_port0000',
61                 r'in_port0001',
62                 r'in_port0002',
63                 r'in_port0003',
64                 r'vlan65535',
65                 r'type0800',
66                 r'type0806',
67                 r'proto0',
68                 r'proto6',
69                 r'proto17',
70                 r'ff:ff:ff:ff:ff:ff',
71                 r'!ff:ff:ff:ff:ff:ff',
72                 r'0\.0\.0\.0',
73                 r'!0\.0\.0\.0',
74                 r'255\.255\.255\.255',
75                 r'!255\.255\.255\.255',
76                 r'never',
77                 r'drop',
78                 r'!never',
79                 r'!drop',
80                 r'(never|drop)',
81                 r'!(never|drop)'
82             ]
83         
84     def emitUpdated(self):
85         self.emit(QtCore.SIGNAL("configUpdated()"))