Class: OpenC3::InterfaceModel
- Defined in:
- lib/openc3/models/interface_model.rb
Direct Known Subclasses
Constant Summary collapse
- INTERFACES_PRIMARY_KEY =
'openc3_interfaces'
- ROUTERS_PRIMARY_KEY =
'openc3_routers'
Instance Attribute Summary collapse
-
#auto_reconnect ⇒ Object
Returns the value of attribute auto_reconnect.
-
#cmd_target_names ⇒ Object
Returns the value of attribute cmd_target_names.
-
#config_params ⇒ Object
Returns the value of attribute config_params.
-
#connect_on_startup ⇒ Object
Returns the value of attribute connect_on_startup.
-
#disable_disconnect ⇒ Object
Returns the value of attribute disable_disconnect.
-
#interfaces ⇒ Object
Returns the value of attribute interfaces.
-
#log ⇒ Object
Returns the value of attribute log.
-
#log_raw ⇒ Object
Returns the value of attribute log_raw.
-
#needs_dependencies ⇒ Object
Returns the value of attribute needs_dependencies.
-
#options ⇒ Object
Returns the value of attribute options.
-
#protocols ⇒ Object
Returns the value of attribute protocols.
-
#reconnect_delay ⇒ Object
Returns the value of attribute reconnect_delay.
-
#secret_options ⇒ Object
Returns the value of attribute secret_options.
-
#secrets ⇒ Object
Returns the value of attribute secrets.
-
#target_names ⇒ Object
Redundant superset of cmd_target_names and tlm_target_names for backwards compat.
-
#tlm_target_names ⇒ Object
Returns the value of attribute tlm_target_names.
Attributes inherited from Model
#name, #plugin, #scope, #updated_at
Class Method Summary collapse
-
._get_key ⇒ Object
Helper method to return the correct primary key based on class name.
-
._get_type ⇒ Object
Helper method to return the correct type based on class name.
- .all(scope:) ⇒ Object
-
.get(name:, scope:) ⇒ Object
NOTE: The following three class methods are used by the ModelController and are reimplemented to enable various Model class methods to work.
-
.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:) ⇒ Object
Called by the PluginModel to allow this class to validate it’s top-level keyword: “INTERFACE” Interface/Router specific keywords are handled by the instance method “handle_config” NOTE: See RouterModel for the router method implementation.
- .names(scope:) ⇒ Object
Instance Method Summary collapse
- #as_json(*a) ⇒ Object
-
#build ⇒ Object
Called by InterfaceMicroservice to instantiate the Interface defined by the model configuration.
-
#deploy(gem_path, variables, validate_only: false) ⇒ Object
Creates a MicroserviceModel to deploy the Interface/Router.
- #ensure_target_exists(target_name) ⇒ Object
-
#handle_config(parser, keyword, parameters) ⇒ Object
Handles Interface/Router specific configuration keywords.
-
#initialize(name:, config_params: [], target_names: [], cmd_target_names: [], tlm_target_names: [], connect_on_startup: true, auto_reconnect: true, reconnect_delay: 5.0, disable_disconnect: false, options: [], secret_options: [], protocols: [], log: true, log_raw: false, updated_at: nil, plugin: nil, needs_dependencies: false, secrets: [], scope:) ⇒ InterfaceModel
constructor
A new instance of InterfaceModel.
- #map_target(target_name, cmd_only: false, tlm_only: false, unmap_old: true) ⇒ Object
-
#undeploy ⇒ Object
Looks up the deployed MicroserviceModel and destroy the microservice model should should trigger the operator to kill the microservice that in turn will destroy the InterfaceStatusModel when a stop is called.
- #unmap_target(target_name, cmd_only: false, tlm_only: false) ⇒ Object
Methods inherited from Model
#create, #destroy, #destroyed?, filter, find_all_by_plugin, from_json, get_all_models, get_model, set, store, #update
Constructor Details
#initialize(name:, config_params: [], target_names: [], cmd_target_names: [], tlm_target_names: [], connect_on_startup: true, auto_reconnect: true, reconnect_delay: 5.0, disable_disconnect: false, options: [], secret_options: [], protocols: [], log: true, log_raw: false, updated_at: nil, plugin: nil, needs_dependencies: false, secrets: [], scope:) ⇒ InterfaceModel
Returns a new instance of InterfaceModel.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/openc3/models/interface_model.rb', line 95 def initialize( name:, config_params: [], target_names: [], cmd_target_names: [], tlm_target_names: [], connect_on_startup: true, auto_reconnect: true, reconnect_delay: 5.0, disable_disconnect: false, options: [], secret_options: [], protocols: [], log: true, log_raw: false, updated_at: nil, plugin: nil, needs_dependencies: false, secrets: [], scope: ) if self.class._get_type == 'INTERFACE' super("#{scope}__#{INTERFACES_PRIMARY_KEY}", name: name, updated_at: updated_at, plugin: plugin, scope: scope) else super("#{scope}__#{ROUTERS_PRIMARY_KEY}", name: name, updated_at: updated_at, plugin: plugin, scope: scope) end @config_params = config_params @target_names = target_names @cmd_target_names = cmd_target_names @tlm_target_names = tlm_target_names @connect_on_startup = connect_on_startup @auto_reconnect = auto_reconnect @reconnect_delay = reconnect_delay @disable_disconnect = disable_disconnect @options = @secret_options = @protocols = protocols @log = log @log_raw = log_raw @needs_dependencies = needs_dependencies @secrets = secrets end |
Instance Attribute Details
#auto_reconnect ⇒ Object
Returns the value of attribute auto_reconnect.
37 38 39 |
# File 'lib/openc3/models/interface_model.rb', line 37 def auto_reconnect @auto_reconnect end |
#cmd_target_names ⇒ Object
Returns the value of attribute cmd_target_names.
34 35 36 |
# File 'lib/openc3/models/interface_model.rb', line 34 def cmd_target_names @cmd_target_names end |
#config_params ⇒ Object
Returns the value of attribute config_params.
32 33 34 |
# File 'lib/openc3/models/interface_model.rb', line 32 def config_params @config_params end |
#connect_on_startup ⇒ Object
Returns the value of attribute connect_on_startup.
36 37 38 |
# File 'lib/openc3/models/interface_model.rb', line 36 def connect_on_startup @connect_on_startup end |
#disable_disconnect ⇒ Object
Returns the value of attribute disable_disconnect.
39 40 41 |
# File 'lib/openc3/models/interface_model.rb', line 39 def disable_disconnect @disable_disconnect end |
#interfaces ⇒ Object
Returns the value of attribute interfaces.
43 44 45 |
# File 'lib/openc3/models/interface_model.rb', line 43 def interfaces @interfaces end |
#log ⇒ Object
Returns the value of attribute log.
44 45 46 |
# File 'lib/openc3/models/interface_model.rb', line 44 def log @log end |
#log_raw ⇒ Object
Returns the value of attribute log_raw.
45 46 47 |
# File 'lib/openc3/models/interface_model.rb', line 45 def log_raw @log_raw end |
#needs_dependencies ⇒ Object
Returns the value of attribute needs_dependencies.
46 47 48 |
# File 'lib/openc3/models/interface_model.rb', line 46 def needs_dependencies @needs_dependencies end |
#options ⇒ Object
Returns the value of attribute options.
40 41 42 |
# File 'lib/openc3/models/interface_model.rb', line 40 def @options end |
#protocols ⇒ Object
Returns the value of attribute protocols.
42 43 44 |
# File 'lib/openc3/models/interface_model.rb', line 42 def protocols @protocols end |
#reconnect_delay ⇒ Object
Returns the value of attribute reconnect_delay.
38 39 40 |
# File 'lib/openc3/models/interface_model.rb', line 38 def reconnect_delay @reconnect_delay end |
#secret_options ⇒ Object
Returns the value of attribute secret_options.
41 42 43 |
# File 'lib/openc3/models/interface_model.rb', line 41 def @secret_options end |
#secrets ⇒ Object
Returns the value of attribute secrets.
47 48 49 |
# File 'lib/openc3/models/interface_model.rb', line 47 def secrets @secrets end |
#target_names ⇒ Object
Redundant superset of cmd_target_names and tlm_target_names for backwards compat
33 34 35 |
# File 'lib/openc3/models/interface_model.rb', line 33 def target_names @target_names end |
#tlm_target_names ⇒ Object
Returns the value of attribute tlm_target_names.
35 36 37 |
# File 'lib/openc3/models/interface_model.rb', line 35 def tlm_target_names @tlm_target_names end |
Class Method Details
._get_key ⇒ Object
Helper method to return the correct primary key based on class name
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/openc3/models/interface_model.rb', line 83 def self._get_key type = _get_type case type when 'INTERFACE' INTERFACES_PRIMARY_KEY when 'ROUTER' ROUTERS_PRIMARY_KEY else raise "Unknown type #{type} from class #{self.name}" end end |
._get_type ⇒ Object
Helper method to return the correct type based on class name
78 79 80 |
# File 'lib/openc3/models/interface_model.rb', line 78 def self._get_type self.name.to_s.split("Model")[0].upcase.split("::")[-1] end |
.all(scope:) ⇒ Object
59 60 61 |
# File 'lib/openc3/models/interface_model.rb', line 59 def self.all(scope:) super("#{scope}__#{_get_key}") end |
.get(name:, scope:) ⇒ Object
NOTE: The following three class methods are used by the ModelController and are reimplemented to enable various Model class methods to work
51 52 53 |
# File 'lib/openc3/models/interface_model.rb', line 51 def self.get(name:, scope:) super("#{scope}__#{_get_key}", name: name) end |
.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:) ⇒ Object
Called by the PluginModel to allow this class to validate it’s top-level keyword: “INTERFACE” Interface/Router specific keywords are handled by the instance method “handle_config” NOTE: See RouterModel for the router method implementation
67 68 69 70 71 72 73 74 75 |
# File 'lib/openc3/models/interface_model.rb', line 67 def self.handle_config(parser, keyword, parameters, plugin: nil, needs_dependencies: false, scope:) case keyword when 'INTERFACE' parser.verify_num_parameters(2, nil, "INTERFACE <Name> <Filename> <Specific Parameters>") return self.new(name: parameters[0].upcase, config_params: parameters[1..-1], plugin: plugin, needs_dependencies: needs_dependencies, scope: scope) else raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Interface: #{keyword} #{parameters.join(" ")}") end end |
.names(scope:) ⇒ Object
55 56 57 |
# File 'lib/openc3/models/interface_model.rb', line 55 def self.names(scope:) super("#{scope}__#{_get_key}") end |
Instance Method Details
#as_json(*a) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/openc3/models/interface_model.rb', line 171 def as_json(*a) { 'name' => @name, 'config_params' => @config_params, 'target_names' => @target_names, 'cmd_target_names' => @cmd_target_names, 'tlm_target_names' => @tlm_target_names, 'connect_on_startup' => @connect_on_startup, 'auto_reconnect' => @auto_reconnect, 'reconnect_delay' => @reconnect_delay, 'disable_disconnect' => @disable_disconnect, 'options' => @options, 'secret_options' => @secret_options, 'protocols' => @protocols, 'log' => @log, 'log_raw' => @log_raw, 'plugin' => @plugin, 'needs_dependencies' => @needs_dependencies, 'secrets' => @secrets.as_json(*a), 'updated_at' => @updated_at } end |
#build ⇒ Object
Called by InterfaceMicroservice to instantiate the Interface defined by the model configuration. Must be called after get_model which calls from_json to instantiate the class and populate the attributes.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/openc3/models/interface_model.rb', line 141 def build klass = OpenC3.require_class(@config_params[0]) if @config_params.length > 1 interface_or_router = klass.new(*@config_params[1..-1]) else interface_or_router = klass.new end interface_or_router.secrets.setup(@secrets) interface_or_router.target_names = @target_names.dup interface_or_router.cmd_target_names = @cmd_target_names.dup interface_or_router.tlm_target_names = @tlm_target_names.dup interface_or_router.connect_on_startup = @connect_on_startup interface_or_router.auto_reconnect = @auto_reconnect interface_or_router.reconnect_delay = @reconnect_delay interface_or_router.disable_disconnect = @disable_disconnect @options.each do |option| interface_or_router.set_option(option[0], option[1..-1]) end @secret_options.each do |option| secret_name = option[1] secret_value = interface_or_router.secrets.get(secret_name, scope: @scope) interface_or_router.set_option(option[0], [secret_value]) end @protocols.each do |protocol| klass = OpenC3.require_class(protocol[1]) interface_or_router.add_protocol(klass, protocol[2..-1], protocol[0].upcase.intern) end interface_or_router end |
#deploy(gem_path, variables, validate_only: false) ⇒ Object
Creates a MicroserviceModel to deploy the Interface/Router
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/openc3/models/interface_model.rb', line 276 def deploy(gem_path, variables, validate_only: false) type = self.class._get_type microservice_name = "#{@scope}__#{type}__#{@name}" microservice = MicroserviceModel.new( name: microservice_name, work_dir: '/openc3/lib/openc3/microservices', cmd: ["ruby", "#{type.downcase}_microservice.rb", microservice_name], target_names: @target_names, plugin: @plugin, needs_dependencies: @needs_dependencies, secrets: @secrets, scope: @scope ) unless validate_only @target_names.each { |target_name| ensure_target_exists(target_name) } microservice.create microservice.deploy(gem_path, variables) ConfigTopic.write({ kind: 'created', type: type.downcase, name: @name, plugin: @plugin }, scope: @scope) Logger.info "Configured #{type.downcase} microservice #{microservice_name}" end microservice end |
#ensure_target_exists(target_name) ⇒ Object
194 195 196 197 198 |
# File 'lib/openc3/models/interface_model.rb', line 194 def ensure_target_exists(target_name) target = TargetModel.get(name: target_name, scope: @scope) raise "Target #{target_name} does not exist" unless target target end |
#handle_config(parser, keyword, parameters) ⇒ Object
Handles Interface/Router specific configuration keywords
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/openc3/models/interface_model.rb', line 201 def handle_config(parser, keyword, parameters) case keyword when 'MAP_TARGET' parser.verify_num_parameters(1, 1, "#{keyword} <Target Name>") target_name = parameters[0].upcase @target_names << target_name unless @target_names.include?(target_name) @cmd_target_names << target_name unless @cmd_target_names.include?(target_name) @tlm_target_names << target_name unless @tlm_target_names.include?(target_name) when 'MAP_CMD_TARGET' parser.verify_num_parameters(1, 1, "#{keyword} <Target Name>") target_name = parameters[0].upcase @target_names << target_name unless @target_names.include?(target_name) @cmd_target_names << target_name unless @cmd_target_names.include?(target_name) when 'MAP_TLM_TARGET' parser.verify_num_parameters(1, 1, "#{keyword} <Target Name>") target_name = parameters[0].upcase @target_names << target_name unless @target_names.include?(target_name) @tlm_target_names << target_name unless @tlm_target_names.include?(target_name) when 'DONT_CONNECT' parser.verify_num_parameters(0, 0, "#{keyword}") @connect_on_startup = false when 'DONT_RECONNECT' parser.verify_num_parameters(0, 0, "#{keyword}") @auto_reconnect = false when 'RECONNECT_DELAY' parser.verify_num_parameters(1, 1, "#{keyword} <Delay in Seconds>") @reconnect_delay = Float(parameters[0]) when 'DISABLE_DISCONNECT' parser.verify_num_parameters(0, 0, "#{keyword}") @disable_disconnect = true when 'OPTION' parser.verify_num_parameters(2, nil, "#{keyword} <Option Name> <Option Value 1> <Option Value 2 (optional)> <etc>") @options << parameters.dup when 'PROTOCOL' usage = "#{keyword} <READ WRITE READ_WRITE> <protocol filename or classname> <Protocol specific parameters>" parser.verify_num_parameters(2, nil, usage) unless %w(READ WRITE READ_WRITE).include? parameters[0].upcase raise parser.error("Invalid protocol type: #{parameters[0]}", usage) end @protocols << parameters.dup when 'DONT_LOG' parser.verify_num_parameters(0, 0, "#{keyword}") @log = false when 'LOG_RAW' parser.verify_num_parameters(0, 0, "#{keyword}") @log_raw = true when 'SECRET' parser.verify_num_parameters(3, 4, "#{keyword} <Secret Type: ENV or FILE> <Secret Name> <Environment Variable Name or File Path> <Option Name (Optional)>") @secrets << parameters[0..2] if parameters[3] # Option Name, Secret Name @secret_options << [parameters[3], parameters[1]] end else raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Interface/Router: #{keyword} #{parameters.join(" ")}") end return nil end |
#map_target(target_name, cmd_only: false, tlm_only: false, unmap_old: true) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/openc3/models/interface_model.rb', line 348 def map_target(target_name, cmd_only: false, tlm_only: false, unmap_old: true) if cmd_only and tlm_only cmd_only = false tlm_only = false end target_name = target_name.to_s.upcase ensure_target_exists(target_name) if unmap_old # Remove from old interface all_interfaces = InterfaceModel.all(scope: scope) old_interface = nil all_interfaces.each do |old_interface_name, old_interface_details| if old_interface_details['target_names'].include?(target_name) old_interface = InterfaceModel.from_json(old_interface_details, scope: scope) old_interface.unmap_target(target_name, cmd_only: cmd_only, tlm_only: tlm_only) if old_interface end end end # Add to this interface @target_names << target_name unless @target_names.include?(target_name) @cmd_target_names << target_name unless @cmd_target_names.include?(target_name) or tlm_only @tlm_target_names << target_name unless @tlm_target_names.include?(target_name) or cmd_only update() # Respawn the microservice type = self.class._get_type microservice_name = "#{@scope}__#{type}__#{@name}" microservice = MicroserviceModel.get_model(name: microservice_name, scope: scope) microservice.target_names << target_name unless microservice.target_names.include?(target_name) microservice.update end |
#undeploy ⇒ Object
Looks up the deployed MicroserviceModel and destroy the microservice model should should trigger the operator to kill the microservice that in turn will destroy the InterfaceStatusModel when a stop is called.
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/openc3/models/interface_model.rb', line 302 def undeploy type = self.class._get_type name = "#{@scope}__#{type}__#{@name}" model = MicroserviceModel.get_model(name: name, scope: @scope) if model model.destroy ConfigTopic.write({ kind: 'deleted', type: type.downcase, name: @name, plugin: @plugin }, scope: @scope) end if type == 'INTERFACE' status_model = InterfaceStatusModel.get_model(name: @name, scope: @scope) else status_model = RouterStatusModel.get_model(name: @name, scope: @scope) end status_model.destroy if status_model end |
#unmap_target(target_name, cmd_only: false, tlm_only: false) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/openc3/models/interface_model.rb', line 319 def unmap_target(target_name, cmd_only: false, tlm_only: false) if cmd_only and tlm_only cmd_only = false tlm_only = false end target_name = target_name.to_s.upcase # Remove from this interface if cmd_only @cmd_target_names.delete(target_name) @target_names.delete(target_name) unless @tlm_target_names.include?(target_name) elsif tlm_only @tlm_target_names.delete(target_name) @target_names.delete(target_name) unless @cmd_target_names.include?(target_name) else @cmd_target_names.delete(target_name) @tlm_target_names.delete(target_name) @target_names.delete(target_name) end update() # Respawn the microservice type = self.class._get_type microservice_name = "#{@scope}__#{type}__#{@name}" microservice = MicroserviceModel.get_model(name: microservice_name, scope: scope) microservice.target_names.delete(target_name) unless @target_names.include?(target_name) microservice.update end |