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.collector import BaseFactCollector
20
21
22class Network:
23    """
24    This is a generic Network subclass of Facts.  This should be further
25    subclassed to implement per platform.  If you subclass this,
26    you must define:
27    - interfaces (a list of interface names)
28    - interface_<name> dictionary of ipv4, ipv6, and mac address information.
29
30    All subclasses MUST define platform.
31    """
32    platform = 'Generic'
33
34    # FIXME: remove load_on_init when we can
35    def __init__(self, module, load_on_init=False):
36        self.module = module
37
38    # TODO: more or less abstract/NotImplemented
39    def populate(self, collected_facts=None):
40        return {}
41
42
43class NetworkCollector(BaseFactCollector):
44    # MAYBE: we could try to build this based on the arch specific implementation of Network() or its kin
45    name = 'network'
46    _fact_class = Network
47    _fact_ids = set(['interfaces',
48                     'default_ipv4',
49                     'default_ipv6',
50                     'all_ipv4_addresses',
51                     'all_ipv6_addresses'])
52
53    IPV6_SCOPE = {'0': 'global',
54                  '10': 'host',
55                  '20': 'link',
56                  '40': 'admin',
57                  '50': 'site',
58                  '80': 'organization'}
59
60    def collect(self, module=None, collected_facts=None):
61        collected_facts = collected_facts or {}
62        if not module:
63            return {}
64
65        # Network munges cached_facts by side effect, so give it a copy
66        facts_obj = self._fact_class(module)
67
68        facts_dict = facts_obj.populate(collected_facts=collected_facts)
69
70        return facts_dict
71