Class: Aio::DeviceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/aio/core/device_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(module_manager) ⇒ DeviceManager

当初始化完成,所有cmd模块已经整理完毕



22
23
24
25
26
27
28
29
# File 'lib/aio/core/device_manager.rb', line 22

def initialize(module_manager)
  self.cmds = {}
  self.cmds_reg = {}
  self.devices = {}
  @module_manager = module_manager
  @warning_summarize = Aio::Warning::WarningSummarize.new(self)
  tidy_cmds
end

Instance Attribute Details

#cmdsObject

按设备型号保存cmd cmds = => [module_klass1, module_klass2]



9
10
11
# File 'lib/aio/core/device_manager.rb', line 9

def cmds
  @cmds
end

#cmds_regObject

命令的正则表达式匹配cmds_reg = => [“device_type”, “cmd_reg”]



13
14
15
# File 'lib/aio/core/device_manager.rb', line 13

def cmds_reg
  @cmds_reg
end

#devicesObject

保存所有设备类devices = => device_klass



5
6
7
# File 'lib/aio/core/device_manager.rb', line 5

def devices
  @devices
end

#module_managerObject

模块管理信息,只读



19
20
21
# File 'lib/aio/core/device_manager.rb', line 19

def module_manager
  @module_manager
end

#warning_summarizeObject (readonly)

警告信息汇总类



16
17
18
# File 'lib/aio/core/device_manager.rb', line 16

def warning_summarize
  @warning_summarize
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
# File 'lib/aio/core/device_manager.rb', line 35

def [](key)
  @devices[key]
end

#[]=(key, val) ⇒ Object



31
32
33
# File 'lib/aio/core/device_manager.rb', line 31

def []=(key, val)
  @devices[key] = val
end

#add_device(opts) ⇒ Object

在状态机中使用了,添加设备,并添加cmd-context



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
# File 'lib/aio/core/device_manager.rb', line 194

def add_device(opts)

  # 判断是否已经有了device,如果有,则沿用,如果没有则新建
  if has_device?(opts)
    device_klass = self.devices[opts[:device_name]]
  else
    device_klass = Aio::Device::ParentDevice.new
    device_klass.device_name = opts[:device_name]
  end

  # cmd 和 context 必须同时存在的情况下才能加载的
  if opts[:cmd] and opts[:context]

    cmd_arr = cmd_module_assign(opts)
    if cmd_arr.nil?
      device_klass.add_cmd_context_by_cmd_name(
        opts[:cmd],
        opts[:context]
      )
    elsif cmd_arr.size == 1
      device_klass.add_cmd_context_by_klass(
        cmd_arr[0],
        opts[:context]
      )
    else
      # 有多个cmd模块的情况,全部依次加进去,但是最后分析的时候,出现明显问题的将被删除
      cmd_arr.each do |cmd_klass|
        device_klass.add_cmd_context_by_klass(
          cmd_klass,
          opts[:context]
        )
      end
    end

  end

  self.devices[opts[:device_name]] = device_klass
end

#cmd_module_assign(opts) ⇒ Object

查找是否有符合指定的cmd模块有可能两种设备类型同时存在同样的命令,在不确定设备类型的情况下,全部输出注意此处输出的样式为 res = [ [device_type, cmd_klass], … ] info = { cmd_klass => [type, cmd_reg] }



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/aio/core/device_manager.rb', line 167

def cmd_module_assign(opts)
  cmd = opts[:cmd]
  res = []

  device_type = opts[:device_type]
  cmds_reg.each do |cmd_klass, info|
    reg = info[1]
    type = info[0]
    if reg.match(cmd)

      # 判断如果没有指定device_type,那么全部输出
      # 如果指定,则只输出指定类型
      # cmd_klass 会被覆盖, 已经修复
      if device_type == type
        res << [type, cmd_klass.division]
      elsif device_type.nil?
        res << [type, cmd_klass.division]
      end

    end
  end

  return res.empty? ? nil : res
end

#device_exist?(opts = {}) ⇒ Boolean

判断设备名是否存在

Returns:

  • (Boolean)


140
141
142
143
144
145
146
147
# File 'lib/aio/core/device_manager.rb', line 140

def device_exist?(opts={})
  each_devices do |devices, _|
    if devices == opts[:device_name]
      return true
    end
  end
  return false
end

#device_type_classifyObject

设备类型分类返回 { “cisco” => [device_name1, …] }



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/aio/core/device_manager.rb', line 151

def device_type_classify
  res = {}
  each_devices do |device_name, device_klass|
    type = device_klass.device_type
    unless res.has_key?(type)
      res[type] = []
    end
    res[type] << device_name
  end

  return res
