def _parse_network_data_links(links_data): links = [] for link_data in links_data: link_id = link_data.get("id") mac = link_data.get("ethernet_mac_address") mtu = link_data.get("mtu") openstack_link_type = link_data.get("type") bond = None vlan_id = None vlan_link = None if openstack_link_type == NETWORK_LINK_TYPE_BOND: link_type = network_model.LINK_TYPE_BOND bond_links = link_data.get("bond_links") bond_mode = link_data.get("bond_mode") bond_xmit_hash_policy = link_data.get("bond_xmit_hash_policy") if bond_mode not in network_model.AVAILABLE_BOND_TYPES: raise exception.CloudbaseInitException( "Unsupported bond mode: %s" % bond_mode) if (bond_xmit_hash_policy is not None and bond_xmit_hash_policy not in network_model.AVAILABLE_BOND_LB_ALGORITHMS): raise exception.CloudbaseInitException( "Unsupported bond hash policy: %s" % bond_xmit_hash_policy) bond = network_model.Bond( members=bond_links, type=bond_mode, lb_algorithm=bond_xmit_hash_policy, lacp_rate=None, ) elif openstack_link_type == NETWORK_LINK_TYPE_VLAN: link_type = network_model.LINK_TYPE_VLAN vlan_id = link_data.get("vlan_id") vlan_link = link_data.get("vlan_link") vlan_mac_address = link_data.get("vlan_mac_address") if vlan_mac_address is not None: mac = vlan_mac_address else: # Any other link type is considered physical link_type = network_model.LINK_TYPE_PHYSICAL link = network_model.Link( id=link_id, name=link_id, type=link_type, enabled=True, mac_address=mac, mtu=mtu, bond=bond, vlan_id=vlan_id, vlan_link=vlan_link) links.append(link) return links
def _parse_bond_config_item(self, item): if not item.get('name'): LOG.warning("Bond does not have a name.") return bond_params = item.get('params') if not bond_params: LOG.warning("Bond does not have parameters") return bond_mode = bond_params.get('bond-mode') if bond_mode not in network_model.AVAILABLE_BOND_TYPES: raise exception.CloudbaseInitException( "Unsupported bond mode: %s" % bond_mode) bond_lacp_rate = None if bond_mode == network_model.BOND_TYPE_8023AD: bond_lacp_rate = bond_params.get('bond-lacp-rate') if (bond_lacp_rate and bond_lacp_rate not in network_model.AVAILABLE_BOND_LACP_RATES): raise exception.CloudbaseInitException( "Unsupported bond lacp rate: %s" % bond_lacp_rate) bond_xmit_hash_policy = bond_params.get('xmit_hash_policy') if (bond_xmit_hash_policy and bond_xmit_hash_policy not in network_model.AVAILABLE_BOND_LB_ALGORITHMS): raise exception.CloudbaseInitException( "Unsupported bond hash policy: %s" % bond_xmit_hash_policy) bond_interfaces = item.get('bond_interfaces') bond = network_model.Bond( members=bond_interfaces, type=bond_mode, lb_algorithm=bond_xmit_hash_policy, lacp_rate=bond_lacp_rate, ) link = network_model.Link(id=item.get('name'), name=item.get('name'), type=network_model.LINK_TYPE_BOND, enabled=True, mac_address=item.get('mac_address'), mtu=item.get('mtu'), bond=bond, vlan_link=None, vlan_id=None) return network_model.NetworkDetailsV2(links=[link], networks=self._parse_subnets( item.get("subnets"), link.name), services=[])
def _parse_physical_config_item(self, item): if not item.get('name'): LOG.warning("Physical NIC does not have a name.") return link = network_model.Link(id=item.get('name'), name=item.get('name'), type=network_model.LINK_TYPE_PHYSICAL, enabled=True, mac_address=item.get('mac_address'), mtu=item.get('mtu'), bond=None, vlan_link=None, vlan_id=None) return network_model.NetworkDetailsV2(links=[link], networks=self._parse_subnets( item.get("subnets"), link.name), services=[])
def _get_network_details_v2(self): links = [] link1 = network_model.Link(id=mock.sentinel.link_id1, name=mock.sentinel.link_name1, type=network_model.LINK_TYPE_PHYSICAL, enabled=mock.sentinel.link_enabled1, mac_address=mock.sentinel.link_mac1, mtu=mock.sentinel.link_mtu1, bond=None, vlan_link=None, vlan_id=None) links.append(link1) bond1 = network_model.Bond(members=[mock.sentinel.link_id1], type=mock.sentinel.bond_type1, lb_algorithm=mock.sentinel.bond_lb_algo1, lacp_rate=mock.sentinel.lacp_rate1) bond_link1 = network_model.Link( id=mock.sentinel.bond_link_id1, name=mock.sentinel.bond_link_name1, type=network_model.LINK_TYPE_BOND, enabled=mock.sentinel.bond_link_enabled1, mac_address=mock.sentinel.bond_link_mac1, mtu=mock.sentinel.bond_link_mtu1, bond=bond1, vlan_link=None, vlan_id=None) links.append(bond_link1) vlan_link1 = network_model.Link( id=mock.sentinel.vlan_link_id1, name=mock.sentinel.vlan_link_name1, type=network_model.LINK_TYPE_VLAN, enabled=mock.sentinel.vlan_link_enabled1, mac_address=mock.sentinel.vlan_link_mac1, mtu=mock.sentinel.vlan_link_mtu1, bond=None, vlan_link=mock.sentinel.bond_link_id1, vlan_id=mock.sentinel.vlan_id1) links.append(vlan_link1) networks = [] route1 = network_model.Route(network_cidr=mock.sentinel.network_cidr1, gateway=mock.sentinel.gateway1) route2 = network_model.Route(network_cidr=mock.sentinel.network_cidr2, gateway=mock.sentinel.gateway2) network1 = network_model.Network( link=mock.sentinel.link_id1, address_cidr=mock.sentinel.address_cidr1, dns_nameservers=mock.sentinel.network_dns_list1, routes=[route1, route2]) networks.append(network1) services = [] service1 = network_model.NameServerService( addresses=[mock.sentinel.dns1, mock.sentinel.dns3], search=mock.sentinel.dns_search1) services.append(service1) return network_model.NetworkDetailsV2(links=links, networks=networks, services=services)
def _parse_config_link(config): link_id = config.get("id") name = config.get("name") mac = config.get("mac_address") mtu = config.get("mtu") maas_link_type = config.get("type") subnets = config.get("subnets", []) params = config.get("params", {}) bond = None vlan_id = None vlan_link = None link_enabled = False if maas_link_type == MAAS_CONFIG_TYPE_PHYSICAL: link_type = network_model.LINK_TYPE_PHYSICAL link_enabled = MaaSHttpService._is_link_enabled(subnets) elif maas_link_type == MAAS_CONFIG_TYPE_BOND: link_type = network_model.LINK_TYPE_BOND bond_interfaces = config.get("bond_interfaces") bond_mode = params.get("bond-mode") bond_xmit_hash_policy = params.get("bond-xmit-hash-policy") maas_bond_lacp_rate = params.get("bond-lacp-rate") if bond_mode not in network_model.AVAILABLE_BOND_TYPES: raise exception.CloudbaseInitException( "Unsupported bond mode: %s" % bond_mode) if (bond_xmit_hash_policy is not None and bond_xmit_hash_policy not in network_model.AVAILABLE_BOND_LB_ALGORITHMS): raise exception.CloudbaseInitException( "Unsupported bond hash policy: %s" % bond_xmit_hash_policy) bond = network_model.Bond( members=bond_interfaces, type=bond_mode, lb_algorithm=bond_xmit_hash_policy, lacp_rate=BOND_LACP_RATE_MAP.get(maas_bond_lacp_rate)) link_enabled = True elif maas_link_type == MAAS_CONFIG_TYPE_VLAN: link_type = network_model.LINK_TYPE_VLAN vlan_link = config.get("vlan_link") vlan_id = config.get("vlan_id") link_enabled = True else: raise exception.CloudbaseInitException( "Unsupported MAAS link type: %s" % maas_link_type) link = network_model.Link(id=link_id, name=name, type=link_type, enabled=link_enabled, mac_address=mac, mtu=mtu, bond=bond, vlan_id=vlan_id, vlan_link=vlan_link) networks = [] subnets = config.get("subnets", []) for subnet in subnets: maas_subnet_type = subnet.get("type") if maas_subnet_type == MAAS_SUBNET_TYPE_STATIC: address_cidr = subnet.get("address") gateway = subnet.get("gateway") dns_nameservers = subnet.get("dns_nameservers") # TODO(alexpilotti): Add support for extra routes if gateway is not None: if netaddr.valid_ipv6(gateway): default_network_cidr = u"::/0" else: default_network_cidr = u"0.0.0.0/0" routes = [ network_model.Route(network_cidr=default_network_cidr, gateway=gateway) ] else: routes = [] net = network_model.Network( link=link_id, address_cidr=address_cidr, dns_nameservers=dns_nameservers, routes=routes, ) networks.append(net) return link, networks
def test_network_details_v2(self): expected_bond = nm.Bond( members=["gbe0", "gbe1"], type=nm.BOND_TYPE_ACTIVE_BACKUP, lb_algorithm=None, lacp_rate=None, ) expected_link_bond = nm.Link( id='bond0', name='bond0', type=nm.LINK_TYPE_BOND, enabled=True, mac_address="52:54:00:12:34:00", mtu=1450, bond=expected_bond, vlan_link=None, vlan_id=None, ) expected_link = nm.Link( id='interface0', name='interface0', type=nm.LINK_TYPE_PHYSICAL, enabled=True, mac_address="52:54:00:12:34:00", mtu=1450, bond=None, vlan_link=None, vlan_id=None, ) expected_link_vlan = nm.Link( id='vlan0', name='vlan0', type=nm.LINK_TYPE_VLAN, enabled=True, mac_address="52:54:00:12:34:00", mtu=1450, bond=None, vlan_link='eth1', vlan_id=150, ) expected_network = nm.Network( link='interface0', address_cidr='192.168.1.10/24', dns_nameservers=['192.168.1.11'], routes=[nm.Route(network_cidr='0.0.0.0/0', gateway="192.168.1.1")]) expected_network_bond = nm.Network( link='bond0', address_cidr='192.168.1.10/24', dns_nameservers=['192.168.1.11'], routes=[], ) expected_network_vlan = nm.Network( link='vlan0', address_cidr='192.168.1.10/24', dns_nameservers=['192.168.1.11'], routes=[], ) expected_nameservers = nm.NameServerService( addresses=['192.168.23.2', '8.8.8.8'], search='acme.local') parser_data = """ - type: physical name: interface0 mac_address: "52:54:00:12:34:00" mtu: 1450 subnets: - type: static address: 192.168.1.10 netmask: 255.255.255.0 gateway: 192.168.1.1 dns_nameservers: - 192.168.1.11 - type: bond name: bond0 bond_interfaces: - gbe0 - gbe1 mac_address: "52:54:00:12:34:00" params: bond-mode: active-backup bond-lacp-rate: false mtu: 1450 subnets: - type: static address: 192.168.1.10 netmask: 255.255.255.0 dns_nameservers: - 192.168.1.11 - type: vlan name: vlan0 vlan_link: eth1 vlan_id: 150 mac_address: "52:54:00:12:34:00" mtu: 1450 subnets: - type: static address: 192.168.1.10 netmask: 255.255.255.0 dns_nameservers: - 192.168.1.11 - type: nameserver address: - 192.168.23.2 - 8.8.8.8 search: acme.local """ result = self._parser.parse(serialization.parse_json_yaml(parser_data)) self.assertEqual(result.links[0], expected_link) self.assertEqual(result.networks[0], expected_network) self.assertEqual(result.links[1], expected_link_bond) self.assertEqual(result.networks[1], expected_network_bond) self.assertEqual(result.links[2], expected_link_vlan) self.assertEqual(result.networks[2], expected_network_vlan) self.assertEqual(result.services[0], expected_nameservers)