summaryrefslogtreecommitdiff
path: root/lib/ansible/plugins/cliconf/junos.py
blob: eb92d8132f6dfd709cff34683a708f01f7085fe3 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#
# (c) 2017 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible 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 Ansible.  If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import collections
import json
import re

from itertools import chain
from functools import wraps

from ansible.module_utils._text import to_text
from ansible.module_utils.network.common.utils import to_list
from ansible.plugins.cliconf import CliconfBase


def configure(func):
    @wraps(func)
    def wrapped(self, *args, **kwargs):
        prompt = self._connection.get_prompt()
        if not to_text(prompt, errors='surrogate_or_strict').strip().endswith('#'):
            self.send_command('configure')
        return func(self, *args, **kwargs)
    return wrapped


class Cliconf(CliconfBase):

    def get_text(self, ele, tag):
        try:
            return to_text(ele.find(tag).text, errors='surrogate_then_replace').strip()
        except AttributeError:
            pass

    def get_device_info(self):
        device_info = dict()
        device_info['network_os'] = 'junos'

        reply = self.get(command='show version')
        data = to_text(reply, errors='surrogate_or_strict').strip()

        match = re.search(r'Junos: (\S+)', data)
        if match:
            device_info['network_os_version'] = match.group(1)

        match = re.search(r'Model: (\S+)', data, re.M)
        if match:
            device_info['network_os_model'] = match.group(1)

        match = re.search(r'Hostname: (\S+)', data, re.M)
        if match:
            device_info['network_os_hostname'] = match.group(1)
        return device_info

    def get_config(self, source='running', format='text', flags=None):
        if source != 'running':
            raise ValueError("fetching configuration from %s is not supported" % source)

        options_values = self.get_option_values()
        if format not in options_values['format']:
            raise ValueError("'format' value %s is invalid. Valid values are %s" % (format, ','.join(options_values['format'])))

        if format == 'text':
            cmd = 'show configuration'
        else:
            cmd = 'show configuration | display %s' % format

        cmd += ' '.join(to_list(flags))
        cmd = cmd.strip()
        return self.send_command(cmd)

    @configure
    def edit_config(self, candidate=None, commit=True, replace=None, comment=None):

        operations = self.get_device_operations()
        self.check_edit_config_capability(operations, candidate, commit, replace, comment)

        resp = {}
        results = []
        requests = []

        if replace:
            candidate = 'load replace {0}'.format(replace)

        for line in to_list(candidate):
            if not isinstance(line, collections.Mapping):
                line = {'command': line}
            cmd = line['command']
            results.append(self.send_command(**line))
            requests.append(cmd)

        diff = self.compare_configuration()
        if diff:
            resp['diff'] = diff

            if commit:
                self.commit(comment=comment)
            else:
                self.discard_changes()

        else:
            for cmd in ['top', 'exit']:
                self.send_command(cmd)

        resp['request'] = requests
        resp['response'] = results
        return resp

    def get(self, command, prompt=None, answer=None, sendonly=False, output=None, check_all=False):
        if output:
            command = self._get_command_with_output(command, output)
        return self.send_command(command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all)

    @configure
    def commit(self, comment=None, confirmed=False, at_time=None, synchronize=False):
        """
        Execute commit command on remote device.
        :param comment: Comment to be associated with commit
        :param confirmed: Boolean flag to indicate if the previous commit should confirmed
        :param at_time: Time at which to activate configuration changes
        :param synchronize: Boolean flag to indicate if commit should synchronize on remote peers
        :return: Command response received from device
        """
        command = 'commit'
        if comment:
            command += ' comment {0}'.format(comment)
        if confirmed:
            command += ' confirmed'
        if at_time:
            command += ' {0}'.format(at_time)
        if synchronize:
            command += ' peers-synchronize'

        command += ' and-quit'
        return self.send_command(command)

    @configure
    def discard_changes(self):
        command = 'rollback 0'
        for cmd in chain(to_list(command), 'exit'):
            self.send_command(cmd)

    @configure
    def validate(self):
        return self.send_command('commit check')

    @configure
    def compare_configuration(self, rollback_id=None):
        command = 'show | compare'
        if rollback_id is not None:
            command += ' rollback %s' % int(rollback_id)
        resp = self.send_command(command)

        r = resp.splitlines()
        if len(r) == 1 and '[edit]' in r[0]:
            resp = ''

        return resp

    @configure
    def rollback(self, rollback_id, commit=True):
        resp = {}
        self.send_command('rollback %s' % int(rollback_id))
        resp['diff'] = self.compare_configuration()
        if commit:
            self.commit()
        else:
            self.discard_changes()
        return resp

    def get_diff(self, rollback_id=None):
        diff = {'config_diff': None}
        response = self.compare_configuration(rollback_id=rollback_id)
        if response:
            diff['config_diff'] = response
        return diff

    def get_device_operations(self):
        return {
            'supports_diff_replace': False,
            'supports_commit': True,
            'supports_rollback': True,
            'supports_defaults': False,
            'supports_onbox_diff': True,
            'supports_commit_comment': True,
            'supports_multiline_delimiter': False,
            'supports_diff_match': False,
            'supports_diff_ignore_lines': False,
            'supports_generate_diff': False,
            'supports_replace': True
        }

    def get_option_values(self):
        return {
            'format': ['text', 'set', 'xml', 'json'],
            'diff_match': [],
            'diff_replace': [],
            'output': ['text', 'set', 'xml', 'json']
        }

    def get_capabilities(self):
        result = dict()
        result['rpc'] = self.get_base_rpc() + ['commit', 'discard_changes', 'run_commands', 'compare_configuration', 'validate', 'get_diff']
        result['network_api'] = 'cliconf'
        result['device_info'] = self.get_device_info()
        result['device_operations'] = self.get_device_operations()
        result.update(self.get_option_values())
        return json.dumps(result)

    def _get_command_with_output(self, command, output):
        options_values = self.get_option_values()
        if output not in options_values['output']:
            raise ValueError("'output' value %s is invalid. Valid values are %s" % (output, ','.join(options_values['output'])))

        if output == 'json' and not command.endswith('| display json'):
            cmd = '%s | display json' % command
        elif output == 'xml' and not command.endswith('| display xml'):
            cmd = '%s | display xml' % command
        elif output == 'text' and (command.endswith('| display json') or command.endswith('| display xml')):
            cmd = command.rsplit('|', 1)[0]
        else:
            cmd = command
        return cmd