def _register_event_listener(self, url): """ Registers given url for all event types. """ log.debug("%r: registering event listener: %s", self._platform_id, url) try: already_registered = self._rsn_oms.event.get_registered_event_listeners( ) except Exception as e: raise PlatformConnectionException( msg="%r: Cannot get registered event listeners: %s" % (self._platform_id, e)) if url in already_registered: log.debug("listener %r was already registered", url) return try: result = self._rsn_oms.event.register_event_listener(url) except Exception as e: raise PlatformConnectionException( msg="%r: Cannot register_event_listener: %s" % (self._platform_id, e)) log.debug("%r: register_event_listener(%r) => %s", self._platform_id, url, result)
def set_over_current(self, port_id, ma, us, src=None): """ @param port_id @param ma @param us @param src Some provenance information: actor, mission, etc. """ log.debug("%r: set_over_current: port_id=%s ma=%s us=%s src=%s", self._platform_id, port_id, ma, us, src) try: response = self._rsn_oms.port.set_over_current( self._platform_id, port_id, ma, us, src) except Exception as e: raise PlatformConnectionException( msg="Cannot set_over_current: %s" % str(e)) log.debug("%r: set_over_current response: %s", self._platform_id, response) dic_plat = self._verify_platform_id_in_response(response) self._verify_port_id_in_response(port_id, dic_plat) return dic_plat # note: return the dic for the platform
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 turn_off_port(self, port_id, src=None): """ Turns off a port. @param port_id Port ID @param src Some provenance information: actor, mission, etc. """ log.debug("%r: turning off port: port_id=%s src=%s", self._platform_id, port_id, src) try: response = self._rsn_oms.port.turn_off_platform_port( self._platform_id, port_id, src) except Exception as e: raise PlatformConnectionException( msg="Cannot turn_off_platform_port: %s" % str(e)) log.debug("%r: turn_off_platform_port response: %s", self._platform_id, response) dic_plat = self._verify_platform_id_in_response(response) self._verify_port_id_in_response(port_id, dic_plat) return dic_plat # note: return the dic for the platform
def disconnect_instrument(self, port_id, instrument_id): log.debug("%r: disconnect_instrument: port_id=%r instrument_id=%r", self._platform_id, port_id, instrument_id) self._assert_rsn_oms() try: response = self._rsn_oms.instr.disconnect_instrument(self._platform_id, port_id, instrument_id) except Exception as e: raise PlatformConnectionException(msg="Cannot disconnect_instrument: %s" % str(e)) log.debug("%r: disconnect_instrument response: %s", self._platform_id, response) dic_plat = self._verify_platform_id_in_response(response) port_dic = self._verify_port_id_in_response(port_id, dic_plat) instr_res = self._verify_instrument_id_in_response(port_id, instrument_id, port_dic) # update local image if instrument was actually disconnected in this call: if instr_res == NormalResponse.INSTRUMENT_DISCONNECTED: del self._pnode.ports[port_id].instruments[instrument_id] log.debug("%r: port_id=%s disconnect_instrument: local image updated: %s", self._platform_id, port_id, instrument_id) return dic_plat # note: return the dic for the platform
def connect_instrument(self, port_id, instrument_id, attributes): log.debug("%r: connect_instrument: port_id=%r instrument_id=%r attributes=%s", self._platform_id, port_id, instrument_id, attributes) self._assert_rsn_oms() try: response = self._rsn_oms.instr.connect_instrument(self._platform_id, port_id, instrument_id, attributes) except Exception as e: raise PlatformConnectionException(msg="Cannot connect_instrument: %s" % str(e)) log.debug("%r: connect_instrument response: %s", self._platform_id, response) dic_plat = self._verify_platform_id_in_response(response) port_dic = self._verify_port_id_in_response(port_id, dic_plat) instr_res = self._verify_instrument_id_in_response(port_id, instrument_id, port_dic) # update local image if instrument was actually connected in this call: if isinstance(instr_res, dict): attrs = instr_res instrumentNode = InstrumentNode(instrument_id, attrs) self._pnode.ports[port_id].add_instrument(instrumentNode) log.debug("%r: port_id=%s connect_instrument: local image updated: %s", self._platform_id, port_id, instrument_id) return dic_plat # note: return the dic for the platform
def _unregister_event_listener(self, url): """ Unregisters given url for all event types. """ log.debug("%r: unregistering event listener: %s", self._platform_id, url) try: result = self._rsn_oms.event.unregister_event_listener(url) except Exception as e: raise PlatformConnectionException( msg="%r: Cannot unregister_event_listener: %s" % (self._platform_id, e)) log.debug("%r: unregister_event_listener(%r) => %s", self._platform_id, url, result)
def _get_ports(self): try: response = self._rsn_oms.port.get_platform_ports(self._platform_id) ports = response[self._platform_id] except Exception as e: msg = "Cannot get_platform_ports(platform_id=%r): %s" % ( self._platform_id, e) raise PlatformConnectionException(msg=msg) log.debug("%r: _get_ports: %s", self._platform_id, ports) return ports
def ping(self): """ Verifies communication with external platform returning "PONG" if this verification completes OK. @retval "PONG" iff all OK. @raise PlatformConnectionException Cannot ping external platform or got unexpected response. """ log.debug("%r: pinging OMS...", self._platform_id) self._assert_rsn_oms() try: retval = self._rsn_oms.hello.ping() except Exception as e: raise PlatformConnectionException(msg="Cannot ping: %s" % str(e)) if retval is None or retval.upper() != "PONG": raise PlatformConnectionException(msg="Unexpected ping response: %r" % retval) log.debug("%r: ping completed: response: %s", self._platform_id, retval) return "PONG"
def set_attribute_values(self, attrs): """ """ log.debug("set_attribute_values: attrs = %s", attrs) self._assert_rsn_oms() 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 ping(self): """ Verifies communication with external platform returning "PONG" if this verification completes OK. @retval "PONG" iff all OK. @raise PlatformConnectionException Cannot ping external platform or got unexpected response. """ log.debug("%r: pinging OMS...", self._platform_id) self._assert_rsn_oms() try: retval = self._rsn_oms.hello.ping() except Exception, e: raise PlatformConnectionException(msg="Cannot ping %s" % str(e))
def get_external_checksum(self): """ Returns the checksum of the external platform associated with this driver. @return SHA1 hash value as string of hexadecimal digits. """ log.debug("%r: get_checksum...", self._platform_id) self._assert_rsn_oms() try: response = self._rsn_oms.config.get_checksum(self._platform_id) except Exception as e: raise PlatformConnectionException(msg="Cannot get_checksum: %s" % str(e)) dic_plat = self._verify_platform_id_in_response(response) log.debug("%r: get_checksum... dic_plat=%s" % (self._platform_id, dic_plat)) return dic_plat # note: return the dic for the platform
def get_metadata(self): """ """ self._assert_rsn_oms() 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 turn_off_port(self, port_id): log.debug("%r: turning off port: port_id=%s", self._platform_id, port_id) self._assert_rsn_oms() try: response = self._rsn_oms.port.turn_off_platform_port(self._platform_id, port_id) except Exception as e: raise PlatformConnectionException(msg="Cannot turn_off_platform_port: %s" % str(e)) log.debug("%r: turn_off_platform_port response: %s", self._platform_id, response) dic_plat = self._verify_platform_id_in_response(response) self._verify_port_id_in_response(port_id, dic_plat) return dic_plat # note: return the dic for the platform
def get_connected_instruments(self, port_id): log.debug("%r: get_connected_instruments: port_id=%s", self._platform_id, port_id) self._assert_rsn_oms() try: response = self._rsn_oms.instr.get_connected_instruments(self._platform_id, port_id) except Exception as e: raise PlatformConnectionException(msg="Cannot get_connected_instruments: %s" % str(e)) log.debug("%r: port_id=%r: get_connected_instruments response: %s", self._platform_id, port_id, response) dic_plat = self._verify_platform_id_in_response(response) port_dic = self._verify_port_id_in_response(port_id, dic_plat) return dic_plat # note: return the dic for the platform
class RSNPlatformDriver(PlatformDriver): """ The main RSN OMS platform driver class. """ def __init__(self, pnode, evt_recv): """ Creates an RSNPlatformDriver instance. @param pnode Root PlatformNode defining the platform network rooted at this platform. @param evt_recv Listener of events generated by this driver """ PlatformDriver.__init__(self, pnode, evt_recv) # CIOMSClient instance created by connect() self._rsn_oms = None # external event listener: we can instantiate this here as the the # actual http server is started via corresponding method. self._event_listener = OmsEventListener(self._notify_driver_event) def _validate_driver_configuration(self, driver_config): """ Driver config must include 'oms_uri' entry. """ if not 'oms_uri' in driver_config: raise PlatformDriverException( msg="driver_config does not indicate 'oms_uri'") def configure(self, driver_config): """ Nothing special done here, only calls super.configure(driver_config) @param driver_config with required 'oms_uri' entry. """ PlatformDriver.configure(self, driver_config) def _assert_rsn_oms(self): assert self._rsn_oms is not None, "_rsn_oms object required (created via connect() call)" def ping(self): """ Verifies communication with external platform returning "PONG" if this verification completes OK. @retval "PONG" iff all OK. @raise PlatformConnectionException Cannot ping external platform or got unexpected response. """ log.debug("%r: pinging OMS...", self._platform_id) self._assert_rsn_oms() try: retval = self._rsn_oms.hello.ping() except Exception, e: raise PlatformConnectionException(msg="Cannot ping %s" % str(e)) if retval is None or retval.upper() != "PONG": raise PlatformConnectionException( msg="Unexpected ping response: %r" % retval) if log.isEnabledFor(logging.DEBUG): log.debug("%r: ping completed: response: %s" % (self._platform_id, retval)) return "PONG"