Merge branch 'master' of ssh://git.planet-lab.org/git/plstackapi
[plstackapi.git] / planetstack / model_policies / model_policy_Slice.py
1
2 def handle(slice):
3         from core.models import SiteDeployments,SliceDeployments,Deployment,Network,NetworkSlice,NetworkTemplate
4         from collections import defaultdict
5         site_deployments = SiteDeployments.objects.all()
6         site_deploy_lookup = defaultdict(list)
7         for site_deployment in site_deployments:
8                 site_deploy_lookup[site_deployment.site].append(site_deployment.deployment)
9         
10         slice_deployments = SliceDeployments.objects.all()
11         slice_deploy_lookup = defaultdict(list)
12         for slice_deployment in slice_deployments:
13                 slice_deploy_lookup[slice_deployment.slice].append(slice_deployment.deployment)
14         
15         all_deployments = Deployment.objects.all() 
16         # slices are added to all deployments for now
17         expected_deployments = all_deployments
18         #expected_deployments = site_deploy_lookup[slice.site]
19         for expected_deployment in expected_deployments:
20                 if slice not in slice_deploy_lookup or \
21                    expected_deployment not in slice_deploy_lookup[slice]:
22                         sd = SliceDeployments(slice=slice, deployment=expected_deployment)
23                         sd.save()
24
25         # make sure slice has at least 1 public and 1 private networkd
26         public_net = None
27         private_net = None
28         networks = Network.objects.filter(owner=slice)
29         for network in networks:
30                 if network.template.name == 'Public dedicated IPv4':
31                         public_net = network
32                 elif network.template.name == 'Private':
33                         private_net = network 
34         if not public_net:
35                 public_net = Network(
36                 name = slice.name+'-public',
37                 template = NetworkTemplate.objects.get(name='Public dedicated IPv4'),
38                 owner = slice
39                 )
40                 public_net.save()
41         
42         if not private_net:
43                 private_net = Network(
44                 name = slice.name+'-private',
45                 template = NetworkTemplate.objects.get(name='Private'),
46                 owner = slice
47                 )
48                 private_net.save()
49         # create slice networks
50         public_net_slice = None
51         private_net_slice = None
52         net_slices = NetworkSlice.objects.filter(slice=slice, network__in=[public_net,private_net])
53         for net_slice in net_slices:
54                 if net_slice.network == public_net:
55                         public_net_slice = net_slice 
56                 elif net_slice.network == private_net:
57                         private_net_slice = net_slice 
58         if not public_net_slice:
59                 public_net_slice = NetworkSlice(slice=slice, network=public_net)
60                 public_net_slice.save()
61         if not private_net_slice:
62                 private_net_slice = NetworkSlice(slice=slice, network=private_net)
63                 private_net_slice.save()                
64                       
65              
66         
67