end

#devices_numberObject

设备台数



40
41
42
# File 'lib/aio/core/device_manager.rb', line 40

def devices_number
  self.devices.keys.size
end

#each_devicesObject



85
86
87
88
89
# File 'lib/aio/core/device_manager.rb', line 85

def each_devices
  devices.each_pair do |device_name, device_klass|
    yield device_name, device_klass
  end
end

#each_devices_of(*types) ⇒ Object

只返回按照特定的设备,types 为数组



108
109
110
111
112
113
# File 'lib/aio/core/device_manager.rb', line 108

def each_devices_of(*types)
  each_devices do |name, klass|
    next unless types.include? klass.device_type
    yield klass
  end
end

#each_devices_of_ciscoObject

返回只是cisco的设备



116
117
118
119
120
121
# File 'lib/aio/core/device_manager.rb', line 116

def each_devices_of_cisco
  each_devices do |name, klass|
    next unless klass.device_type == 'cisco'
    yield klass
  end
end

#each_devices_of_h3cObject

返回只是h3c的设备



124
125
126
127
128
129
# File 'lib/aio/core/device_manager.rb', line 124

def each_devices_of_h3c
  each_devices do |name, klass|
    next unless klass.device_type == 'h3c'
    yield klass
  end
end

#each_devices_of_maipuObject

返回只是maipu的设备



132
133
134
135
136
137
# File 'lib/aio/core/device_manager.rb', line 132

def each_devices_of_maipu
  each_devices do |name, klass|
    next unless klass.device_type == 'maipu'
    yield klass
  end
end

#each_devices_with_indexObject



91
92
93
94
95
96
97
# File 'lib/aio/core/device_manager.rb', line 91

def each_devices_with_index
  i = 0
  devices.each_pair do |device_name, device_klass|
    yield device_name, device_klass, i
    i += 1
  end
end

#each_devices_with_usefulObject

轮询设备名以及有用信息



100
101
102
103
104
105
# File 'lib/aio/core/device_manager.rb', line 100

def each_devices_with_useful
  each_devices do |device_name, device_klass|
    useful = device_klass.cmds_useful
    yield device_name, useful
  end
end

#has_device?(opts) ⇒ Boolean

是否已经有device_name

Returns:

  • (Boolean)


256
257
258
# File 'lib/aio/core/device_manager.rb', line 256

def has_device?(opts)
  self.devices.has_key?(opts[:device_name])
end

#inspectObject



44
45
46
47
48
49
50
51
52
# File 'lib/aio/core/device_manager.rb', line 44

def inspect
  total = devices_number
  names = []
  each_devices do |name, klass|
    names << name
  end

  "#<Aio::DeviceManager @device_number: #{total}, @devices_name=#{names.to_s} >"
end

#just_cmds_regObject

仅仅返回各个命令的正则表达式的数组,在input/console中使用



77
78
79
80
81
82
83
# File 'lib/aio/core/device_manager.rb', line 77

def just_cmds_reg
  res = []
  cmds_reg.each_pair do |klass, info|
    res << info[1]
  end
  return res
end

#merge_warning(info) ⇒ Object

将compare中得到的warning信息合并到各个设备的warning_klass中



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/aio/core/device_manager.rb', line 234

def merge_warning(info)

  # info = {
  #    cm1: [ [device_name, cmd, str], .. ],
  #    cm2: [ [device_name, cmd, str], .. ]
  # }
  info.each_pair do |key, element|

    element.each do |e|
      name = e.shift
      # e = [cmd, match_str_info]
      self[name].warning_klass.warning_compare({cm: key, e: e})
    end
  end
end

#tidy_cmdsObject

整理cmds 和 cmds_reg 返回为Hash = => [klass, klass]



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/aio/core/device_manager.rb', line 56

def tidy_cmds
  device_type = @module_manager.get_modules_device_type_to_s

  device_type.each do |type|
    # 整理cmds
    type_modules = @module_manager.get_modules_by_device_type(type)
    self.cmds[type] = type_modules

    # 整理cmds_reg
    type_modules.each do |cmd_klass|

      # 将cmd_short转变为正则表达式
      # 相同的cmd_short会导致重复
      cmd_short_reg = Aio::Base::Toolkit::Regexp.to_reg(cmd_klass.cmd_short)
      cmds_reg[cmd_klass] = [type, cmd_short_reg]
    end
  end
  return cmds
end

#warning_summarize_tidyObject

整理汇总警告信息



251
252
253
# File 'lib/aio/core/device_manager.rb', line 251

def warning_summarize_tidy
  @warning_summarize.tidy_warning
end