def set_route_targets_config(self): if self.policy_config is None: self.policy_config = PolicyOptions(comment=DMUtils.policy_options_comment()) for route_target in self.route_targets: comm = CommunityType(name=DMUtils.make_community_name(route_target), members=route_target) self.policy_config.add_community(comm) # add community for switch options comm = CommunityType(name=DMUtils.get_switch_policy_name(), members=DMUtils.get_switch_vrf_import(self.get_asn())) self.policy_config.add_community(comm)
def set_route_targets_config(self): if self.policy_config is None: self.policy_config = PolicyOptions(comment=DMUtils.policy_options_comment()) # add export community export_comm = CommunityType(name=DMUtils.get_switch_export_community_name()) for route_target in self.route_targets: comm = CommunityType(name=DMUtils.make_community_name(route_target)) comm.add_members(route_target) self.policy_config.add_community(comm) # add route-targets to export community export_comm.add_members(route_target) # if no members, no need to add community if export_comm.get_members(): self.policy_config.add_community(export_comm) # add community for switch options comm = CommunityType(name=DMUtils.get_switch_policy_name()) comm.add_members(DMUtils.get_switch_vrf_import(self.get_asn())) self.policy_config.add_community(comm)
def add_routing_instance(self, ri_conf): ri_name = ri_conf.get("ri_name") vn = ri_conf.get("vn") is_l2 = ri_conf.get("is_l2", False) is_l2_l3 = ri_conf.get("is_l2_l3", False) import_targets = ri_conf.get("import_targets", set()) export_targets = ri_conf.get("export_targets", set()) prefixes = ri_conf.get("prefixes", []) gateways = ri_conf.get("gateways", []) interfaces = ri_conf.get("interfaces", []) vni = ri_conf.get("vni", None) network_id = ri_conf.get("network_id", None) self.routing_instances[ri_name] = ri_conf ri_config = None policy_config = self.policy_config or \ PolicyOptions(comment=DMUtils.policy_options_comment()) ri = None ri_opt = None if not is_l2: ri_config = self.ri_config or \ RoutingInstances(comment=DMUtils.routing_instances_comment()) ri = Instance(name=ri_name) ri_config.add_instance(ri) ri.set_vrf_import(DMUtils.make_import_name(ri_name)) ri.set_vrf_export(DMUtils.make_export_name(ri_name)) has_ipv6_prefixes = DMUtils.has_ipv6_prefixes(prefixes) has_ipv4_prefixes = DMUtils.has_ipv4_prefixes(prefixes) if not is_l2: if ri_opt is None: ri_opt = RoutingInstanceRoutingOptions() ri.set_routing_options(ri_opt) ri.set_instance_type("vrf") for interface in interfaces: ri.add_interface(Interface(name=interface.name)) family = Family() if has_ipv4_prefixes: family.set_inet(FamilyInet(unicast='')) if has_ipv6_prefixes: family.set_inet6(FamilyInet6(unicast='')) if has_ipv4_prefixes or has_ipv6_prefixes: auto_export = AutoExport(family=family) ri_opt.set_auto_export(auto_export) # add policies for export route targets if self.is_spine(): ps = PolicyStatement(name=DMUtils.make_export_name(ri_name)) ps.set_comment(DMUtils.vn_ps_comment(vn, "Export")) then = Then() ps.add_term(Term(name="t1", then=then)) for route_target in export_targets: comm = Community(add='', community_name=DMUtils.make_community_name(route_target)) then.add_community(comm) then.set_accept('') policy_config.add_policy_statement(ps) self.add_to_global_switch_opts(DMUtils.make_export_name(ri_name), False) # add policies for import route targets ps = PolicyStatement(name=DMUtils.make_import_name(ri_name)) ps.set_comment(DMUtils.vn_ps_comment(vn, "Import")) # add term switch policy from_ = From() term = Term(name=DMUtils.get_switch_policy_name(), fromxx=from_) ps.add_term(term) from_.add_community(DMUtils.get_switch_policy_name()) term.set_then(Then(accept='')) from_ = From() term = Term(name="t1", fromxx=from_) ps.add_term(term) for route_target in import_targets: from_.add_community(DMUtils.make_community_name(route_target)) if not self.is_spine(): self.add_vni_option(vni or network_id, route_target) term.set_then(Then(accept='')) policy_config.add_policy_statement(ps) self.add_to_global_switch_opts(DMUtils.make_import_name(ri_name), True) # add L2 EVPN and BD config interfaces_config = self.interfaces_config if (is_l2 and vni is not None and self.is_family_configured(self.bgp_params, "e-vpn")): # add vlan config vlan_conf = self.add_vlan_config(ri_name, vni) interfaces_config = self.interfaces_config or Interfaces(comment=DMUtils.interfaces_comment()) if is_l2_l3 and self.is_spine(): irb_intf = Interface(name='irb', gratuitous_arp_reply='') interfaces_config.add_interface(irb_intf) if gateways is not None: intf_unit = Unit(name=str(network_id), comment=DMUtils.vn_irb_comment(vn, False, is_l2_l3)) irb_intf.add_unit(intf_unit) if self.is_spine(): intf_unit.set_proxy_macip_advertisement('') family = Family() intf_unit.set_family(family) inet = None inet6 = None for (irb_ip, gateway) in gateways: if ':' in irb_ip: if not inet6: inet6 = FamilyInet6() family.set_inet6(inet6) addr = Address() inet6.add_address(addr) else: if not inet: inet = FamilyInet() family.set_inet(inet) addr = Address() inet.add_address(addr) addr.set_name(irb_ip) addr.set_comment(DMUtils.irb_ip_comment(irb_ip)) if len(gateway) and gateway != '0.0.0.0': addr.set_virtual_gateway_address(gateway) self.build_l2_evpn_interface_config(interfaces_config, interfaces, vn, vlan_conf) if (not is_l2 and vni is not None and self.is_family_configured(self.bgp_params, "e-vpn")): ri.set_vtep_source_interface("lo0.0") evpn = self.build_evpn_config() if evpn: ri.set_protocols(RoutingInstanceProtocols(evpn=evpn)) #add vlans self.add_ri_vlan_config(ri, vni) if (not is_l2 and not is_l2_l3 and gateways): interfaces_config = self.interfaces_config or \ Interfaces(comment=DMUtils.interfaces_comment()) ifl_num = str(1000 + int(network_id)) lo_intf = Interface(name="lo0") interfaces_config.add_interface(lo_intf) intf_unit = Unit(name=ifl_num, comment=DMUtils.l3_lo_intf_comment(vn)) lo_intf.add_unit(intf_unit) family = Family() intf_unit.set_family(family) inet = None inet6 = None for (lo_ip, _) in gateways: subnet = lo_ip (ip, _) = lo_ip.split('/') if ':' in lo_ip: if not inet6: inet6 = FamilyInet6() family.set_inet6(inet6) addr = Address() inet6.add_address(addr) lo_ip = ip + '/' + '128' else: if not inet: inet = FamilyInet() family.set_inet(inet) addr = Address() inet.add_address(addr) lo_ip = ip + '/' + '32' addr.set_name(lo_ip) addr.set_comment(DMUtils.lo0_ip_comment(subnet)) ri.add_interface(Interface(name="lo0." + ifl_num, comment=DMUtils.lo0_ri_intf_comment(vn))) self.policy_config = policy_config self.interfaces_config = interfaces_config self.route_targets |= import_targets | export_targets self.ri_config = ri_config
def add_routing_instance(self, ri_conf): ri_name = ri_conf.get("ri_name") vn = ri_conf.get("vn") is_l2 = ri_conf.get("is_l2", False) is_l2_l3 = ri_conf.get("is_l2_l3", False) import_targets = ri_conf.get("import_targets", set()) export_targets = ri_conf.get("export_targets", set()) prefixes = ri_conf.get("prefixes", []) gateways = ri_conf.get("gateways", []) router_external = ri_conf.get("router_external", False) interfaces = ri_conf.get("interfaces", []) vni = ri_conf.get("vni", None) fip_map = ri_conf.get("fip_map", None) network_id = ri_conf.get("network_id", None) is_internal_vn = True if '_contrail_lr_internal_vn_' in vn.name else False highest_encapsulation_priority = \ ri_conf.get("highest_encapsulation_priority") or "MPLSoGRE" self.routing_instances[ri_name] = ri_conf self.ri_config = self.ri_config or [] self.policy_config = self.policy_config or\ Policy(comment=DMUtils.policy_options_comment()) ri = RoutingInstance(name=ri_name) if vn: is_nat = True if fip_map else False ri.set_comment( DMUtils.vn_ri_comment(vn, is_l2, is_l2_l3, is_nat, router_external)) self.ri_config.append(ri) ri.set_is_public_network(router_external) has_ipv6_prefixes = DMUtils.has_ipv6_prefixes(prefixes) has_ipv4_prefixes = DMUtils.has_ipv4_prefixes(prefixes) if not is_l2: ri.set_instance_type("vrf") if fip_map is None: for interface in interfaces: ri.add_interfaces(LogicalInterface(name=interface.name)) else: if highest_encapsulation_priority == "VXLAN": ri.set_routing_instance_type("virtual-switch") elif highest_encapsulation_priority in ["MPLSoGRE", "MPLSoUDP"]: ri.set_routing_instance_type("evpn") if is_internal_vn: self.internal_vn_ris.append(ri) self.add_bogus_lo0(ri, network_id, vn) if self.is_spine() and is_l2_l3: self.add_irb_config(ri_conf) self.attach_irb(ri_conf, ri) if fip_map is not None: ri.add_interfaces(LogicalInterface(name=interfaces[0].name)) public_vrf_ips = {} for pip in fip_map.values(): if pip["vrf_name"] not in public_vrf_ips: public_vrf_ips[pip["vrf_name"]] = set() public_vrf_ips[pip["vrf_name"]].add(pip["floating_ip"]) for public_vrf, fips in public_vrf_ips.items(): floating_ips = [] ri.add_floating_ip_list( FloatingIpList(public_routing_instance=public_vrf, floating_ips=floating_ips)) for fip in fips: floating_ips.append(FloatingIpMap(floating_ip=fip)) # add policies for export route targets if self.is_spine(): p = PolicyRule(name=DMUtils.make_export_name(ri_name)) p.set_comment(DMUtils.vn_ps_comment(vn, "Export")) then = Then() p.add_term(Term(name="t1", then=then)) for route_target in export_targets: then.add_community(DMUtils.make_community_name(route_target)) then.set_accept_or_reject(True) self.policy_config.add_policy_rule(p) # add policies for import route targets p = PolicyRule(name=DMUtils.make_import_name(ri_name)) p.set_comment(DMUtils.vn_ps_comment(vn, "Import")) # add term switch policy from_ = From() term = Term(name=DMUtils.get_switch_policy_name(), fromxx=from_) p.add_term(term) from_.add_community(DMUtils.get_switch_policy_name()) term.set_then(Then(accept_or_reject=True)) from_ = From() term = Term(name="t1", fromxx=from_) p.add_term(term) for route_target in import_targets: from_.add_community(DMUtils.make_community_name(route_target)) if not is_internal_vn: self.add_vni_option(vni or network_id, route_target) term.set_then(Then(accept_or_reject=True)) self.policy_config.add_policy_rule(p) # add L2 EVPN and BD config if (is_l2 and vni is not None and self.is_family_configured(self.bgp_params, "e-vpn")): # add vlan config vlan_conf = self.add_vlan_config(ri_name, vni, is_l2_l3, "irb." + str(network_id)) self.interfaces_config = self.interfaces_config or [] self.build_l2_evpn_interface_config(self.interfaces_config, interfaces, vn, vlan_conf) if (not is_l2 and vni is not None and self.is_family_configured(self.bgp_params, "e-vpn")): ri.set_vtep_source_interface("lo0.0") evpn = self.build_evpn_config() if evpn: ri.set_protocols(RoutingInstanceProtocols(evpn=evpn)) #add vlans self.add_ri_vlan_config(ri, vni) if (not is_l2 and not is_l2_l3 and gateways): self.interfaces_config = self.interfaces_config or [] ifl_num = str(1000 + int(network_id)) lo_intf = Interface(name="lo0") interfaces_config.add_interface(lo_intf) intf_unit = Unit(name=ifl_num, comment=DMUtils.l3_lo_intf_comment(vn)) lo_intf.add_logical_interfaces(intf_unit) family = Family() intf_unit.set_family(family) inet = None inet6 = None for (lo_ip, _) in gateways: subnet = lo_ip (ip, _) = lo_ip.split('/') if ':' in lo_ip: if not inet6: inet6 = FamilyInet6() family.set_inet6(inet6) addr = Address() inet6.add_address(addr) lo_ip = ip + '/' + '128' else: if not inet: inet = FamilyInet() family.set_inet(inet) addr = Address() inet.add_address(addr) lo_ip = ip + '/' + '32' addr.set_name(lo_ip) addr.set_comment(DMUtils.lo0_ip_comment(subnet)) ri.add_interface( Interface(name="lo0." + ifl_num, comment=DMUtils.lo0_ri_intf_comment(vn))) self.route_targets |= import_targets | export_targets
def add_routing_instance(self, ri_conf): ri_name = ri_conf.get("ri_name") vn = ri_conf.get("vn") is_l2 = ri_conf.get("is_l2", False) is_l2_l3 = ri_conf.get("is_l2_l3", False) import_targets = ri_conf.get("import_targets", set()) export_targets = ri_conf.get("export_targets", set()) prefixes = ri_conf.get("prefixes", []) gateways = ri_conf.get("gateways", []) interfaces = ri_conf.get("interfaces", []) vni = ri_conf.get("vni", None) network_id = ri_conf.get("network_id", None) is_internal_vn = True if '_contrail_lr_internal_vn_' in vn.name else False self.routing_instances[ri_name] = ri_conf ri_config = None policy_config = self.policy_config or \ PolicyOptions(comment=DMUtils.policy_options_comment()) ri = None ri_opt = None ri_config = self.ri_config or \ RoutingInstances(comment=DMUtils.routing_instances_comment()) ri = Instance(name=ri_name) if not is_l2: ri_config.add_instance(ri) ri.set_vrf_import(DMUtils.make_import_name(ri_name)) ri.set_vrf_export(DMUtils.make_export_name(ri_name)) has_ipv6_prefixes = DMUtils.has_ipv6_prefixes(prefixes) has_ipv4_prefixes = DMUtils.has_ipv4_prefixes(prefixes) if not is_l2: if ri_opt is None: ri_opt = RoutingInstanceRoutingOptions() ri.set_routing_options(ri_opt) ri.set_instance_type("vrf") for interface in interfaces: ri.add_interface(Interface(name=interface.name)) family = Family() if has_ipv4_prefixes: family.set_inet(FamilyInet(unicast='')) if has_ipv6_prefixes: family.set_inet6(FamilyInet6(unicast='')) if has_ipv4_prefixes or has_ipv6_prefixes: auto_export = AutoExport(family=family) ri_opt.set_auto_export(auto_export) if is_internal_vn: self.internal_vn_ris.append(ri) self.add_bogus_lo0(ri, network_id, vn) if self.is_spine() and is_l2_l3: self.add_irb_config(ri_conf) self.attach_irb(ri_conf, ri) lr_uuid = None if is_internal_vn: lr_uuid = DMUtils.extract_lr_uuid_from_internal_vn_name(ri_name) # add policies for export route targets if self.is_spine(): ps = PolicyStatement(name=DMUtils.make_export_name(ri_name)) ps.set_comment(DMUtils.vn_ps_comment(vn, "Export")) then = Then() ps.add_term(Term(name="t1", then=then)) for route_target in export_targets: comm = Community(add='', community_name=DMUtils.make_community_name(route_target)) then.add_community(comm) then.set_accept('') policy_config.add_policy_statement(ps) self.add_to_global_switch_opts(DMUtils.make_export_name(ri_name), False) # add policies for import route targets ps = PolicyStatement(name=DMUtils.make_import_name(ri_name)) ps.set_comment(DMUtils.vn_ps_comment(vn, "Import")) # add term switch policy from_ = From() term = Term(name=DMUtils.get_switch_policy_name(), fromxx=from_) ps.add_term(term) from_.add_community(DMUtils.get_switch_policy_name()) term.set_then(Then(accept='')) from_ = From() term = Term(name="t1", fromxx=from_) ps.add_term(term) for route_target in import_targets: from_.add_community(DMUtils.make_community_name(route_target)) if not is_internal_vn: self.add_vni_option(vni or network_id, route_target) term.set_then(Then(accept='')) policy_config.add_policy_statement(ps) self.add_to_global_switch_opts(DMUtils.make_import_name(ri_name), True) # add L2 EVPN and BD config interfaces_config = self.interfaces_config if (is_l2 and vni is not None and self.is_family_configured(self.bgp_params, "e-vpn")): # add vlan config vlan_conf = self.add_vlan_config(ri_name, vni, is_l2_l3, "irb." + str(network_id)) interfaces_config = self.interfaces_config or Interfaces(comment=DMUtils.interfaces_comment()) self.build_l2_evpn_interface_config(interfaces_config, interfaces, vn, vlan_conf) if (not is_l2 and (vni is not None or (is_internal_vn and lr_uuid)) and \ self.is_family_configured(self.bgp_params, "e-vpn")): evpn = self.build_evpn_config(int_vn = is_internal_vn) if evpn: ri.set_protocols(RoutingInstanceProtocols(evpn=evpn)) if is_internal_vn and lr_uuid: ip_prefix_support = IpPrefixSupport() #ip_prefix_support.set_forwarding_mode("symmetric") ip_prefix_support.set_encapsulation("vxlan") ip_prefix_support.set_vni(str(vni)) ip_prefix_support.set_advertise("direct-nexthop") evpn.set_ip_prefix_support(ip_prefix_support) else: ri.set_vtep_source_interface("lo0.0") if not is_internal_vn: #add vlans self.add_ri_vlan_config(ri, vni) if (not is_l2 and not is_l2_l3 and gateways): interfaces_config = self.interfaces_config or \ Interfaces(comment=DMUtils.interfaces_comment()) ifl_num = str(1000 + int(network_id)) lo_intf = Interface(name="lo0") interfaces_config.add_interface(lo_intf) intf_unit = Unit(name=ifl_num, comment=DMUtils.l3_lo_intf_comment(vn)) lo_intf.add_unit(intf_unit) family = Family() intf_unit.set_family(family) inet = None inet6 = None for (lo_ip, _) in gateways: subnet = lo_ip (ip, _) = lo_ip.split('/') if ':' in lo_ip: if not inet6: inet6 = FamilyInet6() family.set_inet6(inet6) addr = Address() inet6.add_address(addr) lo_ip = ip + '/' + '128' else: if not inet: inet = FamilyInet() family.set_inet(inet) addr = Address() inet.add_address(addr) lo_ip = ip + '/' + '32' addr.set_name(lo_ip) addr.set_comment(DMUtils.lo0_ip_comment(subnet)) ri.add_interface(Interface(name="lo0." + ifl_num, comment=DMUtils.lo0_ri_intf_comment(vn))) self.policy_config = policy_config self.interfaces_config = interfaces_config self.route_targets |= import_targets | export_targets self.ri_config = ri_config
def add_routing_instance(self, ri_conf): ri_name = ri_conf.get("ri_name") vn = ri_conf.get("vn") is_l2 = ri_conf.get("is_l2", False) is_l2_l3 = ri_conf.get("is_l2_l3", False) import_targets = ri_conf.get("import_targets", set()) export_targets = ri_conf.get("export_targets", set()) prefixes = ri_conf.get("prefixes", []) gateways = ri_conf.get("gateways", []) interfaces = ri_conf.get("interfaces", []) vni = ri_conf.get("vni", None) network_id = ri_conf.get("network_id", None) is_internal_vn = True if '_contrail_lr_internal_vn_' in vn.name else False self.routing_instances[ri_name] = ri_conf ri_config = None policy_config = self.policy_config or \ PolicyOptions(comment=DMUtils.policy_options_comment()) ri = None ri_opt = None ri_config = self.ri_config or \ RoutingInstances(comment=DMUtils.routing_instances_comment()) ri = Instance(name=ri_name) if not is_l2: ri_config.add_instance(ri) ri.set_vrf_import(DMUtils.make_import_name(ri_name)) ri.set_vrf_export(DMUtils.make_export_name(ri_name)) has_ipv6_prefixes = DMUtils.has_ipv6_prefixes(prefixes) has_ipv4_prefixes = DMUtils.has_ipv4_prefixes(prefixes) if not is_l2: if ri_opt is None: ri_opt = RoutingInstanceRoutingOptions() ri.set_routing_options(ri_opt) ri.set_instance_type("vrf") for interface in interfaces: ri.add_interface(Interface(name=interface.name)) family = Family() if has_ipv4_prefixes: family.set_inet(FamilyInet(unicast='')) if has_ipv6_prefixes: family.set_inet6(FamilyInet6(unicast='')) if has_ipv4_prefixes or has_ipv6_prefixes: auto_export = AutoExport(family=family) ri_opt.set_auto_export(auto_export) if is_internal_vn: self.internal_vn_ris.append(ri) self.add_bogus_lo0(ri, network_id, vn) if self.is_spine() and is_l2_l3: self.add_irb_config(ri_conf) self.attach_irb(ri_conf, ri) lr_uuid = None if is_internal_vn: lr_uuid = DMUtils.extract_lr_uuid_from_internal_vn_name(ri_name) # add policies for export route targets if self.is_spine(): ps = PolicyStatement(name=DMUtils.make_export_name(ri_name)) ps.set_comment(DMUtils.vn_ps_comment(vn, "Export")) then = Then() ps.add_term(Term(name="t1", then=then)) for route_target in export_targets: comm = Community( add='', community_name=DMUtils.make_community_name(route_target)) then.add_community(comm) then.set_accept('') policy_config.add_policy_statement(ps) self.add_to_global_switch_opts(DMUtils.make_export_name(ri_name), False) # add policies for import route targets ps = PolicyStatement(name=DMUtils.make_import_name(ri_name)) ps.set_comment(DMUtils.vn_ps_comment(vn, "Import")) # add term switch policy from_ = From() term = Term(name=DMUtils.get_switch_policy_name(), fromxx=from_) ps.add_term(term) from_.add_community(DMUtils.get_switch_policy_name()) term.set_then(Then(accept='')) from_ = From() term = Term(name="t1", fromxx=from_) ps.add_term(term) for route_target in import_targets: from_.add_community(DMUtils.make_community_name(route_target)) if not is_internal_vn: self.add_vni_option(vni or network_id, route_target) term.set_then(Then(accept='')) policy_config.add_policy_statement(ps) self.add_to_global_switch_opts(DMUtils.make_import_name(ri_name), True) # add L2 EVPN and BD config interfaces_config = self.interfaces_config if (is_l2 and vni is not None and self.is_family_configured(self.bgp_params, "e-vpn")): # add vlan config vlan_conf = self.add_vlan_config(ri_name, vni, is_l2_l3, "irb." + str(network_id)) interfaces_config = self.interfaces_config or Interfaces( comment=DMUtils.interfaces_comment()) self.build_l2_evpn_interface_config(interfaces_config, interfaces, vn, vlan_conf) if (not is_l2 and (vni is not None or (is_internal_vn and lr_uuid)) and \ self.is_family_configured(self.bgp_params, "e-vpn")): evpn = self.build_evpn_config(int_vn=is_internal_vn) if evpn: ri.set_protocols(RoutingInstanceProtocols(evpn=evpn)) if is_internal_vn and lr_uuid: ip_prefix_support = IpPrefixSupport() #ip_prefix_support.set_forwarding_mode("symmetric") ip_prefix_support.set_encapsulation("vxlan") ip_prefix_support.set_vni(str(vni)) ip_prefix_support.set_advertise("direct-nexthop") evpn.set_ip_prefix_support(ip_prefix_support) else: ri.set_vtep_source_interface("lo0.0") if not is_internal_vn: #add vlans self.add_ri_vlan_config(ri, vni) if (not is_l2 and not is_l2_l3 and gateways): interfaces_config = self.interfaces_config or \ Interfaces(comment=DMUtils.interfaces_comment()) ifl_num = str(1000 + int(network_id)) lo_intf = Interface(name="lo0") interfaces_config.add_interface(lo_intf) intf_unit = Unit(name=ifl_num, comment=DMUtils.l3_lo_intf_comment(vn)) lo_intf.add_unit(intf_unit) family = Family() intf_unit.set_family(family) inet = None inet6 = None for (lo_ip, _) in gateways: subnet = lo_ip (ip, _) = lo_ip.split('/') if ':' in lo_ip: if not inet6: inet6 = FamilyInet6() family.set_inet6(inet6) addr = Address() inet6.add_address(addr) lo_ip = ip + '/' + '128' else: if not inet: inet = FamilyInet() family.set_inet(inet) addr = Address() inet.add_address(addr) lo_ip = ip + '/' + '32' addr.set_name(lo_ip) addr.set_comment(DMUtils.lo0_ip_comment(subnet)) ri.add_interface( Interface(name="lo0." + ifl_num, comment=DMUtils.lo0_ri_intf_comment(vn))) self.policy_config = policy_config self.interfaces_config = interfaces_config self.route_targets |= import_targets | export_targets self.ri_config = ri_config
def add_routing_instance(self, ri_conf): ri_name = ri_conf.get("ri_name") vn = ri_conf.get("vn") is_l2 = ri_conf.get("is_l2", False) is_l2_l3 = ri_conf.get("is_l2_l3", False) import_targets = ri_conf.get("import_targets", set()) export_targets = ri_conf.get("export_targets", set()) prefixes = ri_conf.get("prefixes", []) gateways = ri_conf.get("gateways", []) router_external = ri_conf.get("router_external", False) interfaces = ri_conf.get("interfaces", []) vni = ri_conf.get("vni", None) fip_map = ri_conf.get("fip_map", None) network_id = ri_conf.get("network_id", None) is_internal_vn = True if '_contrail_lr_internal_vn_' in vn.name else False highest_encapsulation_priority = \ ri_conf.get("highest_encapsulation_priority") or "MPLSoGRE" self.routing_instances[ri_name] = ri_conf self.ri_config = self.ri_config or [] self.policy_config = self.policy_config or\ Policy(comment=DMUtils.policy_options_comment()) ri = RoutingInstance(name=ri_name) if vn: is_nat = True if fip_map else False ri.set_comment( DMUtils.vn_ri_comment(vn, is_l2, is_l2_l3, is_nat, router_external)) self.ri_config.append(ri) ri.set_virtual_network_id(str(network_id)) ri.set_vxlan_id(str(vni)) ri.set_virtual_network_is_internal(is_internal_vn) ri.set_is_public_network(router_external) if is_l2_l3: ri.set_virtual_network_mode('l2-l3') elif is_l2: ri.set_virtual_network_mode('l2') else: ri.set_virtual_network_mode('l3') has_ipv6_prefixes = DMUtils.has_ipv6_prefixes(prefixes) has_ipv4_prefixes = DMUtils.has_ipv4_prefixes(prefixes) if not is_l2: ri.set_routing_instance_type("vrf") if fip_map is None: for interface in interfaces: ri.add_interfaces(LogicalInterface(name=interface.name)) if prefixes: for prefix in prefixes: ri.add_static_routes(self.get_route_for_cidr(prefix)) ri.add_prefixes(self.get_subnet_for_cidr(prefix)) else: if highest_encapsulation_priority == "VXLAN": ri.set_routing_instance_type("virtual-switch") elif highest_encapsulation_priority in ["MPLSoGRE", "MPLSoUDP"]: ri.set_routing_instance_type("evpn") if is_internal_vn: self.internal_vn_ris.append(ri) self.add_bogus_lo0(ri, network_id, vn) if self.is_spine() and is_l2_l3: self.add_irb_config(ri_conf) self.attach_irb(ri_conf, ri) if fip_map is not None: ri.add_interfaces(LogicalInterface(name=interfaces[0].name)) public_vrf_ips = {} for pip in fip_map.values(): if pip["vrf_name"] not in public_vrf_ips: public_vrf_ips[pip["vrf_name"]] = set() public_vrf_ips[pip["vrf_name"]].add(pip["floating_ip"]) for public_vrf, fips in public_vrf_ips.items(): ri.add_interfaces(LogicalInterface(name=interfaces[1].name)) floating_ips = [] for fip in fips: ri.add_static_routes( Route(prefix=fip, prefix_len=32, next_hop=interfaces[1].name, comment=DMUtils.fip_egress_comment())) floating_ips.append(FloatingIpMap(floating_ip=fip + "/32")) ri.add_floating_ip_list( FloatingIpList(public_routing_instance=public_vrf, floating_ips=floating_ips)) # add policies for export route targets if self.is_spine(): p = PolicyRule(name=DMUtils.make_export_name(ri_name)) p.set_comment(DMUtils.vn_ps_comment(vn, "Export")) then = Then() p.add_term(Term(name="t1", then=then)) for route_target in export_targets: then.add_community(DMUtils.make_community_name(route_target)) then.set_accept_or_reject(True) self.policy_config.add_policy_rule(p) # add policies for import route targets p = PolicyRule(name=DMUtils.make_import_name(ri_name)) p.set_comment(DMUtils.vn_ps_comment(vn, "Import")) # add term switch policy from_ = From() term = Term(name=DMUtils.get_switch_policy_name(), fromxx=from_) p.add_term(term) from_.add_community(DMUtils.get_switch_policy_name()) term.set_then(Then(accept_or_reject=True)) from_ = From() term = Term(name="t1", fromxx=from_) p.add_term(term) for route_target in import_targets: from_.add_community(DMUtils.make_community_name(route_target)) if not is_internal_vn: self.add_vni_option(vni or network_id, route_target) term.set_then(Then(accept_or_reject=True)) self.policy_config.add_policy_rule(p) # add firewall config for public VRF if router_external and is_l2 is False: self.firewall_config = self.firewall_config or Firewall( comment=DMUtils.firewall_comment()) if has_ipv4_prefixes and not self.inet4_forwarding_filter: # create single instance inet4 filter self.inet4_forwarding_filter = self.add_inet_public_vrf_filter( self.firewall_config, "inet") if has_ipv6_prefixes and not self.inet6_forwarding_filter: # create single instance inet6 filter self.inet6_forwarding_filter = self.add_inet_public_vrf_filter( self.firewall_config, "inet6") if has_ipv4_prefixes: # add terms to inet4 filter term = self.add_inet_filter_term(ri_name, prefixes, "inet4") # insert before the last term terms = self.inet4_forwarding_filter.get_terms() terms = [term] + (terms or []) self.inet4_forwarding_filter.set_terms(terms) if has_ipv6_prefixes: # add terms to inet6 filter term = self.add_inet_filter_term(ri_name, prefixes, "inet6") # insert before the last term terms = self.inet6_forwarding_filter.get_terms() terms = [term] + (terms or []) self.inet6_forwarding_filter.set_terms(terms) if fip_map is not None: self.firewall_config = self.firewall_config or Firewall( comment=DMUtils.firewall_comment()) f = FirewallFilter( name=DMUtils.make_private_vrf_filter_name(ri_name)) f.set_comment(DMUtils.vn_firewall_comment(vn, "private")) self.firewall_config.add_firewall_filters(f) term = Term(name=DMUtils.make_vrf_term_name(ri_name)) from_ = From() for fip_user_ip in fip_map.keys(): from_.add_source_address(fip_user_ip) term.set_from(from_) term.set_then(Then(routing_instance=[ri_name])) f.add_terms(term) irb_intf = PhysicalInterface(name='irb', interface_type='irb') self.interfaces_config.append(irb_intf) intf_unit = LogicalInterface( name="irb." + str(network_id), unit=network_id, comment=DMUtils.vn_irb_fip_inet_comment(vn)) irb_intf.add_logical_interfaces(intf_unit) intf_unit.set_family("inet") intf_unit.add_firewall_filters( DMUtils.make_private_vrf_filter_name(ri_name)) ri.add_routing_interfaces(intf_unit) if gateways is not None: for (ip, gateway) in gateways: ri.add_gateways( GatewayRoute(ip_address=self.get_subnet_for_cidr(ip), gateway=self.get_subnet_for_cidr(gateway))) # add L2 EVPN and BD config if (is_l2 and vni is not None and self.is_family_configured(self.bgp_params, "e-vpn")): vlan = None if self.is_spine(): if highest_encapsulation_priority == "VXLAN": self.vlans_config = self.vlans_config or [] vlan = Vlan(name=DMUtils.make_bridge_name(vni), vxlan_id=vni, vlan_or_bridge_domain=False) vlan.set_comment(DMUtils.vn_bd_comment(vn, "VXLAN")) self.vlans_config.append(vlan) for interface in interfaces: vlan.add_interfaces( LogicalInterface(name=interface.name)) if is_l2_l3: # network_id is unique, hence irb irb_intf = "irb." + str(network_id) vlan.add_interfaces(LogicalInterface(name=irb_intf)) elif highest_encapsulation_priority in [ "MPLSoGRE", "MPLSoUDP" ]: self.evpn = Evpn( encapsulation=highest_encapsulation_priority) self.evpn.set_comment( DMUtils.vn_evpn_comment( vn, highest_encapsulation_priority)) for interface in interfaces: self.evpn.add_interfaces( LogicalInterface(name=interface.name)) else: # add vlan config vlan = self.add_vlan_config(ri_name, vni, is_l2_l3, "irb." + str(network_id)) self.interfaces_config = self.interfaces_config or [] self.build_l2_evpn_interface_config(self.interfaces_config, interfaces, vn, vlan) if (not is_l2 and vni is not None and self.is_family_configured(self.bgp_params, "e-vpn")): self.evpn = self.build_evpn_config() # add vlans self.add_ri_vlan_config(ri, vni) if (not is_l2 and not is_l2_l3 and gateways): self.interfaces_config = self.interfaces_config or [] ifl_num = 1000 + int(network_id) lo_intf = PhysicalInterface(name="lo0", interface_type='loopback') self.interfaces_config.append(lo_intf) intf_unit = LogicalInterface( name="lo0." + str(ifl_num), unit=ifl_num, comment=DMUtils.l3_lo_intf_comment(vn)) lo_intf.add_logical_interfaces(intf_unit) for (lo_ip, _) in gateways: subnet = lo_ip (ip, _) = lo_ip.split('/') if ':' in lo_ip: lo_ip = ip + '/' + '128' else: lo_ip = ip + '/' + '32' intf_unit.add_ip_list( IpType(address=lo_ip, comment=DMUtils.lo0_ip_comment(subnet))) ri.add_loopback_interfaces( LogicalInterface(name="lo0." + str(ifl_num), comment=DMUtils.lo0_ri_intf_comment(vn))) # fip services config if fip_map is not None: nat_rules = NatRules(allow_overlapping_nat_pools=True, name=DMUtils.make_services_set_name(ri_name), comment=DMUtils.service_set_comment(vn)) ri.set_nat_rules(nat_rules) snat_rule = NatRule(name=DMUtils.make_snat_rule_name(ri_name), comment=DMUtils.service_set_nat_rule_comment( vn, "SNAT"), direction="input", translation_type="basic-nat44") snat_rule.set_comment(DMUtils.snat_rule_comment()) nat_rules.add_rules(snat_rule) dnat_rule = NatRule(name=DMUtils.make_dnat_rule_name(ri_name), comment=DMUtils.service_set_nat_rule_comment( vn, "DNAT"), direction="output", translation_type="dnat-44") dnat_rule.set_comment(DMUtils.dnat_rule_comment()) nat_rules.add_rules(dnat_rule) nat_rules.set_inside_interface(interfaces[0].name) nat_rules.set_outside_interface(interfaces[1].name) for pip, fip_vn in fip_map.items(): fip = fip_vn["floating_ip"] # private ip snat_rule.add_source_addresses(self.get_subnet_for_cidr(pip)) # public ip snat_rule.add_source_prefixes(self.get_subnet_for_cidr(fip)) # public ip dnat_rule.add_destination_addresses( self.get_subnet_for_cidr(fip)) # private ip dnat_rule.add_destination_prefixes( self.get_subnet_for_cidr(pip)) intf_unit = LogicalInterface( name=interfaces[0].name, unit=interfaces[0].unit, comment=DMUtils.service_intf_comment("Ingress")) intf_unit.set_family("inet") ri.add_service_interfaces(intf_unit) intf_unit = LogicalInterface( name=interfaces[1].name, unit=interfaces[1].unit, comment=DMUtils.service_intf_comment("Egress")) intf_unit.set_family("inet") ri.add_service_interfaces(intf_unit) for target in import_targets: ri.add_import_targets(target) for target in export_targets: ri.add_export_targets(target)