RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
mysql_wrapper.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #Copyright 2015 RAPP
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 # Author: Athanassios Kintsakis
18 # contact: akintsakis@issel.ee.auth.gr
19 
20 import rospy
21 import MySQLdb as mdb
22 import sys
23 
24 from rapp_platform_ros_communications.srv import (
25  addStoreTokenToDeviceSrv,
26  addStoreTokenToDeviceSrvResponse,
27  getUserOntologyAliasSrv,
28  getUserOntologyAliasSrvRequest,
29  getUserOntologyAliasSrvResponse,
30  registerUserOntologyAliasSrv,
31  registerUserOntologyAliasSrvRequest,
32  registerUserOntologyAliasSrvResponse,
33  checkIfUserExistsSrv,
34  checkIfUserExistsSrvResponse,
35  checkIfUserExistsSrvRequest,
36  getUserLanguageSrv,
37  getUserLanguageSrvRequest,
38  getUserLanguageSrvResponse,
39  getUserPasswordSrv,
40  getUserPasswordSrvResponse,
41  getUserPasswordSrvRequest,
42  getUsernameAssociatedWithApplicationTokenSrv,
43  getUsernameAssociatedWithApplicationTokenSrvResponse,
44  getUsernameAssociatedWithApplicationTokenSrvRequest,
45  createNewPlatformUserSrv,
46  createNewPlatformUserSrvResponse,
47  createNewPlatformUserSrvRequest,
48  createNewApplicationTokenSrv,
49  createNewApplicationTokenSrvRequest,
50  createNewApplicationTokenSrvResponse,
51  checkActiveRobotSessionSrv,
52  checkActiveRobotSessionSrvRequest,
53  checkActiveRobotSessionSrvResponse,
54  checkActiveApplicationTokenSrv,
55  checkActiveApplicationTokenSrvRequest,
56  checkActiveApplicationTokenSrvResponse,
57  validateUserRoleSrv,
58  validateUserRoleSrvResponse,
59  validateUserRoleSrvRequest,
60  validateExistingPlatformDeviceTokenSrv,
61  validateExistingPlatformDeviceTokenSrvResponse,
62  validateExistingPlatformDeviceTokenSrvRequest,
63  removePlatformUserSrv,
64  removePlatformUserSrvRequest,
65  removePlatformUserSrvResponse,
66  createNewCloudAgentServiceSrv,
67  createNewCloudAgentServiceSrvRequest,
68  createNewCloudAgentServiceSrvResponse,
69  createNewCloudAgentSrv,
70  createNewCloudAgentSrvRequest,
71  createNewCloudAgentSrvResponse,
72  getCloudAgentServiceTypeAndHostPortSrv,
73  getCloudAgentServiceTypeAndHostPortSrvRequest,
74  getCloudAgentServiceTypeAndHostPortSrvResponse
75  )
76 
77 ## @class MySQLdbWrapper
78 # @brief The mysql wrapper ros node
80 
81  ## @brief Default contructor
82  #
83  # Declares the callbacks of the node's services
84  def __init__(self):
85 
86  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_get_user_ontology_alias_service_topic")
87  if(not self.serv_topic):
88  rospy.logerror("rapp_mysql_wrapper_get_user_ontology_alias_service_topic Not found error")
89  self.serv=rospy.Service(self.serv_topic, getUserOntologyAliasSrv, self.getUserOntologyAliasDataHandler)
90 
91  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_register_user_ontology_alias_service_topic")
92  if(not self.serv_topic):
93  rospy.logerror("rapp_mysql_wrapper_register_user_ontology_alias_service_topic Not found error")
94  self.serv=rospy.Service(self.serv_topic, registerUserOntologyAliasSrv, self.registerUserOntologyAliasDataHandler)
95 
96  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_get_user_language_service_topic")
97  if(not self.serv_topic):
98  rospy.logerror("rapp_mysql_wrapper_get_user_language_service_topic Not found error")
99  self.serv=rospy.Service(self.serv_topic, getUserLanguageSrv, self.getUserLanguageDataHandler)
100 
101  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_check_if_user_exists_service_topic")
102  if(not self.serv_topic):
103  rospy.logerror("rapp_mysql_wrapper_check_if_user_exists_service_topic Not found error")
104  self.serv=rospy.Service(self.serv_topic, checkIfUserExistsSrv, self.checkIfUserExistsDataHandler)
105 
106  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_get_user_password_service_topic")
107  if(not self.serv_topic):
108  rospy.logerror("rapp_mysql_wrapper_get_user_password_service_topic Not found error")
109  self.serv=rospy.Service(self.serv_topic, getUserPasswordSrv, self.getUserPasswordDataHandler)
110 
111  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_get_username_associated_with_application_token_service_topic")
112  if(not self.serv_topic):
113  rospy.logerror("rapp_mysql_wrapper_get_username_associated_with_application_token_service_topic Not found error")
114  self.serv=rospy.Service(self.serv_topic, getUsernameAssociatedWithApplicationTokenSrv, self.getUsernameAssociatedWithApplicationTokenDataHandler)
115 
116  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_create_new_platform_user_service_topic")
117  if(not self.serv_topic):
118  rospy.logerror("rapp_mysql_wrapper_create_new_platform_user_service_topic Not found error")
119  self.serv=rospy.Service(self.serv_topic, createNewPlatformUserSrv, self.createNewPlatformUserDataHandler)
120 
121  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_create_new_application_token_service_topic")
122  if(not self.serv_topic):
123  rospy.logerror("rapp_mysql_wrapper_create_new_application_token_service_topic Not found error")
124  self.serv=rospy.Service(self.serv_topic, createNewApplicationTokenSrv, self.createNewApplicationTokenDataHandler)
125 
126  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_validate_existing_platform_device_token_topic")
127  if(not self.serv_topic):
128  rospy.logerror("rapp_mysql_wrapper_validate_existing_platform_device_token_topic Not found error")
129  self.serv=rospy.Service(self.serv_topic, validateExistingPlatformDeviceTokenSrv, self.validateExistingPlatformDeviceTokenDataHandler)
130 
131  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_check_active_application_token_service_topic")
132  if(not self.serv_topic):
133  rospy.logerror("rapp_mysql_wrapper_check_active_application_token_service_topic Not found error")
134  self.serv=rospy.Service(self.serv_topic, checkActiveApplicationTokenSrv, self.checkActiveApplicationTokenDataHandler)
135 
136  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_remove_platform_user_service_topic")
137  if(not self.serv_topic):
138  rospy.logerror("rapp_mysql_wrapper_remove_platform_user_service_topic Not found error")
139  self.serv=rospy.Service(self.serv_topic, removePlatformUserSrv, self.removePlatformUserDataHandler)
140 
141 
142  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_check_active_robot_session_service_topic")
143  if(not self.serv_topic):
144  rospy.logerror("rapp_mysql_wrapper_check_active_robot_session_service_topic Not found error")
145  self.serv=rospy.Service(self.serv_topic, checkActiveRobotSessionSrv, self.checkActiveRobotSessionDataHandler)
146 
147  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_add_store_token_to_device_topic")
148  if(not self.serv_topic):
149  rospy.logerror("rapp_mysql_wrapper_add_store_token_to_device_topic Not found error")
150  self.serv=rospy.Service(self.serv_topic, addStoreTokenToDeviceSrv, self.addStoreTokenToDeviceDataHandler)
151 
152  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_validate_user_role_topic")
153  if(not self.serv_topic):
154  rospy.logerror("rapp_mysql_wrapper_validate_user_role_topic Not found error")
155  self.serv=rospy.Service(self.serv_topic, validateUserRoleSrv, self.validateUserRoleDataHandler)
156 
157  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_create_new_cloud_agent_topic")
158  if(not self.serv_topic):
159  rospy.logerror("rapp_mysql_wrapper_create_new_cloud_agent_topic Not found error")
160  self.serv=rospy.Service(self.serv_topic, createNewCloudAgentSrv, self.createNewCloudAgentDataHandler)
161 
162  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_create_new_cloud_agent_service_topic")
163  if(not self.serv_topic):
164  rospy.logerror("rapp_mysql_wrapper_create_new_cloud_agent_service_topic Not found error")
165  self.serv=rospy.Service(self.serv_topic, createNewCloudAgentServiceSrv, self.createNewCloudAgentServiceDataHandler)
166 
167  self.serv_topic = rospy.get_param("rapp_mysql_wrapper_get_cloud_agent_service_type_and_host_port_topic")
168  if(not self.serv_topic):
169  rospy.logerror("rapp_mysql_wrapper_get_cloud_agent_service_type_and_host_port_topic Not found error")
170  self.serv=rospy.Service(self.serv_topic, getCloudAgentServiceTypeAndHostPortSrv, self.getCloudAgentServiceTypeAndHostPortDataHandler)
171 
172  ## @brief Implements the getUserOntologyAlias service main function
173  # @param req [rapp_platform_ros_communications::getUserOntologyAliasSrvRequest::Request&] The ROS service request
174  #
175  # @return res [rapp_platform_ros_communications::getUserOntologyAliasSrvResponse::Response&] The ROS service response
176  # @exception Exception IndexError
177  # @exception Exception IOError
178  # @exception Exception mdbError
179  def getUserOntologyAlias(self,req):
180  try:
181  res = getUserOntologyAliasSrvResponse()
182  db_username,db_password=self.getLogin()
183  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
184  cur = con.cursor()
185  cur.execute("select ontology_alias from platform_user where username=%s",(req.username))
186  result_set = cur.fetchall()
187  #print result_set
188  if(result_set and len(result_set[0])>0):
189  res.ontology_alias=str(result_set[0][0])
190  res.success=True
191  con.close()
192  except mdb.Error, e:
193  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
194  res.success=False
195  res.error="Error %d: %s" % (e.args[0],e.args[1])
196  con.close()
197  except IndexError, e:
198  res.trace.append("IndexError: " +str(e))
199  res.success=False
200  res.error="IndexError: " +str(e)
201  con.close()
202  except IOError, e:
203  res.success=False
204  res.trace.append("IOError: " +str(e))
205  res.error="IOError: " +str(e)
206  con.close()
207  return res
208 
209  ## @brief Implements the registerUserOntologyAlias service main function
210  # @param req [rapp_platform_ros_communications::registerUserOntologyAliasSrvRequest::Request&] The ROS service request
211  #
212  # @return res [rapp_platform_ros_communications::registerUserOntologyAliasSrvResponse::Response&] The ROS service response
213  # @exception Exception IndexError
214  # @exception Exception IOError
215  # @exception Exception mdbError
217  try:
218  res = registerUserOntologyAliasSrvResponse()
219  db_username,db_password=self.getLogin()
220  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
221  cur = con.cursor()
222  cur.execute("LOCK TABLES platform_user WRITE")
223  cur.execute("update platform_user set ontology_alias=%s where username=%s",(req.ontology_alias,req.username))
224  cur.execute("UNLOCK TABLES")
225  result_set = cur.fetchall()
226  res.success=True
227  con.close()
228  except mdb.Error, e:
229  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
230  res.success=False
231  res.error="Error %d: %s" % (e.args[0],e.args[1])
232  con.close()
233  except IndexError, e:
234  res.trace.append("IndexError: " +str(e))
235  res.success=False
236  res.error="IndexError: " +str(e)
237  con.close()
238  except IOError, e:
239  res.success=False
240  res.trace.append("IOError: " +str(e))
241  res.error="IOError: " +str(e)
242  con.close()
243  return res
244 
245  ## @brief Implements the getUserLanguage service main function
246  # @param req [rapp_platform_ros_communications::getUserLanguageSrvRequest::Request&] The ROS service request
247  #
248  # @return res [rapp_platform_ros_communications::getUserLanguageSrvResponse::Response&] The ROS service response
249  # @exception Exception IndexError
250  # @exception Exception IOError
251  # @exception Exception mdbError
252  def getUserLanguage(self,req):
253  try:
254  res = getUserLanguageSrvResponse()
255  db_username,db_password=self.getLogin()
256  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
257  cur = con.cursor()
258  cur.execute("select name from language where id=(select language_id from platform_user where username=%s)",(req.username))
259  result_set = cur.fetchall()
260  if(result_set and len(result_set[0])>0):
261  res.user_language=result_set[0][0]
262  res.success=True
263  else:
264  res.success=False
265  res.error="User language is NULL"
266  con.close()
267  except mdb.Error, e:
268  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
269  res.success=False
270  res.error="Error %d: %s" % (e.args[0],e.args[1])
271  con.close()
272  except IndexError, e:
273  res.trace.append("IndexError: " +str(e))
274  res.success=False
275  res.error="IndexError: " +str(e)
276  con.close()
277  except IOError, e:
278  res.success=False
279  res.trace.append("IOError: " +str(e))
280  res.error="IOError: " +str(e)
281  con.close()
282  return res
283 
284  ## @brief Implements the checkIfUserExists service main function
285  # @param req [rapp_platform_ros_communications::checkIfUserExistsSrvRequest::Request&] The ROS service request
286  #
287  # @return res [rapp_platform_ros_communications::checkIfUserExistsSrvResponse::Response&] The ROS service response
288  # @exception Exception IndexError
289  # @exception Exception IOError
290  # @exception Exception mdbError
291  def checkIfUserExists(self,req):
292  try:
293  res = checkIfUserExistsSrvResponse()
294  db_username,db_password=self.getLogin()
295  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
296  cur = con.cursor()
297  cur.execute("select username from platform_user where username=%s",(req.username))
298  result_set = cur.fetchall()
299  res.user_exists=False
300  if(result_set and len(result_set[0])>0):
301  res.user_exists=True
302  res.success=True
303  con.close()
304  except mdb.Error, e:
305  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
306  res.success=False
307  res.error="Error %d: %s" % (e.args[0],e.args[1])
308  con.close()
309  except IndexError, e:
310  res.trace.append("IndexError: " +str(e))
311  res.success=False
312  res.error="IndexError: " +str(e)
313  con.close()
314  except IOError, e:
315  res.success=False
316  res.trace.append("IOError: " +str(e))
317  res.error="IOError: " +str(e)
318  con.close()
319  return res
320 
321  ## @brief Implements the getUserPassword service main function
322  # @param req [rapp_platform_ros_communications::getUserPasswordSrvRequest::Request&] The ROS service request
323  #
324  # @return res [rapp_platform_ros_communications::getUserPasswordSrvResponse::Response&] The ROS service response
325  # @exception Exception IndexError
326  # @exception Exception IOError
327  # @exception Exception mdbError
328  def getUserPassword(self,req):
329  try:
330  res = getUserPasswordSrvResponse()
331  db_username,db_password=self.getLogin()
332  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
333  cur = con.cursor()
334  cur.execute("select password from platform_user where username=%s",(req.username))
335  result_set = cur.fetchall()
336  res.success=False
337  if(result_set and len(result_set[0])>0):
338  res.success=True
339  res.password=result_set[0][0]
340  con.close()
341  except mdb.Error, e:
342  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
343  res.success=False
344  res.error="Error %d: %s" % (e.args[0],e.args[1])
345  con.close()
346  except IndexError, e:
347  res.trace.append("IndexError: " +str(e))
348  res.success=False
349  res.error="IndexError: " +str(e)
350  con.close()
351  except IOError, e:
352  res.success=False
353  res.trace.append("IOError: " +str(e))
354  res.error="IOError: " +str(e)
355  con.close()
356  return res
357 
358  ## @brief Implements the getUsernameAssociatedWithApplicationToken service main function
359  # @param req [rapp_platform_ros_communications::getUsernameAssociatedWithApplicationTokenSrvRequest::Request&] The ROS service request
360  #
361  # @return res [rapp_platform_ros_communications::getUsernameAssociatedWithApplicationTokenSrvResponse::Response&] The ROS service response
362  # @exception Exception IndexError
363  # @exception Exception IOError
364  # @exception Exception mdbError
366  try:
367  res = getUsernameAssociatedWithApplicationTokenSrvResponse()
368  db_username,db_password=self.getLogin()
369  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
370  cur = con.cursor()
371  cur.execute("select username from platform_user where id=(select platform_user_id from application_token where token=%s)",(req.application_token))
372  result_set = cur.fetchall()
373  if(result_set and len(result_set[0])>0):
374  res.success=True
375  res.username=result_set[0][0]
376  res.success=True
377  con.close()
378  except mdb.Error, e:
379  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
380  res.success=False
381  res.error="Error %d: %s" % (e.args[0],e.args[1])
382  con.close()
383  except IndexError, e:
384  res.trace.append("IndexError: " +str(e))
385  res.success=False
386  res.error="IndexError: " +str(e)
387  con.close()
388  except IOError, e:
389  res.success=False
390  res.trace.append("IOError: " +str(e))
391  res.error="IOError: " +str(e)
392  con.close()
393  return res
394 
395  ## @brief Implements the createNewPlatformUser service main function
396  # @param req [rapp_platform_ros_communications::createNewPlatformUserSrvRequest::Request&] The ROS service request
397  #
398  # @return res [rapp_platform_ros_communications::createNewPlatformUserSrvResponse::Response&] The ROS service response
399  # @exception Exception IndexError
400  # @exception Exception IOError
401  # @exception Exception mdbError
402  def createNewPlatformUser(self,req):
403  try:
404  res = createNewPlatformUserSrvResponse()
405  db_username,db_password=self.getLogin()
406  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
407  cur = con.cursor()
408  cur.execute("LOCK TABLES platform_user WRITE, platform_user as p2 READ, language READ")
409  cur.execute("insert into `platform_user` (`username`,`password`,`language_id`,`creator_id`) values (%s, %s, (select `id` from `language` where `name`=%s), (select `id` from platform_user as p2 where `username`=%s))",(req.username,req.password,req.language,req.creator_username))
410  cur.execute("UNLOCK TABLES")
411  res.success=True
412  con.close()
413  except mdb.Error, e:
414  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
415  res.success=False
416  res.error="Error %d: %s" % (e.args[0],e.args[1])
417  con.close()
418  except IndexError, e:
419  res.trace.append("IndexError: " +str(e))
420  res.success=False
421  res.error="IndexError: " +str(e)
422  con.close()
423  except IOError, e:
424  res.success=False
425  res.trace.append("IOError: " +str(e))
426  res.error="IOError: " +str(e)
427  con.close()
428  return res
429 
430  ## @brief Implements the removePlatformUser service main function
431  # @param req [rapp_platform_ros_communications::removePlatformUserSrvRequest::Request&] The ROS service request
432  #
433  # @return res [rapp_platform_ros_communications::removePlatformUserSrvResponse::Response&] The ROS service response
434  # @exception Exception IndexError
435  # @exception Exception IOError
436  # @exception Exception mdbError
437  def removePlatformUser(self,req):
438  try:
439  res = removePlatformUserSrvResponse()
440  db_username,db_password=self.getLogin()
441  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
442  cur = con.cursor()
443  cur.execute("LOCK TABLES platform_user WRITE")
444  cur.execute("delete from platform_user where username=%s",(req.username))
445  cur.execute("UNLOCK TABLES")
446  res.success=True
447  con.close()
448  except mdb.Error, e:
449  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
450  res.success=False
451  res.error="Error %d: %s" % (e.args[0],e.args[1])
452  con.close()
453  except IndexError, e:
454  res.trace.append("IndexError: " +str(e))
455  res.success=False
456  res.error="IndexError: " +str(e)
457  con.close()
458  except IOError, e:
459  res.success=False
460  res.trace.append("IOError: " +str(e))
461  res.error="IOError: " +str(e)
462  con.close()
463  return res
464 
465  ## @brief Implements the createNewApplicationToken service main function
466  # @param req [rapp_platform_ros_communications::createNewApplicationTokenSrvRequest::Request&] The ROS service request
467  #
468  # @return res [rapp_platform_ros_communications::createNewApplicationTokenSrvResponse::Response&] The ROS service response
469  # @exception Exception IndexError
470  # @exception Exception IOError
471  # @exception Exception mdbError
473  try:
474  res = createNewApplicationTokenSrvResponse()
475  db_username,db_password=self.getLogin()
476  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
477  cur = con.cursor()
478  cur.execute("LOCK TABLES application_token WRITE, platform_user READ, device READ")
479  cur.execute("insert into application_token (token,platform_user_id,device_id) VALUES (%s,(select id from platform_user where username=%s), (select id from device where token=%s))",(req.application_token,req.username,req.store_token))
480  cur.execute("UNLOCK TABLES")
481  res.success=True
482  con.close()
483  except mdb.Error, e:
484  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
485  res.success=False
486  res.error="Error %d: %s" % (e.args[0],e.args[1])
487  con.close()
488  except IndexError, e:
489  res.trace.append("IndexError: " +str(e))
490  res.success=False
491  res.error="IndexError: " +str(e)
492  con.close()
493  except IOError, e:
494  res.success=False
495  res.trace.append("IOError: " +str(e))
496  res.error="IOError: " +str(e)
497  con.close()
498  return res
499 
500  ## @brief Implements the addStoreTokenToDevice service main function
501  # @param req [rapp_platform_ros_communications::addStoreTokenToDeviceSrvRequest::Request&] The ROS service request
502  #
503  # @return res [rapp_platform_ros_communications::addStoreTokenToDeviceSrvResponse::Response&] The ROS service response
504  # @exception Exception IndexError
505  # @exception Exception IOError
506  # @exception Exception mdbError
507  def addStoreTokenToDevice(self,req):
508  try:
509  res = addStoreTokenToDeviceSrvResponse()
510  db_username,db_password=self.getLogin()
511  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
512  cur = con.cursor()
513  cur.execute("LOCK TABLES device WRITE")
514  cur.execute("insert into device (token, description) values (%s,'rapp store device') on duplicate key update token=token",(req.store_token))
515  cur.execute("UNLOCK TABLES")
516  result_set = cur.fetchall()
517  res.error = ''
518  con.close()
519  except mdb.Error, e:
520  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
521  res.success=False
522  res.error="Error %d: %s" % (e.args[0],e.args[1])
523  con.close()
524  except IndexError, e:
525  res.trace.append("IndexError: " +str(e))
526  res.success=False
527  res.error="IndexError: " +str(e)
528  con.close()
529  except IOError, e:
530  res.success=False
531  res.trace.append("IOError: " +str(e))
532  res.error="IOError: " +str(e)
533  con.close()
534  return res
535 
536  ## @brief Implements the validateUserRole service main function
537  # @param req [rapp_platform_ros_communications::validateUserRoleSrvRequest::Request&] The ROS service request
538  #
539  # @return res [rapp_platform_ros_communications::validateUserRoleSrvResponse::Response&] The ROS service response
540  # @exception Exception IndexError
541  # @exception Exception IOError
542  # @exception Exception mdbError
543  def validateUserRole(self,req):
544  try:
545  res = validateUserRoleSrvResponse()
546  db_username,db_password=self.getLogin()
547  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
548  cur = con.cursor()
549  cur.execute("select username from platform_user where username=%s and role <= 10",(req.username))
550  result_set = cur.fetchall()
551  res.error = 'Invalid role'
552  if(result_set and len(result_set[0])>0):
553  res.error = ''
554  con.close()
555  except mdb.Error, e:
556  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
557  res.error="Error %d: %s" % (e.args[0],e.args[1])
558  con.close()
559  except IndexError, e:
560  res.trace.append("IndexError: " +str(e))
561  res.error="IndexError: " +str(e)
562  con.close()
563  except IOError, e:
564  res.trace.append("IOError: " +str(e))
565  res.error="IOError: " +str(e)
566  con.close()
567  return res
568 
569  ## @brief Implements the checkActiveRobotSession service main function
570  # @param req [rapp_platform_ros_communications::checkActiveRobotSessionSrvRequest::Request&] The ROS service request
571  #
572  # @return res [rapp_platform_ros_communications::checkActiveRobotSessionSrvResponse::Response&] The ROS service response
573  # @exception Exception IndexError
574  # @exception Exception IOError
575  # @exception Exception mdbError
576  def checkActiveRobotSession(self,req):
577  try:
578  res = checkActiveRobotSessionSrvResponse()
579  db_username,db_password=self.getLogin()
580  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
581  cur = con.cursor()
582  cur.execute("select token from application_token where platform_user_id=(select id from platform_user where username=%s) and status=1 and device_id=(select id from device where token=%s)",(req.username,req.device_token))
583  result_set = cur.fetchall()
584  res.application_token_exists=False
585  if(result_set and len(result_set[0])>0):
586  res.application_token_exists=True
587  res.success=True
588  con.close()
589  except mdb.Error, e:
590  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
591  res.success=False
592  res.error="Error %d: %s" % (e.args[0],e.args[1])
593  con.close()
594  except IndexError, e:
595  res.trace.append("IndexError: " +str(e))
596  res.success=False
597  res.error="IndexError: " +str(e)
598  con.close()
599  except IOError, e:
600  res.success=False
601  res.trace.append("IOError: " +str(e))
602  res.error="IOError: " +str(e)
603  con.close()
604  return res
605 
606  ## @brief Implements the checkActiveRobotSession service main function
607  # @param req [rapp_platform_ros_communications::checkActiveRobotSessionSrvRequest::Request&] The ROS service request
608  #
609  # @return res [rapp_platform_ros_communications::checkActiveRobotSessionSrvResponse::Response&] The ROS service response
610  # @exception Exception IndexError
611  # @exception Exception IOError
612  # @exception Exception mdbError
614  try:
615  res = checkActiveApplicationTokenSrvResponse()
616  db_username,db_password=self.getLogin()
617  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
618  cur = con.cursor()
619  cur.execute("select token from application_token where token=%s and status=1",(req.application_token))
620  result_set = cur.fetchall()
621  res.application_token_exists=False
622  if(result_set and len(result_set[0])>0):
623  res.application_token_exists=True
624  res.success=True
625  con.close()
626  except mdb.Error, e:
627  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
628  res.success=False
629  res.error="Error %d: %s" % (e.args[0],e.args[1])
630  con.close()
631  except IndexError, e:
632  res.trace.append("IndexError: " +str(e))
633  res.success=False
634  res.error="IndexError: " +str(e)
635  con.close()
636  except IOError, e:
637  res.success=False
638  res.trace.append("IOError: " +str(e))
639  res.error="IOError: " +str(e)
640  con.close()
641  return res
642 
643  ## @brief Implements the validateExistingPlatformDeviceToken service main function
644  # @param req [rapp_platform_ros_communications::validateExistingPlatformDeviceTokenSrvRequest::Request&] The ROS service request
645  #
646  # @return res [rapp_platform_ros_communications::validateExistingPlatformDeviceTokenSrvResponse::Response&] The ROS service response
647  # @exception Exception IndexError
648  # @exception Exception IOError
649  # @exception Exception mdbError
651  try:
652  res = validateExistingPlatformDeviceTokenSrvResponse()
653  db_username,db_password=self.getLogin()
654  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
655  cur = con.cursor()
656  cur.execute("select token from device where token=%s and status=1",(req.device_token))
657  result_set = cur.fetchall()
658  res.device_token_exists=False
659  res.success=False
660  if(result_set and len(result_set[0])>0):
661  res.device_token_exists=True
662  res.success=True
663  con.close()
664  except mdb.Error, e:
665  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
666  res.success=False
667  res.error="Error %d: %s" % (e.args[0],e.args[1])
668  con.close()
669  except IndexError, e:
670  res.trace.append("IndexError: " +str(e))
671  res.success=False
672  res.error="IndexError: " +str(e)
673  con.close()
674  except IOError, e:
675  res.success=False
676  res.trace.append("IOError: " +str(e))
677  res.error="IOError: " +str(e)
678  con.close()
679  return res
680 
681  ## @brief Implements the createNewCloudAgent service main function
682  # @param req [rapp_platform_ros_communications::createNewCloudAgentSrvRequest::Request&] The ROS service request
683  #
684  # @return res [rapp_platform_ros_communications::createNewCloudAgentSrvResponse::Response&] The ROS service response
685  # @exception Exception IndexError
686  # @exception Exception IOError
687  # @exception Exception mdbError
688  def createNewCloudAgent(self,req):
689  try:
690  res = createNewCloudAgentSrvResponse()
691  db_username,db_password=self.getLogin()
692  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
693  cur = con.cursor()
694  cur.execute("LOCK TABLES cloud_agent WRITE, platform_user READ")
695  cur.execute("insert into cloud_agent (platform_user_id,tarball_path,container_identifier,image_identifier,container_type) VALUES ((select id from platform_user where username=%s), %s,%s,%s,%s)",(req.username,req.tarball_path,req.container_identifier,req.image_identifier,req.container_type))
696  cur.execute("UNLOCK TABLES")
697  res.success=True
698  con.close()
699  except mdb.Error, e:
700  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
701  res.success=False
702  res.error="Error %d: %s" % (e.args[0],e.args[1])
703  con.close()
704  except IndexError, e:
705  res.trace.append("IndexError: " +str(e))
706  res.success=False
707  res.error="IndexError: " +str(e)
708  con.close()
709  except IOError, e:
710  res.success=False
711  res.trace.append("IOError: " +str(e))
712  res.error="IOError: " +str(e)
713  con.close()
714  return res
715 
716  ## @brief Implements the createNewCloudAgentService service main function
717  # @param req [rapp_platform_ros_communications::createNewCloudAgentServiceSrvRequest::Request&] The ROS service request
718  #
719  # @return res [rapp_platform_ros_communications::createNewCloudAgentServiceSrvResponse::Response&] The ROS service response
720  # @exception Exception IndexError
721  # @exception Exception IOError
722  # @exception Exception mdbError
724  try:
725  res = createNewCloudAgentServiceSrvResponse()
726  print "right"
727  db_username,db_password=self.getLogin()
728  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
729  cur = con.cursor()
730  cur.execute("LOCK TABLES cloud_agent READ, cloud_agent_services WRITE")
731  cur.execute("insert into cloud_agent_services (cloud_agent_id,service_name,service_type,container_port,host_port) VALUES ((select id from cloud_agent where container_identifier=%s), %s,%s,%s,%s)",(req.container_identifier,req.service_name,req.service_type,req.container_port,req.host_port))
732  cur.execute("UNLOCK TABLES")
733  res.success=True
734  con.close()
735  except mdb.Error, e:
736  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
737  res.success=False
738  res.error="Error %d: %s" % (e.args[0],e.args[1])
739  con.close()
740  except IndexError, e:
741  res.trace.append("IndexError: " +str(e))
742  res.success=False
743  res.error="IndexError: " +str(e)
744  con.close()
745  except IOError, e:
746  res.success=False
747  res.trace.append("IOError: " +str(e))
748  res.error="IOError: " +str(e)
749  con.close()
750  return res
751 
752  ## @brief Implements the getCloudAgentServiceTypeAndHostPort service main function
753  # @param req [rapp_platform_ros_communications::getCloudAgentServiceTypeAndHostPortSrvRequest::Request&] The ROS service request
754  #
755  # @return res [rapp_platform_ros_communications::getCloudAgentServiceTypeAndHostPortSrvResponse::Response&] The ROS service response
756  # @exception Exception IndexError
757  # @exception Exception IOError
758  # @exception Exception mdbError
760  try:
761  res = getCloudAgentServiceTypeAndHostPortSrvResponse()
762  db_username,db_password=self.getLogin()
763  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
764  cur = con.cursor()
765  cur.execute("select service_type,host_port from cloud_agent_services where cloud_agent_id=(select id from cloud_agent where container_identifier=%s) and service_name=%s",(req.container_identifier,req.service_name))
766  result_set = cur.fetchall()
767  if(result_set and len(result_set[0])>0):
768  res.success=True
769  res.service_type=result_set[0][0]
770  res.host_port=result_set[0][1]
771  res.success=True
772  con.close()
773  except mdb.Error, e:
774  res.trace.append(("Database Error %d: %s" % (e.args[0],e.args[1])))
775  res.success=False
776  res.error="Error %d: %s" % (e.args[0],e.args[1])
777  con.close()
778  except IndexError, e:
779  res.trace.append("IndexError: " +str(e))
780  res.success=False
781  res.error="IndexError: " +str(e)
782  con.close()
783  except IOError, e:
784  res.success=False
785  res.trace.append("IOError: " +str(e))
786  res.error="IOError: " +str(e)
787  con.close()
788  return res
789 
790  ## @brief Gets the columns of the table
791  # @param tblName [string] the name of the table
792  #
793  # @return Columns [list] the columns of the table
794  def getTableColumnNames(self,tblName):
795  db_username,db_password=self.getLogin()
796  try:
797  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform');
798  cur = con.cursor()
799  cur.execute("Show columns from "+tblName)
800  result_set = cur.fetchall()
801  Columns=[]
802  for row in result_set:
803  Columns=Columns+[String(str(row[0]))]
804  return Columns
805  except mdb.Error, e:
806  print "Error %d: %s" % (e.args[0],e.args[1])
807 
808  ## @brief Loads login details from file
809  # @return db_username [string] the login username
810  # @return db_password [string] the login password
811  def getLogin(self):
812  fh = open("/etc/db_credentials", "r")
813  db_username=fh.readline()
814  db_username=db_username.split( )[0]
815  db_password=fh.readline()
816  db_password=db_password.split()[0]
817  return db_username,db_password
818 
819  ## @brief Checks connectivity to the DB
820  # @return [ void ] Connection performed successfully
821  def checkConnection(self):
822  try:
823  db_username,db_password=self.getLogin()
824  con = mdb.connect('localhost', db_username, db_password, 'rapp_platform')
825  cur = con.cursor()
826  cur.execute("SELECT VERSION()")
827  ver = cur.fetchone()
828  print "Database version : %s " % ver
829  con.close()
830  except mdb.Error, e:
831  print "Error %d: %s" % (e.args[0],e.args[1])
832 
833  ## @brief The getUserOntologyAlias service callback
834  # @param req [rapp_platform_ros_communications::getUserOntologyAliasSrvResponse::Request&] The ROS service request
835  # @return res [rapp_platform_ros_communications::getUserOntologyAliasSrvRequest::Response&] The ROS service response
837  res = getUserOntologyAliasSrvResponse()
838  res=self.getUserOntologyAlias(req)
839  return res
840 
841  ## @brief The registerUserOntologyAliasSrv service callback
842  # @param req [rapp_platform_ros_communications::registerUserOntologyAliasSrvResponse::Request&] The ROS service request
843  # @return res [rapp_platform_ros_communications::registerUserOntologyAliasSrvRequest::Response&] The ROS service response
845  res = registerUserOntologyAliasSrvResponse()
846  res=self.registerUserOntologyAlias(req)
847  return res
848 
849  ## @brief The getUserLanguage service callback
850  # @param req [rapp_platform_ros_communications::getUserLanguageSrvResponse::Request&] The ROS service request
851  # @return res [rapp_platform_ros_communications::getUserLanguageSrvRequest::Response&] The ROS service response
853  res = getUserLanguageSrvResponse()
854  res=self.getUserLanguage(req)
855  return res
856 
857  ## @brief The registerNewTokenSrv service callback
858  # @param req [rapp_platform_ros_communications::registerNewTokenSrvResponse::Request&] The ROS service request
859  # @return res [rapp_platform_ros_communications::registerNewTokenSrvRequest::Response&] The ROS service response
861  res = registerNewTokenSrvResponse()
862  res=self.registerNewToken(req)
863  return res
864 
865  ## @brief The checkIfUserExistsSrv service callback
866  # @param req [rapp_platform_ros_communications::checkIfUserExistsSrvResponse::Request&] The ROS service request
867  # @return res [rapp_platform_ros_communications::checkIfUserExistsSrvRequest::Response&] The ROS service response
869  res = checkIfUserExistsSrvResponse()
870  res=self.checkIfUserExists(req)
871  return res
872 
873  ## @brief The getUserPasswordSrv service callback
874  # @param req [rapp_platform_ros_communications::getUserPasswordSrvResponse::Request&] The ROS service request
875  # @return res [rapp_platform_ros_communications::getUserPasswordSrvRequest::Response&] The ROS service response
877  res = getUserPasswordSrvResponse()
878  res=self.getUserPassword(req)
879  return res
880 
881  ## @brief The getUsernameAssociatedWithApplicationTokenSrv service callback
882  # @param req [rapp_platform_ros_communications::getUsernameAssociatedWithApplicationTokenSrvResponse::Request&] The ROS service request
883  # @return res [rapp_platform_ros_communications::getUsernameAssociatedWithApplicationTokenSrvRequest::Response&] The ROS service response
885  res = getUsernameAssociatedWithApplicationTokenSrvResponse()
887  return res
888 
889  ## @brief The createNewPlatformUserSrv service callback
890  # @param req [rapp_platform_ros_communications::createNewPlatformUserSrvResponse::Request&] The ROS service request
891  # @return res [rapp_platform_ros_communications::createNewPlatformUserSrvRequest::Response&] The ROS service response
893  res = createNewPlatformUserSrvResponse()
894  res=self.createNewPlatformUser(req)
895  return res
896 
897  ## @brief The createNewApplicationTokenSrv service callback
898  # @param req [rapp_platform_ros_communications::createNewApplicationTokenSrvResponse::Request&] The ROS service request
899  # @return res [rapp_platform_ros_communications::createNewApplicationTokenSrvRequest::Response&] The ROS service response
901  res = createNewApplicationTokenSrvResponse()
902  res=self.createNewApplicationToken(req)
903  return res
904 
905  ## @brief The checkActiveApplicationTokenSrv service callback
906  # @param req [rapp_platform_ros_communications::checkActiveApplicationTokenSrvResponse::Request&] The ROS service request
907  # @return res [rapp_platform_ros_communications::checkActiveApplicationTokenSrvRequest::Response&] The ROS service response
909  res = checkActiveApplicationTokenSrvResponse()
910  res=self.checkActiveApplicationToken(req)
911  return res
912 
913  ## @brief The checkActiveRobotSessionSrv service callback
914  # @param req [rapp_platform_ros_communications::checkActiveRobotSessionSrvRequest::Request&] The ROS service request
915  # @return res [rapp_platform_ros_communications::checkActiveRobotSessionSrvResponse::Response&] The ROS service response
917  res = checkActiveRobotSessionSrvResponse()
918  res=self.checkActiveRobotSession(req)
919  return res
920 
921  ## @brief The addStoreTokenToDeviceSrv service callback
922  # @param req [rapp_platform_ros_communications::addStoreTokenToDeviceSrvRequest::Request&] The ROS service request
923  # @return res [rapp_platform_ros_communications::addStoreTokenToDeviceSrvResponse::Response&] The ROS service response
925  res = addStoreTokenToDeviceSrvResponse()
926  res=self.addStoreTokenToDevice(req)
927  return res
928 
929  ## @brief The validateUserRoleSrv service callback
930  # @param req [rapp_platform_ros_communications::validateUserRoleSrvRequest::Request&] The ROS service request
931  # @return res [rapp_platform_ros_communications::validateUserRoleSrvResponse::Response&] The ROS service response
933  res = validateUserRoleSrvResponse()
934  res=self.validateUserRole(req)
935  return res
936 
937  ## @brief The validateExistingPlatformDeviceTokenSrv service callback
938  # @param req [rapp_platform_ros_communications::validateExistingPlatformDeviceTokenSrvRequest::Request&] The ROS service request
939  # @return res [rapp_platform_ros_communications::validateExistingPlatformDeviceTokenSrvResponse::Response&] The ROS service response
941  res = validateExistingPlatformDeviceTokenSrvResponse()
943  return res
944 
945  ## @brief The removePlatformUserSrv service callback
946  # @param req [rapp_platform_ros_communications::removePlatformUserSrvRequest::Request&] The ROS service request
947  # @return res [rapp_platform_ros_communications::removePlatformUserSrvResponse::Response&] The ROS service response
949  res = removePlatformUserSrvResponse()
950  res=self.removePlatformUser(req)
951  return res
952 
953  ## @brief The createNewCloudAgentSrv service callback
954  # @param req [rapp_platform_ros_communications::createNewCloudAgentSrvRequest::Request&] The ROS service request
955  # @return res [rapp_platform_ros_communications::createNewCloudAgentSrvResponse::Response&] The ROS service response
957  res = createNewCloudAgentSrvResponse()
958  res=self.createNewCloudAgent(req)
959  return res
960 
961  ## @brief The createNewCloudAgentServiceSrv service callback
962  # @param req [rapp_platform_ros_communications::createNewCloudAgentServiceSrvRequest::Request&] The ROS service request
963  # @return res [rapp_platform_ros_communications::createNewCloudAgentServiceSrvResponse::Response&] The ROS service response
965  res = createNewCloudAgentServiceSrvResponse()
966  res=self.createNewCloudAgentService(req)
967  return res
968 
969  ## @brief The getCloudAgentServiceTypeAndHostPortSrv service callback
970  # @param req [rapp_platform_ros_communications::getCloudAgentServiceTypeAndHostPortSrvRequest::Request&] The ROS service request
971  # @return res [rapp_platform_ros_communications::getCloudAgentServiceTypeAndHostPortSrvResponse::Response&] The ROS service response
973  res = getCloudAgentServiceTypeAndHostPortSrvResponse()
975  return res
def getUserPasswordDataHandler
The getUserPasswordSrv service callback.
def createNewCloudAgentDataHandler
The createNewCloudAgentSrv service callback.
def checkConnection
Checks connectivity to the DB.
def createNewApplicationTokenDataHandler
The createNewApplicationTokenSrv service callback.
def checkIfUserExistsDataHandler
The checkIfUserExistsSrv service callback.
def createNewPlatformUser
Implements the createNewPlatformUser service main function.
def getLogin
Loads login details from file.
def getCloudAgentServiceTypeAndHostPortDataHandler
The getCloudAgentServiceTypeAndHostPortSrv service callback.
def checkIfUserExists
Implements the checkIfUserExists service main function.
def validateExistingPlatformDeviceToken
Implements the validateExistingPlatformDeviceToken service main function.
def registerUserOntologyAliasDataHandler
The registerUserOntologyAliasSrv service callback.
def getUsernameAssociatedWithApplicationToken
Implements the getUsernameAssociatedWithApplicationToken service main function.
def addStoreTokenToDevice
Implements the addStoreTokenToDevice service main function.
def __init__
Default contructor.
def addStoreTokenToDeviceDataHandler
The addStoreTokenToDeviceSrv service callback.
def createNewApplicationToken
Implements the createNewApplicationToken service main function.
def checkActiveRobotSession
Implements the checkActiveRobotSession service main function.
def checkActiveApplicationToken
Implements the checkActiveRobotSession service main function.
def createNewCloudAgent
Implements the createNewCloudAgent service main function.
def getUsernameAssociatedWithApplicationTokenDataHandler
The getUsernameAssociatedWithApplicationTokenSrv service callback.
def removePlatformUserDataHandler
The removePlatformUserSrv service callback.
def createNewPlatformUserDataHandler
The createNewPlatformUserSrv service callback.
def getUserOntologyAlias
Implements the getUserOntologyAlias service main function.
def removePlatformUser
Implements the removePlatformUser service main function.
def registerNewTokenDataHandler
The registerNewTokenSrv service callback.
def getTableColumnNames
Gets the columns of the table.
def getUserLanguage
Implements the getUserLanguage service main function.
def validateUserRoleDataHandler
The validateUserRoleSrv service callback.
def validateExistingPlatformDeviceTokenDataHandler
The validateExistingPlatformDeviceTokenSrv service callback.
def getUserPassword
Implements the getUserPassword service main function.
def getUserOntologyAliasDataHandler
The getUserOntologyAlias service callback.
def createNewCloudAgentServiceDataHandler
The createNewCloudAgentServiceSrv service callback.
def getCloudAgentServiceTypeAndHostPort
Implements the getCloudAgentServiceTypeAndHostPort service main function.
def validateUserRole
Implements the validateUserRole service main function.
The mysql wrapper ros node.
def checkActiveRobotSessionDataHandler
The checkActiveRobotSessionSrv service callback.
def checkActiveApplicationTokenDataHandler
The checkActiveApplicationTokenSrv service callback.
def getUserLanguageDataHandler
The getUserLanguage service callback.
def registerUserOntologyAlias
Implements the registerUserOntologyAlias service main function.
def createNewCloudAgentService
Implements the createNewCloudAgentService service main function.