blob: 094c8dffe051d7b5481e9f96b009998ae52f0796 [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
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050021from twisted.internet.defer import inlineCallbacks, returnValue
22
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050023
24log = structlog.get_logger()
25
26
27class GetSchemas(Rpc):
28 def __init__(self, request, request_xml, grpc_client, session, capabilities):
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050029 super(GetSchemas, self).__init__(request, request_xml, grpc_client,
30 session, capabilities)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050031 self._validate_parameters()
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050032
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050033 @inlineCallbacks
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050034 def execute(self):
35 if self.rpc_response.is_error:
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050036 returnValue(self.rpc_response)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050037
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 Nursimuluc7991dd2017-01-05 17:05:48 -050061 self.rpc_response.node = yield self.rpc_response.build_xml_response(
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050062 self.request, top)
63
64 self.rpc_response.is_error = False
65
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050066 returnValue(self.rpc_response)
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050067
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 Nursimuluc7991dd2017-01-05 17:05:48 -050076 self.rpc_response.node = ncerror.BadMsg(self.request_xml)
77 return
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050078
79 if self.request.has_key('filter'):
80 if not self.request.has_key('class'):
81 self.rpc_response.is_error = True
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050082 self.rpc_response.node = ncerror.BadMsg(self.request_xml)
83 return
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050084
85 except Exception as e:
86 self.rpc_response.is_error = True
87 self.rpc_response.node = ncerror.BadMsg(self.request)
88 return
89