Class: Layer3Network

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/antfarm/layer3_network.rb

Overview

Layer3Network class that wraps the layer3_networks table in the ANTFARM database.

  • has many layer 3 interfaces

  • has one IP network

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.merge(network, merge_certainty_factor = Antfarm::CF_PROVEN_TRUE) ⇒ Object

Take the given network and merge with it any sub_networks of the given network.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/antfarm/layer3_network.rb', line 37

def self.merge(network, merge_certainty_factor = Antfarm::CF_PROVEN_TRUE)
  unless network 
    raise(ArgumentError, "nil argument supplied", caller)
  end

  for sub_network in self.networks_contained_within(network.ip_network.address)
    unless sub_network == network 
      unless merge_certainty_factor
        merge_certainty_factor = Antfarm::CF_LACK_OF_PROOF
      end

      merge_certainty_factor = Antfarm.clamp(merge_certainty_factor)

      network.layer3_interfaces << sub_network.layer3_interfaces
      network.layer3_interfaces.flatten!
      network.layer3_interfaces.uniq!

      # TODO: update network's certainty factor using sub_network's certainty factor.
      
      network.save false

      # Because of :dependent => :destroy above, calling destroy
      # here will also cause destroy to be called on ip_network
      sub_network.destroy
    end
  end
end

.network_addressed(ip_net_str) ⇒ Object

Find the Layer3Network with the given address.



66
67
68
69
70
71
# File 'lib/antfarm/layer3_network.rb', line 66

def self.network_addressed(ip_net_str)
  # Calling network_containing here because if a network already exists that encompasses
  # the given network, we want to automatically use that network instead.
  # TODO: figure out how to use alias with class methods
  self.network_containing(ip_net_str)
end

.network_containing(ip_net_str) ⇒ Object

Find the network the given network is a sub_network of, if one exists.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/antfarm/layer3_network.rb', line 74

def self.network_containing(ip_net_str)
  unless ip_net_str
    raise(ArgumentError, "nil argument supplied", caller)
  end

  # Don't want to require a Layer3Network to be passed in case a check is being performed
  # before a Layer3Network is created.
  network = Antfarm::IPAddrExt.new(ip_net_str)

  ip_nets = IpNetwork.find(:all)
  for ip_net in ip_nets
    if Antfarm::IPAddrExt.new(ip_net.address).network_in_network?(network)
      return Layer3Network.find(ip_net.id)
    end
  end

  return nil
end

.networks_contained_within(ip_net_str) ⇒ Object

Find any Layer3Networks that are sub_networks of the given network.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/antfarm/layer3_network.rb', line 94

def self.networks_contained_within(ip_net_str)
  unless ip_net_str
    raise(ArgumentError, "nil argument supplied", caller)
  end

  # Don't want to require a Layer3Network to be passed in case a check is being performed
  # before a Layer3Network is created.
  network = Antfarm::IPAddrExt.new(ip_net_str)
  sub_networks = Array.new

  ip_nets = IpNetwork.find(:all)
  for ip_net in ip_nets
    sub_networks << Layer3Network.find(ip_net.id) if network.network_in_network?(ip_net.address)
  end

  return sub_networks
end

Instance Method Details

#to_labelObject

This is for ActiveScaffold



113
114
115
116
# File 'lib/antfarm/layer3_network.rb', line 113

def to_label #:nodoc:
  return "#{id} -- #{ip_network.address}" if ip_network
  return "#{id} -- Generic Layer3 Network"
end