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