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 | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 27 | def __init__(self, request, request_xml, grpc_client, session, |
| 28 | capabilities): |
| 29 | super(Get, self).__init__(request, request_xml, grpc_client, session, |
| 30 | capabilities) |
| 31 | self.service = None |
| 32 | self.method = None |
| 33 | self.metadata = None |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 34 | self._validate_parameters() |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 35 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 36 | @inlineCallbacks |
| 37 | def execute(self): |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 38 | if self.rpc_response.is_error: |
| 39 | returnValue(self.rpc_response) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 40 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 41 | log.info('get-request', session=self.session.session_id, |
| 42 | request=self.request) |
| 43 | |
| 44 | rpc = self.get_voltha_rpc(self.request) |
| 45 | if not rpc: |
| 46 | log.info('unsupported-request', request=self.request) |
| 47 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 48 | self.rpc_response.node = ncerror.NotImpl(self.request_xml) |
| 49 | returnValue(self.rpc_response) |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 50 | |
Khen Nursimulu | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 51 | # Extract the service and method name from the rpc |
| 52 | command = rpc.split('-') |
| 53 | if len(command) != 2: |
| 54 | log.debug('unsupported-rpc', rpc=rpc) |
| 55 | self.rpc_response.is_error = True |
| 56 | self.rpc_response.node = ncerror.NotImpl(self.request_xml) |
| 57 | returnValue(self.rpc_response) |
| 58 | |
| 59 | self.service = command[0] |
| 60 | self.method = command[1] |
| 61 | self.params = {} |
| 62 | if self.request.has_key('metadata'): |
| 63 | self.metadata = self.request['metadata'] |
| 64 | |
| 65 | # Execute the request |
Khen Nursimulu | 3676b7c | 2017-01-31 13:48:38 -0500 | [diff] [blame] | 66 | res_dict, yang_options = yield self.grpc_client.invoke_voltha_rpc( |
Khen Nursimulu | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 67 | service=self.service, |
| 68 | method=self.method, |
| 69 | params=self.params, |
| 70 | metadata=self.metadata) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 71 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 72 | # convert dict to xml |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 73 | xml = dicttoxml.dicttoxml(res_dict, attr_type=True) |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 74 | log.info('voltha-info', res=res_dict, xml=xml) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 75 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 76 | root_elem = self.get_root_element(xml) |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 77 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 78 | # Build the yang response |
| 79 | self.rpc_response.node = self.rpc_response.build_yang_response( |
Khen Nursimulu | 3676b7c | 2017-01-31 13:48:38 -0500 | [diff] [blame] | 80 | root_elem, self.request, yang_options=yang_options) |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 81 | self.rpc_response.is_error = False |
| 82 | |
| 83 | returnValue(self.rpc_response) |
| 84 | |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 85 | def _validate_parameters(self): |
| 86 | log.info('validate-parameters', session=self.session.session_id) |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 87 | # Validate the GET command |
| 88 | if self.request: |
| 89 | try: |
| 90 | if self.request['command'] != 'get': |
| 91 | self.rpc_response.is_error = True |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 92 | self.rpc_response.node = ncerror.BadMsg(self.request_xml) |
| 93 | return |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 94 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 95 | if self.request.has_key('filter'): |
| 96 | if not self.request.has_key('class'): |
| 97 | self.rpc_response.is_error = True |
Khen Nursimulu | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 98 | self.rpc_response.node = ncerror.NotImpl( |
| 99 | self.request_xml) |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 100 | return |
Khen Nursimulu | 7626ce1 | 2016-12-21 11:51:46 -0500 | [diff] [blame] | 101 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 102 | except Exception as e: |
| 103 | self.rpc_response.is_error = True |
Khen Nursimulu | 8ffb893 | 2017-01-26 13:40:49 -0500 | [diff] [blame] | 104 | self.rpc_response.node = ncerror.ServerException( |
| 105 | self.request_xml) |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 106 | return |
Khen Nursimulu | a7b842a | 2016-12-03 23:28:42 -0500 | [diff] [blame] | 107 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 108 | def get_voltha_rpc(self, request): |
| 109 | if request.has_key('class'): |
| 110 | rpcs = self.rpc_request_mapping.get(request['class']) |
| 111 | if rpcs is None: |
| 112 | return None |
| 113 | for rpc in rpcs: |
| 114 | if request.has_key('subclass'): |
| 115 | # search first for subclass |
| 116 | if rpc['subclass'] and request['subclass'] == rpc[ |
| 117 | 'subclass']: |
| 118 | return rpc['rpc'] |
| 119 | |
Khen Nursimulu | c7991dd | 2017-01-05 17:05:48 -0500 | [diff] [blame] | 120 | # If the request has a subclass and is not in the list of |
| 121 | # supported rpc then return None |
| 122 | if request.has_key('subclass'): |
| 123 | return None |
| 124 | |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 125 | # If we are here then no subclass exists. Just return the rpc |
| 126 | # associated with theNone subclass |
| 127 | for rpc in rpcs: |
| 128 | if rpc['subclass'] is None: |
| 129 | return rpc['rpc'] |
| 130 | |
| 131 | return None |
| 132 | |
| 133 | # Supported Get Methods |
| 134 | rpc_request_mapping = { |
| 135 | 'Voltha': [ |
| 136 | {'subclass': None, |
| 137 | 'rpc': 'VolthaGlobalService-GetVoltha' |
| 138 | }], |
| 139 | 'VolthaInstance': [ |
| 140 | {'subclass': None, |
| 141 | 'rpc': 'VolthaLocalService-GetVolthaInstance' |
| 142 | }, |
| 143 | {'subclass': 'health', |
| 144 | 'rpc': 'VolthaLocalService-GetHealth' |
| 145 | }, |
| 146 | {'subclass': 'adapters', |
| 147 | 'rpc': 'VolthaLocalService-ListAdapters' |
| 148 | }, |
| 149 | {'subclass': 'logical_devices', |
| 150 | 'rpc': 'VolthaLocalService-ListLogicalDevices' |
| 151 | }, |
| 152 | {'subclass': 'devices', |
| 153 | 'rpc': 'VolthaLocalService-ListDevices' |
| 154 | }, |
| 155 | {'subclass': 'device_types', |
| 156 | 'rpc': 'VolthaLocalService-ListDeviceTypes' |
| 157 | }, |
| 158 | {'subclass': 'device_groups', |
| 159 | 'rpc': 'VolthaLocalService-ListDeviceGroups' |
| 160 | }, |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 161 | {'subclass': 'channel_groups', |
| 162 | 'rpc': 'VolthaLocalService-GetAllChannelgroupConfig' |
| 163 | }, |
| 164 | {'subclass': 'channel_partitions', |
| 165 | 'rpc': 'VolthaLocalService-GetAllChannelpartitionConfig' |
| 166 | }, |
| 167 | {'subclass': 'channel_pairs', |
| 168 | 'rpc': 'VolthaLocalService-GetAllChannelpairConfig' |
| 169 | }, |
| 170 | {'subclass': 'channel_terminations', |
| 171 | 'rpc': 'VolthaLocalService-GetAllChannelterminationConfig' |
| 172 | }, |
| 173 | {'subclass': 'ont_anis', |
| 174 | 'rpc': 'VolthaLocalService-GetAllOntaniConfig' |
| 175 | }, |
| 176 | {'subclass': 'v_ont_anis', |
| 177 | 'rpc': 'VolthaLocalService-GetAllVOntaniConfig' |
| 178 | }, |
| 179 | {'subclass': 'v_enets', |
| 180 | 'rpc': 'VolthaLocalService-GetAllVEnetConfig' |
| 181 | }, |
Khen Nursimulu | 3676b7c | 2017-01-31 13:48:38 -0500 | [diff] [blame] | 182 | ], |
| 183 | 'VolthaInstances': [ |
| 184 | {'subclass': None, |
| 185 | 'rpc': 'VolthaGlobalService-ListVolthaInstances' |
| 186 | }], |
Khen Nursimulu | a497274 | 2016-12-23 17:15:20 -0500 | [diff] [blame] | 187 | } |