blob: bdf53a025432e0228aadea4d34ab9cc86df5ef76 [file] [log] [blame]
Tony Mack51c4a7d2014-11-30 15:33:35 -05001import os
2import base64
3from collections import defaultdict
4from netaddr import IPAddress, IPNetwork
5from django.db.models import F, Q
Scott Baker86e132c2015-02-11 21:38:09 -08006from xos.config import Config
Tony Mack51c4a7d2014-11-30 15:33:35 -05007from observer.openstacksyncstep import OpenStackSyncStep
8from core.models.network import *
9from core.models.slice import *
10from core.models.sliver import Sliver
11from util.logger import Logger, logging
Sapan Bhatiadedc41e2014-12-22 01:42:18 -050012from observer.ansible import *
S.Çağlar Onurfdeacc52015-02-18 09:59:55 -050013from openstack.driver import OpenStackDriver
Tony Mack51c4a7d2014-11-30 15:33:35 -050014
Sapan Bhatia39a775f2015-01-29 20:58:25 +000015import pdb
16
Tony Mack51c4a7d2014-11-30 15:33:35 -050017logger = Logger(level=logging.INFO)
18
Tony Mack80c42542015-01-07 12:48:37 -050019class SyncControllerNetworks(OpenStackSyncStep):
Tony Mack51c4a7d2014-11-30 15:33:35 -050020 requested_interval = 0
Sapan Bhatia5d662c72015-01-27 03:52:19 +000021 provides=[Network]
Sapan Bhatia39a775f2015-01-29 20:58:25 +000022 observes=ControllerNetwork
Tony Mack51c4a7d2014-11-30 15:33:35 -050023
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050024 def alloc_subnet(self, uuid):
Sapan Bhatiaf1705c12015-01-23 16:22:12 +000025 # 16 bits only
26 uuid_masked = uuid & 0xffff
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050027 a = 10
Sapan Bhatiaf1705c12015-01-23 16:22:12 +000028 b = uuid_masked >> 8
29 c = uuid_masked & 0xff
30 d = 0
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050031
Sapan Bhatiaf1705c12015-01-23 16:22:12 +000032 cidr = '%d.%d.%d.%d/24'%(a,b,c,d)
33 return cidr
34
Sapan Bhatiabc13cb72014-12-19 13:21:30 -050035
Tony Mack51c4a7d2014-11-30 15:33:35 -050036 def fetch_pending(self, deleted):
37 if (deleted):
Tony Mack3066a952015-01-05 22:48:11 -050038 return ControllerNetwork.deleted_objects.all()
Tony Mack51c4a7d2014-11-30 15:33:35 -050039 else:
Tony Mack3066a952015-01-05 22:48:11 -050040 return ControllerNetwork.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
Tony Mack51c4a7d2014-11-30 15:33:35 -050041
Tony Mack51c4a7d2014-11-30 15:33:35 -050042
Tony Mack06c8e472014-11-30 15:53:08 -050043 def save_controller_network(self, controller_network):
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000044 network_name = controller_network.network.name
45 subnet_name = '%s-%d'%(network_name,controller_network.pk)
46 cidr = self.alloc_subnet(controller_network.pk)
47 slice = controller_network.network.slices.all()[0] # XXX: FIXME!!
Sapan Bhatiadedc41e2014-12-22 01:42:18 -050048
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000049 network_fields = {'endpoint':controller_network.controller.auth_url,
50 'admin_user':slice.creator.email, # XXX: FIXME
51 'tenant_name':slice.name, # XXX: FIXME
52 'admin_password':slice.creator.remote_password,
53 'name':network_name,
54 'subnet_name':subnet_name,
55 'ansible_tag':'%s-%s@%s'%(network_name,slice.slicename,controller_network.controller.name),
56 'cidr':cidr
57 }
Tony Mack51c4a7d2014-11-30 15:33:35 -050058
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000059 res = run_template('sync_controller_networks.yaml', network_fields, path = 'controller_networks',expected_num=2)
Tony Mack51c4a7d2014-11-30 15:33:35 -050060
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000061 network_id = res[0]['id']
62 subnet_id = res[1]['id']
63 controller_network.net_id = network_id
64 controller_network.subnet = cidr
65 controller_network.subnet_id = subnet_id
Sapan Bhatiac88c9a82015-01-27 03:52:43 +000066 controller_network.backend_status = '1 - OK'
Sapan Bhatia3b3e1e12015-01-23 16:21:57 +000067 controller_network.save()
Tony Mack51c4a7d2014-11-30 15:33:35 -050068
Tony Mack51c4a7d2014-11-30 15:33:35 -050069
Tony Mack06c8e472014-11-30 15:53:08 -050070 def sync_record(self, controller_network):
71 logger.info("sync'ing network controller %s for network %s slice %s controller %s" % (controller_network, controller_network.network, str(controller_network.network.owner), controller_network.controller))
Tony Mack51c4a7d2014-11-30 15:33:35 -050072
Tony Mack06c8e472014-11-30 15:53:08 -050073 if not controller_network.controller.admin_user:
74 logger.info("controller %r has no admin_user, skipping" % controller_network.controller)
Tony Mack51c4a7d2014-11-30 15:33:35 -050075 return
76
Tony Mack06c8e472014-11-30 15:53:08 -050077 if controller_network.network.owner and controller_network.network.owner.creator:
Sapan Bhatia7b4cee72015-01-23 16:22:27 +000078 self.save_controller_network(controller_network)
79 logger.info("saved network controller: %s" % (controller_network))
Tony Mack51c4a7d2014-11-30 15:33:35 -050080
Tony Mack06c8e472014-11-30 15:53:08 -050081 def delete_record(self, controller_network):
82 driver = OpenStackDriver().client_driver(caller=controller_network.network.owner.creator,
83 tenant=controller_network.network.owner.name,
84 controller=controller_network.controller.name)
85 if (controller_network.router_id) and (controller_network.subnet_id):
86 driver.delete_router_interface(controller_network.router_id, controller_network.subnet_id)
87 if controller_network.subnet_id:
88 driver.delete_subnet(controller_network.subnet_id)
89 if controller_network.router_id:
90 driver.delete_router(controller_network.router_id)
91 if controller_network.net_id:
92 driver.delete_network(controller_network.net_id)