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