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