Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 1 | #!/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 | # |
| 17 | from lxml import etree |
| 18 | import structlog |
| 19 | from netconf.nc_rpc.rpc import Rpc |
| 20 | import netconf.nc_common.error as ncerror |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 21 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 22 | |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 23 | |
| 24 | log = structlog.get_logger() |
| 25 | |
| 26 | |
| 27 | class GetSchemas(Rpc): |
| 28 | def __init__(self, request, request_xml, grpc_client, session, capabilities): |
| 29 | super(GetSchemas, self).__init__(request, request_xml, grpc_client, session) |
| 30 | self._validate_parameters() |
| 31 | self.capabilities = capabilities |
| 32 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 33 | @inlineCallbacks |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 34 | def execute(self): |
| 35 | if self.rpc_response.is_error: |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 36 | returnValue(self.rpc_response) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 37 | |
| 38 | log.info('get-schemas-request', session=self.session.session_id, |
| 39 | request=self.request) |
| 40 | |
| 41 | # Get the schema definitions |
| 42 | schema_defs = self.capabilities.get_yang_schemas_definitions() |
| 43 | log.info('schema-defs', definitions=schema_defs) |
| 44 | |
| 45 | # format the schemas in xml |
| 46 | top = etree.Element('yang') |
| 47 | for dict in schema_defs: |
| 48 | schema = etree.SubElement(top, 'schema') |
| 49 | node = etree.SubElement(schema, 'identifier') |
| 50 | node.text = dict['id'] |
| 51 | node = etree.SubElement(schema, 'version') |
| 52 | node.text = dict['version'] |
| 53 | node = etree.SubElement(schema, 'format') |
| 54 | node.text = dict['format'] |
| 55 | node = etree.SubElement(schema, 'namespace') |
| 56 | node.text = dict['namespace'] |
| 57 | node = etree.SubElement(schema, 'location') |
| 58 | node.text = dict['location'] |
| 59 | |
| 60 | # Build the yang response |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 61 | self.rpc_response.node = yield self.rpc_response.build_xml_response( |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 62 | self.request, top) |
| 63 | |
| 64 | self.rpc_response.is_error = False |
| 65 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 66 | returnValue(self.rpc_response) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 67 | |
| 68 | |
| 69 | def _validate_parameters(self): |
| 70 | log.info('validate-parameters', session=self.session.session_id) |
| 71 | # Validate the GET command |
| 72 | if self.request: |
| 73 | try: |
| 74 | if self.request['command'] != 'get-schemas': |
| 75 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 76 | self.rpc_response.node = ncerror.BadMsg(self.request_xml) |
| 77 | return |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 78 | |
| 79 | if self.request.has_key('filter'): |
| 80 | if not self.request.has_key('class'): |
| 81 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 82 | self.rpc_response.node = ncerror.BadMsg(self.request_xml) |
| 83 | return |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 84 | |
| 85 | except Exception as e: |
| 86 | self.rpc_response.is_error = True |
| 87 | self.rpc_response.node = ncerror.BadMsg(self.request) |
| 88 | return |
| 89 | |