blob: b2bb9fe2bf8365fa88d3f80fc5ad266314b80888 [file] [log] [blame]
Khen Nursimulua7b842a2016-12-03 23:28:42 -05001#!/usr/bin/env python
2#
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -05003# Copyright 2017 the original author or authors.
Khen Nursimulua7b842a2016-12-03 23:28:42 -05004#
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 Nursimulua7b842a2016-12-03 23:28:42 -050017import structlog
18from netconf.nc_rpc.rpc import Rpc
19import netconf.nc_common.error as ncerror
Khen Nursimulu7626ce12016-12-21 11:51:46 -050020from twisted.internet.defer import inlineCallbacks, returnValue
21import dicttoxml
Khen Nursimulua7b842a2016-12-03 23:28:42 -050022
23log = structlog.get_logger()
24
Khen Nursimulu7626ce12016-12-21 11:51:46 -050025
Khen Nursimulua7b842a2016-12-03 23:28:42 -050026class Get(Rpc):
Khen Nursimuluc9ef7c12017-01-04 20:40:53 -050027 def __init__(self, request, request_xml, grpc_client, session, capabilities):
28 super(Get, self).__init__(request, request_xml, grpc_client, session)
Khen Nursimulu7626ce12016-12-21 11:51:46 -050029 self._validate_parameters()
Khen Nursimulua7b842a2016-12-03 23:28:42 -050030
Khen Nursimulu7626ce12016-12-21 11:51:46 -050031 @inlineCallbacks
32 def execute(self):
Khen Nursimulu7626ce12016-12-21 11:51:46 -050033 if self.rpc_response.is_error:
34 returnValue(self.rpc_response)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050035
Khen Nursimulua4972742016-12-23 17:15:20 -050036 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 Nursimuluc7991dd2017-01-05 17:05:48 -050043 self.rpc_response.node = ncerror.NotImpl(self.request_xml)
44 returnValue(self.rpc_response)
Khen Nursimulua4972742016-12-23 17:15:20 -050045
Khen Nursimulu7626ce12016-12-21 11:51:46 -050046 # Invoke voltha via the grpc client
Khen Nursimulua4972742016-12-23 17:15:20 -050047 res_dict = yield self.grpc_client.invoke_voltha_api(rpc)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050048
Khen Nursimulu7626ce12016-12-21 11:51:46 -050049 # convert dict to xml
Khen Nursimulua4972742016-12-23 17:15:20 -050050 xml = dicttoxml.dicttoxml(res_dict, attr_type=True)
Khen Nursimulu7626ce12016-12-21 11:51:46 -050051 log.info('voltha-info', res=res_dict, xml=xml)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050052
Khen Nursimulu7626ce12016-12-21 11:51:46 -050053 root_elem = self.get_root_element(xml)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050054
Khen Nursimulua4972742016-12-23 17:15:20 -050055 # Build the yang response
56 self.rpc_response.node = self.rpc_response.build_yang_response(
57 root_elem, self.request)
Khen Nursimulu7626ce12016-12-21 11:51:46 -050058 self.rpc_response.is_error = False
59
60 returnValue(self.rpc_response)
61
Khen Nursimulu7626ce12016-12-21 11:51:46 -050062 def _validate_parameters(self):
63 log.info('validate-parameters', session=self.session.session_id)
Khen Nursimulua4972742016-12-23 17:15:20 -050064 # Validate the GET command
65 if self.request:
66 try:
67 if self.request['command'] != 'get':
68 self.rpc_response.is_error = True
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050069 self.rpc_response.node = ncerror.BadMsg(self.request_xml)
70 return
Khen Nursimulu7626ce12016-12-21 11:51:46 -050071
Khen Nursimulua4972742016-12-23 17:15:20 -050072 if self.request.has_key('filter'):
73 if not self.request.has_key('class'):
74 self.rpc_response.is_error = True
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050075 self.rpc_response.node = ncerror.NotImpl(self.request_xml)
76 return
Khen Nursimulu7626ce12016-12-21 11:51:46 -050077
Khen Nursimulua4972742016-12-23 17:15:20 -050078 except Exception as e:
79 self.rpc_response.is_error = True
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050080 self.rpc_response.node = ncerror.ServerException(self.request_xml)
Khen Nursimulua4972742016-12-23 17:15:20 -050081 return
Khen Nursimulua7b842a2016-12-03 23:28:42 -050082
Khen Nursimulua4972742016-12-23 17:15:20 -050083 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 Nursimuluc7991dd2017-01-05 17:05:48 -050095 # 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 Nursimulua4972742016-12-23 17:15:20 -0500100 # 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 Nursimuluc7991dd2017-01-05 17:05:48 -0500136 ]
137 # 'VolthaInstances': [
138 # {'subclass': None,
139 # 'rpc': 'VolthaGlobalService-ListVolthaInstances'
140 # }],
Khen Nursimulua4972742016-12-23 17:15:20 -0500141 }