ovsdbmonitor: Use ovs.json module instead of JsonReader and JsonWriter.
authorBen Pfaff <blp@nicira.com>
Mon, 2 May 2011 23:17:11 +0000 (16:17 -0700)
committerBen Pfaff <blp@nicira.com>
Mon, 23 May 2011 20:41:18 +0000 (13:41 -0700)
I can't figure out where JsonReader and JsonWriter come from.  I know that
they must exist, because I (and others) have used ovsdbmonitor before, but
I can't find them now.

Switch to using ovs.json, which is part of Open vSwitch so we know that
it exists.  At the same time, we have to start translating the Unicode
strings that ovs.json outputs into standard Python strings; otherwise
the "twisted conch" ssh implementation craps out because it tries to
concatenate this Unicode string with a standard string that contains
non-ASCII characters.

ovsdb/ovsdbmonitor/OVEConfig.py
ovsdb/ovsdbmonitor/OVEFetch.py
ovsdb/ovsdbmonitor/OVEStandard.py

index 7cc18eb..4754c67 100644 (file)
@@ -1,3 +1,4 @@
+# Copyright (c) 2011 Nicira Networks.
 # Copyright (c) 2010 Citrix Systems, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 
 from OVEStandard import *
 from OVELogger import *
+import ovs.json
+
+def str_recursive(x):
+    t = type(x)
+    if t == unicode:
+        return str(x)
+    elif t == list:
+        return [str_recursive(_) for _ in x]
+    elif t == dict:
+        out = {}
+        for k,v in x.iteritems():
+            out[str_recursive(k)] = str_recursive(v)
+        return out
+    else:
+        return x
 
 class OVEConfig(QtCore.QObject):
     instance = None
@@ -40,21 +56,21 @@ class OVEConfig(QtCore.QObject):
 
     def saveConfig(self):
         settings = QtCore.QSettings()
-        settings.setValue('config/hosts', QVariant(json.JsonWriter().write(self.hosts)))
+        settings.setValue('config/hosts', QVariant(ovs.json.to_string((self.hosts))))
         settings.setValue('config/logTraffic', QVariant(self.logTraffic))
         settings.setValue('config/truncateUuids', QVariant(self.truncateUuids))
-        settings.setValue('config/ssgList', QVariant(json.JsonWriter().write(self.ssgList)))
+        settings.setValue('config/ssgList', QVariant(ovs.json.to_string(self.ssgList)))
         settings.sync()
         self.emitUpdated()
-        
+
     def loadConfig(self):
         settings = QtCore.QSettings()
         jsonText = unicode(settings.value('config/hosts', QVariant('[]')).toString())
-        self.hosts = json.JsonReader().read(str(jsonText))
+        self.hosts = str_recursive(ovs.json.from_string(str(jsonText)))
         self.logTraffic = settings.value('config/logTraffic', QVariant(False)).toBool()
         self.truncateUuids = settings.value('config/truncateUuids', QVariant(False)).toBool()
         jsonText = unicode(settings.value('config/ssgList', QVariant('[]')).toString())
-        self.ssgList = json.JsonReader().read(str(jsonText))
+        self.ssgList = ovs.json.from_string(str(jsonText))
         if len(self.ssgList) == 0:
             self.ssgList = [
                 r'in_port0000',
index 9dd1118..8bc5757 100644 (file)
@@ -1,3 +1,4 @@
+# Copyright (c) 2011 Nicira Networks.
 # Copyright (c) 2010 Citrix Systems, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,6 +16,7 @@
 from OVEStandard import *
 from OVEConfig import *
 from OVELogger import *
+import ovs.json
 
 # This sequence installs the qt4reactor before twisted gets a chance to install its reactor
 import qt4reactor
@@ -166,7 +168,7 @@ class OVECommandChannel(channel.SSHChannel, QtCore.QObject):
                 if self.commandType == 'JSON':
                     try:
                         # Decode the JSON data, to confirm that we have all of the data
-                        self._jsonValues = json.read(str(self._data)) # FIXME: Should handle unicode
+                        self._jsonValues = ovs.json.from_string(str(self._data)) # FIXME: Should handle unicode
                         self.sendResult()
                     except:
                         pass # Wait for more data
index b9bc419..23b3e66 100644 (file)
@@ -1,3 +1,4 @@
+# Copyright (c) 2011 Nicira Networks.
 # Copyright (c) 2010 Citrix Systems, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -20,9 +21,9 @@ from pprint import pprint
 globalForcePySide = False
 
 try:
-    import json
+    import ovs.json
 except Exception, e:
-    print('+++ Python JSON module is required\n')
+    print('+++ OVS JSON module is required\n')
     raise
 
 try: