Class: Cosmos::InterfaceModel

Inherits:
Model show all
Defined in:
lib/cosmos/models/interface_model.rb

Direct Known Subclasses

RouterModel

Constant Summary collapse

INTERFACES_PRIMARY_KEY =
'cosmos_interfaces'
ROUTERS_PRIMARY_KEY =
'cosmos_routers'

Instance Attribute Summary collapse

Attributes inherited from Model

#name, #plugin, #scope, #updated_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#create, #destroy, filter, find_all_by_plugin, from_json, get_all_models, get_model, set, #update

Constructor Details

#initialize(name:, config_params: [], target_names: [], connect_on_startup: true, auto_reconnect: true, reconnect_delay: 5.0, disable_disconnect: false, options: [], protocols: [], log: true, log_raw: false, updated_at: nil, plugin: nil, needs_dependencies: false, scope:) ⇒ InterfaceModel

Returns a new instance of InterfaceModel.



87
88
89
90
91
92
93
94
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
# File 'lib/cosmos/models/interface_model.rb', line 87

def initialize(
  name:,
  config_params: [],
  target_names: [],
  connect_on_startup: true,
  auto_reconnect: true,
  reconnect_delay: 5.0,
  disable_disconnect: false,
  options: [],
  protocols: [],
  log: true,
  log_raw: false,
  updated_at: nil,
  plugin: nil,
  needs_dependencies: false,
  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
  @connect_on_startup = connect_on_startup
  @auto_reconnect = auto_reconnect
  @reconnect_delay = reconnect_delay
  @disable_disconnect = disable_disconnect
  @options = options
  @protocols = protocols
  @log = log
  @log_raw = log_raw
  @needs_dependencies = needs_dependencies
end

Instance Attribute Details

#auto_reconnectObject

Returns the value of attribute auto_reconnect.



31
32
33
# File 'lib/cosmos/models/interface_model.rb', line 31

def auto_reconnect
  @auto_reconnect
end

#config_paramsObject

Returns the value of attribute config_params.



28
29
30
# File 'lib/cosmos/models/interface_model.rb', line 28

def config_params
  @config_params
end

#connect_on_startupObject

Returns the value of attribute connect_on_startup.



30
31
32
# File 'lib/cosmos/models/interface_model.rb', line 30

def connect_on_startup
  @connect_on_startup
end

#disable_disconnectObject

Returns the value of attribute disable_disconnect.



33
34
35
# File 'lib/cosmos/models/interface_model.rb', line 33

def disable_disconnect
  @disable_disconnect
end

#interfacesObject

Returns the value of attribute interfaces.



36
37
38
# File 'lib/cosmos/models/interface_model.rb', line 36

def interfaces
  @interfaces
end

#logObject

Returns the value of attribute log.



37
38
39
# File 'lib/cosmos/models/interface_model.rb', line 37

def log
  @log
end

#log_rawObject

Returns the value of attribute log_raw.



38
39
40
# File 'lib/cosmos/models/interface_model.rb', line 38

def log_raw
  @log_raw
end

#needs_dependenciesObject

Returns the value of attribute needs_dependencies.



39
40
41
# File 'lib/cosmos/models/interface_model.rb', line 39

def needs_dependencies
  @needs_dependencies
end

#optionsObject

Returns the value of attribute options.



34
35
36
# File 'lib/cosmos/models/interface_model.rb', line 34

def options
  @options
end

#protocolsObject

Returns the value of attribute protocols.



35
36
37
# File 'lib/cosmos/models/interface_model.rb', line 35

def protocols
  @protocols
end

#reconnect_delayObject

Returns the value of attribute reconnect_delay.



32
33
34
# File 'lib/cosmos/models/interface_model.rb', line 32

def reconnect_delay
  @reconnect_delay
end

#target_namesObject

Returns the value of attribute target_names.



29
30
31
# File 'lib/cosmos/models/interface_model.rb', line 29

def target_names
  @target_names
end

Class Method Details

._get_keyObject

Helper method to return the correct primary key based on class name



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cosmos/models/interface_model.rb', line 75

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_typeObject

Helper method to return the correct type based on class name



70
71
72
# File 'lib/cosmos/models/interface_model.rb', line 70

def self._get_type
  self.name.to_s.split("Model")[0].upcase.split("::")[-1]
end

.all(scope:) ⇒ Object



51
52
53
# File 'lib/cosmos/models/interface_model.rb', line 51

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



43
44
45
# File 'lib/cosmos/models/interface_model.rb', line 43

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



59
60
61
62
63
64
65
66
67
# File 'lib/cosmos/models/interface_model.rb', line 59

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, scope: scope)
  else
    raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Interface: #{keyword} #{parameters.join(" ")}")
  end
