summaryrefslogtreecommitdiffstats
path: root/ansible_collections/junipernetworks/junos/plugins/cliconf/junos.py
blob: 4461c9cb0065f9e385fb19a2b2a07533a9165790 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#
# (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

DOCUMENTATION = """
author: Ansible Networking Team (@ansible-network)
name: junos
short_description: Use junos cliconf to run command on Juniper Junos OS platform
description:
- This junos plugin provides low level abstraction apis for sending and receiving
  CLI commands from Juniper Junos OS network devices.
version_added: 1.0.0
options:
  config_commands:
    description:
    - Specifies a list of commands that can make configuration changes
      to the target device.
    - When `ansible_network_single_user_mode` is enabled, if a command sent
      to the device is present in this list, the existing cache is invalidated.
    version_added: 2.0.0
    type: list
    elements: str
    default: []
    vars:
    - name: ansible_junos_config_commands
"""

import json
import re

from functools import wraps
from itertools import chain

from ansible.errors import AnsibleConnectionFailure
from ansible.module_utils._text import to_text
from ansible.module_utils.common._collections_compat import Mapping
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list
from ansible_collections.ansible.netcommon.plugins.plugin_utils.cliconf_base 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 __init__(self, *args, **kwargs):
        self._device_info = {}
        super(Cliconf, self).__init__(*args, **kwargs)

    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):
        if not self._device_info:
            device_info = {}
            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)

            self._device_info = device_info

        return self._device_info

    def get_config(self, source="running", flags=None, format="text"):
        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 override {0}".format(replace)

        for line in to_list(candidate):
            if not isinstance(line, Mapping):
                line = {"command": line}
            cmd = line["command"]
            try:
                results.append(self.send_command(**line))
            except AnsibleConnectionFailure as exc:
                if "error: commit failed" in exc.message:
                    self.discard_changes()
                raise
            requests.append(cmd)

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

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

        else:
            self.send_command("top")
            self.discard_changes()

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

    def get(
        self,
        command,
        prompt=None,
        answer=None,
        sendonly=False,
        newline=True,
        output=None,
        check_all=False,
    ):
        if output:
            command = self._get_command_with_output(command, output)
        return self.send_command(
            command=command,
            prompt=prompt,
            answer=answer,
            sendonly=sendonly,
            newline=newline,
            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"

        try:
            response = self.send_command(command)
        except AnsibleConnectionFailure:
            self.discard_changes()
            raise

        return response

    @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] or len(r) == 4 and r[1].startswith("- version"):
            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

    @configure
    def restore(self, filename=None, path=""):
        if not filename:
            raise ValueError("'file_name' value is required for restore")
        cmd = f"load override {path}{filename}"
        resp = self.send_command(cmd)
        self.commit()
        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 = super(Cliconf, self).get_capabilities()
        result["rpc"] += [
            "commit",
            "discard_changes",
            "run_commands",
            "compare_configuration",
            "validate",
            "get_diff",
        ]
        result["device_operations"] = self.get_device_operations()
        result.update(self.get_option_values())
        return json.dumps(result)

    def set_cli_prompt_context(self):
        """
        Make sure we are in the operational cli mode
        :return: None
        """
        if self._connection.connected:
            self._update_cli_prompt_context(config_context="#")

    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