blob: 2501fe6c6eddd2e35eb7e806f9a28822e91d9f40 [file] [log] [blame]
Matteo Scandolof5e10332017-08-08 13:05:25 -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
Andy Bavier89a95422016-11-02 14:38:39 -040017from xos.config import Config
18
19def handle_delete(slice):
20 from core.models import Controller, ControllerSlice, SiteDeployment, Network, NetworkSlice,NetworkTemplate, Slice
21 from collections import defaultdict
22
23 public_nets = []
24 private_net = None
25 networks = Network.objects.filter(owner=slice)
26
27 for n in networks:
28 n.delete()
29
30 # Note that sliceprivileges and slicecontrollers are autodeleted, through the dependency graph
31
32def handle(slice):
33 from core.models import Controller, ControllerSlice, SiteDeployment, Network, NetworkSlice,NetworkTemplate, Slice
34 from collections import defaultdict
35
36 # only create nat_net if not using VTN
37 support_nat_net = not getattr(Config(), "networking_use_vtn", False)
38
39 print "MODEL POLICY: slice", slice
40
41 # slice = Slice.get(slice_id)
42
43 controller_slices = ControllerSlice.objects.filter(slice=slice)
44 existing_controllers = [cs.controller for cs in controller_slices]
45
46 print "MODEL POLICY: slice existing_controllers=", existing_controllers
47
48 all_controllers = Controller.objects.all()
49 for controller in all_controllers:
50 if controller not in existing_controllers:
51 print "MODEL POLICY: slice adding controller", controller
52 sd = ControllerSlice(slice=slice, controller=controller)
53 sd.save()
54
55 if slice.network in ["host", "bridged"]:
56 # Host and Bridged docker containers need no networks and they will
57 # only get in the way.
58 print "MODEL POLICY: Skipping network creation"
59 elif slice.network in ["noauto"]:
60 # do nothing
61 pass
62 else:
63 # make sure slice has at least 1 public and 1 private networkd
64 public_nets = []
65 private_nets = []
66 networks = Network.objects.filter(owner=slice)
67 for network in networks:
68 if not network.autoconnect:
69 continue
70 if network.template.name == 'Public dedicated IPv4':
71 public_nets.append(network)
72 elif network.template.name == 'Public shared IPv4':
73 public_nets.append(network)
74 elif network.template.name == 'Private':
75 private_nets.append(network)
76 if support_nat_net and (not public_nets):
77 # ensure there is at least one public network, and default it to dedicated
78 nat_net = Network(
79 name = slice.name+'-nat',
80 template = NetworkTemplate.objects.get(name='Public shared IPv4'),
81 owner = slice
82 )
83 if slice.exposed_ports:
84 nat_net.ports = slice.exposed_ports
85 nat_net.save()
86 public_nets.append(nat_net)
87 print "MODEL POLICY: slice", slice, "made nat-net"
88
89 if not private_nets:
90 private_net = Network(
91 name = slice.name+'-private',
92 template = NetworkTemplate.objects.get(name='Private'),
93 owner = slice
94 )
95 private_net.save()
96 print "MODEL POLICY: slice", slice, "made private net"
97 private_nets = [private_net]
98 # create slice networks
99 public_net_slice = None
100 private_net_slice = None
101 net_slices = NetworkSlice.objects.filter(slice=slice, network__in=private_nets+public_nets)
102 for net_slice in net_slices:
103 if net_slice.network in public_nets:
104 public_net_slice = net_slice
105 elif net_slice.network in private_nets:
106 private_net_slice = net_slice
107 if support_nat_net and (not public_net_slice):
108 public_net_slice = NetworkSlice(slice=slice, network=public_nets[0])
109 public_net_slice.save()
110 print "MODEL POLICY: slice", slice, "made public_net_slice"
111 if not private_net_slice:
112 private_net_slice = NetworkSlice(slice=slice, network=private_nets[0])
113 private_net_slice.save()
114 print "MODEL POLICY: slice", slice, "made private_net_slice"
115
116 print "MODEL POLICY: slice", slice, "DONE"
117
118