summaryrefslogtreecommitdiffstats
path: root/src/com.gluster.storage.management.gateway/WebContent/scripts/Protocol.py
blob: ff073593e36339efac2b334f5c0364811f1984c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#  Copyright (C) 2009 Gluster, Inc. <http://www.gluster.com>
#  This file is part of Gluster Storage Platform.
#
#  Gluster Storage Platform is free software; you can redistribute it
#  and/or modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 3 of
#  the License, or (at your option) any later version.
#
#  Gluster Storage Platform is distributed in the hope that it will be
#  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see
#  <http://www.gnu.org/licenses/>.

import xml
import xml.parsers.expat
import xml.dom.minidom as MDOM
import os
import Globals
import copy
import Utils

XML_STRING = 0
XML_FILE = 1

class XDOM:
    _domObj = None

    def __init__(self):
        self._domObj = MDOM.Document()
        return

    @classmethod
    def getText(self, nodeList):
        rc = ""
        for node in nodeList:
            if node.nodeType == node.TEXT_NODE:
                rc = rc + node.data
        return rc.strip()

    def parseString(self, requestString):
        try:
            self._domObj = MDOM.parseString(requestString)
        except xml.parsers.expat.ExpatError, e:
            Utils.log("XML string parse error: %s" % str(e))
            return False
        return True

    def parseFile(self, fileName):
        try:
            self._domObj = MDOM.parse(fileName)
        except IOError, e:
            Utils.log("error reading file: %s" % str(e))
            return False
        except xml.parsers.expat.ExpatError, e:
            Utils.log("XML file %s parse error: %s" % (fileName, str(e)))
            return False
        return True

    def setDomObj(self, dom):
        if dom and type(dom) != type([]):
            self._domObj = dom
            return True
        return False

    def createTextNode(self, text):
        if not self._domObj:
            return False
        if not text:
            return False
        return self._domObj.createTextNode(str(text))

    def createTag(self, tag, text=None):
        if not self._domObj:
            return None
        if tag == None:
            return None

        tagE = self._domObj.createElement(str(tag))
        if text:
            tagEText = self._domObj.createTextNode(str(text))
            tagE.appendChild(tagEText)
        return tagE

    def addTag(self, tag):
        if not self._domObj:
            return False
        if not tag:
            return False

        self._domObj.appendChild(tag)
        return True

    def createTagRoute(self, tagRoute, text=None):
        if not tagRoute:
            return False

        tagList = tagRoute.split(".")
        tag = None
        previousTag = None
        for tagName in tagList[:-1]:
            newTag = self.createTag(tagName, None)
            if not tag:
                tag = newTag
                previousTag = newTag
                continue
            previousTag.appendChild(newTag)
            previousTag = newTag

        if previousTag:
            previousTag.appendChild(self.createTag(tagList[-1], text))
        else:
            tag = self.createTag(tagList[-1], text)
        return tag

    def appendTagRoute(self, tagRoute, value=None):
        if not self._domObj:
            return False
        if not tagRoute:
            return False

        parentTagE = self._domObj

        tagNameList = tagRoute.split(".")
        newTagRoute = tagNameList.pop(-1)

        for i in range(len(tagNameList), 0, -1):
            tagE = self.getElementsByTagRoute(".".join(tagNameList[:i]))
            if tagE:
                parentTagE = tagE[0]
                break
            newTagRoute = tagNameList[i-1] + "." + newTagRoute

        newTagE = self.createTagRoute(newTagRoute, value)
        if not newTagE:
            return False
        try:
            parentTagE.appendChild(newTagE)
        except xml.dom.HierarchyRequestErr, e:
            Utils.log("error occured.  %s" + str(e))
            return False
        return True

    def setTextByTagRoute(self, tagRoute, tagValue):
        if not self._domObj:
            return None

        if not tagRoute:
            return None

        tagE  = self.getElementsByTagRoute(tagRoute)
        if not tagE:
            return False

        parentTagE = self.getElementsByTagRoute(".".join(tagRoute.split(".")[:-1]))
        if not parentTagE:
            return False
        
        parentTagE[0].childNodes.remove(tagE[0])
        parentTagE[0].appendChild(self.createTag(tagRoute.split(".")[-1], tagValue))
        return True

    def getElementsByTagRoute(self, tagRoute):
        if not self._domObj:
            return None
        
        if not tagRoute:
            return None

        x = None
        for tag in tagRoute.split("."):
            if x is None:
                x = self._domObj.getElementsByTagName(tag)
                continue
            if x == []:
                break
            x = x[0].getElementsByTagName(tag)
        return x

    def getTextByTagRoute(self, tagRoute):
        if not self._domObj:
            return None

        x = self.getElementsByTagRoute(tagRoute)
        if x:
            return self.getText(x[0].childNodes)
        return None

    def getElementsByTagName(self, name):
        if not self._domObj:
            return None
        return self._domObj.getElementsByTagName(name)

    def writexml(self, fileName, indent="", addindent="", newl=""):
        if not self._domObj:
            return None
        try:
            fp = open(fileName, "w")
            self._domObj.writexml(fp, indent, addindent, newl)
            fp.close()
            return True
        except IOError:
            return False

    def toString(self, indent="  ", newl="\n", encoding = None):
        if not self._domObj:
            return None
        return self._domObj.toprettyxml(indent, newl, encoding)

    def toxml(self, encoding = None):
        if not self._domObj:
            return None
        return self._domObj.toxml(encoding)

    def toprettyxml(self, indent="  ", newl="\n", encoding = None):
        return self.toString(indent, newl, encoding)

    def getAttribute(self, attributeName):
        if not attributeName:
            return None
        try:
            return self.getElementsByTagName("command")[0].getAttribute(attributeName)
        except IndexError:
            return False

    def setAttribute(self, attributeName, attributeValue):
        if not (attributeName and attributeValue):
            return None
        try:
            return self.getElementsByTagName("command")[0].setAttribute(attributeName, attributeValue)
        except IndexError:
            return False

    def getRequestCommand(self):
        return self.getAttribute("request")

    def getResponseCommand(self):
        return self.getAttribute("response")

    def getResponseCode(self):
        return self.getAttribute("response-code")

    def getMessageId(self):
        return self.getAttribute("id")

    def getVersion(self):
        return self.getAttribute("version")

    def getRequestAction(self):
        return self.getAttribute("action")

    def setVersion(self, value):
        return self.setAttribute("version", value)

    def setRequestAction(self, value):
        return self.setAttribute("action", value)
            
    def createCommandTag(self, command, responseCode, id, version=Globals.GLUSTER_PLATFORM_VERSION):
        commandTag = self._domObj.createElement("command")
        commandTag.setAttribute("response", command)
        commandTag.setAttribute("response-code", responseCode)
        commandTag.setAttribute("id", id)
        commandTag.setAttribute("version", version)
        return commandTag
