summaryrefslogtreecommitdiff
path: root/cloudinit/sources/helpers/vmware/imc/nic.py
blob: bb45a9e6a534bbadd92f9e2f42d891dd6c355754 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# vi: ts=4 expandtab
#
#    Copyright (C) 2015 Canonical Ltd.
#    Copyright (C) 2015 VMware Inc.
#
#    Author: Sankar Tanguturi <stanguturi@vmware.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3, as
#    published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

from .boot_proto import BootProto


class Nic:
    """
    Holds the information about each NIC specified
    in the customization specification file
    """

    def __init__(self, name, configFile):
        self._name = name
        self._configFile = configFile

    def _get(self, what):
        return self._configFile.get(self.name + what, None)

    def _get_count(self, prefix):
        return self._configFile.get_count(self.name + prefix)

    @property
    def name(self):
        return self._name

    @property
    def mac(self):
        return self._get('|MACADDR').lower()

    @property
    def bootProto(self):
        return self._get('|BOOTPROTO').lower()

    @property
    def ipv4(self):
        """
        Retrieves the DHCP or Static IPv6 configuration
        based on the BOOTPROTO property associated with the NIC
        """
        if self.bootProto == BootProto.STATIC:
            return StaticIpv4Conf(self)

        return DhcpIpv4Conf(self)

    @property
    def ipv6(self):
        cnt = self._get_count("|IPv6ADDR|")

        if cnt != 0:
            return StaticIpv6Conf(self)

        return DhcpIpv6Conf(self)


class DhcpIpv4Conf:
    """DHCP Configuration Setting."""

    def __init__(self, nic):
        self._nic = nic


class StaticIpv4Addr:
    """Static IPV4  Setting."""

    def __init__(self, nic):
        self._nic = nic

    @property
    def ip(self):
        return self._nic._get('|IPADDR')

    @property
    def netmask(self):
        return self._nic._get('|NETMASK')

    @property
    def gateway(self):
        return self._nic._get('|GATEWAY')


class StaticIpv4Conf(DhcpIpv4Conf):
    """Static IPV4 Configuration."""

    @property
    def addrs(self):
        """Return the list of associated IPv4 addresses."""
        return [StaticIpv4Addr(self._nic)]


class DhcpIpv6Conf:
    """DHCP IPV6 Configuration."""

    def __init__(self, nic):
        self._nic = nic


class StaticIpv6Addr:
    """Static IPV6 Address."""

    def __init__(self, nic, index):
        self._nic = nic
        self._index = index

    @property
    def ip(self):
        return self._nic._get("|IPv6ADDR|" + str(self._index))

    @property
    def prefix(self):
        return self._nic._get("|IPv6NETMASK|" + str(self._index))

    @property
    def gateway(self):
        return self._nic._get("|IPv6GATEWAY|" + str(self._index))


class StaticIpv6Conf(DhcpIpv6Conf):
    """Static IPV6 Configuration."""

    @property
    def addrs(self):
        """Return the list Associated IPV6 addresses."""
        cnt = self._nic._get_count("|IPv6ADDR|")

        res = []

        for i in range(1, cnt + 1):
            res.append(StaticIpv6Addr(self._nic, i))

        return res