blob: a90906eaabf4b15e670c13fc500cf868c4d43b8e [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
21from netconf.constants import Constants as C
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050022from netconf.nc_common.utils import qmap
23from twisted.internet.defer import inlineCallbacks, returnValue
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050024
25log = structlog.get_logger()
26
27class GetSchema(Rpc):
28 def __init__(self, request, request_xml, grpc_client, session, capabilities):
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050029 super(GetSchema, self).__init__(request, request_xml, grpc_client,
30 session, capabilities)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050031 # specific schema parsing required
32 self.parse_schema_request(request_xml)
33 self._validate_parameters()
34
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050035 @inlineCallbacks
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050036 def execute(self):
37 if self.rpc_response.is_error:
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050038 returnValue(self.rpc_response)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050039
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 Nursimuluc7991dd2017-01-05 17:05:48 -050045 content = yield self.capabilities.get_schema_content(self.request[
46 'identifier'])
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050047 if not content:
48 self.rpc_response.is_error = True
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050049 self.rpc_response.node = ncerror.BadMsg(self.request_xml)
50 returnValue(self.rpc_response)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050051
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050052 self.rpc_response.node = yield self.create_xml_response(content)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050053
54 self.rpc_response.is_error = False
55
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050056 returnValue(self.rpc_response)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050057
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 Nursimuluc7991dd2017-01-05 17:05:48 -050068 self.rpc_response.node = ncerror.BadMsg(self.request_xml)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050069 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 Nursimuluc7991dd2017-01-05 17:05:48 -050075 self.request_xml)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050076 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 Nursimuluc7991dd2017-01-05 17:05:48 -050083 self.rpc_response.node = ncerror.BadMsg(self.request_xml)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050084 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