##--end of XDOM

class RequestXml(XDOM):
    def __init__(self, requestString, type=None):
        if None == requestString:
            XDOM.__init__(self)
            return
        try:
            if None == type:
                if os.path.isfile(requestString):
                    self._domObj = MDOM.parse(requestString)
                else:
                    self._domObj = MDOM.parseString(requestString)
            elif XML_FILE == type:
                self._domObj = MDOM.parse(requestString)
            elif XML_STRING == type:
                self._domObj = MDOM.parseString(requestString)
        except IOError:
            XDOM.__init__(self)
        except xml.parsers.expat.ExpatError:
            XDOM.__init__(self)

##--end of RequestXML

class ResponseXml(XDOM):
    _commandTag = None
    def __init__(self, command, responseCode, id, version=Globals.GLUSTER_PLATFORM_VERSION):
        XDOM.__init__(self)
        if command and responseCode and id:
            self._commandTag = self.createCommandTag(command, responseCode, id, version)
            self._domObj.appendChild(self._commandTag)

    def appendCommand(self, command, responseCode, id, version=Globals.GLUSTER_PLATFORM_VERSION):
        if command and responseCode and id:
            self._commandTag = self.createCommandTag(command, responseCode, id, version)
            self._domObj.appendChild(self._commandTag)
            return True
        return False

    def append(self, tagName, tagValue=None):
        if not self._commandTag:
            return False
        tag = self.createTag(tagName, tagValue)
        if tag:
            self._commandTag.appendChild(tag)
            return True
        return False

    def appendTag(self, tag):
        if not tag:
            return False
        if not self._commandTag:
            return False
        self._commandTag.appendChild(tag)
        return True

    def appendTagRoute(self, tagRoute, value=None):
        if not self._commandTag:
            return False
        if not tagRoute:
            return False

        parentTagE = self._commandTag

        tagNameList = tagRoute.split(".")
        newTagRoute = tagNameList.pop(-1)

        for i in range(len(tagNameList), 0, -1):
            tagE = self.getElementsByTagRoute(".".join(["command"] + tagNameList[:i]))
            if tagE:
                parentTagE = tagE[0]
                break
            newTagRoute = tagNameList[i-1] + "." + newTagRoute

        newTagE = self.createTagRoute(newTagRoute, value)
        if not newTagE:
            return False
        try:
            parentTagE.appendChild(newTagE)
        except xml.dom.HierarchyRequestErr, e:
            Utils.log("error occured.  %s" + str(e))
            return False
        return True

    def appendTagRouteOld(self, tagRoute, value=None):
        if not tagRoute:
            return False
        if not self._commandTag:
            return False
        
        tmpTagRoute = ""
        previousTagE = self._commandTag
        tagE = None
        for tagName in tagRoute.split("."):
            if not tmpTagRoute:
                tagE = self.getElementsByTagRoute("command." + tagName)
            else:
                tagE = self.getElementsByTagRoute("command." + tmpTagRoute + "." + tagName)
            if not tagE:
                break
            if len(tagE) != 1:
                return False
            previousTagE = tagE[0]
            if not tmpTagRoute:
                tmpTagRoute = tagName
            else:
                tmpTagRoute = tmpTagRoute + "." + tagName

        if tmpTagRoute == tagRoute:
            return False
        newTagRoute = tagRoute[len(tmpTagRoute):]
        if newTagRoute[0] == '.':
            newTagRoute = newTagRoute[1:]

        if previousTagE.childNodes and previousTagE.childNodes[0].nodeType == previousTagE.TEXT_NODE:
            return False
        previousTagE.appendChild(self.createTagRoute(newTagRoute, value))
        return True
