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