summaryrefslogtreecommitdiff
path: root/lib/ansible/module_utils/network/eos/providers/cli/config/bgp/neighbors.py
blob: 329916b69f6e48b15e44569e7b77c8c4c7f01cdf (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#
# (c) 2019, Ansible by Red Hat, inc
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#
import re

from ansible.module_utils.six import iteritems
from ansible.module_utils.network.common.utils import to_list
from ansible.module_utils.network.eos.providers.providers import CliProvider


class Neighbors(CliProvider):

    def render(self, config=None, nbr_list=None):
        commands = list()
        safe_list = list()
        if not nbr_list:
            nbr_list = self.get_value('config.neighbors')

        for item in nbr_list:
            neighbor_commands = list()
            context = 'neighbor %s' % item['neighbor']
            cmd = '%s remote-as %s' % (context, item['remote_as'])

            if not config or cmd not in config:
                neighbor_commands.append(cmd)

            for key, value in iteritems(item):
                if value is not None:
                    meth = getattr(self, '_render_%s' % key, None)
                    if meth:
                        resp = meth(item, config)
                        if resp:
                            neighbor_commands.extend(to_list(resp))

            commands.extend(neighbor_commands)
            safe_list.append(context)

        if self.params['operation'] == 'replace':
            if config and safe_list:
                commands.extend(self._negate_config(config, safe_list))

        return commands

    def _negate_config(self, config, safe_list=None):
        commands = list()
        matches = re.findall(r'(neighbor \S+)', config, re.M)
        for item in set(matches).difference(safe_list):
            commands.append('no %s' % item)
        return commands

    def _render_description(self, item, config=None):
        cmd = 'neighbor %s description %s' % (item['neighbor'], item['description'])
        if not config or cmd not in config:
            return cmd

    def _render_enabled(self, item, config=None):
        cmd = 'neighbor %s shutdown' % item['neighbor']
        if item['enabled'] is True:
            if not config or cmd in config:
                cmd = 'no %s' % cmd
                return cmd
        elif not config or cmd not in config:
            return cmd

    def _render_update_source(self, item, config=None):
        cmd = 'neighbor %s update-source %s' % (item['neighbor'], item['update_source'])
        if not config or cmd not in config:
            return cmd

    def _render_password(self, item, config=None):
        cmd = 'neighbor %s password %s' % (item['neighbor'], item['password'])
        if not config or cmd not in config:
            return cmd

    def _render_ebgp_multihop(self, item, config=None):
        cmd = 'neighbor %s ebgp-multihop %s' % (item['neighbor'], item['ebgp_multihop'])
        if not config or cmd not in config:
            return cmd

    def _render_peer_group(self, item, config=None):
        cmd = 'neighbor %s peer-group %s' % (item['neighbor'], item['peer_group'])
        if not config or cmd not in config:
            return cmd

    def _render_route_reflector_client(self, item, config=None):
        cmd = 'neighbor %s route-reflector-client' % item['neighbor']
        if item['route_reflector_client'] is False:
            if not config or cmd in config:
                cmd = 'no %s' % cmd
                return cmd
        elif not config or cmd not in config:
            return cmd

    def _render_maximum_prefix(self, item, config=None):
        cmd = 'neighbor %s maximum-routes %s' % (item['neighbor'], item['maximum_prefix'])
        if not config or cmd not in config:
            return cmd

    def _render_remove_private_as(self, item, config=None):
        cmd = 'neighbor %s remove-private-AS' % item['neighbor']
        if item['remove_private_as'] is False:
            if not config or cmd in config:
                cmd = 'no %s' % cmd
                return cmd
        elif not config or cmd not in config:
            return cmd

    def _render_timers(self, item, config):
        """generate bgp timer related configuration
        """
        keepalive = item['timers']['keepalive']
        holdtime = item['timers']['holdtime']
        neighbor = item['neighbor']

        if keepalive and holdtime:
            cmd = 'neighbor %s timers %s %s' % (neighbor, keepalive, holdtime)
            if not config or cmd not in config:
                return cmd


class AFNeighbors(CliProvider):

    def render(self, config=None, nbr_list=None):
        commands = list()
        if not nbr_list:
            return

        for item in nbr_list:
            neighbor_commands = list()
            for key, value in iteritems(item):
                if value is not None:
                    meth = getattr(self, '_render_%s' % key, None)
                    if meth:
                        resp = meth(item, config)
                        if resp:
                            neighbor_commands.extend(to_list(resp))

            commands.extend(neighbor_commands)

        return commands

    def _render_activate(self, item, config=None):
        cmd = 'neighbor %s activate' % item['neighbor']
        if item['activate'] is False:
            if not config or cmd in config:
                cmd = 'no %s' % cmd
                return cmd
        elif not config or cmd not in config:
            return cmd

    def _render_default_originate(self, item, config=None):
        cmd = 'neighbor %s default-originate' % item['neighbor']
        if item['activate'] is False:
            if not config or cmd in config:
                cmd = 'no %s' % cmd
                return cmd
        elif not config or cmd not in config:
            return cmd

    def _render_graceful_restart(self, item, config=None):
        cmd = 'neighbor %s graceful-restart' % item['neighbor']
        if item['activate'] is False:
            if not config or cmd in config:
                cmd = 'no %s' % cmd
                return cmd
        elif not config or cmd not in config:
            return cmd

    def _render_weight(self, item, config=None):
        cmd = 'neighbor %s weight %s' % (item['neighbor'], item['weight'])
        if not config or cmd not in config:
            return cmd