def get_attribute_values(self, attrs): """ """ log.debug("get_attribute_values: attrs=%s", attrs) if not isinstance(attrs, (list, tuple)): raise PlatformException('get_attribute_values: attrs argument must be a ' 'list [(attrName, from_time), ...]. Given: %s', attrs) # convert the ION system time from_time to NTP, as this is the time # format used by the RSN OMS interface: attrs_ntp = [(attr_id, ion_ts_2_ntp(from_time)) for (attr_id, from_time) in attrs] try: retval = self._rsn_oms.attr.get_platform_attribute_values(self._platform_id, attrs_ntp) except Exception as e: raise PlatformConnectionException(msg="Cannot get_platform_attribute_values: %s" % str(e)) if not self._platform_id in retval: raise PlatformException("Unexpected: response does not include " "requested platform '%s'" % self._platform_id) attr_values = retval[self._platform_id] # reported timestamps are already in NTP. Just return the dict: return attr_values
def _handler_connected_set_port_over_current_limits(self, *args, **kwargs): """ """ port_id = kwargs.get('port_id', None) if port_id is None: raise PlatformException('set_port_over_current_limits: missing port_id argument') milliamps = kwargs.get('milliamps', None) if milliamps is None: raise PlatformException('set_port_over_current_limits: missing milliamps argument') microseconds = kwargs.get('microseconds', None) if milliamps is None: raise PlatformException('set_port_over_current_limits: missing microseconds argument') src = kwargs.get('src', None) if src is None: raise PlatformException('set_port_over_current_limits: missing src argument') try: result = self.set_overcurrent_limit(port_id, milliamps, microseconds, src) return None, result except PlatformConnectionException as e: return self._connection_lost(RSNPlatformDriverEvent.SET_PORT_OVER_CURRENT_LIMITS, args, kwargs, e)
def _verify_response(response, key=None, msg=None): if key is not None: if key not in response: raise PlatformException("Error in %s response: %r" % (msg, response)) response = response[key] if not response.startswith('OK'): raise PlatformException("Error in %s for key %s: %r" % (msg, key, response))
def _verify_response(rsp): try: message = rsp[port_id] if not message.startswith('OK'): raise PlatformException( msg="Error in setting overcurrent for port %s: %s" % (port_id, message)) except KeyError: raise PlatformException(msg="Error in response: %s" % rsp)
def _verify_response(rsp): try: message = rsp[mission_name] if not message.startswith('OK'): raise PlatformException( msg="Error in starting mission %s: %s" % (mission_name, message)) except KeyError: raise PlatformException( msg="Error in starting mission response: %s" % rsp)
def get_attribute_values(self, attrs): """ """ if not isinstance(attrs, (list, tuple)): raise PlatformException( 'get_attribute_values: attrs argument must be a ' 'list [(attrName, from_time), ...]. Given: %s', attrs) if self._rsn_oms is None: raise PlatformConnectionException( "Cannot get_platform_attribute_values: _rsn_oms object required (created via connect() call)" ) # convert the ION system time from_time to NTP, as this is the time # format used by the RSN OMS interface: # also convert the ION parameter names to RSN attribute IDs attrs_ntp = [(self.nodeCfgFile.GetAttrFromParameter(attr_id), ion_ts_2_ntp(from_time)) for (attr_id, from_time) in attrs] log.debug("get_attribute_values(ntp): attrs=%s", attrs_ntp) try: retval = self._rsn_oms.attr.get_platform_attribute_values( self._platform_id, attrs_ntp) except Exception as e: raise PlatformConnectionException( msg="Cannot get_platform_attribute_values: %s" % str(e)) if not self._platform_id in retval: raise PlatformException("Unexpected: response does not include " "requested platform '%s'" % self._platform_id) attr_values = retval[self._platform_id] attrs_return = {} #convert back to ION parameter name and scale from OMS to ION for key in attr_values: newAttrList = [] scaleFactor = self.nodeCfgFile.GetScaleFactorFromAttr(key) for v, ts in attr_values[key]: newAttrList.append((v * scaleFactor, ts)) attrs_return[self.nodeCfgFile.GetParameterFromAttr( key)] = newAttrList log.debug("Back to ION=%s", attrs_return) # reported timestamps are already in NTP. Just return the dict: return attrs_return
def _handler_connected_turn_off_port(self, *args, **kwargs): """ """ port_id = kwargs.get('port_id', None) if port_id is None: raise PlatformException('turn_off_port: missing port_id argument') src = kwargs.get('src', None) if port_id is None: raise PlatformException('turn_off_port: missing src argument') try: result = self.turn_off_port(port_id, src) return None, result except PlatformConnectionException as e: return self._connection_lost(RSNPlatformDriverEvent.TURN_OFF_PORT, args, kwargs, e)
def _handler_connected_stop_profiler_mission(self, *args, **kwargs): """ """ flag = kwargs.get('flag', None) if flag is None: raise PlatformException('_handler_connected_stop_profiler_mission: missing flag argument') src = kwargs.get('src', None) if src is None: raise PlatformException('set_port_over_current_limits: missing src argument') try: result = self.stop_profiler_mission(flag, src) return None, result except PlatformConnectionException as e: return self._connection_lost(RSNPlatformDriverEvent.STOP_PROFILER_MISSION, args, kwargs, e)
def _handler_connected_start_profiler_mission(self, *args, **kwargs): """ """ profile_mission_name = kwargs.get('profile_mission_name') if profile_mission_name is None: raise PlatformException('start_profiler_mission: missing profile_mission_name argument') src = kwargs.get('src', None) if src is None: raise PlatformException('set_port_over_current_limits: missing src argument') try: result = self.start_profiler_mission(profile_mission_name, src) return None, result except PlatformConnectionException as e: return self._connection_lost(RSNPlatformDriverEvent.START_PROFILER_MISSION, args, kwargs, e)
def _verify_platform_id_in_response(self, response): """ Verifies the presence of my platform_id in the response. @param response Dictionary returned by _rsn_oms @retval response[self._platform_id] """ if not self._platform_id in response: msg = "unexpected: response does not contain entry for %r" % self._platform_id log.error(msg) raise PlatformException(msg=msg) if response[self._platform_id] == InvalidResponse.PLATFORM_ID: msg = "response reports invalid platform_id for %r" % self._platform_id log.error(msg) raise PlatformException(msg=msg) else: return response[self._platform_id]
def set_attribute_values(self, attrs): """ """ if self._rsn_oms is None: raise PlatformConnectionException( "Cannot set_platform_attribute_values: _rsn_oms object required (created via connect() call)" ) error_vals = self._validate_set_attribute_values(attrs) if len(error_vals) > 0: # remove offending attributes for the request below attrs_dict = dict(attrs) for bad_attr_name in error_vals: del attrs_dict[bad_attr_name] # no good attributes at all? if len(attrs_dict) == 0: # just immediately return with the errors: return error_vals # else: update attrs with the good attributes: attrs = attrs_dict.items() # ok, now make the request to RSN OMS: try: retval = self._rsn_oms.attr.set_platform_attribute_values( self._platform_id, attrs) except Exception as e: raise PlatformConnectionException( msg="Cannot set_platform_attribute_values: %s" % str(e)) log.debug("set_platform_attribute_values = %s", retval) if not self._platform_id in retval: raise PlatformException("Unexpected: response does not include " "requested platform '%s'" % self._platform_id) attr_values = retval[self._platform_id] # Note that the reported timestamps are in NTP. # (Timestamps indicate the time when the value was set for each attribute.) # ret_attr_values: dictionary to return, initialized with the error ones # determined above, if any: ret_attr_values = error_vals # add the info returned from RSN OMS: for attr_name, attr_val_ts in attr_values.iteritems(): ret_attr_values[attr_name] = attr_val_ts log.debug("set_attribute_values: returning %s", ret_attr_values) return ret_attr_values
def _verify_returned_dict(attr_dict): try: for key in attr_dict.iterkeys(): value_list = attr_dict[key] if value_list == 'INVALID_ATTRIBUTE_ID': continue if not isinstance(value_list, list): raise PlatformException( msg="Error in getting values for attribute %s. %s" % (key, value_list)) if value_list and value_list[0][ 0] == "ERROR_DATA_REQUEST_TOO_FAR_IN_PAST": raise PlatformException( msg="Time requested for %s too far in the past" % key) except AttributeError: raise PlatformException( msg="Error returned in requesting attributes: %s" % attr_dict)
def _fetch_attrs(proxy, platform_id, attrs): with stopwatch(label='get_platform_attribute_values: %s' % platform_id, logger=log.info): try: response = proxy.attr.get_platform_attribute_values(platform_id, attrs).get(platform_id, {}) except socket.error: log.exception('Error connecting to OMS') response = {} return_dict = {} count = 0 for key, value_list in response.iteritems(): if value_list == 'INVALID_ATTRIBUTE_ID': continue if not isinstance(value_list, list): raise PlatformException(msg="Error in getting values for attribute %s. %s" % (key, value_list)) if value_list and value_list[0][0] == "ERROR_DATA_REQUEST_TOO_FAR_IN_PAST": raise PlatformException(msg="Time requested for %s too far in the past" % key) return_dict[key] = value_list count += len(value_list) log.info('_fetch_attrs %s returning %d items', platform_id, count) return return_dict
def execute(self, cmd, *args, **kwargs): """ Executes the given command. @param cmd command @return result of the execution """ if self.read_only_mode: raise PlatformException("Command %s not executed: Platform in READ ONLY mode." % cmd) return self._fsm.on_event(cmd, *args, **kwargs)
def get_attribute_values_from_oms(self, attrs): """ Fetch values from the OMS :param attrs: """ if not isinstance(attrs, (list, tuple)): msg = 'get_attribute_values: attrs argument must be a list [(attrName, from_time), ...]. Given: %s' % attrs raise PlatformException(msg) response = None try: response = self._rsn_oms.attr.get_platform_attribute_values( self._platform_id, attrs) response = self._verify_platform_id_in_response(response) return_dict = {} for key in response: value_list = response[key] if value_list == 'INVALID_ATTRIBUTE_ID': continue if not isinstance(value_list, list): raise PlatformException( msg="Error in getting values for attribute %s. %s" % (key, value_list)) if value_list and value_list[0][ 0] == "ERROR_DATA_REQUEST_TOO_FAR_IN_PAST": raise PlatformException( msg="Time requested for %s too far in the past" % key) return_dict[key] = value_list return return_dict except (Fault, ProtocolError, SocketError) as e: msg = "get_attribute_values_from_oms Cannot get_platform_attribute_values: %s" % e raise PlatformConnectionException(msg) except AttributeError: msg = "Error returned in requesting attributes: %s" % response raise PlatformException(msg)
def get_attribute_values_from_oms(self, attrs): """ """ def _verify_returned_dict(attr_dict): try: for key in attr_dict.iterkeys(): value_list = attr_dict[key] if value_list == 'INVALID_ATTRIBUTE_ID': continue if not isinstance(value_list, list): raise PlatformException( msg="Error in getting values for attribute %s. %s" % (key, value_list)) if value_list and value_list[0][ 0] == "ERROR_DATA_REQUEST_TOO_FAR_IN_PAST": raise PlatformException( msg="Time requested for %s too far in the past" % key) except AttributeError: raise PlatformException( msg="Error returned in requesting attributes: %s" % attr_dict) if not isinstance(attrs, (list, tuple)): raise PlatformException( 'get_attribute_values: attrs argument must be a ' 'list [(attrName, from_time), ...]. Given: %s', attrs) self._verify_rsn_oms('get_attribute_values_from_oms') log.debug("get_attribute_values: attrs=%s", self._platform_id) log.debug("get_attribute_values: attrs=%s", attrs) try: response = self._rsn_oms.attr.get_platform_attribute_values( self._platform_id, attrs) except Exception as e: raise PlatformConnectionException( msg= "get_attribute_values_from_oms Cannot get_platform_attribute_values: %s" % str(e)) dic_plat = self._verify_platform_id_in_response(response) _verify_returned_dict(dic_plat) # reported timestamps are already in NTP. Just return the dict: return dic_plat
def get_metadata(self): """ """ try: retval = self._rsn_oms.config.get_platform_metadata(self._platform_id) except Exception as e: raise PlatformConnectionException(msg="Cannot get_platform_metadata: %s" % str(e)) log.debug("get_platform_metadata = %s", retval) if not self._platform_id in retval: raise PlatformException("Unexpected: response does not include " "requested platform '%s'" % self._platform_id) md = retval[self._platform_id] return md
def _verify_response(rsp): if not rsp.startswith('OK'): raise PlatformException(msg="Error in stopping profiler: %s" % rsp)