##--end of ResponseXml

def test():
    #volumes = RequestXml(VolumeFile, XML_FILE).getElementsByTagRoute("volume-list.volume")
    requestStr = '''<command request="create-volume" id="123" version="3.1">
<volume>
<name>movies1</name>
<type>cluster mirror</type>
<start>512000</start>
<server>zresearch</server>
<vacl>192.168.20.*</vacl>
<vacl>192.168.30.*</vacl>
<nfs>
<export>no</export>
</nfs>
<cifs>
<export>no</export>
</cifs>
<webdav>
<export>no</export>
</webdav>
</volume>
</command>'''

    requestXml = RequestXml(requestStr)
    print requestXml.getAttribute("")

def test1():
    rs = ResponseXml("create-volume", "OK", "xyz")
    rs.appendTagRoute("volume.detail.name", "music")
    print rs.toprettyxml()
    rs.append("volume", "data")
    print rs.toprettyxml()
    rs.appendTagRoute("volume.detail.ipaddr", "192.168.10.1")
    print rs.toprettyxml()
    print rs.appendTagRoute("volume.detail.ipaddr.v6", "ff:ff::ff::")
    print rs.toprettyxml()

    print rs.getTextByTagRoute("command.volume.detail")

def test2():
    rs = ResponseXml("download-volume-logs", "OK", "xyz")
    te = rs.createTag("interface", None)
    te.appendChild(rs.createTag("device", "DEVICE1"))
    te.appendChild(rs.createTag("description", "my device one"))
    rs.appendTag(te)

    te = rs.createTag("interface", None)
    te.appendChild(rs.createTag("device", "DEVICE2"))
    te.appendChild(rs.createTag("description", "my device two"))
    rs.appendTag(te)
    print rs.toprettyxml()