blob: 68aa59648baf6953a16e2cd782ce11adbad93332 [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001from xos.config import Config
Scott Bakeraf599eb2017-03-21 12:43:26 -07002from synchronizers.new_base.modelaccessor import *
Scott Bakerb63ea792016-08-11 10:24:48 -07003
4def handle_delete(slice):
Scott Bakerb63ea792016-08-11 10:24:48 -07005 public_nets = []
6 private_net = None
Scott Bakeraf599eb2017-03-21 12:43:26 -07007 networks = Network.objects.filter(owner_id=slice.id)
Scott Bakerb63ea792016-08-11 10:24:48 -07008
9 for n in networks:
Scott Bakeraf599eb2017-03-21 12:43:26 -070010 n.delete()
11
Scott Bakerb63ea792016-08-11 10:24:48 -070012 # Note that sliceprivileges and slicecontrollers are autodeleted, through the dependency graph
13
14def handle(slice):
Scott Bakerb63ea792016-08-11 10:24:48 -070015 # only create nat_net if not using VTN
16 support_nat_net = not getattr(Config(), "networking_use_vtn", False)
17
18 print "MODEL POLICY: slice", slice
19
20 # slice = Slice.get(slice_id)
21
Scott Bakeraf599eb2017-03-21 12:43:26 -070022 controller_slices = ControllerSlice.objects.filter(slice_id=slice.id)
Scott Baker75bae452017-03-27 20:10:58 -070023 existing_controllers = [cs.controller for cs in controller_slices]
24 existing_controllers_ids = [x.id for x in existing_controllers]
25
Scott Bakerb63ea792016-08-11 10:24:48 -070026 print "MODEL POLICY: slice existing_controllers=", existing_controllers
27
28 all_controllers = Controller.objects.all()
29 for controller in all_controllers:
Scott Baker75bae452017-03-27 20:10:58 -070030 if controller.id not in existing_controllers_ids:
Scott Bakerb63ea792016-08-11 10:24:48 -070031 print "MODEL POLICY: slice adding controller", controller
32 sd = ControllerSlice(slice=slice, controller=controller)
33 sd.save()
34
35 if slice.network in ["host", "bridged"]:
36 # Host and Bridged docker containers need no networks and they will
37 # only get in the way.
38 print "MODEL POLICY: Skipping network creation"
39 elif slice.network in ["noauto"]:
40 # do nothing
41 pass
42 else:
43 # make sure slice has at least 1 public and 1 private networkd
44 public_nets = []
45 private_nets = []
Scott Bakeraf599eb2017-03-21 12:43:26 -070046 networks = Network.objects.filter(owner_id=slice.id)
Scott Bakerb63ea792016-08-11 10:24:48 -070047 for network in networks:
48 if not network.autoconnect:
49 continue
50 if network.template.name == 'Public dedicated IPv4':
51 public_nets.append(network)
52 elif network.template.name == 'Public shared IPv4':
53 public_nets.append(network)
54 elif network.template.name == 'Private':
55 private_nets.append(network)
56 if support_nat_net and (not public_nets):
57 # ensure there is at least one public network, and default it to dedicated
58 nat_net = Network(
59 name = slice.name+'-nat',
60 template = NetworkTemplate.objects.get(name='Public shared IPv4'),
61 owner = slice
62 )
63 if slice.exposed_ports:
64 nat_net.ports = slice.exposed_ports
65 nat_net.save()
66 public_nets.append(nat_net)
67 print "MODEL POLICY: slice", slice, "made nat-net"
68
69 if not private_nets:
70 private_net = Network(
71 name = slice.name+'-private',
72 template = NetworkTemplate.objects.get(name='Private'),
73 owner = slice
74 )
75 private_net.save()
76 print "MODEL POLICY: slice", slice, "made private net"
77 private_nets = [private_net]
78 # create slice networks
79 public_net_slice = None
80 private_net_slice = None
Scott Bakeraf599eb2017-03-21 12:43:26 -070081
82 public_net_ids = [x.id for x in public_nets]
83 private_net_ids = [x.id for x in private_nets]
84 net_slices = NetworkSlice.objects.filter(slice_id=slice.id)
85 net_slices = [x for x in net_slices if x.network_id in public_net_ids+private_net_ids]
86
Scott Bakerb63ea792016-08-11 10:24:48 -070087 for net_slice in net_slices:
88 if net_slice.network in public_nets:
89 public_net_slice = net_slice
90 elif net_slice.network in private_nets:
91 private_net_slice = net_slice
92 if support_nat_net and (not public_net_slice):
93 public_net_slice = NetworkSlice(slice=slice, network=public_nets[0])
94 public_net_slice.save()
95 print "MODEL POLICY: slice", slice, "made public_net_slice"
96 if not private_net_slice:
97 private_net_slice = NetworkSlice(slice=slice, network=private_nets[0])
98 private_net_slice.save()
99 print "MODEL POLICY: slice", slice, "made private_net_slice"
100
101 print "MODEL POLICY: slice", slice, "DONE"
102
103