blob: 71449ad15d4f7bef98b0039ca01289f6f01c5c65 [file] [log] [blame]
Khen Nursimulua7b842a2016-12-03 23:28:42 -05001#!/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#
17from lxml import etree
18import structlog
19from netconf.nc_rpc.rpc import Rpc
20import netconf.nc_common.error as ncerror
21from netconf.constants import Constants as C
22from netconf.utils import filter_tag_match
Khen Nursimulu7626ce12016-12-21 11:51:46 -050023from twisted.internet.defer import inlineCallbacks, returnValue
24import dicttoxml
25from simplejson import dumps, load
Khen Nursimulua7b842a2016-12-03 23:28:42 -050026
27log = structlog.get_logger()
28
Khen Nursimulu7626ce12016-12-21 11:51:46 -050029
Khen Nursimulua7b842a2016-12-03 23:28:42 -050030class Get(Rpc):
Khen Nursimulua4972742016-12-23 17:15:20 -050031 def __init__(self, request, grpc_client, session):
32 super(Get, self).__init__(request, grpc_client, session)
Khen Nursimulu7626ce12016-12-21 11:51:46 -050033 self._validate_parameters()
Khen Nursimulua7b842a2016-12-03 23:28:42 -050034
Khen Nursimulu7626ce12016-12-21 11:51:46 -050035 @inlineCallbacks
36 def execute(self):
Khen Nursimulu7626ce12016-12-21 11:51:46 -050037 if self.rpc_response.is_error:
38 returnValue(self.rpc_response)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050039
Khen Nursimulua4972742016-12-23 17:15:20 -050040 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 Nursimulu7626ce12016-12-21 11:51:46 -050050 # Invoke voltha via the grpc client
Khen Nursimulua4972742016-12-23 17:15:20 -050051 res_dict = yield self.grpc_client.invoke_voltha_api(rpc)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050052
Khen Nursimulu7626ce12016-12-21 11:51:46 -050053 # convert dict to xml
Khen Nursimulua4972742016-12-23 17:15:20 -050054 xml = dicttoxml.dicttoxml(res_dict, attr_type=True)
Khen Nursimulu7626ce12016-12-21 11:51:46 -050055 log.info('voltha-info', res=res_dict, xml=xml)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050056
Khen Nursimulu7626ce12016-12-21 11:51:46 -050057 root_elem = self.get_root_element(xml)
Khen Nursimulua7b842a2016-12-03 23:28:42 -050058
Khen Nursimulua4972742016-12-23 17:15:20 -050059 # Build the yang response
60 self.rpc_response.node = self.rpc_response.build_yang_response(
61 root_elem, self.request)
Khen Nursimulu7626ce12016-12-21 11:51:46 -050062 self.rpc_response.is_error = False
63
64 returnValue(self.rpc_response)
65
Khen Nursimulu7626ce12016-12-21 11:51:46 -050066 def _validate_parameters(self):
67 log.info('validate-parameters', session=self.session.session_id)
Khen Nursimulua4972742016-12-23 17:15:20 -050068 # 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 Nursimulu7626ce12016-12-21 11:51:46 -050075
Khen Nursimulua4972742016-12-23 17:15:20 -050076 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 Nursimulu7626ce12016-12-21 11:51:46 -050081
Khen Nursimulua4972742016-12-23 17:15:20 -050082 except Exception as e:
83 self.rpc_response.is_error = True
84 self.rpc_response.node = ncerror.BadMsg(self.request)
85 return
Khen Nursimulua7b842a2016-12-03 23:28:42 -050086
Khen Nursimulua4972742016-12-23 17:15:20 -050087 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 }