@@ -181,6 +181,8 @@ def __parse_xpath_to_json(self, xpath, namespace):
181
181
"""Parses an XPath to JSON representation, and appends
182
182
namespace into the JSON request.
183
183
"""
184
+ if not namespace :
185
+ raise ValueError ('Must include namespace if constructing from xpath!' )
184
186
xpath_dict = {}
185
187
xpath_split = xpath .split ('/' )
186
188
first = True
@@ -216,6 +218,13 @@ def __fulfill_request(self, request_method, request_args):
216
218
metadata = self .__gen_metadata ()
217
219
)
218
220
)
221
+
222
+ def __validate_enum_arg (self , name , valid_options , message = None ):
223
+ r"""Construct error around enumeration validation."""
224
+ if name not in valid_options :
225
+ if not message :
226
+ message = '%s must be one of %s' % (name , ', ' .join (valid_options ))
227
+ raise ValueError (message )
219
228
220
229
def get_oper (self , yang_path , namespace = None , request_id = 0 , path_is_payload = False ):
221
230
r"""Get operational data from device.
@@ -228,7 +237,7 @@ def get_oper(self, yang_path, namespace=None, request_id=0, path_is_payload=Fals
228
237
YANG namespace applicable to the specified XPath.
229
238
request_id : uint, optional
230
239
The request ID to indicate to the device.
231
- path_is_payload : { True, False } , optional
240
+ path_is_payload : bool , optional
232
241
Indicates that the yang_path parameter contains a preformed JSON
233
242
payload and should not be parsed into JSON as an XPath.
234
243
@@ -238,8 +247,6 @@ def get_oper(self, yang_path, namespace=None, request_id=0, path_is_payload=Fals
238
247
Response wrapper object with ReqID, YangData, and Errors fields.
239
248
"""
240
249
if not path_is_payload :
241
- if not namespace :
242
- raise Exception ('Must include namespace if yang_path is not payload.' )
243
250
yang_path = self .__parse_xpath_to_json (yang_path , namespace )
244
251
request_args = proto .GetOperArgs (ReqID = request_id , YangPath = yang_path )
245
252
return self .__fulfill_request (
@@ -258,7 +265,7 @@ def get(self, yang_path, namespace=None, request_id=0, path_is_payload=False):
258
265
YANG namespace applicable to the specified XPath.
259
266
request_id : uint, optional
260
267
The request ID to indicate to the device.
261
- path_is_payload : { True, False } , optional
268
+ path_is_payload : bool , optional
262
269
Indicates that the yang_path parameter contains a preformed JSON
263
270
payload and should not be parsed into JSON as an XPath.
264
271
@@ -268,8 +275,6 @@ def get(self, yang_path, namespace=None, request_id=0, path_is_payload=False):
268
275
Response wrapper object with ReqID, YangData, and Errors fields.
269
276
"""
270
277
if not path_is_payload :
271
- if not namespace :
272
- raise Exception ('Must include namespace if yangpath is not payload.' )
273
278
yang_path = self .__parse_xpath_to_json (yang_path , namespace )
274
279
request_args = proto .GetArgs (ReqID = request_id , YangPath = yang_path )
275
280
return self .__fulfill_request (
@@ -292,7 +297,7 @@ def get_config(self, yang_path, namespace=None, request_id=0,
292
297
The request ID to indicate to the device.
293
298
source : { 'running', ? }, optional
294
299
Source to retrieve configuration from.
295
- path_is_payload : { True, False } , optional
300
+ path_is_payload : bool , optional
296
301
Indicates that the yang_path parameter contains a preformed JSON
297
302
payload and should not be parsed into JSON as an XPath.
298
303
@@ -305,14 +310,13 @@ def get_config(self, yang_path, namespace=None, request_id=0,
305
310
-----
306
311
Need to verify whether source param may be something other than running.
307
312
"""
313
+ self .__validate_enum_arg (source , {'running' })
308
314
if not path_is_payload :
309
- if not namespace :
310
- raise Exception ('Must include namespace if yang_path is not payload.' )
311
315
yang_path = self .__parse_xpath_to_json (yang_path , namespace )
312
316
request_args = proto .GetConfigArgs (ReqID = request_id , Source = source , YangPath = yang_path )
313
317
return self .__fulfill_request (
314
- self .__client .GetConfig ,
315
- request_args
318
+ request_method = self .__client .GetConfig ,
319
+ request_args = request_args
316
320
)
317
321
318
322
def edit_config (self , yang_path ,
@@ -344,14 +348,18 @@ def edit_config(self, yang_path,
344
348
gRPCResponse
345
349
Response wrapper object with ReqID, YangData, and Errors fields.
346
350
"""
351
+ self .__validate_enum_arg (operation , {'merge' , 'create' , 'replace' , 'delete' , 'remove' })
352
+ self .__validate_enum_arg (default_operation , {'merge' , 'replace' , 'none' })
353
+ self .__validate_enum_arg (target , {'running' })
354
+ self .__validate_enum_arg (error_operation , {'roll-back' , 'stop' , 'continue' })
347
355
request_args = proto .EditConfigArgs (
348
356
YangPath = yang_path , Operation = operation , SessionID = session_id ,
349
357
ReqID = request_id , Target = target , DefOp = default_operation ,
350
358
ErrorOp = error_operation
351
359
)
352
360
return self .__fulfill_request (
353
- self .__client .EditConfig ,
354
- request_args
361
+ request_method = self .__client .EditConfig ,
362
+ request_args = request_args
355
363
)
356
364
357
365
def start_session (self , request_id = 0 ):
@@ -369,8 +377,8 @@ def start_session(self, request_id=0):
369
377
"""
370
378
request_args = proto .SessionArgs (ReqID = request_id )
371
379
return self .__fulfill_request (
372
- self .__client .StartSession ,
373
- request_args
380
+ request_method = self .__client .StartSession ,
381
+ request_args = request_args
374
382
)
375
383
376
384
def close_session (self , session_id , request_id = 0 ):
@@ -390,8 +398,8 @@ def close_session(self, session_id, request_id=0):
390
398
"""
391
399
request_args = proto .CloseSessionArgs (ReqID = request_id , SessionID = session_id )
392
400
return self .__fulfill_request (
393
- self .__client .CloseSession ,
394
- request_args
401
+ request_method = self .__client .CloseSession ,
402
+ request_args = request_args
395
403
)
396
404
397
405
def kill_session (self , session_id , session_id_to_kill , request_id = 0 ):
@@ -421,6 +429,6 @@ def kill_session(self, session_id, session_id_to_kill, request_id=0):
421
429
SessionIDToKill = session_id_to_kill
422
430
)
423
431
return self .__fulfill_request (
424
- self .__client .KillSession ,
425
- request_args
432
+ request_method = self .__client .KillSession ,
433
+ request_args = request_args
426
434
)
0 commit comments