1# (C) 2017 Red Hat Inc. 2# Copyright (C) 2017 Lenovo. 3# 4# GNU General Public License v3.0+ 5# 6# This program is distributed in the hope that it will be useful, 7# but WITHOUT ANY WARRANTY; without even the implied warranty of 8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9# GNU General Public License for more details. 10# 11# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 12# 13# Contains CLIConf Plugin methods for CNOS Modules 14# Lenovo Networking 15# 16from __future__ import (absolute_import, division, print_function) 17__metaclass__ = type 18 19DOCUMENTATION = """ 20--- 21cliconf: cnos 22short_description: Use cnos cliconf to run command on Lenovo CNOS platform 23description: 24 - This cnos plugin provides low level abstraction apis for 25 sending and receiving CLI commands from Lenovo CNOS network devices. 26version_added: 2.6 27""" 28 29import re 30import json 31 32from ansible.errors import AnsibleConnectionFailure 33from ansible.module_utils.common._collections_compat import Mapping 34from ansible.module_utils._text import to_bytes, to_text 35from ansible.module_utils.network.common.utils import to_list 36from ansible.plugins.cliconf import CliconfBase, enable_mode 37 38 39class Cliconf(CliconfBase): 40 41 def get_device_info(self): 42 device_info = {} 43 44 device_info['network_os'] = 'cnos' 45 reply = self.get('show sys-info') 46 data = to_text(reply, errors='surrogate_or_strict').strip() 47 host = self.get('show hostname') 48 hostname = to_text(host, errors='surrogate_or_strict').strip() 49 if data: 50 device_info['network_os_version'] = self.parse_version(data) 51 device_info['network_os_model'] = self.parse_model(data) 52 device_info['network_os_hostname'] = hostname 53 54 return device_info 55 56 def parse_version(self, data): 57 for line in data.split('\n'): 58 line = line.strip() 59 match = re.match(r'System Software Revision (.*?)', 60 line, re.M | re.I) 61 if match: 62 vers = line.split(':') 63 ver = vers[1].strip() 64 return ver 65 return "NA" 66 67 def parse_model(self, data): 68 for line in data.split('\n'): 69 line = line.strip() 70 match = re.match(r'System Model (.*?)', line, re.M | re.I) 71 if match: 72 mdls = line.split(':') 73 mdl = mdls[1].strip() 74 return mdl 75 return "NA" 76 77 @enable_mode 78 def get_config(self, source='running', format='text', flags=None): 79 if source not in ('running', 'startup'): 80 msg = "fetching configuration from %s is not supported" 81 return self.invalid_params(msg % source) 82 if source == 'running': 83 cmd = 'show running-config' 84 else: 85 cmd = 'show startup-config' 86 return self.send_command(cmd) 87 88 @enable_mode 89 def edit_config(self, candidate=None, commit=True, 90 replace=None, comment=None): 91 resp = {} 92 results = [] 93 requests = [] 94 if commit: 95 self.send_command('configure terminal') 96 for line in to_list(candidate): 97 if not isinstance(line, Mapping): 98 line = {'command': line} 99 100 cmd = line['command'] 101 if cmd != 'end' and cmd[0] != '!': 102 results.append(self.send_command(**line)) 103 requests.append(cmd) 104 105 self.send_command('end') 106 else: 107 raise ValueError('check mode is not supported') 108 109 resp['request'] = requests 110 resp['response'] = results 111 return resp 112 113 def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False): 114 return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all) 115 116 def get_capabilities(self): 117 result = super(Cliconf, self).get_capabilities() 118 return json.dumps(result) 119 120 def set_cli_prompt_context(self): 121 """ 122 Make sure we are in the operational cli mode 123 :return: None 124 """ 125 if self._connection.connected: 126 out = self._connection.get_prompt() 127 128 if out is None: 129 raise AnsibleConnectionFailure(message=u'cli prompt is not identified from the last received' 130 u' response window: %s' % self._connection._last_recv_window) 131 132 if to_text(out, errors='surrogate_then_replace').strip().endswith(')#'): 133 self._connection.queue_message('vvvv', 'In Config mode, sending exit to device') 134 self._connection.send_command('exit') 135 else: 136 self._connection.send_command('enable') 137