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