def handle(slice): from core.models import Controller, ControllerSlice, SiteDeployment, Network, NetworkSlice, NetworkTemplate, Slice from collections import defaultdict # slice = Slice.get(slice_id) controller_slices = ControllerSlice.objects.filter(slice=slice) existing_controllers = [cs.controller for cs in controller_slices] all_controllers = Controller.objects.all() for controller in all_controllers: if controller not in existing_controllers: sd = ControllerSlice(slice=slice, controller=controller) sd.save() # make sure slice has at least 1 public and 1 private networkd public_nets = [] private_net = None networks = Network.objects.filter(owner=slice) for network in networks: if network.template.name == 'Public dedicated IPv4': public_nets.append(network) elif network.template.name == 'Public shared IPv4': public_nets.append(network) elif network.template.name == 'Private': private_net = network if not public_nets: # ensure there is at least one public network, and default it to dedicated nat_net = Network( name=slice.name + '-nat', template=NetworkTemplate.objects.get(name='Public shared IPv4'), owner=slice) nat_net.save() public_nets.append(nat_net) if not private_net: private_net = Network( name=slice.name + '-private', template=NetworkTemplate.objects.get(name='Private'), owner=slice) private_net.save() # create slice networks public_net_slice = None private_net_slice = None net_slices = NetworkSlice.objects.filter(slice=slice, network__in=[private_net] + public_nets) for net_slice in net_slices: if net_slice.network in public_nets: public_net_slice = net_slice elif net_slice.network == private_net: private_net_slice = net_slice if not public_net_slice: public_net_slice = NetworkSlice(slice=slice, network=public_nets[0]) public_net_slice.save() if not private_net_slice: private_net_slice = NetworkSlice(slice=slice, network=private_net) private_net_slice.save()
def create(self): nodetemplate = self.nodetemplate xos_args = self.get_xos_args() if not xos_args.get("owner", None): raise Exception("Must specify slice when creating network") if not xos_args.get("template", None): raise Exception( "Must specify network template when creating network") # XXX TODO: investigate using transaction.atomic instead of setting # no_sync and no_policy network = Network(**xos_args) network.caller = self.user network.no_sync = True # postprocess might set the cidr network.no_policy = True network.save() self.postprocess(network) network.no_sync = False network.no_policy = False network.save() self.info("Created Network '%s' owned by Slice '%s'" % (str(network), str(network.owner)))
def test_nat_net(self): slice3Name = self.make_slice_name() slice3 = Slice(name = slice3Name, omf_friendly=True, site=self.testSite, creator=self.testUser) slice3=self.save_and_wait_for_enacted(slice3, nonempty_fields=["tenant_id"]) network3 = Network(name = slice3Name + "-nat", template = self.get_network_template("private-nat"), owner = slice3) # note that router_id will not be filled in for nat-net, since nat-net has no routers network3=self.save_and_wait_for_enacted(network3, nonempty_fields=["network_id", "subnet_id", "subnet"]) network3_slice3 = NetworkSlice(network=network3, slice=slice3) network3_slice3.save() # does not need to be enacted sliver3_1 = Sliver(image = self.testImage, creator=self.testUser, slice=slice3, node=self.testNode, deploymentNetwork=self.testDeployment) sliver3_1=self.save_and_wait_for_enacted(sliver3_1, nonempty_fields=["instance_id", "ip"]) ports = self.wait_for_ports(sliver3_1, count=2) self.verify_network_names(ports, [slice3.name, "nat-net"])
def test_slice2(self): slice2Name = self.make_slice_name() slice2 = Slice(name = slice2Name, omf_friendly=True, site=self.testSite, creator=self.testUser) slice2=self.save_and_wait_for_enacted(slice2, nonempty_fields=["tenant_id"]) network2 = Network(name = slice2Name + "-pvt", template = self.get_network_template("private"), owner = slice2) network2=self.save_and_wait_for_enacted(network2, nonempty_fields=["network_id", "subnet_id", "router_id", "subnet"]) network2_slice2 = NetworkSlice(network=network2, slice=slice2) network2_slice2.save() # does not need to be enacted sliver2_1 = Sliver(image = self.testImage, creator=self.testUser, slice=slice2, node=self.testNode, deploymentNetwork=self.testDeployment) sliver2_1=self.save_and_wait_for_enacted(sliver2_1, nonempty_fields=["instance_id", "ip"]) ports = self.wait_for_ports(sliver2_1, count=2) self.verify_network_names(ports, [slice2.name, network2.name]) self.slice2 = slice2 self.network2 = network2
def test_slice1(self): slice1Name = self.make_slice_name() slice1 = Slice(name=slice1Name, omf_friendly=True, site=self.testSite, creator=self.testUser) slice1 = self.save_and_wait_for_enacted(slice1, nonempty_fields=["tenant_id"]) instance1 = Instance(image=self.testImage, creator=self.testUser, slice=slice1, node=self.testNode, deploymentNetwork=self.testDeployment) instance1 = self.save_and_wait_for_enacted( instance1, nonempty_fields=["instance_id", "ip"]) # instance1 should have only one port, its private network ports = self.wait_for_ports(instance1, count=1) self.verify_network_names(ports, [slice1.name]) network1 = Network(name=slice1Name + "-pvt", template=self.get_network_template("private"), owner=slice1) network1 = self.save_and_wait_for_enacted( network1, nonempty_fields=["network_id", "subnet_id", "router_id", "subnet"]) network1_slice1 = NetworkSlice(network=network1, slice=slice1) network1_slice1.save() # does not need to be enacted instance1_2 = Instance(image=self.testImage, creator=self.testUser, slice=slice1, node=self.testNode, deploymentNetwork=self.testDeployment) instance1_2 = self.save_and_wait_for_enacted( instance1_2, nonempty_fields=["instance_id", "ip"]) ports = self.wait_for_ports(instance1_2, count=2) self.verify_network_names(ports, [slice1.name, network1.name]) self.slice1 = slice1 self.network1 = network1
def create(self): nodetemplate = self.nodetemplate xos_args = self.get_xos_args() if not xos_args.get("owner", None): raise Exception("Must specify slice when creating network") if not xos_args.get("template", None): raise Exception( "Must specify network template when creating network") network = Network(**xos_args) network.caller = self.user network.save() self.postprocess(network) self.info("Created Network '%s' owned by Slice '%s'" % (str(network), str(network.owner)))
def handle(slice): from core.models import Controller, ControllerSlice, SiteDeployment, Network, NetworkSlice, NetworkTemplate, Slice from collections import defaultdict # only create nat_net if not using VTN support_nat_net = not getattr(Config(), "networking_use_vtn", False) print "MODEL POLICY: slice", slice # slice = Slice.get(slice_id) controller_slices = ControllerSlice.objects.filter(slice=slice) existing_controllers = [cs.controller for cs in controller_slices] print "MODEL POLICY: slice existing_controllers=", existing_controllers all_controllers = Controller.objects.all() for controller in all_controllers: if controller not in existing_controllers: print "MODEL POLICY: slice adding controller", controller sd = ControllerSlice(slice=slice, controller=controller) sd.save() if slice.network in ["host", "bridged"]: # Host and Bridged docker containers need no networks and they will # only get in the way. print "MODEL POLICY: Skipping network creation" elif slice.network in ["noauto"]: # do nothing pass else: # make sure slice has at least 1 public and 1 private networkd public_nets = [] private_nets = [] networks = Network.objects.filter(owner=slice) for network in networks: if not network.autoconnect: continue if network.template.name == 'Public dedicated IPv4': public_nets.append(network) elif network.template.name == 'Public shared IPv4': public_nets.append(network) elif network.template.name == 'Private': private_nets.append(network) if support_nat_net and (not public_nets): # ensure there is at least one public network, and default it to dedicated nat_net = Network(name=slice.name + '-nat', template=NetworkTemplate.objects.get( name='Public shared IPv4'), owner=slice) if slice.exposed_ports: nat_net.ports = slice.exposed_ports nat_net.save() public_nets.append(nat_net) print "MODEL POLICY: slice", slice, "made nat-net" if not private_nets: private_net = Network( name=slice.name + '-private', template=NetworkTemplate.objects.get(name='Private'), owner=slice) private_net.save() print "MODEL POLICY: slice", slice, "made private net" private_nets = [private_net] # create slice networks public_net_slice = None private_net_slice = None net_slices = NetworkSlice.objects.filter(slice=slice, network__in=private_nets + public_nets) for net_slice in net_slices: if net_slice.network in public_nets: public_net_slice = net_slice elif net_slice.network in private_nets: private_net_slice = net_slice if support_nat_net and (not public_net_slice): public_net_slice = NetworkSlice(slice=slice, network=public_nets[0]) public_net_slice.save() print "MODEL POLICY: slice", slice, "made public_net_slice" if not private_net_slice: private_net_slice = NetworkSlice(slice=slice, network=private_nets[0]) private_net_slice.save() print "MODEL POLICY: slice", slice, "made private_net_slice" print "MODEL POLICY: slice", slice, "DONE"