blob: 19535f54021e00c995814d73198395661efe8f64 [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 Nursimulu8ffb8932017-01-26 13:40:49 -050027 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 Nursimulu7626ce12016-12-21 11:51:46 -050034 self._validate_parameters()
Khen Nursimulua7b842a2016-12-03 23:28:42 -050035
Khen Nursimulu7626ce12016-12-21 11:51:46 -050036 @inlineCallbacks
37 def execute(self):
Khen Nursimulu7626ce12016-12-21 11:51:46 -050038 if self.rpc_response.is_error:
39 returnValue(self.rpc_response)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050040
Khen Nursimulua4972742016-12-23 17:15:20 -050041 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 Nursimuluc7991dd2017-01-05 17:05:48 -050048 self.rpc_response.node = ncerror.NotImpl(self.request_xml)
49 returnValue(self.rpc_response)
Khen Nursimulua4972742016-12-23 17:15:20 -050050
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050051 # 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 Nursimulu3676b7c2017-01-31 13:48:38 -050066 res_dict, yang_options = yield self.grpc_client.invoke_voltha_rpc(
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050067 service=self.service,
68 method=self.method,
69 params=self.params,
70 metadata=self.metadata)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050071
Khen Nursimulu7626ce12016-12-21 11:51:46 -050072 # convert dict to xml
Khen Nursimulua4972742016-12-23 17:15:20 -050073 xml = dicttoxml.dicttoxml(res_dict, attr_type=True)
Khen Nursimulu7626ce12016-12-21 11:51:46 -050074 log.info('voltha-info', res=res_dict, xml=xml)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050075
Khen Nursimulu7626ce12016-12-21 11:51:46 -050076 root_elem = self.get_root_element(xml)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050077
Khen Nursimulua4972742016-12-23 17:15:20 -050078 # Build the yang response
79 self.rpc_response.node = self.rpc_response.build_yang_response(
Khen Nursimulu3676b7c2017-01-31 13:48:38 -050080 root_elem, self.request, yang_options=yang_options)
Khen Nursimulu7626ce12016-12-21 11:51:46 -050081 self.rpc_response.is_error = False
82
83 returnValue(self.rpc_response)
84
Khen Nursimulu7626ce12016-12-21 11:51:46 -050085 def _validate_parameters(self):
86 log.info('validate-parameters', session=self.session.session_id)
Khen Nursimulua4972742016-12-23 17:15:20 -050087 # Validate the GET command
88 if self.request:
89 try:
90 if self.request['command'] != 'get':
91 self.rpc_response.is_error = True
Khen Nursimuluc7991dd2017-01-05 17:05:48 -050092 self.rpc_response.node = ncerror.BadMsg(self.request_xml)
93 return
Khen Nursimulu7626ce12016-12-21 11:51:46 -050094
Khen Nursimulua4972742016-12-23 17:15:20 -050095 if self.request.has_key('filter'):
96 if not self.request.has_key('class'):
97 self.rpc_response.is_error = True
Khen Nursimulu8ffb8932017-01-26 13:40:49 -050098 self.rpc_response.node = ncerror.NotImpl(
99 self.request_xml)
Khen Nursimuluc7991dd2017-01-05 17:05:48 -0500100 return
Khen Nursimulu7626ce12016-12-21 11:51:46 -0500101
Khen Nursimulua4972742016-12-23 17:15:20 -0500102 except Exception as e:
103 self.rpc_response.is_error = True
Khen Nursimulu8ffb8932017-01-26 13:40:49 -0500104 self.rpc_response.node = ncerror.ServerException(
105 self.request_xml)
Khen Nursimulua4972742016-12-23 17:15:20 -0500106 return
Khen Nursimulua7b842a2016-12-03 23:28:42 -0500107
Khen Nursimulua4972742016-12-23 17:15:20 -0500108 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 Nursimuluc7991dd2017-01-05 17:05:48 -0500120 # 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 Nursimulua4972742016-12-23 17:15:20 -0500125 # 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 },
Khen Nursimulu3676b7c2017-01-31 13:48:38 -0500161 ],
162 'VolthaInstances': [
163 {'subclass': None,
164 'rpc': 'VolthaGlobalService-ListVolthaInstances'
165 }],
Khen Nursimulua4972742016-12-23 17:15:20 -0500166 }