blob: a24e9a296a3b01db865a80ea5dc9b33d175397de [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 Bakerc808c672019-02-04 11:38:20 -080017from xossynchronizer.modelaccessor import *
18from xossynchronizer.model_policies.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):
Scott Baker62c7eaf2018-05-22 15:59:26 -070027 # Ignore new-style slices as we don't want to run all the old policies
28 if (slice.trust_domain != None):
29 self.logger.info("This is a new-style openstack slice, which this policy shall ignore")
30 return
31
Scott Baker46a3ee92017-05-30 16:44:56 -070032 support_nat_net = False # Assume we're using VTN rather than nat-net
Scott Bakerb63ea792016-08-11 10:24:48 -070033
Scott Baker46a3ee92017-05-30 16:44:56 -070034 # slice = Slice.get(slice_id)
Scott Bakerb63ea792016-08-11 10:24:48 -070035
Scott Baker46a3ee92017-05-30 16:44:56 -070036 controller_slices = ControllerSlice.objects.filter(slice_id=slice.id)
37 existing_controllers = [cs.controller for cs in controller_slices]
38 existing_controllers_ids = [x.id for x in existing_controllers]
Scott Bakerb63ea792016-08-11 10:24:48 -070039
Scott Baker46a3ee92017-05-30 16:44:56 -070040 self.logger.info("MODEL POLICY: slice existing_controllers=%s" % existing_controllers)
Scott Bakerb63ea792016-08-11 10:24:48 -070041
Scott Baker46a3ee92017-05-30 16:44:56 -070042 all_controllers = Controller.objects.all()
43 for controller in all_controllers:
44 if controller.id not in existing_controllers_ids:
45 self.logger.info("MODEL POLICY: slice adding controller %s" % controller)
46 sd = ControllerSlice(slice=slice, controller=controller)
47 sd.save()
Scott Baker75bae452017-03-27 20:10:58 -070048
Scott Baker46a3ee92017-05-30 16:44:56 -070049 if slice.network in ["host", "bridged"]:
50 # Host and Bridged docker containers need no networks and they will
51 # only get in the way.
52 self.logger.info("MODEL POLICY: Skipping network creation")
53 elif slice.network in ["noauto"]:
54 # do nothing
55 pass
56 else:
57 # make sure slice has at least 1 public and 1 private networkd
58 public_nets = []
59 private_nets = []
60 networks = Network.objects.filter(owner_id=slice.id)
61 for network in networks:
62 if not network.autoconnect:
63 continue
64 if network.template.name == 'Public dedicated IPv4':
65 public_nets.append(network)
66 elif network.template.name == 'Public shared IPv4':
67 public_nets.append(network)
68 elif network.template.name == 'Private':
69 private_nets.append(network)
70 if support_nat_net and (not public_nets):
71 # ensure there is at least one public network, and default it to dedicated
72 nat_net = Network(
73 name = slice.name+'-nat',
74 template = NetworkTemplate.objects.get(name='Public shared IPv4'),
75 owner = slice
76 )
77 if slice.exposed_ports:
78 nat_net.ports = slice.exposed_ports
79 nat_net.save()
80 public_nets.append(nat_net)
81 self.logger.info("MODEL POLICY: slice %s made nat-net" % slice)
Scott Bakerb63ea792016-08-11 10:24:48 -070082
Scott Baker46a3ee92017-05-30 16:44:56 -070083 if not private_nets:
84 private_net = Network(
85 name = slice.name+'-private',
86 template = NetworkTemplate.objects.get(name='Private'),
Scott Bakerb63ea792016-08-11 10:24:48 -070087 owner = slice
Scott Baker46a3ee92017-05-30 16:44:56 -070088 )
89 private_net.save()
90 self.logger.info("MODEL POLICY: slice %s made private net" % slice)
91 private_nets = [private_net]
92 # create slice networks
93 public_net_slice = None
94 private_net_slice = None
Scott Bakerb63ea792016-08-11 10:24:48 -070095
Scott Baker46a3ee92017-05-30 16:44:56 -070096 public_net_ids = [x.id for x in public_nets]
97 private_net_ids = [x.id for x in private_nets]
98 net_slices = NetworkSlice.objects.filter(slice_id=slice.id)
99 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 -0700100
Scott Baker46a3ee92017-05-30 16:44:56 -0700101 for net_slice in net_slices:
Scott Bakerd644d312017-07-27 15:26:04 -0700102 if net_slice.network.id in public_net_ids:
Scott Baker46a3ee92017-05-30 16:44:56 -0700103 public_net_slice = net_slice
Scott Bakerd644d312017-07-27 15:26:04 -0700104 elif net_slice.network.id in private_net_ids:
Scott Baker46a3ee92017-05-30 16:44:56 -0700105 private_net_slice = net_slice
106 if support_nat_net and (not public_net_slice):
107 public_net_slice = NetworkSlice(slice=slice, network=public_nets[0])
108 public_net_slice.save()
109 self.logger.info("MODEL POLICY: slice %s made public_net_slice" % slice)
110 if not private_net_slice:
111 private_net_slice = NetworkSlice(slice=slice, network=private_nets[0])
112 private_net_slice.save()
113 self.logger.info("MODEL POLICY: slice %s made private_net_slice" % slice)
Scott Bakeraf599eb2017-03-21 12:43:26 -0700114
Scott Baker46a3ee92017-05-30 16:44:56 -0700115 # TODO: This feels redundant with the reaper
Scott Bakerd644d312017-07-27 15:26:04 -0700116 def handle_delete(self, slice):
Scott Baker62c7eaf2018-05-22 15:59:26 -0700117 # Ignore new-style slices as we don't want to run all the old policies
118 if (slice.trust_domain != None):
119 self.logger.info("This is a new-style openstack slice, which this policy shall ignore")
120 return
Scott Bakerb63ea792016-08-11 10:24:48 -0700121
Scott Baker62c7eaf2018-05-22 15:59:26 -0700122 networks = Network.objects.filter(owner_id=slice.id)
Scott Baker46a3ee92017-05-30 16:44:56 -0700123 for n in networks:
124 n.delete()
Scott Bakerb63ea792016-08-11 10:24:48 -0700125
Scott Baker46a3ee92017-05-30 16:44:56 -0700126 # Note that sliceprivileges and slicecontrollers are autodeleted, through the dependency graph