|
| 1 | +import click |
| 2 | + |
| 3 | +import utilities_common.cli as clicommon |
| 4 | +from .utils import log |
| 5 | + |
| 6 | +# |
| 7 | +# 'vlan' group ('config vlan ...') |
| 8 | +# |
| 9 | +@click.group(cls=clicommon.AbbreviationGroup, name='vlan') |
| 10 | +def vlan(): |
| 11 | + """VLAN-related configuration tasks""" |
| 12 | + pass |
| 13 | + |
| 14 | +@vlan.command('add') |
| 15 | +@click.argument('vid', metavar='<vid>', required=True, type=int) |
| 16 | +@clicommon.pass_db |
| 17 | +def add_vlan(db, vid): |
| 18 | + """Add VLAN""" |
| 19 | + |
| 20 | + ctx = click.get_current_context() |
| 21 | + |
| 22 | + if not clicommon.is_vlanid_in_range(vid): |
| 23 | + ctx.fail("Invalid VLAN ID {} (1-4094)".format(vid)) |
| 24 | + |
| 25 | + vlan = 'Vlan{}'.format(vid) |
| 26 | + if clicommon.check_if_vlanid_exist(db.cfgdb, vlan): |
| 27 | + ctx.fail("{} already exists".format(vlan)) |
| 28 | + |
| 29 | + db.cfgdb.set_entry('VLAN', vlan, {'vlanid': vid}) |
| 30 | + |
| 31 | +@vlan.command('del') |
| 32 | +@click.argument('vid', metavar='<vid>', required=True, type=int) |
| 33 | +@clicommon.pass_db |
| 34 | +def del_vlan(db, vid): |
| 35 | + """Delete VLAN""" |
| 36 | + |
| 37 | + log.log_info("'vlan del {}' executing...".format(vid)) |
| 38 | + |
| 39 | + ctx = click.get_current_context() |
| 40 | + |
| 41 | + if not clicommon.is_vlanid_in_range(vid): |
| 42 | + ctx.fail("Invalid VLAN ID {} (1-4094)".format(vid)) |
| 43 | + |
| 44 | + vlan = 'Vlan{}'.format(vid) |
| 45 | + if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False: |
| 46 | + ctx.fail("{} does not exist".format(vlan)) |
| 47 | + |
| 48 | + keys = [ (k, v) for k, v in db.cfgdb.get_table('VLAN_MEMBER') if k == 'Vlan{}'.format(vid) ] |
| 49 | + for k in keys: |
| 50 | + db.cfgdb.set_entry('VLAN_MEMBER', k, None) |
| 51 | + db.cfgdb.set_entry('VLAN', 'Vlan{}'.format(vid), None) |
| 52 | + |
| 53 | +# |
| 54 | +# 'member' group ('config vlan member ...') |
| 55 | +# |
| 56 | +@vlan.group(cls=clicommon.AbbreviationGroup, name='member') |
| 57 | +def vlan_member(): |
| 58 | + pass |
| 59 | + |
| 60 | +@vlan_member.command('add') |
| 61 | +@click.argument('vid', metavar='<vid>', required=True, type=int) |
| 62 | +@click.argument('port', metavar='port', required=True) |
| 63 | +@click.option('-u', '--untagged', is_flag=True) |
| 64 | +@clicommon.pass_db |
| 65 | +def add_vlan_member(db, vid, port, untagged): |
| 66 | + """Add VLAN member""" |
| 67 | + |
| 68 | + ctx = click.get_current_context() |
| 69 | + |
| 70 | + log.log_info("'vlan member add {} {}' executing...".format(vid, port)) |
| 71 | + |
| 72 | + if not clicommon.is_vlanid_in_range(vid): |
| 73 | + ctx.fail("Invalid VLAN ID {} (1-4094)".format(vid)) |
| 74 | + |
| 75 | + vlan = 'Vlan{}'.format(vid) |
| 76 | + if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False: |
| 77 | + ctx.fail("{} does not exist".format(vlan)) |
| 78 | + |
| 79 | + if clicommon.get_interface_naming_mode() == "alias": |
| 80 | + alias = port |
| 81 | + iface_alias_converter = clicommon.InterfaceAliasConverter(db) |
| 82 | + port = iface_alias_converter.alias_to_name(alias) |
| 83 | + if port is None: |
| 84 | + ctx.fail("cannot find port name for alias {}".format(alias)) |
| 85 | + |
| 86 | + if clicommon.is_port_mirror_dst_port(db.cfgdb, port): |
| 87 | + ctx.fail("{} is configured as mirror destination port".format(port)) |
| 88 | + |
| 89 | + if clicommon.is_port_vlan_member(db.cfgdb, port, vlan): |
| 90 | + ctx.fail("{} is already a member of {}".format(port, vlan)) |
| 91 | + |
| 92 | + if clicommon.is_valid_port(db.cfgdb, port): |
| 93 | + is_port = True |
| 94 | + elif clicommon.is_valid_portchannel(db.cfgdb, port): |
| 95 | + is_port = False |
| 96 | + else: |
| 97 | + ctx.fail("{} does not exist".format(port)) |
| 98 | + |
| 99 | + if (is_port and clicommon.is_port_router_interface(db.cfgdb, port)) or \ |
| 100 | + (not is_port and clicommon.is_pc_router_interface(db.cfgdb, port)): |
| 101 | + ctx.fail("{} is a router interface!".format(port)) |
| 102 | + |
| 103 | + db.cfgdb.set_entry('VLAN_MEMBER', (vlan, port), {'tagging_mode': "untagged" if untagged else "tagged" }) |
| 104 | + |
| 105 | +@vlan_member.command('del') |
| 106 | +@click.argument('vid', metavar='<vid>', required=True, type=int) |
| 107 | +@click.argument('port', metavar='<port>', required=True) |
| 108 | +@clicommon.pass_db |
| 109 | +def del_vlan_member(db, vid, port): |
| 110 | + """Delete VLAN member""" |
| 111 | + |
| 112 | + ctx = click.get_current_context() |
| 113 | + |
| 114 | + log.log_info("'vlan member del {} {}' executing...".format(vid, port)) |
| 115 | + |
| 116 | + if not clicommon.is_vlanid_in_range(vid): |
| 117 | + ctx.fail("Invalid VLAN ID {} (1-4094)".format(vid)) |
| 118 | + |
| 119 | + vlan = 'Vlan{}'.format(vid) |
| 120 | + if clicommon.check_if_vlanid_exist(db.cfgdb, vlan) == False: |
| 121 | + ctx.fail("{} does not exist".format(vlan)) |
| 122 | + |
| 123 | + if clicommon.get_interface_naming_mode() == "alias": |
| 124 | + alias = port |
| 125 | + iface_alias_converter = clicommon.InterfaceAliasConverter(db) |
| 126 | + port = iface_alias_converter.alias_to_name(alias) |
| 127 | + if port is None: |
| 128 | + ctx.fail("cannot find port name for alias {}".format(alias)) |
| 129 | + |
| 130 | + if not clicommon.is_port_vlan_member(db.cfgdb, port, vlan): |
| 131 | + ctx.fail("{} is not a member of {}".format(port, vlan)) |
| 132 | + |
| 133 | + db.cfgdb.set_entry('VLAN_MEMBER', (vlan, port), None) |
| 134 | + |
| 135 | +@vlan.group(cls=clicommon.AbbreviationGroup, name='dhcp_relay') |
| 136 | +def vlan_dhcp_relay(): |
| 137 | + pass |
| 138 | + |
| 139 | +@vlan_dhcp_relay.command('add') |
| 140 | +@click.argument('vid', metavar='<vid>', required=True, type=int) |
| 141 | +@click.argument('dhcp_relay_destination_ip', metavar='<dhcp_relay_destination_ip>', required=True) |
| 142 | +@clicommon.pass_db |
| 143 | +def add_vlan_dhcp_relay_destination(db, vid, dhcp_relay_destination_ip): |
| 144 | + """ Add a destination IP address to the VLAN's DHCP relay """ |
| 145 | + |
| 146 | + ctx = click.get_current_context() |
| 147 | + |
| 148 | + if not clicommon.is_ipaddress(dhcp_relay_destination_ip): |
| 149 | + ctx.fail('{} is invalid IP address'.format(dhcp_relay_destination_ip)) |
| 150 | + |
| 151 | + vlan_name = 'Vlan{}'.format(vid) |
| 152 | + vlan = db.cfgdb.get_entry('VLAN', vlan_name) |
| 153 | + if len(vlan) == 0: |
| 154 | + ctx.fail("{} doesn't exist".format(vlan_name)) |
| 155 | + |
| 156 | + dhcp_relay_dests = vlan.get('dhcp_servers', []) |
| 157 | + if dhcp_relay_destination_ip in dhcp_relay_dests: |
| 158 | + click.echo("{} is already a DHCP relay destination for {}".format(dhcp_relay_destination_ip, vlan_name)) |
| 159 | + return |
| 160 | + |
| 161 | + dhcp_relay_dests.append(dhcp_relay_destination_ip) |
| 162 | + vlan['dhcp_servers'] = dhcp_relay_dests |
| 163 | + db.cfgdb.set_entry('VLAN', vlan_name, vlan) |
| 164 | + click.echo("Added DHCP relay destination address {} to {}".format(dhcp_relay_destination_ip, vlan_name)) |
| 165 | + try: |
| 166 | + click.echo("Restarting DHCP relay service...") |
| 167 | + clicommon.run_command("systemctl restart dhcp_relay", display_cmd=False) |
| 168 | + except SystemExit as e: |
| 169 | + ctx.fail("Restart service dhcp_relay failed with error {}".format(e)) |
| 170 | + |
| 171 | +@vlan_dhcp_relay.command('del') |
| 172 | +@click.argument('vid', metavar='<vid>', required=True, type=int) |
| 173 | +@click.argument('dhcp_relay_destination_ip', metavar='<dhcp_relay_destination_ip>', required=True) |
| 174 | +@clicommon.pass_db |
| 175 | +def del_vlan_dhcp_relay_destination(db, vid, dhcp_relay_destination_ip): |
| 176 | + """ Remove a destination IP address from the VLAN's DHCP relay """ |
| 177 | + |
| 178 | + ctx = click.get_current_context() |
| 179 | + |
| 180 | + if not clicommon.is_ipaddress(dhcp_relay_destination_ip): |
| 181 | + ctx.fail('{} is invalid IP address'.format(dhcp_relay_destination_ip)) |
| 182 | + |
| 183 | + vlan_name = 'Vlan{}'.format(vid) |
| 184 | + vlan = db.cfgdb.get_entry('VLAN', vlan_name) |
| 185 | + if len(vlan) == 0: |
| 186 | + ctx.fail("{} doesn't exist".format(vlan_name)) |
| 187 | + |
| 188 | + dhcp_relay_dests = vlan.get('dhcp_servers', []) |
| 189 | + if not dhcp_relay_destination_ip in dhcp_relay_dests: |
| 190 | + ctx.fail("{} is not a DHCP relay destination for {}".format(dhcp_relay_destination_ip, vlan_name)) |
| 191 | + |
| 192 | + dhcp_relay_dests.remove(dhcp_relay_destination_ip) |
| 193 | + if len(dhcp_relay_dests) == 0: |
| 194 | + del vlan['dhcp_servers'] |
| 195 | + else: |
| 196 | + vlan['dhcp_servers'] = dhcp_relay_dests |
| 197 | + db.cfgdb.set_entry('VLAN', vlan_name, vlan) |
| 198 | + click.echo("Removed DHCP relay destination address {} from {}".format(dhcp_relay_destination_ip, vlan_name)) |
| 199 | + try: |
| 200 | + click.echo("Restarting DHCP relay service...") |
| 201 | + clicommon.run_command("systemctl restart dhcp_relay", display_cmd=False) |
| 202 | + except SystemExit as e: |
| 203 | + ctx.fail("Restart service dhcp_relay failed with error {}".format(e)) |
0 commit comments