Matteo Scandolo | f044103 | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 17 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 18 | from synchronizers.new_base.policy import Policy |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 19 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 20 | class SlicePolicy(Policy): |
| 21 | model_name = "Slice" |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 22 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 23 | def handle_create(self, slice): |
| 24 | return self.handle_update(slice) |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 25 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 26 | def handle_update(self, slice): |
Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 27 | # Ignore new-style slices as we don't want to run all the old policies |
| 28 | if (slice.trust_domain != None): |
| 29 | self.logger.info("This is a new-style openstack slice, which this policy shall ignore") |
| 30 | return |
| 31 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 32 | 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] | 33 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 34 | # slice = Slice.get(slice_id) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 35 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 36 | controller_slices = ControllerSlice.objects.filter(slice_id=slice.id) |
| 37 | existing_controllers = [cs.controller for cs in controller_slices] |
| 38 | existing_controllers_ids = [x.id for x in existing_controllers] |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 39 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 40 | self.logger.info("MODEL POLICY: slice existing_controllers=%s" % existing_controllers) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 41 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 42 | all_controllers = Controller.objects.all() |
| 43 | for controller in all_controllers: |
| 44 | if controller.id not in existing_controllers_ids: |
| 45 | self.logger.info("MODEL POLICY: slice adding controller %s" % controller) |
| 46 | sd = ControllerSlice(slice=slice, controller=controller) |
| 47 | sd.save() |
Scott Baker | 75bae45 | 2017-03-27 20:10:58 -0700 | [diff] [blame] | 48 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 49 | if slice.network in ["host", "bridged"]: |
| 50 | # Host and Bridged docker containers need no networks and they will |
| 51 | # only get in the way. |
| 52 | self.logger.info("MODEL POLICY: Skipping network creation") |
| 53 | elif slice.network in ["noauto"]: |
| 54 | # do nothing |
| 55 | pass |
| 56 | else: |
| 57 | # make sure slice has at least 1 public and 1 private networkd |
| 58 | public_nets = [] |
| 59 | private_nets = [] |
| 60 | networks = Network.objects.filter(owner_id=slice.id) |
| 61 | for network in networks: |
| 62 | if not network.autoconnect: |
| 63 | continue |
| 64 | if network.template.name == 'Public dedicated IPv4': |
| 65 | public_nets.append(network) |
| 66 | elif network.template.name == 'Public shared IPv4': |
| 67 | public_nets.append(network) |
| 68 | elif network.template.name == 'Private': |
| 69 | private_nets.append(network) |
| 70 | if support_nat_net and (not public_nets): |
| 71 | # ensure there is at least one public network, and default it to dedicated |
| 72 | nat_net = Network( |
| 73 | name = slice.name+'-nat', |
| 74 | template = NetworkTemplate.objects.get(name='Public shared IPv4'), |
| 75 | owner = slice |
| 76 | ) |
| 77 | if slice.exposed_ports: |
| 78 | nat_net.ports = slice.exposed_ports |
| 79 | nat_net.save() |
| 80 | public_nets.append(nat_net) |
| 81 | self.logger.info("MODEL POLICY: slice %s made nat-net" % slice) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 82 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 83 | if not private_nets: |
| 84 | private_net = Network( |
| 85 | name = slice.name+'-private', |
| 86 | template = NetworkTemplate.objects.get(name='Private'), |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 87 | owner = slice |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 88 | ) |
| 89 | private_net.save() |
| 90 | self.logger.info("MODEL POLICY: slice %s made private net" % slice) |
| 91 | private_nets = [private_net] |
| 92 | # create slice networks |
| 93 | public_net_slice = None |
| 94 | private_net_slice = None |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 95 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 96 | public_net_ids = [x.id for x in public_nets] |
| 97 | private_net_ids = [x.id for x in private_nets] |
| 98 | net_slices = NetworkSlice.objects.filter(slice_id=slice.id) |
| 99 | 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] | 100 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 101 | for net_slice in net_slices: |
Scott Baker | d644d31 | 2017-07-27 15:26:04 -0700 | [diff] [blame] | 102 | if net_slice.network.id in public_net_ids: |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 103 | public_net_slice = net_slice |
Scott Baker | d644d31 | 2017-07-27 15:26:04 -0700 | [diff] [blame] | 104 | elif net_slice.network.id in private_net_ids: |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 105 | private_net_slice = net_slice |
| 106 | if support_nat_net and (not public_net_slice): |
| 107 | public_net_slice = NetworkSlice(slice=slice, network=public_nets[0]) |
| 108 | public_net_slice.save() |
| 109 | self.logger.info("MODEL POLICY: slice %s made public_net_slice" % slice) |
| 110 | if not private_net_slice: |
| 111 | private_net_slice = NetworkSlice(slice=slice, network=private_nets[0]) |
| 112 | private_net_slice.save() |
| 113 | self.logger.info("MODEL POLICY: slice %s made private_net_slice" % slice) |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 114 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 115 | # TODO: This feels redundant with the reaper |
Scott Baker | d644d31 | 2017-07-27 15:26:04 -0700 | [diff] [blame] | 116 | def handle_delete(self, slice): |
Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 117 | # Ignore new-style slices as we don't want to run all the old policies |
| 118 | if (slice.trust_domain != None): |
| 119 | self.logger.info("This is a new-style openstack slice, which this policy shall ignore") |
| 120 | return |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 121 | |
Scott Baker | 62c7eaf | 2018-05-22 15:59:26 -0700 | [diff] [blame] | 122 | networks = Network.objects.filter(owner_id=slice.id) |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 123 | for n in networks: |
| 124 | n.delete() |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 125 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 126 | # Note that sliceprivileges and slicecontrollers are autodeleted, through the dependency graph |