blob: 7ec45559355987ee77a3e3d2f4153443ee6dd4ce [file] [log] [blame]
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -05001#!/usr/bin/env python
2#
3# Copyright 2017 the original author or authors.
4#
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
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
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.
16#
17from lxml import etree
18import structlog
19from netconf.nc_rpc.rpc import Rpc
20import netconf.nc_common.error as ncerror
21
22log = structlog.get_logger()
23
24
25class GetSchemas(Rpc):
26 def __init__(self, request, request_xml, grpc_client, session, capabilities):
27 super(GetSchemas, self).__init__(request, request_xml, grpc_client, session)
28 self._validate_parameters()
29 self.capabilities = capabilities
30
31 def execute(self):
32 if self.rpc_response.is_error:
33 return(self.rpc_response)
34
35 log.info('get-schemas-request', session=self.session.session_id,
36 request=self.request)
37
38 # Get the schema definitions
39 schema_defs = self.capabilities.get_yang_schemas_definitions()
40 log.info('schema-defs', definitions=schema_defs)
41
42 # format the schemas in xml
43 top = etree.Element('yang')
44 for dict in schema_defs:
45 schema = etree.SubElement(top, 'schema')
46 node = etree.SubElement(schema, 'identifier')
47 node.text = dict['id']
48 node = etree.SubElement(schema, 'version')
49 node.text = dict['version']
50 node = etree.SubElement(schema, 'format')
51 node.text = dict['format']
52 node = etree.SubElement(schema, 'namespace')
53 node.text = dict['namespace']
54 node = etree.SubElement(schema, 'location')
55 node.text = dict['location']
56
57 # Build the yang response
58 self.rpc_response.node = self.rpc_response.build_xml_response(
59 self.request, top)
60
61 self.rpc_response.is_error = False
62
63 return(self.rpc_response)
64
65
66 def _validate_parameters(self):
67 log.info('validate-parameters', session=self.session.session_id)
68 # Validate the GET command
69 if self.request:
70 try:
71 if self.request['command'] != 'get-schemas':
72 self.rpc_response.is_error = True
73 self.rpc_response.node = ncerror.BadMsg('Improperly '
74 'formatted get '
75 'schemas request')
76
77 if self.request.has_key('filter'):
78 if not self.request.has_key('class'):
79 self.rpc_response.is_error = True
80 self.rpc_response.node = ncerror.BadMsg(
81 'Missing filter sub-element')
82
83 except Exception as e:
84 self.rpc_response.is_error = True
85 self.rpc_response.node = ncerror.BadMsg(self.request)
86 return
87