end

.names(scope:) ⇒ Object



47
48
49
# File 'lib/cosmos/models/interface_model.rb', line 47

def self.names(scope:)
  super("#{scope}__#{_get_key}")
end

Instance Method Details

#as_configObject

TODO: Not currently used but may be used by a XTCE or other format to COSMOS conversion



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/cosmos/models/interface_model.rb', line 167

def as_config
  result = "#{self.class._get_type} #{@name} #{@config_params.join(' ')}\n"
  @target_names.each do |target_name|
    result << "  MAP_TARGET #{target_name}\n"
  end
  result << "  DONT_CONNECT\n" unless @connect_on_startup
  result << "  DONT_RECONNECT\n" unless @auto_reconnect
  result << "  RECONNECT_DELAY #{@reconnect_delay}\n"
  result << "  DISABLE_DISCONNECT\n" if @disable_disconnect
  @options.each do |option|
    result << "  OPTION #{option.join(' ')}\n"
  end
  @protocols.each do |protocol|
    result << "  PROTOCOL #{protocol.join(' ')}\n"
  end
  result << "  DONT_LOG" unless @log
  result << "  LOG_RAW" if @log_raw
  result
end

#as_jsonObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cosmos/models/interface_model.rb', line 147

def as_json
  {
    'name' => @name,
    'config_params' => @config_params,
    'target_names' => @target_names,
    'connect_on_startup' => @connect_on_startup,
    'auto_reconnect' => @auto_reconnect,
    'reconnect_delay' => @reconnect_delay,
    'disable_disconnect' => @disable_disconnect,
    'options' => @options,
    'protocols' => @protocols,
    'log' => @log,
    'log_raw' => @log_raw,
    'plugin' => @plugin,
    'needs_dependencies' => @needs_dependencies,
    'updated_at' => @updated_at
  }
end

#buildObject

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.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cosmos/models/interface_model.rb', line 125

def build
  klass = Cosmos.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.target_names = @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
  @protocols.each do |protocol|
    klass = Cosmos.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) ⇒ Object

Creates a MicroserviceModel to deploy the Interface/Router



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cosmos/models/interface_model.rb', line 240

def deploy(gem_path, variables)
  type = self.class._get_type
  microservice_name = "#{@scope}__#{type}__#{@name}"
  microservice = MicroserviceModel.new(
    name: microservice_name,
    work_dir: '/cosmos/lib/cosmos/microservices',
    cmd: ["ruby", "#{type.downcase}_microservice.rb", microservice_name],
    target_names: @target_names,
    plugin: @plugin,
    needs_dependencies: @needs_dependencies,
    scope: @scope
  )
  microservice.create
  microservice.deploy(gem_path, variables)
  Logger.info "Configured #{type.downcase} microservice #{microservice_name}"
  microservice
end

#handle_config(parser, keyword, parameters) ⇒ Object

Handles Interface/Router specific configuration keywords



188
189
190
191
192
193
194
195
196
197
198
199
200
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
# File 'lib/cosmos/models/interface_model.rb', line 188

def handle_config(parser, keyword, parameters)
  case keyword
  when 'MAP_TARGET'
    parser.verify_num_parameters(1, 1, "#{keyword} <Target Name>")
    @target_names << parameters[0].upcase

  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

  else
    raise ConfigParser::Error.new(parser, "Unknown keyword and parameters for Interface/Router: #{keyword} #{parameters.join(" ")}")

  end

  return nil
end

#undeployObject

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.



261
262
263
264
265
266
267
268
269
270
# File 'lib/cosmos/models/interface_model.rb', line 261

def undeploy
  model = MicroserviceModel.get_model(name: "#{@scope}__#{self.class._get_type}__#{@name}", scope: @scope)
  model.destroy if model
  if self.class._get_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