Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 1 | from synchronizers.new_base.modelaccessor import * |
| 2 | from collections import defaultdict |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 3 | from synchronizers.new_base.policy import Policy |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 4 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 5 | class NetworkPolicy(Policy): |
| 6 | model_name = "Network" |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 7 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 8 | def handle_create(self, network): |
| 9 | return self.handle_update(network) |
| 10 | |
| 11 | def handle_update(self, network): |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 12 | # if (not network.owner.policed) or (network.owner.policed<network.owner.updated): |
| 13 | # raise Exception("Cannot apply network policies until slice policies have completed") |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 14 | |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 15 | #expected_controllers = [] |
| 16 | #for slice_controller in ControllerSlice.objects.all(): |
| 17 | # if slice_controller.slice.id == network.owner.id: |
| 18 | # expected_controllers.append(slice_controller.controller) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 19 | |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 20 | # For simplicity, let's assume that a network gets deployed on all controllers. |
| 21 | expected_controllers = Controller.objects.all() |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 22 | |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 23 | existing_controllers = [] |
| 24 | for cn in ControllerNetwork.objects.all(): |
| 25 | if cn.network.id == network.id: |
Scott Baker | 959cba3 | 2017-06-11 19:41:23 -0700 | [diff] [blame] | 26 | existing_controllers.append(cn.controller) |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 27 | |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 28 | existing_controller_ids = [c.id for c in existing_controllers] |
| 29 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 30 | for expected_controller in expected_controllers: |
Scott Baker | b5fa8a2 | 2017-06-07 17:31:15 -0700 | [diff] [blame] | 31 | if expected_controller.id not in existing_controller_ids: |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 32 | lazy_blocked=True |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 33 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 34 | # check and see if some instance already exists |
| 35 | for networkslice in network.networkslices.all(): |
| 36 | found = False |
| 37 | for instance in networkslice.slice.instances.all(): |
| 38 | if instance.node.site_deployment.controller.id == expected_controller.id: |
| 39 | found = True |
| 40 | if found: |
| 41 | self.logger.info("MODEL_POLICY: network %s setting lazy_blocked to false because instance on controller already exists" % network) |
| 42 | lazy_blocked=False |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 43 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame] | 44 | nd = ControllerNetwork(network=network, controller=expected_controller, lazy_blocked=lazy_blocked) |
| 45 | self.logger.info("MODEL POLICY: network %s create ControllerNetwork %s lazy_blocked %s" % (network, nd, lazy_blocked)) |
| 46 | if network.subnet: |
| 47 | # XXX: Possibly unpredictable behavior if there is |
| 48 | # more than one ControllerNetwork and the subnet |
| 49 | # is specified. |
| 50 | nd.subnet = network.subnet |
| 51 | nd.save() |