Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 3 | # Copyright 2017 the original author or authors. |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 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 | # |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 17 | import structlog |
| 18 | from netconf.nc_rpc.rpc import Rpc |
| 19 | import netconf.nc_common.error as ncerror |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 20 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 21 | import dicttoxml |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 22 | |
| 23 | log = structlog.get_logger() |
| 24 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 25 | |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 26 | class Get(Rpc): |
Khen Nursimulu | c9ef7c1 | 2017-01-04 20:40:53 -0500 | [diff] [blame] | 27 | def __init__(self, request, request_xml, grpc_client, session, capabilities): |
| 28 | super(Get, self).__init__(request, request_xml, grpc_client, session) |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 29 | self._validate_parameters() |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 30 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 31 | @inlineCallbacks |
| 32 | def execute(self): |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 33 | if self.rpc_response.is_error: |
| 34 | returnValue(self.rpc_response) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 35 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 36 | log.info('get-request', session=self.session.session_id, |
| 37 | request=self.request) |
| 38 | |
| 39 | rpc = self.get_voltha_rpc(self.request) |
| 40 | if not rpc: |
| 41 | log.info('unsupported-request', request=self.request) |
| 42 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 43 | self.rpc_response.node = ncerror.NotImpl(self.request_xml) |
| 44 | returnValue(self.rpc_response) |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 45 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 46 | # Invoke voltha via the grpc client |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 47 | res_dict = yield self.grpc_client.invoke_voltha_api(rpc) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 48 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 49 | # convert dict to xml |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 50 | xml = dicttoxml.dicttoxml(res_dict, attr_type=True) |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 51 | log.info('voltha-info', res=res_dict, xml=xml) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 52 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 53 | root_elem = self.get_root_element(xml) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 54 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 55 | # Build the yang response |
| 56 | self.rpc_response.node = self.rpc_response.build_yang_response( |
| 57 | root_elem, self.request) |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 58 | self.rpc_response.is_error = False |
| 59 | |
| 60 | returnValue(self.rpc_response) |
| 61 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 62 | def _validate_parameters(self): |
| 63 | log.info('validate-parameters', session=self.session.session_id) |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 64 | # Validate the GET command |
| 65 | if self.request: |
| 66 | try: |
| 67 | if self.request['command'] != 'get': |
| 68 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 69 | self.rpc_response.node = ncerror.BadMsg(self.request_xml) |
| 70 | return |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 71 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 72 | if self.request.has_key('filter'): |
| 73 | if not self.request.has_key('class'): |
| 74 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 75 | self.rpc_response.node = ncerror.NotImpl(self.request_xml) |
| 76 | return |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 77 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 78 | except Exception as e: |
| 79 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 80 | self.rpc_response.node = ncerror.ServerException(self.request_xml) |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 81 | return |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 82 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 83 | def get_voltha_rpc(self, request): |
| 84 | if request.has_key('class'): |
| 85 | rpcs = self.rpc_request_mapping.get(request['class']) |
| 86 | if rpcs is None: |
| 87 | return None |
| 88 | for rpc in rpcs: |
| 89 | if request.has_key('subclass'): |
| 90 | # search first for subclass |
| 91 | if rpc['subclass'] and request['subclass'] == rpc[ |
| 92 | 'subclass']: |
| 93 | return rpc['rpc'] |
| 94 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 95 | # If the request has a subclass and is not in the list of |
| 96 | # supported rpc then return None |
| 97 | if request.has_key('subclass'): |
| 98 | return None |
| 99 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 100 | # If we are here then no subclass exists. Just return the rpc |
| 101 | # associated with theNone subclass |
| 102 | for rpc in rpcs: |
| 103 | if rpc['subclass'] is None: |
| 104 | return rpc['rpc'] |
| 105 | |
| 106 | return None |
| 107 | |
| 108 | # Supported Get Methods |
| 109 | rpc_request_mapping = { |
| 110 | 'Voltha': [ |
| 111 | {'subclass': None, |
| 112 | 'rpc': 'VolthaGlobalService-GetVoltha' |
| 113 | }], |
| 114 | 'VolthaInstance': [ |
| 115 | {'subclass': None, |
| 116 | 'rpc': 'VolthaLocalService-GetVolthaInstance' |
| 117 | }, |
| 118 | {'subclass': 'health', |
| 119 | 'rpc': 'VolthaLocalService-GetHealth' |
| 120 | }, |
| 121 | {'subclass': 'adapters', |
| 122 | 'rpc': 'VolthaLocalService-ListAdapters' |
| 123 | }, |
| 124 | {'subclass': 'logical_devices', |
| 125 | 'rpc': 'VolthaLocalService-ListLogicalDevices' |
| 126 | }, |
| 127 | {'subclass': 'devices', |
| 128 | 'rpc': 'VolthaLocalService-ListDevices' |
| 129 | }, |
| 130 | {'subclass': 'device_types', |
| 131 | 'rpc': 'VolthaLocalService-ListDeviceTypes' |
| 132 | }, |
| 133 | {'subclass': 'device_groups', |
| 134 | 'rpc': 'VolthaLocalService-ListDeviceGroups' |
| 135 | }, |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 136 | ] |
| 137 | # 'VolthaInstances': [ |
| 138 | # {'subclass': None, |
| 139 | # 'rpc': 'VolthaGlobalService-ListVolthaInstances' |
| 140 | # }], |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 141 | } |