blob: 87a5ef46127ddeae549bdccfa0a3657bb58b31c8 [file] [log] [blame]
Matteo Scandolof0441032017-08-08 13:05:26 -07001
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 Bakeraf599eb2017-03-21 12:43:26 -070017from synchronizers.new_base.modelaccessor import *
Scott Baker46a3ee92017-05-30 16:44:56 -070018from synchronizers.new_base.policy import Policy
Scott Bakerb63ea792016-08-11 10:24:48 -070019
Scott Baker46a3ee92017-05-30 16:44:56 -070020class SlicePolicy(Policy):
21 model_name = "Slice"
Scott Bakerb63ea792016-08-11 10:24:48 -070022
Scott Baker46a3ee92017-05-30 16:44:56 -070023 def handle_create(self, slice):
24 return self.handle_update(slice)
Scott Bakeraf599eb2017-03-21 12:43:26 -070025
Scott Baker46a3ee92017-05-30 16:44:56 -070026 def handle_update(self, slice):
27 support_nat_net = False # Assume we're using VTN rather than nat-net
Scott Bakerb63ea792016-08-11 10:24:48 -070028
Scott Baker46a3ee92017-05-30 16:44:56 -070029 # slice = Slice.get(slice_id)
Scott Bakerb63ea792016-08-11 10:24:48 -070030
Scott Baker46a3ee92017-05-30 16:44:56 -070031 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 Bakerb63ea792016-08-11 10:24:48 -070034
Scott Baker46a3ee92017-05-30 16:44:56 -070035 self.logger.info("MODEL POLICY: slice existing_controllers=%s" % existing_controllers)
Scott Bakerb63ea792016-08-11 10:24:48 -070036
Scott Baker46a3ee92017-05-30 16:44:56 -070037 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 Baker75bae452017-03-27 20:10:58 -070043
Scott Baker46a3ee92017-05-30 16:44:56 -070044 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 Bakerb63ea792016-08-11 10:24:48 -070077
Scott Baker46a3ee92017-05-30 16:44:56 -070078 if not private_nets:
79 private_net = Network(
80 name = slice.name+'-private',
81 template = NetworkTemplate.objects.get(name='Private'),
Scott Bakerb63ea792016-08-11 10:24:48 -070082 owner = slice
Scott Baker46a3ee92017-05-30 16:44:56 -070083 )
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 Bakerb63ea792016-08-11 10:24:48 -070090
Scott Baker46a3ee92017-05-30 16:44:56 -070091 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 Bakeraf599eb2017-03-21 12:43:26 -070095
Scott Baker46a3ee92017-05-30 16:44:56 -070096 for net_slice in net_slices:
Scott Bakerd644d312017-07-27 15:26:04 -070097 if net_slice.network.id in public_net_ids:
Scott Baker46a3ee92017-05-30 16:44:56 -070098 public_net_slice = net_slice
Scott Bakerd644d312017-07-27 15:26:04 -070099 elif net_slice.network.id in private_net_ids:
Scott Baker46a3ee92017-05-30 16:44:56 -0700100 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 Bakeraf599eb2017-03-21 12:43:26 -0700109
Scott Baker46a3ee92017-05-30 16:44:56 -0700110 # TODO: This feels redundant with the reaper
Scott Bakerd644d312017-07-27 15:26:04 -0700111 def handle_delete(self, slice):
Scott Baker46a3ee92017-05-30 16:44:56 -0700112 public_nets = []
113 private_net = None
114 networks = Network.objects.filter(owner_id=slice.id)
Scott Bakerb63ea792016-08-11 10:24:48 -0700115
Scott Baker46a3ee92017-05-30 16:44:56 -0700116 for n in networks:
117 n.delete()
Scott Bakerb63ea792016-08-11 10:24:48 -0700118
Scott Baker46a3ee92017-05-30 16:44:56 -0700119 # Note that sliceprivileges and slicecontrollers are autodeleted, through the dependency graph