1# This file is part of Ansible
2#
3# Ansible is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7#
8# Ansible is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
15
16from __future__ import (absolute_import, division, print_function)
17__metaclass__ = type
18
19from ansible.module_utils.facts.network.base import Network, NetworkCollector
20
21
22class HPUXNetwork(Network):
23    """
24    HP-UX-specifig subclass of Network. Defines networking facts:
25    - default_interface
26    - interfaces (a list of interface names)
27    - interface_<name> dictionary of ipv4 address information.
28    """
29    platform = 'HP-UX'
30
31    def populate(self, collected_facts=None):
32        network_facts = {}
33        netstat_path = self.module.get_bin_path('netstat')
34
35        if netstat_path is None:
36            return network_facts
37
38        default_interfaces_facts = self.get_default_interfaces()
39        network_facts.update(default_interfaces_facts)
40
41        interfaces = self.get_interfaces_info()
42        network_facts['interfaces'] = interfaces.keys()
43        for iface in interfaces:
44            network_facts[iface] = interfaces[iface]
45
46        return network_facts
47
48    def get_default_interfaces(self):
49        default_interfaces = {}
50        rc, out, err = self.module.run_command("/usr/bin/netstat -nr")
51        lines = out.splitlines()
52        for line in lines:
53            words = line.split()
54            if len(words) > 1:
55                if words[0] == 'default':
56                    default_interfaces['default_interface'] = words[4]
57                    default_interfaces['default_gateway'] = words[1]
58
59        return default_interfaces
60
61    def get_interfaces_info(self):
62        interfaces = {}
63        rc, out, err = self.module.run_command("/usr/bin/netstat -ni")
64        lines = out.splitlines()
65        for line in lines:
66            words = line.split()
67            for i in range(len(words) - 1):
68                if words[i][:3] == 'lan':
69                    device = words[i]
70                    interfaces[device] = {'device': device}
71                    address = words[i + 3]
72                    interfaces[device]['ipv4'] = {'address': address}
73                    network = words[i + 2]
74                    interfaces[device]['ipv4'] = {'network': network,
75                                                  'interface': device,
76                                                  'address': address}
77        return interfaces
78
79
80class HPUXNetworkCollector(NetworkCollector):
81    _fact_class = HPUXNetwork
82    _platform = 'HP-UX'
83