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 |
| 21 | from netconf.constants import Constants as C |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 22 | from netconf.nc_common.utils import qmap |
| 23 | from twisted.internet.defer import inlineCallbacks, returnValue |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 24 | |
| 25 | log = structlog.get_logger() |
| 26 | |
| 27 | class GetSchema(Rpc): |
| 28 | def __init__(self, request, request_xml, grpc_client, session, capabilities): |
Khen Nursimulu | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 29 | super(GetSchema, self).__init__(request, request_xml, grpc_client, |
| 30 | session, capabilities) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 31 | # specific schema parsing required |
| 32 | self.parse_schema_request(request_xml) |
| 33 | self._validate_parameters() |
| 34 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 35 | @inlineCallbacks |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 36 | def execute(self): |
| 37 | if self.rpc_response.is_error: |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 38 | returnValue(self.rpc_response) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 39 | |
| 40 | log.info('get-schema-request', session=self.session.session_id, |
| 41 | request=self.request) |
| 42 | |
| 43 | # Get the yang schema content |
| 44 | # TODO: Use version as well |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 45 | content = yield self.capabilities.get_schema_content(self.request[ |
| 46 | 'identifier']) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 47 | if not content: |
| 48 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 49 | self.rpc_response.node = ncerror.BadMsg(self.request_xml) |
| 50 | returnValue(self.rpc_response) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 51 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 52 | self.rpc_response.node = yield self.create_xml_response(content) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 53 | |
| 54 | self.rpc_response.is_error = False |
| 55 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 56 | returnValue(self.rpc_response) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 57 | |
| 58 | def _validate_parameters(self): |
| 59 | log.info('validate-parameters', session=self.session.session_id) |
| 60 | # Validate the GET command |
| 61 | if self.request: |
| 62 | try: |
| 63 | if self.request['command'] != 'get-schema' or \ |
| 64 | not self.request.has_key('identifier') or \ |
| 65 | not self.request.has_key('format') or \ |
| 66 | not self.request.has_key('version'): |
| 67 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 68 | self.rpc_response.node = ncerror.BadMsg(self.request_xml) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 69 | return |
| 70 | |
| 71 | if self.request.has_key('filter'): |
| 72 | if not self.request.has_key('class'): |
| 73 | self.rpc_response.is_error = True |
| 74 | self.rpc_response.node = ncerror.BadMsg( |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 75 | self.request_xml) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 76 | return |
| 77 | |
| 78 | # Verify that the requested schema exists |
| 79 | if not self.capabilities.is_schema_supported(self.request[ |
| 80 | 'identifier']) \ |
| 81 | or self.request['format'] != 'yang' : |
| 82 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 83 | self.rpc_response.node = ncerror.BadMsg(self.request_xml) |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 84 | return |
| 85 | |
| 86 | except Exception as e: |
| 87 | self.rpc_response.is_error = True |
| 88 | self.rpc_response.node = ncerror.BadMsg(self.request) |
| 89 | return |
| 90 | |
| 91 | # Parse context-specific parameters |
| 92 | def parse_schema_request(self, node): |
| 93 | if not len(node): |
| 94 | return |
| 95 | schema_node = node.find(''.join([qmap(C.NCM), 'get-schema'])) |
| 96 | if schema_node is not None: |
| 97 | for item in ['identifier', 'version', 'format']: |
| 98 | elem = schema_node.find(''.join([qmap(C.NCM), item])) |
| 99 | if elem is not None: |
| 100 | self.request[item] = elem.text |
| 101 | |
| 102 | def create_xml_response(self, content): |
| 103 | ns = {} |
| 104 | ns['xmlns'] = "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring" |
| 105 | |
| 106 | elem = etree.Element('data', attrib=ns) |
| 107 | elem.text = unicode(content, "utf-8") |
| 108 | return elem |