def __new__(cls, nic_hint, ifindex, wlDefn, default_vlan, allocate_vlan): key = '%s-%d' % (nic_hint, ifindex) if key in cls.Store: return cls.Store[key] # Create & Initialize the instance inst = super(SubInterfaceWorkload, cls).__new__(cls) cls.Store[key] = inst inst.__NicHint = nic_hint inst.__IfIndex = ifindex if cls.Store and allocate_vlan: inst.__vlan = api.Testbed_AllocateVlan() api.Logger.info("Allocating new vlan %d for key %s" % (inst.__vlan, key)) else: inst.__vlan = default_vlan api.Logger.info("Resusing default-vlan %d for key %s" % (default_vlan, key)) wl_spec = TopoWorkloadConfig.GetWorkloadSpecByName(wlDefn) inst.__NetworkSpec = TopoWorkloadConfig.GetNetworkSpecByName( wl_spec.network_spec) inst.__PrimaryIpv4Allocator = TopoWorkloadConfig.AllocIpv4SubnetAllocator( inst.__NetworkSpec.name) inst.__PrimaryIpv4Allocator.Alloc() # To skip 0 ip inst.__PrimaryIpv6Allocator = TopoWorkloadConfig.AllocIpv6SubnetAllocator( inst.__NetworkSpec.name) inst.__PrimaryIpv6Allocator.Alloc() # To skip 0 ip inst.__SeconadryIpv4Allocator = TopoWorkloadConfig.AllocSecondaryIPv4Allocators( inst.__NetworkSpec.name) inst.__SecondaryIpv6Allocator = TopoWorkloadConfig.AllocSecondaryIPv6Allocators( inst.__NetworkSpec.name) return inst
def UpdateTestBedVlans(objects): for obj in objects: if obj.spec.vlan_id == -1: vlan = api.Testbed_AllocateVlan() obj.spec.vlan_id = vlan api.Logger.info("Network Object: %s, Allocated Vlan = %d" % (obj.meta.name, obj.spec.vlan_id))
def __generate_heimdall_template(): tmpl_file = _netagent_cfg_dir + "/heimdall.tmpl.json" tmpl_cfg = json.load(open(tmpl_file)) tmpl_cfg["vlan-offset"] = api.Testbed_AllocateVlan() tmpl_output = cfgOutputDir + "/heimdall.tmpl.json" with open(tmpl_output, 'w') as outfile: json.dump(tmpl_cfg, outfile) return tmpl_output
def AddConfigClassicWorkloads(req, nodes=None): # Using up first vlan for native req.workload_op = topo_svc.ADD native_vlan = api.Testbed_AllocateVlan() api.Logger.info("Allocating native VLAN %d" % native_vlan) # Forming native workloads for workload in TopoWorkloadConfig.GetNativeWorkloadInstances(): wl = workload.workload if nodes and wl.node in nodes: node_wlm = NodeWorkloads(wl, native_vlan) node_wlm.CreateNativeWorkloads(req) tagged_vlan = api.Testbed_AllocateVlan() api.Logger.info("Allocating tagged VLAN %d" % tagged_vlan) for workload in TopoWorkloadConfig.GetTaggedWorkloadInstances(): wl = workload.workload if nodes and wl.node in nodes: node_wlm = NodeWorkloads(wl, tagged_vlan) node_wlm.CreateTaggedWorkloads(req) return api.types.status.SUCCESS
def Setup(tc): tc.nwObjects = agent_api.QueryConfigs(kind='Network') tc.epObjects = agent_api.QueryConfigs(kind='Endpoint') if not agent_api.__config_pushed: api.Logger.info("Config objects are not pushed") agent_api.UpdateNodeUuidEndpoints(tc.epObjects) api.Testbed_ResetVlanAlloc() vlan = api.Testbed_AllocateVlan() agent_api.UpdateTestBedVlans(tc.nwObjects) tc.iters = getattr(tc.args, 'iters', 10) return api.types.status.SUCCESS
def __init_config(): api.Logger.info("Initializing Config Service.") req = cfg_svc_pb2.InitConfigMsg() req.entry_point_type = cfg_svc_pb2.VENICE_REST for venice_ip in api.GetVeniceMgmtIpAddresses(): req.endpoints.append("%s:443" % venice_ip) # Temp change: Send only 10 vlans for v in range(10): req.vlans.append(api.Testbed_AllocateVlan()) resp = api.InitCfgService(req) if resp == None: return api.types.status.FAILURE return api.types.status.SUCCESS
def __allocateWireVlan(self, intf, encap_vlan): wire_vlan = self.__if_vlan_map[intf].get(encap_vlan) if wire_vlan: return wire_vlan #if testbed has mapping wire_vlan = NodeVlan.__tb_vlan_map.get(encap_vlan) if wire_vlan: self.__if_vlan_map[intf][encap_vlan] = wire_vlan return wire_vlan #If here , need to allocate a testbed vlan wire_vlan = api.Testbed_AllocateVlan() if not NodeVlan.__native_vlan: NodeVlan.__native_vlan = wire_vlan self.__tb_vlan_map[encap_vlan] = wire_vlan self.__if_vlan_map[intf][encap_vlan] = wire_vlan return wire_vlan
def UpdateNetworkAndEnpointObject(): nwObj = netagent_api.QueryConfigs(kind='Network') if not nwObj: api.Logger.error("Failed to get network object") return api.types.status.FAILURE api.Testbed_ResetVlanAlloc() vlan = api.Testbed_AllocateVlan() api.Logger.info("Ignoring first vlan as it is native ", vlan) netagent_api.UpdateTestBedVlans(nwObj) epObj = netagent_api.QueryConfigs(kind='Endpoint') if not epObj: api.Logger.error("Failed to get endpoint object") return api.types.status.FAILURE netagent_api.UpdateNodeUuidEndpoints(epObj) return api.types.status.SUCCESS
def PushBaseConfig(node_names=None, device_names=None, ignore_error=True, kinds=None): api.Testbed_ResetVlanAlloc() vlan = api.Testbed_AllocateVlan() api.Logger.info("Ignoring first vlan as it is native ", vlan) if not kinds or 'Namespace' in kinds: objects = QueryConfigs(kind='Namespace') ret = PushConfigObjects(objects, node_names, device_names, ignore_error=ignore_error) if not ignore_error and ret != api.types.status.SUCCESS: return api.types.status.FAILURE if not kinds or 'Network' in kinds: objects = QueryConfigs(kind='Network') if not ignore_error and ret != api.types.status.SUCCESS: return api.types.status.FAILURE UpdateTestBedVlans(objects) ret = PushConfigObjects(objects, node_names, device_names, ignore_error=ignore_error) if not ignore_error and ret != api.types.status.SUCCESS: return api.types.status.FAILURE if not kinds or 'Endpoint' in kinds: objects = QueryConfigs(kind='Endpoint') UpdateNodeUuidEndpoints(objects) ret = PushConfigObjects(objects, node_names, device_names, ignore_error=ignore_error) if not ignore_error and ret != api.types.status.SUCCESS: return api.types.status.FAILURE if not kinds or 'App' in kinds: objects = QueryConfigs(kind='App') ret = PushConfigObjects(objects, node_names, device_names, ignore_error=ignore_error) if not ignore_error and ret != api.types.status.SUCCESS: return api.types.status.FAILURE if not kinds or 'NetworkSecurityPolicy' in kinds: objects = QueryConfigs(kind='NetworkSecurityPolicy') ret = PushConfigObjects(objects, node_names, device_names, ignore_error=ignore_error) if not ignore_error and ret != api.types.status.SUCCESS: return api.types.status.FAILURE if not kinds or 'SecurityProfile' in kinds: objects = QueryConfigs(kind='SecurityProfile') ret = PushConfigObjects(objects, node_names, device_names, ignore_error=ignore_error) if not ignore_error and ret != api.types.status.SUCCESS: return api.types.status.FAILURE # if not kinds or 'Tunnel' in kinds: # objects = QueryConfigs(kind='Tunnel') # ret = PushConfigObjects(objects, node_names, device_names, ignore_error=ignore_error) # if not ignore_error and ret != api.types.status.SUCCESS: # return api.types.status.FAILURE if not kinds or 'MirrorSession' in kinds: objects = QueryConfigs(kind='MirrorSession') if objects: ret = PushConfigObjects(objects, node_names, device_names, ignore_error=ignore_error) if not ignore_error and ret != api.types.status.SUCCESS: return api.types.status.FAILURE #if not kinds or 'FlowExportPolicy' in kinds: # objects = QueryConfigs(kind='FlowExportPolicy') # if objects: # ret = PushConfigObjects(objects, node_names, device_names, ignore_error=ignore_error) # if not ignore_error and ret != api.types.status.SUCCESS: # return api.types.status.FAILURE global __config_pushed __config_pushed = True return api.types.status.SUCCESS
def AddConfigWorkloads(req, nodes=None): mva = api.GetMultiVlanAllocators() third_party_workload_count = 0 ep_objs = netagent_api.QueryConfigs(kind='Endpoint') ep_ref = None for ep in ep_objs: node_name = getattr(ep.spec, "_node_name", None) if not node_name: node_name = ep.spec.node_uuid if nodes and node_name not in nodes: api.Logger.debug("Skipping add workload for node %s" % node_name) continue if not api.IsNaplesNode(node_name): #if api.GetConfigNicMode() == 'hostpin' and third_party_workload_count > 0: # continue third_party_workload_count += 1 req.workload_op = topo_svc.ADD wl_msg = req.workloads.add() wl_msg.workload_name = ep.meta.name wl_msg.node_name = node_name intf = wl_msg.interfaces.add() intf.ip_prefix = __prepare_ip_address_str_for_endpoint(ep) #wl_msg.ipv6_prefix = __prepare_ipv6_address_str_for_endpoint(ep) intf.mac_address = ep.spec.mac_address intf.pinned_port = 1 intf.interface_type = topo_svc.INTERFACE_TYPE_VSS encap_vlan = getattr(ep.spec, 'useg_vlan', None) host_if = None if api.GetTestbedNicMode(node_name) == 'hostpin': host_if = api.AllocateHostInterfaceForNode(wl_msg.node_name) intf.uplink_vlan = __get_l2segment_vlan_for_endpoint(ep) if api.GetNicType(wl_msg.node_name) in ['pensando', 'naples']: intf.encap_vlan = encap_vlan if encap_vlan else intf.uplink_vlan else: intf.encap_vlan = intf.uplink_vlan elif api.GetTestbedNicMode() == 'hostpin_dvs': host_if = api.AllocateHostInterfaceForNode(wl_msg.node_name) intf.interface_type = topo_svc.INTERFACE_TYPE_DVS_PVLAN intf.switch_name = api.GetVCenterDVSName() intf.uplink_vlan = __get_l2segment_vlan_for_endpoint(ep) if api.GetNicType(wl_msg.node_name) in ['pensando', 'naples']: intf.encap_vlan = ep.spec.primary_vlan intf.secondary_encap_vlan = ep.spec.secondary_vlan else: intf.encap_vlan = intf.uplink_vlan elif api.GetTestbedNicMode(node_name) == 'classic': global classic_vlan_map if mva: node_vlan = api.Testbed_AllocateVlan() else: node_vlan = classic_vlan_map.get(node_name) if not node_vlan: node_vlan = NodeVlan(node_name) classic_vlan_map[node_name] = node_vlan #Allocate only if useg_vlan present. host_if, wire_vlan = node_vlan.AllocateHostIntfWireVlan(encap_vlan) if not node_vlan.IsNativeVlan(wire_vlan): #Set encap vlan if its non native. intf.encap_vlan = wire_vlan intf.uplink_vlan = wire_vlan elif api.GetTestbedNicMode(node_name) == 'unified': host_if = api.AllocateHostInterfaceForNode(wl_msg.node_name) intf.ipv6_prefix = __prepare_ipv6_address_str_for_endpoint(ep) intf.uplink_vlan = __get_l2segment_vlan_for_endpoint(ep) intf.encap_vlan = intf.uplink_vlan else: return api.types.status.FAILURE intf.interface = host_if intf.parent_interface = host_if wl_msg.workload_type = api.GetWorkloadTypeForNode(wl_msg.node_name) wl_msg.workload_image = api.GetWorkloadImageForNode(wl_msg.node_name) wl_msg.mgmt_ip = api.GetMgmtIPAddress(wl_msg.node_name) wl_msg.cpus = api.GetWorkloadCpusForNode(wl_msg.node_name) wl_msg.memory = api.GetWorkloadMemoryForNode(wl_msg.node_name) return api.types.status.SUCCESS
def __create_new_workload_outside_store(req, node_name, parent_inf, subifs_count): global __pernode_network_spec global __perhost_subifs global __subifs_vlan_allocator global __perhost_subifs global __ipv4_subnet_allocator global __ipv6_subnet_allocator global __node_native_ifs global __free_vlans __add_subifs_wl = [] assert (req.workload_op == topo_svc.ADD) ifs = __node_native_ifs[node_name] key = "host_" + str(ifs.index(str(parent_inf))) for j in range(subifs_count): index = key + "_" + str(j) if index not in __perhost_subifs: __perhost_subifs[index] = 1 else: __perhost_subifs[index] += 1 if index not in __subifs_vlan_allocator: nw_spec = __pernode_network_spec[node_name] # Check if free vlans available already, reuse it if len(__free_vlans): __subifs_vlan_allocator[index] = __free_vlans.pop(0) api.Logger.info("Reassigned vlan id: %d" % __subifs_vlan_allocator[index]) else: for count in range(api.Testbed_GetVlanCount()): vlan_id = api.Testbed_AllocateVlan() if vlan_id == api.Testbed_GetVlanBase(): vlan_id = api.Testbed_AllocateVlan() if vlan_id not in __subifs_vlan_allocator.values(): break else: api.Logger.info( "No vlans available. Delete existing subifs to add new subifs. Exiting, vlan count: %d\n" % api.Testbed_GetVlanCount()) return __subifs_vlan_allocator[index] = vlan_id ipv4_subnet = (__ipv4_subnet_allocator[nw_spec.name]).Alloc() ipv6_subnet = (__ipv6_subnet_allocator[nw_spec.name]).Alloc() __ipv4_allocators[index] = resmgr.IpAddressStep( ipv4_subnet, "0.0.0.1") __ipv6_allocators[index] = resmgr.Ipv6AddressStep( ipv6_subnet, "0::1") __ipv4_allocators[index].Alloc() # To skip 0 ip __ipv6_allocators[index].Alloc() # To skip 0 ip subif_spec = __pernode_network_spec[node_name] ipv4_allocator = __ipv4_allocators[index] ipv6_allocator = __ipv6_allocators[index] vlan = __subifs_vlan_allocator[index] workload_name = node_name + "_" + parent_inf + "_subif_" + str(vlan) res = False for wreq in req.workloads: if wreq.workload_name == workload_name: res = True break if res: continue #assert(not api.IsWorkloadRunning(workload_name)) wl_msg = req.workloads.add() ip4_addr_str = str(ipv4_allocator.Alloc()) ip6_addr_str = str(ipv6_allocator.Alloc()) intf = wl_msg.interfaces.add() intf.ip_prefix = ip4_addr_str + "/" + str( subif_spec.ipv4.prefix_length) intf.ipv6_prefix = ip6_addr_str + "/" + str( subif_spec.ipv6.prefix_length) intf.mac_address = __mac_allocator.Alloc().get() intf.encap_vlan = vlan intf.uplink_vlan = intf.encap_vlan wl_msg.workload_name = workload_name wl_msg.node_name = node_name intf.pinned_port = 1 intf.interface_type = topo_svc.INTERFACE_TYPE_VSS intf.interface = parent_inf + "_" + str(vlan) intf.parent_interface = parent_inf wl_msg.workload_type = api.GetWorkloadTypeForNode(node_name) wl_msg.workload_image = api.GetWorkloadImageForNode(node_name) __add_subifs_wl.append(wl_msg.interface) # Safety check if vlan in __free_vlans: __free_vlans.remove(vlan) api.Logger.info("Created workload: %s for host: %s on node: %s" % (__add_subifs_wl, parent_inf, node_name)) # store these workloads locally __storeWorkloads_outisde_store(req, __add_subifs_wl) return __add_subifs_wl