3 # Copyright 2016-2017 RIFT.IO Inc
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
23 gi
.require_version('RwNsdYang', '1.0')
24 gi
.require_version('RwProjectNsdYang', '1.0')
25 gi
.require_version('RwVnfdYang', '1.0')
26 gi
.require_version('RwProjectVnfdYang', '1.0')
27 gi
.require_version('RwYang', '1.0')
28 from gi
.repository
import (
40 from rift
.mano
.utils
.project
import NS_PROJECT
43 class UnknownExtensionError(Exception):
47 class SerializationError(Exception):
51 def decode(desc_data
):
52 if isinstance(desc_data
, bytes
):
53 desc_data
= desc_data
.decode()
58 class ProtoMessageSerializer(object):
59 """(De)Serializer/deserializer fo a specific protobuf message into various formats"""
62 def __init__(self
, yang_ns
, yang_pb_cls
,
63 yang_ns_project
, yang_pb_project_cls
):
64 """ Create a serializer for a specific protobuf message """
65 self
._yang
_ns
= yang_ns
66 self
._yang
_pb
_cls
= yang_pb_cls
67 self
._yang
_ns
_project
= yang_ns_project
68 self
._yang
_pb
_project
_cls
= yang_pb_project_cls
70 self
._log
= logging
.getLogger('rw-maon-log')
73 def _deserialize_extension_method_map(cls
):
75 ".xml": cls
._from
_xml
_file
_hdl
,
76 ".yml": cls
._from
_yaml
_file
_hdl
,
77 ".yaml": cls
._from
_yaml
_file
_hdl
,
78 ".json": cls
._from
_json
_file
_hdl
,
82 def _serialize_extension_method_map(cls
):
84 ".xml": cls
.to_xml_string
,
85 ".yml": cls
.to_yaml_string
,
86 ".yaml": cls
.to_yaml_string
,
87 ".json": cls
.to_json_string
,
91 def is_supported_file(cls
, filename
):
92 """Returns whether a file has a supported file extension
95 filename - A descriptor file
98 True if file extension is supported, False otherwise
101 _
, extension
= os
.path
.splitext(filename
)
102 extension_lc
= extension
.lower()
104 return extension_lc
in cls
._deserialize
_extension
_method
_map
()
107 def yang_namespace(self
):
108 """ The Protobuf's GI namespace class (e.g. RwVnfdYang) """
112 def yang_class(self
):
113 """ The Protobuf's GI class (e.g. RwVnfdYang.YangData_Vnfd_VnfdCatalog_Vnfd) """
114 return self
._yang
_pb
_cls
117 def yang_ns_project(self
):
118 """ The Protobuf's GI namespace class (e.g. RwProjectVnfdYang) """
119 return self
._yang
_ns
_project
122 def yang_class_project(self
):
123 """ The Protobuf's GI class (e.g. RwProjectVnfdYang.YangData_RwProject_Project_VnfdCatalog_Vnfd) """
124 return self
._yang
_pb
_project
_cls
130 # Cache the libncx model for the serializer class
131 if cls
.libncx_model
is None:
132 cls
.libncx_model
= RwYang
.model_create_libncx()
133 cls
.libncx_model
.load_schema_ypbc(self
.yang_namespace
.get_schema())
134 cls
.libncx_model
.load_schema_ypbc(self
.yang_ns_project
.get_schema())
136 return cls
.libncx_model
138 def _from_xml_file_hdl(self
, file_hdl
):
139 xml
= file_hdl
.read()
141 return self
.yang_class
.from_xml_v2(self
.model
, decode(xml
), strict
=False)
143 def _from_json_file_hdl(self
, file_hdl
):
144 jstr
= file_hdl
.read()
145 self
._log
.debug("Convert from json file: {}".format(jstr
))
148 desc_msg
= self
.yang_class
.from_json(self
.model
, decode(jstr
), strict
=False)
149 self
._log
.debug("desc_msg: {}".format(desc_msg
.as_dict()))
150 return self
.yang_class_project
.from_dict(desc_msg
.as_dict())
151 except Exception as e
:
152 self
._log
.exception(e
)
155 def _from_yaml_file_hdl(self
, file_hdl
):
156 yml
= file_hdl
.read()
159 desc_msg
= self
.yang_class
.from_yaml(self
.model
, decode(yml
), strict
=False)
160 return self
.yang_class_project
.from_dict(desc_msg
.as_dict())
161 except Exception as e
:
162 self
._log
.exception(e
)
165 def to_desc_msg(self
, pb_msg
, project_rooted
=True):
166 """Convert to and from project rooted pb msg descriptor to catalog
168 project_rooted: if pb_msg is project rooted or not
171 if isinstance(pb_msg
, self
._yang
_pb
_project
_cls
):
172 return self
._yang
_pb
_cls
.from_dict(pb_msg
.as_dict())
173 elif isinstance(pb_msg
, self
._yang
_pb
_cls
):
177 if isinstance(pb_msg
, self
._yang
_pb
_cls
):
178 return self
._yang
_pb
_project
_cls
.from_dict(pb_msg
.as_dict())
179 elif isinstance(pb_msg
, self
._yang
_pb
_project
_cls
):
182 raise TypeError("Invalid protobuf message type provided: {}".format(type(pb_msg
)))
185 def to_json_string(self
, pb_msg
, project_ns
=False):
186 """ Serialize a protobuf message into JSON
189 pb_msg - A GI-protobuf object of type provided into constructor
190 project_ns - Need the desc in project namespace, required for
191 posting to Restconf as part of onboarding
194 A JSON string representing the protobuf message
197 SerializationError - Message could not be serialized
198 TypeError - Incorrect protobuf type provided
200 self
._log
.debug("Convert desc to json (ns:{}): {}".format(project_ns
, pb_msg
.as_dict()))
202 # json_str = pb_msg.to_json(self.model)
204 desc_msg
= self
.to_desc_msg(pb_msg
, not project_ns
)
205 json_str
= desc_msg
.to_json(self
.model
)
207 # Remove rw-project:project top level element
208 dic
= json
.loads(json_str
)
209 jstr
= json
.dumps(dic
[NS_PROJECT
][0])
213 except Exception as e
:
214 raise SerializationError(e
)
216 self
._log
.debug("Convert desc to json: {}".format(jstr
))
219 def to_yaml_string(self
, pb_msg
):
220 """ Serialize a protobuf message into YAML
223 pb_msg - A GI-protobuf object of type provided into constructor
226 A YAML string representing the protobuf message
229 SerializationError - Message could not be serialized
230 TypeError - Incorrect protobuf type provided
233 desc_msg
= self
.to_desc_msg(pb_msg
)
234 yaml_str
= desc_msg
.to_yaml(self
.model
)
236 except Exception as e
:
237 self
._log
.exception("Exception converting to yaml: {}".format(e
))
238 raise SerializationError(e
)
242 def to_xml_string(self
, pb_msg
):
243 """ Serialize a protobuf message into XML
246 pb_msg - A GI-protobuf object of type provided into constructor
249 A XML string representing the protobuf message
252 SerializationError - Message could not be serialized
253 TypeError - Incorrect protobuf type provided
256 desc_msg
= self
.to_desc_msg(pb_msg
)
257 xml_str
= desc_msg
.to_xml_v2(self
.model
)
259 except Exception as e
:
260 self
._log
.exception("Exception converting to xml: {}".format(e
))
261 raise SerializationError(e
)
265 def from_file_hdl(self
, file_hdl
, extension
):
266 """ Returns the deserialized protobuf message from file contents
268 This function determines the serialization format based on file extension
271 file_hdl - The file hdl to deserialize (set at pos 0)
272 extension - Extension of the file format (second item of os.path.splitext())
275 A GI-Proto message of type that was provided into the constructor
278 UnknownExtensionError - File extension is not of a known serialization format
279 SerializationError - File failed to be deserialized into the protobuf message
282 extension_lc
= extension
.lower()
283 extension_map
= self
._deserialize
_extension
_method
_map
()
285 if extension_lc
not in extension_map
:
286 raise UnknownExtensionError("Cannot detect message format for %s extension" % extension_lc
)
289 msg
= extension_map
[extension_lc
](self
, file_hdl
)
290 except Exception as e
:
291 raise SerializationError(e
)
295 def to_string(self
, pb_msg
, extension
):
296 """ Returns the serialized protobuf message for a particular file extension
298 This function determines the serialization format based on file extension
301 pb_msg - A GI-protobuf object of type provided into constructor
302 extension - Extension of the file format (second item of os.path.splitext())
305 A GI-Proto message of type that was provided into the constructor
308 UnknownExtensionError - File extension is not of a known serialization format
309 SerializationError - File failed to be deserialized into the protobuf message
312 extension_lc
= extension
.lower()
313 extension_map
= self
._serialize
_extension
_method
_map
()
315 if extension_lc
not in extension_map
:
316 raise UnknownExtensionError("Cannot detect message format for %s extension" % extension_lc
)
319 msg
= extension_map
[extension_lc
](self
, pb_msg
)
320 except Exception as e
:
321 raise SerializationError(e
)
326 class VnfdSerializer(ProtoMessageSerializer
):
327 """ Creates a serializer for the VNFD descriptor"""
329 super().__init
__(VnfdYang
, VnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd
,
330 ProjectVnfdYang
, ProjectVnfdYang
.YangData_RwProject_Project_VnfdCatalog_Vnfd
)
333 class NsdSerializer(ProtoMessageSerializer
):
334 """ Creates a serializer for the NSD descriptor"""
336 super().__init
__(NsdYang
, NsdYang
.YangData_Nsd_NsdCatalog_Nsd
,
337 ProjectNsdYang
, ProjectNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd
)
340 class RwVnfdSerializer(ProtoMessageSerializer
):
341 """ Creates a serializer for the VNFD descriptor"""
343 super().__init
__(RwVnfdYang
, RwVnfdYang
.YangData_Vnfd_VnfdCatalog_Vnfd
,
344 RwProjectVnfdYang
, RwProjectVnfdYang
.YangData_RwProject_Project_VnfdCatalog_Vnfd
)
347 class RwNsdSerializer(ProtoMessageSerializer
):
348 """ Creates a serializer for the NSD descriptor"""
350 super().__init
__(RwNsdYang
, RwNsdYang
.YangData_Nsd_NsdCatalog_Nsd
,
351 RwProjectNsdYang
, RwProjectNsdYang
.YangData_RwProject_Project_NsdCatalog_Nsd
)