blob: bcda884890cb32cd467c7cfc06a65b743c994396 [file] [log] [blame]
Scott Bakeraf599eb2017-03-21 12:43:26 -07001from synchronizers.new_base.modelaccessor import *
Scott Baker46a3ee92017-05-30 16:44:56 -07002from synchronizers.new_base.policy import Policy
Scott Bakerb63ea792016-08-11 10:24:48 -07003
Scott Baker46a3ee92017-05-30 16:44:56 -07004class SlicePolicy(Policy):
5 model_name = "Slice"
Scott Bakerb63ea792016-08-11 10:24:48 -07006
Scott Baker46a3ee92017-05-30 16:44:56 -07007 def handle_create(self, slice):
8 return self.handle_update(slice)
Scott Bakeraf599eb2017-03-21 12:43:26 -07009
Scott Baker46a3ee92017-05-30 16:44:56 -070010 def handle_update(self, slice):
11 support_nat_net = False # Assume we're using VTN rather than nat-net
Scott Bakerb63ea792016-08-11 10:24:48 -070012
Scott Baker46a3ee92017-05-30 16:44:56 -070013 # slice = Slice.get(slice_id)
Scott Bakerb63ea792016-08-11 10:24:48 -070014
Scott Baker46a3ee92017-05-30 16:44:56 -070015 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 Bakerb63ea792016-08-11 10:24:48 -070018
Scott Baker46a3ee92017-05-30 16:44:56 -070019 self.logger.info("MODEL POLICY: slice existing_controllers=%s" % existing_controllers)
Scott Bakerb63ea792016-08-11 10:24:48 -070020
Scott Baker46a3ee92017-05-30 16:44:56 -070021 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 Baker75bae452017-03-27 20:10:58 -070027
Scott Baker46a3ee92017-05-30 16:44:56 -070028 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 Bakerb63ea792016-08-11 10:24:48 -070061
Scott Baker46a3ee92017-05-30 16:44:56 -070062 if not private_nets:
63 private_net = Network(
64 name = slice.name+'-private',
65 template = NetworkTemplate.objects.get(name='Private'),
Scott Bakerb63ea792016-08-11 10:24:48 -070066 owner = slice
Scott Baker46a3ee92017-05-30 16:44:56 -070067 )
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 Bakerb63ea792016-08-11 10:24:48 -070074
Scott Baker46a3ee92017-05-30 16:44:56 -070075 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 Bakeraf599eb2017-03-21 12:43:26 -070079
Scott Baker46a3ee92017-05-30 16:44:56 -070080 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 Bakeraf599eb2017-03-21 12:43:26 -070093
Scott Baker46a3ee92017-05-30 16:44:56 -070094 # 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 Bakerb63ea792016-08-11 10:24:48 -070099
Scott Baker46a3ee92017-05-30 16:44:56 -0700100 for n in networks:
101 n.delete()
Scott Bakerb63ea792016-08-11 10:24:48 -0700102
Scott Baker46a3ee92017-05-30 16:44:56 -0700103 # Note that sliceprivileges and slicecontrollers are autodeleted, through the dependency graph