Initial commit
[ta/distributed-state-server.git] / src / dss / api / dss_msg.py
1 # Copyright 2019 Nokia
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
16 import json
17 from collections import OrderedDict
18
19 from dss.api import dss_error
20
21 '''
22 {
23     "name": <msg name>,
24     "id": <id>,
25     "result": <description>,
26     "payload": <data>
27 }
28 '''
29 class Msg(object):
30     def __init__(self, name=None, id=None, payload={}, result='OK'):
31         self.name = name
32         self.id = id
33         self.payload = payload
34         self.result = result
35
36     def serialize(self):
37         msg = OrderedDict([('name', self.name), ('id', self.id), ('result', self.result), ('payload', self.payload) ])
38         #msg = {}
39         #msg['name'] = self.name
40         #msg['id'] = self.id
41         #msg['result'] = self.result
42         #msg['payload'] = self.payload
43
44         return json.dumps(msg, separators=(',', ':'))
45
46     def deserialize(self, msg):
47         data = json.loads(msg, object_pairs_hook=OrderedDict)
48         self.name = data['name']
49         self.id = data['id']
50         self.result = data['result']
51         self.payload = data['payload']
52
53         if self.result != 'OK':
54             raise dss_error.Error('Request failed with error %s' % self.result)
55
56     def get_name(self):
57         return self.name
58
59     def get_id(self):
60         return self.id
61
62     def get_result(self):
63         return self.result
64
65     def get_payload(self):
66         return self.payload
67