def get_pi_list(self, rdm, params): full = None if self.PI_FULL_KEY in params and params[self.PI_FULL_KEY] or \ self.PI_EXTENDED_KEY in params and params[self.PI_EXTENDED_KEY]: full = True rows = rdm.get_platform_instance_list(full) pi_list = [] if rows: for row in rows: if row: if self.PI_EXTENDED_KEY in params and params[self.PI_EXTENDED_KEY]: pi_list.append({self.PI_NAME_KEY: row[0], self.PI_TYPE_KEY: row[1], self.PI_ADDRESS_KEY: row[2]}) elif self.PI_FULL_KEY in params and params[self.PI_FULL_KEY]: if row[1] and row[1] == RCMPlatformNode.R_TYPE: if row[4]: sl_row = rdm.get_sl_name_from_id(row[4]) pi_r_row = rdm.get_connection_from_connection_pi_r(row[0]) pi_list.append({self.PI_NAME_KEY: row[0], self.PI_TYPE_KEY: row[1], self.RES_CONNECTED: row[2] is not None, self.RES_SERVICE_LOGIC: sl_row[0] if (sl_row and sl_row[0]) else "", self.RES_PAIRED: pi_r_row[0] if (pi_r_row and pi_r_row[0]) else ""}) else: pi_list.append({self.PI_NAME_KEY: row[0], self.PI_TYPE_KEY: row[1], self.RES_CONNECTED: row[2] is not None, self.PI_LOAD_KEY: row[3]}) else: if row[2]: sl_row = rdm.get_sl_name_from_id(row[2]) pi_r_row = rdm.get_connection_from_connection_pi_r(row[0]) pi_list.append({self.PI_NAME_KEY: row[0], self.RES_CONNECTED: row[1] is not None, self.RES_SERVICE_LOGIC: sl_row[0] if (sl_row and sl_row[0]) else "", self.RES_PAIRED: (pi_r_row and pi_r_row[0]) is not None}) result = RCMPMessage() result.create_ok_response(pi_list) return result
def create_pi_name(self, rdm, params): result = RCMPMessage() if self.PI_SLG_KEY in params: sl_id = rdm.get_sl_id_from_name(params[self.PI_SLG_KEY]) if not (sl_id and sl_id[0]): reason = "Service logic '%s' doesn't exist" % params[self.PI_SLG_KEY] result.create_error_response(reason) return result else: reason = "A robot without service logic cannot be provisioned" result.create_error_response(reason) return result row = rdm.get_pi_ip_address_from_name(params[self.PI_NAME_KEY]) if row: # there is a row with the passed name: we cannot have duplicated names reason = "A %s with name %s already exists" % (self._who, params[self.PI_NAME_KEY]) result.create_error_response(reason) else: # no row with that platform instance name rdm.insert_platform_instance(params[self.PI_NAME_KEY], None, RCMPlatformNode.R_TYPE, s_logic=params[self.PI_SLG_KEY]) reason = "The %s with name %s has been provisioned correctly" % ( self._who, params[self.PI_NAME_KEY]) result.create_ok_response(reason) return result
def read(self, params): """Read the information about the service space using the names of the platform node instance and the service space""" self._logger.info("%s called" % params) result = RCMPMessage() try: res = {} if self.pni[RCMPlatformNode.PNI_IS_MASTER]: res[self.RES_EXISTING_SS_KEY] = self.get_service_spaces(params) if self.I_ADDRESS_KEY in params and params[self.I_ADDRESS_KEY] and \ params[self.I_ADDRESS_KEY] != self.pni[RCMPlatformNode.PNI_ADDRESS]: # the read must be executed by another platform node d_res = self.delegate(params) if d_res.is_ok(): # the search of installed nodes has gone well res.update(d_res.get_response_reason()) else: raise Exception(d_res.get_response_reason()) result.create_ok_response(res) else: # this is the platform node that has to do the read res[self.RES_INSTALLED_SN_KEY] = self.find_packages_info() result.create_ok_response(res) except Exception as e: reason = "The %s read failed: %s" % (self._who, e) result.create_error_response(reason) self._logger.info(result.get_txt()) return result
def ping(self, params): """Ping the platform node to know if it is reachable and alive.""" try: # self._logger.info("%s called" % params) result = self.execute(operation=self.target_on_ping, params=params, t_out=5.0) except Exception as e: reason = "Ping failed: %s" % e result = RCMPMessage() result.create_error_response(reason) # self._logger.info(result.get_txt()) return result
def target_on_ping(self, params): reason = "Ping ack" result = RCMPMessage() if PNodeInstance.PI_SOURCE_ADDRESS_KEY in params and params[PNodeInstance.PI_SOURCE_ADDRESS_KEY] and \ not self.wdm.is_watchdog_available(params): # this is a conditional ping that check if the watchdog corresponding to the source of # the ping is already available reason = "The watchdog on %s paired with %s is no more available" % \ (self.pni[RCMPlatformNode.PNI_ADDRESS], params[PNodeInstance.PI_SOURCE_ADDRESS_KEY]) result.create_error_response(reason) else: result.create_ok_response(reason) return result
def get_pi(self, rdm, params): result = RCMPMessage() rows = rdm.get_unloaded_s_pi() if rows: for row in rows: if row and row[0] and row[1]: # row[0] is not important, but row[1] should keep the ip address so it # has to have a value reason = {self.PI_NAME_KEY: row[0], self.PI_ADDRESS_KEY: row[1]} result.create_ok_response(reason) return result # we haven't found a server to use reason = "No servers available." result.create_error_response(reason) return result
def delete_pi(self, rdm, params): # in case of internal call: for robots we roll back half while for servers we roll back fully # in case of external call result = RCMPMessage() # for sure we are on the master because we are working with the robotics data if params[self.PI_NAME_KEY] == self.pni[RCMPlatformNode.PNI_NAME]: reason = "The %s with name '%s' cannot be deleted" % (self._who, params[self.PI_NAME_KEY]) result.create_error_response(reason) return result row = rdm.get_pi_ip_address_from_name(params[self.PI_NAME_KEY]) if row and row[1] and row[1] == RCMPlatformNode.S_TYPE: # we are trying to remove vms: this can be done only if full is required if self.PI_FULL_KEY in params and params[self.PI_FULL_KEY] is True: rdm.delete_platform_instance(params[self.PI_NAME_KEY], params[self.PI_FULL_KEY]) reason = "The provisioning of the %s with name '%s' has been rolled back completely" % \ (self._who, params[self.PI_NAME_KEY]) else: reason = "No rollback provisioning of the %s with name '%s' has to be done" % \ (self._who, params[self.PI_NAME_KEY]) result.create_ok_response(reason) elif row and row[1] and row[1] == RCMPlatformNode.R_TYPE: # we are trying to remove robots if self.PI_EXTENDED_KEY in params and params[self.PI_EXTENDED_KEY]: # we are in the new case of fiware user in which robots will be removed completely if not row[0]: # we can do the full delete of robot only in case is not connected that is # when it doesn't have row[0] (the ip address) rdm.delete_platform_instance(params[self.PI_NAME_KEY], True) reason = "The provisioning of the %s with name '%s' has been rolled back completely" % \ (self._who, params[self.PI_NAME_KEY]) result.create_ok_response(reason) else: reason = "The rollback provisioning of the %s with name '%s' cannot be done " \ "because is still connected" % (self._who, params[self.PI_NAME_KEY]) result.create_error_response(reason) elif self.PI_FULL_KEY in params and params[self.PI_FULL_KEY] is True: # we can do the full delete of robot only in case is not connected that is # when it doesn't have row[0] (the ip address) rdm.delete_platform_instance(params[self.PI_NAME_KEY], params[self.PI_FULL_KEY]) reason = "The provisioning of the %s with name '%s' has been rolled back completely" % \ (self._who, params[self.PI_NAME_KEY]) result.create_ok_response(reason) else: rdm.delete_platform_instance(params[self.PI_NAME_KEY], False) reason = "The provisioning of the %s with name '%s' has been rolled back half" % \ (self._who, params[self.PI_NAME_KEY]) result.create_ok_response(reason) else: reason = "The %s with name '%s' was already deleted" % (self._who, params[self.PI_NAME_KEY]) result.create_error_response(reason) return result
def create_pi(self, rdm, params): result = RCMPMessage() row = rdm.get_pi_ip_address_from_name(params[self.PI_NAME_KEY]) if params[self.PI_TYPE_KEY] == RCMPlatformNode.R_TYPE: # we are trying to provision a robot: in this case the name must be provided # by a user using the manual provisioning before this automatic provisioning # and will succeed only if there is already the name but it isn't mapped with # an ip address if row: # there is a row with the passed name if row[0]: # this platform instance has already an ip address if row[0] == params[self.PI_ADDRESS_KEY] and \ row[1] and row[1] == params[self.PI_TYPE_KEY]: # the ip address is the same of the passed one reason = "The %s with name '%s' was already provisioned correctly with address %s" % ( self._who, params[self.PI_NAME_KEY], params[self.PI_ADDRESS_KEY]) result.create_ok_response(reason) else: reason = "The %s with name '%s' already exists" % (self._who, params[self.PI_NAME_KEY]) result.create_error_response(reason) else: rdm.update_ip_address_into_platform_instance(params[self.PI_ADDRESS_KEY], params[self.PI_NAME_KEY]) reason = "The %s with name '%s' has been provisioned correctly with address %s" % ( self._who, params[self.PI_NAME_KEY], params[self.PI_ADDRESS_KEY]) result.create_ok_response(reason) else: # no row with that platform instance name reason = "The %s with name '%s' doesn't exist" % (self._who, params[self.PI_NAME_KEY]) result.create_error_response(reason) else: # we are trying to provision a vm: in this case will succeed only if there isn't # a name yet if not row: rdm.insert_platform_instance(params[self.PI_NAME_KEY], params[self.PI_ADDRESS_KEY], RCMPlatformNode.S_TYPE, RCMPlatformNode.S_LOAD) reason = "The %s with name '%s' has been provisioned correctly with address %s" % ( self._who, params[self.PI_NAME_KEY], params[self.PI_ADDRESS_KEY]) result.create_ok_response(reason) elif row and row[0] and row[0] == params[self.PI_ADDRESS_KEY] and \ row[1] and row[1] == params[self.PI_TYPE_KEY]: # if the name already exists and the address is the same of the current # one we can consider the provisioning done correctly reason = "The %s with name '%s' was already provisioned correctly with address %s" % ( self._who, params[self.PI_NAME_KEY], params[self.PI_ADDRESS_KEY]) result.create_ok_response(reason) else: reason = "The %s with name '%s' already exists but has a different ip address" % ( self._who, params[self.PI_NAME_KEY]) result.create_error_response(reason) return result