Example #1
0
 def _ensure_application(self):
     if self.app is None:
         self.app = Application(None, {'uri': self.base_uri}, self.auth)
         try:
             self.app.get()
         except exc.MidoApiConnectionRefused:
             self.app = None
             raise
Example #2
0
 def _ensure_application(self):
     if self.app is None:
         self.app = Application(None, {'uri': self.base_uri}, self.auth)
         try:
             self.app.get()
         except exc.MidoApiConnectionRefused:
             self.app = None
             raise
Example #3
0
class MidonetApi(object):

    def __init__(self, base_uri, username, password, project_id=None):
        self.base_uri = base_uri
        self.project_id = project_id
        self.app = None
        self.auth = auth_lib.Auth(base_uri + '/login', username, password,
                                  project_id)

    def delete_router(self, id_):
        self._ensure_application()
        return self.app.delete_router(id_)

    def get_routers(self, query):
        self._ensure_application()
        return self.app.get_routers(query)

    def delete_bridge(self, id_):
        self._ensure_application()
        return self.app.delete_bridge(id_)

    def get_bridges(self, query):
        self._ensure_application()
        return self.app.get_bridges(query)

    def delete_port_group(self, id_):
        self._ensure_application()
        return self.app.delete_port_group(id_)

    def get_port_groups(self, query):
        self._ensure_application()
        return self.app.get_port_groups(query)

    def get_chains(self, query):
        self._ensure_application()
        return self.app.get_chains(query)

    def delete_chain(self, id_):
        self._ensure_application()
        return self.app.delete_chain(id_)

    def get_chain(self, id_):
        self._ensure_application()
        return self.app.get_chain(id_)

    def get_tunnel_zones(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_tunnel_zones(query)

    def get_hosts(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_hosts(query)

    def get_host(self, id_):
        self._ensure_application()
        return self.app.get_host(id_)

    def delete_ad_route(self, id_):
        self._ensure_application()
        return self.app.delete_ad_route(id_)

    def get_ad_route(self, id_):
        self._ensure_application()
        return self.app.get_ad_route(id_)

    def delete_bgp(self, id_):
        self._ensure_application()
        return self.app.delete_bgp(id_)

    def get_bgp(self, id_):
        self._ensure_application()
        return self.app.get_bgp(id_)

    def get_bridge(self, id_):
        self._ensure_application()
        return self.app.get_bridge(id_)

    def get_port_group(self, id_):
        self._ensure_application()
        return self.app.get_port_group(id_)

    def delete_port(self, id_):
        self._ensure_application()
        return self.app.delete_port(id_)

    def get_port(self, id_):
        self._ensure_application()
        return self.app.get_port(id_)

    def delete_route(self, id_):
        self._ensure_application()
        return self.app.delete_route(id_)

    def get_route(self, id_):
        self._ensure_application()
        return self.app.get_route(id_)

    def get_router(self, id_):
        self._ensure_application()
        return self.app.get_router(id_)

    def delete_rule(self, id_):
        self._ensure_application()
        return self.app.delete_rule(id_)

    def get_rule(self, id_):
        self._ensure_application()
        return self.app.get_rule(id_)

    def add_router(self):
        self._ensure_application()
        return self.app.add_router()

    def add_bridge(self):
        self._ensure_application()
        return self.app.add_bridge()

    def add_port_group(self):
        self._ensure_application()
        return self.app.add_port_group()

    def add_chain(self):
        self._ensure_application()
        return self.app.add_chain()

    def add_gre_tunnel_zone(self):
        self._ensure_application()
        return self.app.add_gre_tunnel_zone()

    def add_capwap_tunnel_zone(self):
        self._ensure_application()
        return self.app.add_capwap_tunnel_zone()

    def _ensure_application(self):
        if self.app is None:
            self.app = Application(None, {'uri': self.base_uri}, self.auth)
            self.app.get()
Example #4
0
 def _ensure_application(self):
     if self.app is None:
         self.app = Application(None, {'uri': self.base_uri}, self.auth)
         self.app.get()
Example #5
0
class MidonetApi(object):
    def __init__(self, base_uri, username, password, project_id=None):
        self.base_uri = base_uri.rstrip('/')
        self.project_id = project_id
        self.app = None
        self.auth = auth_lib.Auth(self.base_uri + '/login', username, password,
                                  project_id)

    def get_tenants(self, query={}):
        self._ensure_application()
        return self.app.get_tenants(query)

    def delete_router(self, id_):
        self._ensure_application()
        return self.app.delete_router(id_)

    def get_routers(self, query):
        self._ensure_application()
        return self.app.get_routers(query)

    def delete_bridge(self, id_):
        self._ensure_application()
        return self.app.delete_bridge(id_)

    def get_bridges(self, query):
        self._ensure_application()
        return self.app.get_bridges(query)

    def get_ports(self, query):
        self._ensure_application()
        return self.app.get_ports(query)

    def delete_port_group(self, id_):
        self._ensure_application()
        return self.app.delete_port_group(id_)

    def delete_ip_addr_group(self, id_):
        self._ensure_application()
        return self.app.delete_ip_addr_group(id_)

    def get_port_groups(self, query):
        self._ensure_application()
        return self.app.get_port_groups(query)

    def get_ip_addr_groups(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_ip_addr_groups(query)

    def get_chains(self, query):
        self._ensure_application()
        return self.app.get_chains(query)

    def delete_chain(self, id_):
        self._ensure_application()
        return self.app.delete_chain(id_)

    def get_chain(self, id_):
        self._ensure_application()
        return self.app.get_chain(id_)

    def get_tunnel_zones(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_tunnel_zones(query)

    def get_tunnel_zone(self, id_):
        self._ensure_application()
        return self.app.get_tunnel_zone(id_)

    def get_hosts(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_hosts(query)

    # L4LB resources
    def get_load_balancers(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_load_balancers(query)

    def get_vips(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_vips(query)

    def get_pools(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_pools(query)

    def get_pool_members(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_pool_members(query)

    def get_health_monitors(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_health_monitors(query)

    def get_pool_statistics(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_pool_statistics(query)

    def get_load_balancer(self, id_):
        self._ensure_application()
        return self.app.get_load_balancer(id_)

    def get_vip(self, id_):
        self._ensure_application()
        return self.app.get_vip(id_)

    def get_pool(self, id_):
        self._ensure_application()
        return self.app.get_pool(id_)

    def get_pool_member(self, id_):
        self._ensure_application()
        return self.app.get_pool_member(id_)

    def get_health_monitor(self, id_):
        self._ensure_application()
        return self.app.get_health_monitor(id_)

    def get_pool_statistic(self, id_):
        self._ensure_application()
        return self.app.get_pool_statistic(id_)

    def add_load_balancer(self):
        self._ensure_application()
        return self.app.add_load_balancer()

    def add_vip(self):
        self._ensure_application()
        return self.app.add_vip()

    def add_pool(self):
        self._ensure_application()
        return self.app.add_pool()

    def add_pool_member(self):
        self._ensure_application()
        return self.app.add_pool_member()

    def add_health_monitor(self):
        self._ensure_application()
        return self.app.add_health_monitor()

    def add_pool_statistic(self):
        self._ensure_application()
        return self.app.add_pool_statistic()

    def delete_load_balancer(self, id_):
        self._ensure_application()
        return self.app.delete_load_balancer(id_)

    def delete_vip(self, id_):
        self._ensure_application()
        return self.app.delete_vips(id_)

    def delete_pool(self, id_):
        self._ensure_application()
        return self.app.delete_pool(id_)

    def delete_pool_member(self, id_):
        self._ensure_application()
        return self.app.delete_pool_member(id_)

    def delete_health_monitor(self, id_):
        self._ensure_application()
        return self.app.delete_health_monitor(id_)

    def delete_pool_statistic(self, id_):
        self._ensure_application()
        return self.app.delete_pool_statistic(id_)

    def add_host_interface_port(self, host, port_id, interface_name):
        return host.add_host_interface_port().port_id(port_id) \
            .interface_name(interface_name).create()

    def get_write_version(self):
        self._ensure_application()
        return self.app.get_write_version()

    def get_system_state(self):
        self._ensure_application()
        return self.app.get_system_state()

    def get_host_versions(self, query=None):
        self._ensure_application()
        return self.app.get_host_versions(query)

    def get_host(self, id_):
        self._ensure_application()
        return self.app.get_host(id_)

    def delete_ad_route(self, id_):
        self._ensure_application()
        return self.app.delete_ad_route(id_)

    def get_ad_route(self, id_):
        self._ensure_application()
        return self.app.get_ad_route(id_)

    def delete_bgp(self, id_):
        self._ensure_application()
        return self.app.delete_bgp(id_)

    def get_bgp(self, id_):
        self._ensure_application()
        return self.app.get_bgp(id_)

    def get_bridge(self, id_):
        self._ensure_application()
        return self.app.get_bridge(id_)

    def get_port_group(self, id_):
        self._ensure_application()
        return self.app.get_port_group(id_)

    def get_ip_addr_group(self, id_):
        self._ensure_application()
        return self.app.get_ip_addr_group(id_)

    def delete_port(self, id_):
        self._ensure_application()
        return self.app.delete_port(id_)

    def get_port(self, id_):
        self._ensure_application()
        return self.app.get_port(id_)

    def delete_route(self, id_):
        self._ensure_application()
        return self.app.delete_route(id_)

    def get_route(self, id_):
        self._ensure_application()
        return self.app.get_route(id_)

    def get_router(self, id_):
        self._ensure_application()
        return self.app.get_router(id_)

    def delete_rule(self, id_):
        self._ensure_application()
        return self.app.delete_rule(id_)

    def get_rule(self, id_):
        self._ensure_application()
        return self.app.get_rule(id_)

    def get_tenant(self, id_):
        self._ensure_application()
        return self.app.get_tenant(id_)

    def add_router(self):
        self._ensure_application()
        return self.app.add_router()

    def add_bridge(self):
        self._ensure_application()
        return self.app.add_bridge()

    def _set_op121(self, dhcp, rts):
        opt121_list = []
        for rt in rts:
            rt_net_addr, rt_net_len = rt['destination'].split('/')
            opt121_list.append({
                'destinationPrefix': rt_net_addr,
                'destinationLength': rt_net_len,
                'gatewayAddr': rt['nexthop']
            })
        dhcp.opt121_routes(opt121_list)

    def update_bridge_dhcp(self,
                           bridge,
                           cidr,
                           gateway_ip,
                           host_rts=None,
                           dns_nservers=None,
                           enabled=None):
        mido_cidr = cidr.replace("/", "_")
        dhcp = bridge.get_dhcp_subnet(mido_cidr)
        if dhcp is None:
            return

        if gateway_ip is not None:
            dhcp.default_gateway(gateway_ip)

        if host_rts is not None:
            self._set_op121(dhcp, host_rts)

        if dns_nservers is not None:
            dhcp.dns_server_addrs(dns_nservers)

        if enabled is not None:
            dhcp.enabled(enabled)

        return dhcp.update()

    def add_bridge_dhcp(self,
                        bridge,
                        gateway_ip,
                        cidr,
                        host_rts=None,
                        dns_nservers=None,
                        enabled=True):
        """Creates a dhcp subnet with the provided gateway ip, cidr,
        host routes, and dns name servers.

        :returns: The new dhcp subnet resource.
        :param bridge: Bridge of the new dhcp subnet.
        :param gateway_ip: Single ipv4 address string.
        :param cidr: Subnet represented by cidr notation [ipv4 addr]/[prefix].
        :param host_rts: An array of dictionaries, each of the form:
            {"destination": <ipv4 cidr>, "nexthop": <ipv4 string>}.
        :param dns_nservers: An array of strings representing ipv4 addresses.
        :param enabled: Enable DHCP
        """
        if host_rts is None:
            host_rts = []

        if dns_nservers is None:
            dns_nservers = []

        net_addr, net_len = cidr.split('/')

        dhcp = bridge.add_dhcp_subnet()
        dhcp.default_gateway(gateway_ip)
        dhcp.subnet_prefix(net_addr)
        dhcp.subnet_length(net_len)

        if host_rts:
            self._set_op121(dhcp, host_rts)

        if dns_nservers:
            dhcp.dns_server_addrs(dns_nservers)

        dhcp.enabled(enabled)
        return dhcp.create()

    def add_port_group(self):
        self._ensure_application()
        return self.app.add_port_group()

    def add_ip_addr_group(self):
        self._ensure_application()
        return self.app.add_ip_addr_group()

    def add_chain(self):
        self._ensure_application()
        return self.app.add_chain()

    def add_tunnel_zone(self):
        self._ensure_application()
        return self.app.add_tunnel_zone()

    def add_gre_tunnel_zone(self):
        self._ensure_application()
        return self.app.add_gre_tunnel_zone()

    def add_vxlan_tunnel_zone(self):
        self._ensure_application()
        return self.app.add_vxlan_tunnel_zone()

    def add_vtep_tunnel_zone(self):
        self._ensure_application()
        return self.app.add_vtep_tunnel_zone()

    def add_bridge_port(self, bridge):
        return bridge.add_port()

    def add_router_port(self, router):
        return router.add_port()

    def link(self, port, peer_id):
        port.link(peer_id)

    def unlink(self, port):
        if port.get_peer_id():
            peer_id = port.get_peer_id()
            port.unlink()
            self.delete_port(peer_id)

    def add_router_route(self,
                         router,
                         route_type='Normal',
                         src_network_addr=None,
                         src_network_length=None,
                         dst_network_addr=None,
                         dst_network_length=None,
                         next_hop_port=None,
                         next_hop_gateway=None,
                         weight=100):
        """Add a route to a router."""
        route = router.add_route().type(route_type)
        route = route.src_network_addr(src_network_addr).src_network_length(
            src_network_length).dst_network_addr(
                dst_network_addr).dst_network_length(dst_network_length)
        route = route.next_hop_port(next_hop_port).next_hop_gateway(
            next_hop_gateway).weight(weight)

        return route.create()

    def get_router_routes(self, router_id):
        """Get a list of routes for a given router."""
        router = self.get_router(router_id)
        if router is None:
            raise ValueError("Invalid router_id passed in %s" % router_id)
        return router.get_routes()

    def add_chain_rule(self, chain, action='accept', **kwargs):
        """Add a rule to a chain."""
        # Set default values
        prop_defaults = {
            "nw_src_addr": None,
            "nw_src_length": None,
            "inv_nw_src": False,
            "tp_src": None,
            "inv_tp_src": None,
            "nw_dst_addr": None,
            "nw_dst_length": None,
            "inv_nw_dst_addr": False,
            "tp_dst": None,
            "inv_tp_dst": None,
            "dl_src": None,
            "inv_dl_src": False,
            "dl_dst": None,
            "inv_dl_dst": False,
            "ip_addr_group_src": None,
            "inv_ip_addr_group_src": False,
            "ip_addr_group_dst": None,
            "inv_ip_addr_group_dst": False,
            "nw_proto": None,
            "inv_nw_proto": False,
            "dl_type": None,
            "inv_dl_type": False,
            "jump_chain_id": None,
            "jump_chain_name": None,
            "match_forward_flow": False,
            "match_return_flow": False,
            "position": None,
            "properties": None
        }

        # Initialize the rule with passed-in or default values
        vals = {}
        for (prop, default) in prop_defaults.iteritems():
            vals[prop] = kwargs.get(prop, default)

        rule = chain.add_rule().type(action)
        rule = rule.nw_src_address(vals.get("nw_src_addr"))
        rule = rule.nw_src_length(vals.get("nw_src_length"))
        rule = rule.inv_nw_src(vals.get("inv_nw_src"))
        rule = rule.nw_dst_address(vals.get("nw_dst_addr"))
        rule = rule.nw_dst_length(vals.get("nw_dst_length"))
        rule = rule.inv_nw_dst(vals.get("inv_nw_dst"))
        rule = rule.tp_src(vals.get("tp_src"))
        rule = rule.inv_tp_src(vals.get("inv_tp_src"))
        rule = rule.tp_dst(vals.get("tp_dst"))
        rule = rule.inv_tp_dst(vals.get("inv_tp_dst"))
        rule = rule.dl_src(vals.get("dl_src"))
        rule = rule.inv_dl_src(vals.get("inv_dl_src"))
        rule = rule.dl_dst(vals.get("dl_dst"))
        rule = rule.inv_dl_dst(vals.get("inv_dl_dst"))
        rule = rule.ip_addr_group_src(vals.get("ip_addr_group_src"))
        rule = rule.inv_ip_addr_group_src(vals.get("inv_ip_addr_group_src"))
        rule = rule.ip_addr_group_dst(vals.get("ip_addr_group_dst"))
        rule = rule.inv_ip_addr_group_dst(vals.get("inv_ip_addr_group_dst"))
        rule = rule.nw_proto(vals.get("nw_proto"))
        rule = rule.inv_nw_proto(vals.get("inv_nw_proto"))
        rule = rule.dl_type(vals.get("dl_type"))
        rule = rule.inv_dl_type(vals.get("inv_dl_type"))
        rule = rule.jump_chain_id(vals.get("jump_chain_id"))
        rule = rule.jump_chain_name(vals.get("jump_chain_name"))
        rule = rule.match_forward_flow(vals.get("match_forward_flow"))
        rule = rule.match_return_flow(vals.get("match_return_flow"))
        rule = rule.position(vals.get("position"))
        rule = rule.properties(vals.get("properties"))
        return rule.create()

    def get_vteps(self):
        self._ensure_application()
        return self.app.get_vteps()

    def add_vtep(self):
        self._ensure_application()
        return self.app.add_vtep()

    def get_vtep(self, mgmt_ip):
        self._ensure_application()
        return self.app.get_vtep(mgmt_ip)

    def delete_vtep(self, mgmt_ip):
        self._ensure_application()
        return self.app.delete_vtep(mgmt_ip)

    def _ensure_application(self):
        if self.app is None:
            self.app = Application(None, {'uri': self.base_uri}, self.auth)
            try:
                self.app.get()
            except exc.MidoApiConnectionRefused:
                self.app = None
                raise
Example #6
0
class MidonetApi(object):

    def __init__(self, base_uri, username, password, project_id=None):
        self.base_uri = base_uri.rstrip('/')
        self.project_id = project_id
        self.app = None
        self.auth = auth_lib.Auth(self.base_uri + '/login', username, password,
                                  project_id)

    def get_tenants(self, query={}):
        self._ensure_application()
        return self.app.get_tenants(query)

    def delete_router(self, id_):
        self._ensure_application()
        return self.app.delete_router(id_)

    def get_routers(self, query):
        self._ensure_application()
        return self.app.get_routers(query)

    def delete_bridge(self, id_):
        self._ensure_application()
        return self.app.delete_bridge(id_)

    def get_bridges(self, query):
        self._ensure_application()
        return self.app.get_bridges(query)

    def get_ports(self, query):
        self._ensure_application()
        return self.app.get_ports(query)

    def delete_port_group(self, id_):
        self._ensure_application()
        return self.app.delete_port_group(id_)

    def delete_ip_addr_group(self, id_):
        self._ensure_application()
        return self.app.delete_ip_addr_group(id_)

    def get_port_groups(self, query):
        self._ensure_application()
        return self.app.get_port_groups(query)

    def get_ip_addr_groups(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_ip_addr_groups(query)

    def get_chains(self, query):
        self._ensure_application()
        return self.app.get_chains(query)

    def delete_chain(self, id_):
        self._ensure_application()
        return self.app.delete_chain(id_)

    def get_chain(self, id_):
        self._ensure_application()
        return self.app.get_chain(id_)

    def get_tunnel_zones(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_tunnel_zones(query)

    def get_tunnel_zone(self, id_):
        self._ensure_application()
        return self.app.get_tunnel_zone(id_)

    def get_hosts(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_hosts(query)

    # L4LB resources
    def get_load_balancers(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_load_balancers(query)

    def get_vips(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_vips(query)

    def get_pools(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_pools(query)

    def get_pool_members(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_pool_members(query)

    def get_health_monitors(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_health_monitors(query)

    def get_pool_statistics(self, query=None):
        if query is None:
            query = {}
        self._ensure_application()
        return self.app.get_pool_statistics(query)

    def get_load_balancer(self, id_):
        self._ensure_application()
        return self.app.get_load_balancer(id_)

    def get_vip(self, id_):
        self._ensure_application()
        return self.app.get_vip(id_)

    def get_pool(self, id_):
        self._ensure_application()
        return self.app.get_pool(id_)

    def get_pool_member(self, id_):
        self._ensure_application()
        return self.app.get_pool_member(id_)

    def get_health_monitor(self, id_):
        self._ensure_application()
        return self.app.get_health_monitor(id_)

    def get_pool_statistic(self, id_):
        self._ensure_application()
        return self.app.get_pool_statistic(id_)

    def add_load_balancer(self):
        self._ensure_application()
        return self.app.add_load_balancer()

    def add_vip(self):
        self._ensure_application()
        return self.app.add_vip()

    def add_pool(self):
        self._ensure_application()
        return self.app.add_pool()

    def add_pool_member(self):
        self._ensure_application()
        return self.app.add_pool_member()

    def add_health_monitor(self):
        self._ensure_application()
        return self.app.add_health_monitor()

    def add_pool_statistic(self):
        self._ensure_application()
        return self.app.add_pool_statistic()

    def delete_load_balancer(self, id_):
        self._ensure_application()
        return self.app.delete_load_balancer(id_)

    def delete_vip(self, id_):
        self._ensure_application()
        return self.app.delete_vips(id_)

    def delete_pool(self, id_):
        self._ensure_application()
        return self.app.delete_pool(id_)

    def delete_pool_member(self, id_):
        self._ensure_application()
        return self.app.delete_pool_member(id_)

    def delete_health_monitor(self, id_):
        self._ensure_application()
        return self.app.delete_health_monitor(id_)

    def delete_pool_statistic(self, id_):
        self._ensure_application()
        return self.app.delete_pool_statistic(id_)

    def add_host_interface_port(self, host, port_id, interface_name):
        return host.add_host_interface_port().port_id(port_id) \
            .interface_name(interface_name).create()

    def get_write_version(self):
        self._ensure_application()
        return self.app.get_write_version()

    def get_system_state(self):
        self._ensure_application()
        return self.app.get_system_state()

    def get_host_versions(self, query=None):
        self._ensure_application()
        return self.app.get_host_versions(query)

    def get_host(self, id_):
        self._ensure_application()
        return self.app.get_host(id_)

    def delete_ad_route(self, id_):
        self._ensure_application()
        return self.app.delete_ad_route(id_)

    def get_ad_route(self, id_):
        self._ensure_application()
        return self.app.get_ad_route(id_)

    def delete_bgp(self, id_):
        self._ensure_application()
        return self.app.delete_bgp(id_)

    def get_bgp(self, id_):
        self._ensure_application()
        return self.app.get_bgp(id_)

    def get_bridge(self, id_):
        self._ensure_application()
        return self.app.get_bridge(id_)

    def get_port_group(self, id_):
        self._ensure_application()
        return self.app.get_port_group(id_)

    def get_ip_addr_group(self, id_):
        self._ensure_application()
        return self.app.get_ip_addr_group(id_)

    def delete_port(self, id_):
        self._ensure_application()
        return self.app.delete_port(id_)

    def get_port(self, id_):
        self._ensure_application()
        return self.app.get_port(id_)

    def delete_route(self, id_):
        self._ensure_application()
        return self.app.delete_route(id_)

    def get_route(self, id_):
        self._ensure_application()
        return self.app.get_route(id_)

    def get_router(self, id_):
        self._ensure_application()
        return self.app.get_router(id_)

    def delete_rule(self, id_):
        self._ensure_application()
        return self.app.delete_rule(id_)

    def get_rule(self, id_):
        self._ensure_application()
        return self.app.get_rule(id_)

    def get_tenant(self, id_):
        self._ensure_application()
        return self.app.get_tenant(id_)

    def add_router(self):
        self._ensure_application()
        return self.app.add_router()

    def add_bridge(self):
        self._ensure_application()
        return self.app.add_bridge()

    def _set_op121(self, dhcp, rts):
        opt121_list = []
        for rt in rts:
            rt_net_addr, rt_net_len = rt['destination'].split('/')
            opt121_list.append({'destinationPrefix': rt_net_addr,
                                'destinationLength': rt_net_len,
                                'gatewayAddr': rt['nexthop']})
        dhcp.opt121_routes(opt121_list)

    def update_bridge_dhcp(self, bridge, cidr, gateway_ip, host_rts=None,
                           dns_nservers=None, enabled=None):
        mido_cidr = cidr.replace("/", "_")
        dhcp = bridge.get_dhcp_subnet(mido_cidr)
        if dhcp is None:
            return

        if gateway_ip is not None:
            dhcp.default_gateway(gateway_ip)

        if host_rts is not None:
            self._set_op121(dhcp, host_rts)

        if dns_nservers is not None:
            dhcp.dns_server_addrs(dns_nservers)

        if enabled is not None:
            dhcp.enabled(enabled)

        return dhcp.update()

    def add_bridge_dhcp(self, bridge, gateway_ip, cidr, host_rts=None,
                        dns_nservers=None, enabled=True):
        """Creates a dhcp subnet with the provided gateway ip, cidr,
        host routes, and dns name servers.

        :returns: The new dhcp subnet resource.
        :param bridge: Bridge of the new dhcp subnet.
        :param gateway_ip: Single ipv4 address string.
        :param cidr: Subnet represented by cidr notation [ipv4 addr]/[prefix].
        :param host_rts: An array of dictionaries, each of the form:
            {"destination": <ipv4 cidr>, "nexthop": <ipv4 string>}.
        :param dns_nservers: An array of strings representing ipv4 addresses.
        :param enabled: Enable DHCP
        """
        if host_rts is None:
            host_rts = []

        if dns_nservers is None:
            dns_nservers = []

        net_addr, net_len = cidr.split('/')

        dhcp = bridge.add_dhcp_subnet()
        dhcp.default_gateway(gateway_ip)
        dhcp.subnet_prefix(net_addr)
        dhcp.subnet_length(net_len)

        if host_rts:
            self._set_op121(dhcp, host_rts)

        if dns_nservers:
            dhcp.dns_server_addrs(dns_nservers)

        dhcp.enabled(enabled)
        return dhcp.create()

    def add_port_group(self):
        self._ensure_application()
        return self.app.add_port_group()

    def add_ip_addr_group(self):
        self._ensure_application()
        return self.app.add_ip_addr_group()

    def add_chain(self):
        self._ensure_application()
        return self.app.add_chain()

    def add_tunnel_zone(self):
        self._ensure_application()
        return self.app.add_tunnel_zone()

    def add_gre_tunnel_zone(self):
        self._ensure_application()
        return self.app.add_gre_tunnel_zone()

    def add_vxlan_tunnel_zone(self):
        self._ensure_application()
        return self.app.add_vxlan_tunnel_zone()

    def add_bridge_port(self, bridge):
        return bridge.add_port()

    def add_router_port(self, router):
        return router.add_port()

    def link(self, port, peer_id):
        port.link(peer_id)

    def unlink(self, port):
        if port.get_peer_id():
            peer_id = port.get_peer_id()
            port.unlink()
            self.delete_port(peer_id)

    def add_router_route(self, router, route_type='Normal',
                         src_network_addr=None, src_network_length=None,
                         dst_network_addr=None, dst_network_length=None,
                         next_hop_port=None, next_hop_gateway=None,
                         weight=100):
        """Add a route to a router."""
        route = router.add_route().type(route_type)
        route = route.src_network_addr(src_network_addr).src_network_length(
            src_network_length).dst_network_addr(
                dst_network_addr).dst_network_length(dst_network_length)
        route = route.next_hop_port(next_hop_port).next_hop_gateway(
            next_hop_gateway).weight(weight)

        return route.create()

    def get_router_routes(self, router_id):
        """Get a list of routes for a given router."""
        router = self.get_router(router_id)
        if router is None:
            raise ValueError("Invalid router_id passed in %s" % router_id)
        return router.get_routes()

    def add_chain_rule(self, chain, action='accept', **kwargs):
        """Add a rule to a chain."""
        # Set default values
        prop_defaults = {
            "nw_src_addr": None,
            "nw_src_length": None,
            "inv_nw_src": False,
            "tp_src": None,
            "inv_tp_src": None,
            "nw_dst_addr": None,
            "nw_dst_length": None,
            "inv_nw_dst_addr": False,
            "tp_dst": None,
            "inv_tp_dst": None,
            "dl_src": None,
            "inv_dl_src": False,
            "dl_dst": None,
            "inv_dl_dst": False,
            "ip_addr_group_src": None,
            "inv_ip_addr_group_src": False,
            "ip_addr_group_dst": None,
            "inv_ip_addr_group_dst": False,
            "nw_proto": None,
            "inv_nw_proto": False,
            "dl_type": None,
            "inv_dl_type": False,
            "jump_chain_id": None,
            "jump_chain_name": None,
            "match_forward_flow": False,
            "match_return_flow": False,
            "position": None,
            "properties": None
        }

        # Initialize the rule with passed-in or default values
        vals = {}
        for (prop, default) in prop_defaults.iteritems():
            vals[prop] = kwargs.get(prop, default)

        rule = chain.add_rule().type(action)
        rule = rule.nw_src_address(vals.get("nw_src_addr"))
        rule = rule.nw_src_length(vals.get("nw_src_length"))
        rule = rule.inv_nw_src(vals.get("inv_nw_src"))
        rule = rule.nw_dst_address(vals.get("nw_dst_addr"))
        rule = rule.nw_dst_length(vals.get("nw_dst_length"))
        rule = rule.inv_nw_dst(vals.get("inv_nw_dst"))
        rule = rule.tp_src(vals.get("tp_src"))
        rule = rule.inv_tp_src(vals.get("inv_tp_src"))
        rule = rule.tp_dst(vals.get("tp_dst"))
        rule = rule.inv_tp_dst(vals.get("inv_tp_dst"))
        rule = rule.dl_src(vals.get("dl_src"))
        rule = rule.inv_dl_src(vals.get("inv_dl_src"))
        rule = rule.dl_dst(vals.get("dl_dst"))
        rule = rule.inv_dl_dst(vals.get("inv_dl_dst"))
        rule = rule.ip_addr_group_src(vals.get("ip_addr_group_src"))
        rule = rule.inv_ip_addr_group_src(vals.get("inv_ip_addr_group_src"))
        rule = rule.ip_addr_group_dst(vals.get("ip_addr_group_dst"))
        rule = rule.inv_ip_addr_group_dst(vals.get("inv_ip_addr_group_dst"))
        rule = rule.nw_proto(vals.get("nw_proto"))
        rule = rule.inv_nw_proto(vals.get("inv_nw_proto"))
        rule = rule.dl_type(vals.get("dl_type"))
        rule = rule.inv_dl_type(vals.get("inv_dl_type"))
        rule = rule.jump_chain_id(vals.get("jump_chain_id"))
        rule = rule.jump_chain_name(vals.get("jump_chain_name"))
        rule = rule.match_forward_flow(vals.get("match_forward_flow"))
        rule = rule.match_return_flow(vals.get("match_return_flow"))
        rule = rule.position(vals.get("position"))
        rule = rule.properties(vals.get("properties"))
        return rule.create()

    def get_vteps(self):
        self._ensure_application()
        return self.app.get_vteps()

    def add_vtep(self):
        self._ensure_application()
        return self.app.add_vtep()

    def get_vtep(self, mgmt_ip):
        self._ensure_application()
        return self.app.get_vtep(mgmt_ip)

    def delete_vtep(self, mgmt_ip):
        self._ensure_application()
        return self.app.delete_vtep(mgmt_ip)

    def _ensure_application(self):
        if self.app is None:
            self.app = Application(None, {'uri': self.base_uri}, self.auth)
            try:
                self.app.get()
            except exc.MidoApiConnectionRefused:
                self.app = None
                raise

    def install_license(self, file):
        self._ensure_application()
        return self.app.install_license(file)

    def get_licenses(self):
        self._ensure_application()
        return self.app.get_licenses()

    def get_license(self, id_):
        self._ensure_application()
        return self.app.get_license(id_)

    def get_license_status(self):
        self._ensure_application()
        return self.app.get_license_status()