|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import ipaddress |
| 5 | +import json |
| 6 | +import re |
| 7 | + |
| 8 | +import XenAPIPlugin |
| 9 | + |
| 10 | +from xcpngutils import configure_logging, error_wrapped, run_command |
| 11 | + |
| 12 | + |
| 13 | +def parse_bridge(args): |
| 14 | + bridge = args.get("bridge") |
| 15 | + if bridge is None: |
| 16 | + raise ValueError("bridge parameter is missing") |
| 17 | + return bridge |
| 18 | + |
| 19 | + |
| 20 | +def parse_mac(args): |
| 21 | + MAC_REGEX = re.compile(r"^([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})$") |
| 22 | + mac_addr = args.get("mac", "") |
| 23 | + if not bool(MAC_REGEX.fullmatch(mac_addr)): |
| 24 | + raise ValueError("mac address is invalid or missing") |
| 25 | + |
| 26 | + return mac_addr |
| 27 | + |
| 28 | + |
| 29 | +def parse_iprange(args): |
| 30 | + ip_range = args.get("iprange", "") |
| 31 | + try: |
| 32 | + ipaddress.ip_network(ip_range, strict=False) |
| 33 | + return ip_range |
| 34 | + except ValueError: |
| 35 | + raise ValueError("ip range is missing or invalid") |
| 36 | + |
| 37 | + |
| 38 | +def parse_direction(args): |
| 39 | + # Check that direction is set and valid |
| 40 | + direction = args.get("direction", "").lower().split("/") |
| 41 | + has_to = "to" in direction |
| 42 | + has_from = "from" in direction |
| 43 | + if not (has_to or has_from): |
| 44 | + raise ValueError("valid direction is missing") |
| 45 | + return has_to, has_from |
| 46 | + |
| 47 | + |
| 48 | +def parse_protocol(args): |
| 49 | + VALID_PROTOCOLS = {"tcp", "udp", "icmp", "ip"} |
| 50 | + protocol = args.get("protocol", "unset").lower() |
| 51 | + if protocol not in VALID_PROTOCOLS: |
| 52 | + raise ValueError("valid protocol is missing") |
| 53 | + return protocol |
| 54 | + |
| 55 | + |
| 56 | +def parse_port(args): |
| 57 | + port = args.get("port", "") |
| 58 | + try: |
| 59 | + int(port) |
| 60 | + return port |
| 61 | + except ValueError: |
| 62 | + raise ValueError("port is missing or is invalid") |
| 63 | + |
| 64 | + |
| 65 | +@error_wrapped |
| 66 | +def add_rule(_session, args): |
| 67 | + _LOGGER.info("Calling add_rule with args {}".format(args)) |
| 68 | + |
| 69 | + try: |
| 70 | + _bridge = parse_bridge(args) |
| 71 | + _mac = parse_mac(args) |
| 72 | + _has_to, _has_from = parse_direction(args) |
| 73 | + _protocol = parse_protocol(args) |
| 74 | + _iprange = parse_iprange(args) |
| 75 | + _port = parse_port(args) |
| 76 | + except ValueError as e: |
| 77 | + _LOGGER.error(str(e)) |
| 78 | + return json.dumps( |
| 79 | + { |
| 80 | + "returncode": 1, |
| 81 | + "command": "add_rule", |
| 82 | + "stderr": str(e), |
| 83 | + "stdout": "", |
| 84 | + } |
| 85 | + ) |
| 86 | + |
| 87 | + return json.dumps(True) |
| 88 | + |
| 89 | + |
| 90 | +@error_wrapped |
| 91 | +def del_rule(_session, args): |
| 92 | + _LOGGER.info("Calling del_rule with args {}".format(args)) |
| 93 | + return json.dumps(True) |
| 94 | + |
| 95 | + |
| 96 | +@error_wrapped |
| 97 | +def dump_flows(_session, args): |
| 98 | + _LOGGER.info("Calling check with args {}".format(args)) |
| 99 | + ofctl_cmd = ["ovs-ofctl", "dump-flows"] |
| 100 | + bridge = args.get("bridge") |
| 101 | + |
| 102 | + ofctl_cmd.append(bridge) |
| 103 | + cmd = run_command(ofctl_cmd, check=False) |
| 104 | + return json.dumps(cmd) |
| 105 | + |
| 106 | + |
| 107 | +_LOGGER = configure_logging("sdn-controller") |
| 108 | +if __name__ == "__main__": |
| 109 | + XenAPIPlugin.dispatch( |
| 110 | + { |
| 111 | + "add-rule": add_rule, |
| 112 | + "del-rule": del_rule, |
| 113 | + "dump-flows": dump_flows, |
| 114 | + } |
| 115 | + ) |
0 commit comments