Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 1 | from xos.config import Config |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 2 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 3 | |
| 4 | def handle_delete(slice): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 5 | public_nets = [] |
| 6 | private_net = None |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 7 | networks = Network.objects.filter(owner_id=slice.id) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 8 | |
| 9 | for n in networks: |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 10 | n.delete() |
| 11 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 12 | # Note that sliceprivileges and slicecontrollers are autodeleted, through the dependency graph |
| 13 | |
| 14 | def handle(slice): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 15 | # only create nat_net if not using VTN |
| 16 | support_nat_net = not getattr(Config(), "networking_use_vtn", False) |
| 17 | |
| 18 | print "MODEL POLICY: slice", slice |
| 19 | |
| 20 | # slice = Slice.get(slice_id) |
| 21 | |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 22 | controller_slices = ControllerSlice.objects.filter(slice_id=slice.id) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 23 | existing_controllers = [cs.controller for cs in controller_slices] |
| 24 | |
| 25 | print "MODEL POLICY: slice existing_controllers=", existing_controllers |
| 26 | |
| 27 | all_controllers = Controller.objects.all() |
| 28 | for controller in all_controllers: |
| 29 | if controller not in existing_controllers: |
| 30 | print "MODEL POLICY: slice adding controller", controller |
| 31 | sd = ControllerSlice(slice=slice, controller=controller) |
| 32 | sd.save() |
| 33 | |
| 34 | if slice.network in ["host", "bridged"]: |
| 35 | # Host and Bridged docker containers need no networks and they will |
| 36 | # only get in the way. |
| 37 | print "MODEL POLICY: Skipping network creation" |
| 38 | elif slice.network in ["noauto"]: |
| 39 | # do nothing |
| 40 | pass |
| 41 | else: |
| 42 | # make sure slice has at least 1 public and 1 private networkd |
| 43 | public_nets = [] |
| 44 | private_nets = [] |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 45 | networks = Network.objects.filter(owner_id=slice.id) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 46 | for network in networks: |
| 47 | if not network.autoconnect: |
| 48 | continue |
| 49 | if network.template.name == 'Public dedicated IPv4': |
| 50 | public_nets.append(network) |
| 51 | elif network.template.name == 'Public shared IPv4': |
| 52 | public_nets.append(network) |
| 53 | elif network.template.name == 'Private': |
| 54 | private_nets.append(network) |
| 55 | if support_nat_net and (not public_nets): |
| 56 | # ensure there is at least one public network, and default it to dedicated |
| 57 | nat_net = Network( |
| 58 | name = slice.name+'-nat', |
| 59 | template = NetworkTemplate.objects.get(name='Public shared IPv4'), |
| 60 | owner = slice |
| 61 | ) |
| 62 | if slice.exposed_ports: |
| 63 | nat_net.ports = slice.exposed_ports |
| 64 | nat_net.save() |
| 65 | public_nets.append(nat_net) |
| 66 | print "MODEL POLICY: slice", slice, "made nat-net" |
| 67 | |
| 68 | if not private_nets: |
| 69 | private_net = Network( |
| 70 | name = slice.name+'-private', |
| 71 | template = NetworkTemplate.objects.get(name='Private'), |
| 72 | owner = slice |
| 73 | ) |
| 74 | private_net.save() |
| 75 | print "MODEL POLICY: slice", slice, "made private net" |
| 76 | private_nets = [private_net] |
| 77 | # create slice networks |
| 78 | public_net_slice = None |
| 79 | private_net_slice = None |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 80 | |
| 81 | public_net_ids = [x.id for x in public_nets] |
| 82 | private_net_ids = [x.id for x in private_nets] |
| 83 | net_slices = NetworkSlice.objects.filter(slice_id=slice.id) |
| 84 | net_slices = [x for x in net_slices if x.network_id in public_net_ids+private_net_ids] |
| 85 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 86 | for net_slice in net_slices: |
| 87 | if net_slice.network in public_nets: |
| 88 | public_net_slice = net_slice |
| 89 | elif net_slice.network in private_nets: |
| 90 | private_net_slice = net_slice |
| 91 | if support_nat_net and (not public_net_slice): |
| 92 | public_net_slice = NetworkSlice(slice=slice, network=public_nets[0]) |
| 93 | public_net_slice.save() |
| 94 | print "MODEL POLICY: slice", slice, "made public_net_slice" |
| 95 | if not private_net_slice: |
| 96 | private_net_slice = NetworkSlice(slice=slice, network=private_nets[0]) |
| 97 | private_net_slice.save() |
| 98 | print "MODEL POLICY: slice", slice, "made private_net_slice" |
| 99 | |
| 100 | print "MODEL POLICY: slice", slice, "DONE" |
| 101 | |
| 102 | |