blob: 535e7a4f063ec00c8ca83f61a70a3d8e306ea9dc [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mackae7f30c2013-09-25 12:46:50 -04003from django.db.models import F, Q
Scott Baker76a840e2015-02-11 21:38:09 -08004from xos.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04005from observer.openstacksyncstep import OpenStackSyncStep
Tony Mack336e0f92014-11-30 15:53:08 -05006from core.models import Controller
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04007from core.models.network import *
Scott Baker378b3222014-08-12 18:00:35 -07008from util.logger import Logger, logging
9
10logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040011
12class SyncNetworkSlivers(OpenStackSyncStep):
Scott Baker378b3222014-08-12 18:00:35 -070013 requested_interval = 0 # 3600
Tony Mackdacfb982013-09-24 21:57:16 -040014 provides=[NetworkSliver]
Sapan Bhatiabb45dbe2015-02-08 06:31:10 +000015 observes=NetworkSliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040016
Scott Baker378b3222014-08-12 18:00:35 -070017 # The way it works is to enumerate the all of the ports that quantum
18 # has, and then work backward from each port's network-id to determine
19 # which Network is associated from the port.
Scott Baker378b3222014-08-12 18:00:35 -070020
21 def call(self, **args):
Scott Baker8ba50012014-11-05 09:05:14 -080022 logger.info("sync'ing network slivers")
23
Tony Mackdacfb982013-09-24 21:57:16 -040024 networkSlivers = NetworkSliver.objects.all()
25 networkSlivers_by_id = {}
26 networkSlivers_by_port = {}
27 for networkSliver in networkSlivers:
28 networkSlivers_by_id[networkSliver.id] = networkSliver
29 networkSlivers_by_port[networkSliver.port_id] = networkSliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040030
Tony Mackdacfb982013-09-24 21:57:16 -040031 networks = Network.objects.all()
32 networks_by_id = {}
33 for network in networks:
Tony Mack336e0f92014-11-30 15:53:08 -050034 for nd in network.controllernetworks.all():
Scott Baker378b3222014-08-12 18:00:35 -070035 networks_by_id[nd.net_id] = network
36
37 #logger.info("networks_by_id = ")
38 #for (network_id, network) in networks_by_id.items():
39 # logger.info(" %s: %s" % (network_id, network.name))
Sapan Bhatia24836f12013-08-27 10:16:05 -040040
Tony Mackdacfb982013-09-24 21:57:16 -040041 slivers = Sliver.objects.all()
Scott Baker11a2f502015-01-30 00:02:31 -080042 slivers_by_instance_uuid = {}
Tony Mackdacfb982013-09-24 21:57:16 -040043 for sliver in slivers:
Scott Baker11a2f502015-01-30 00:02:31 -080044 slivers_by_instance_uuid[sliver.instance_uuid] = sliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040045
Tony Mack336e0f92014-11-30 15:53:08 -050046 # Get all ports in all controllers
Scott Baker5bbaa232014-08-14 17:23:15 -070047
48 ports_by_id = {}
Tony Mack336e0f92014-11-30 15:53:08 -050049 for controller in Controller.objects.all():
50 if not controller.admin_tenant:
51 logger.info("controller %s has no admin_tenant" % controller)
Scott Baker5bbaa232014-08-14 17:23:15 -070052 continue
53 try:
Tony Mack336e0f92014-11-30 15:53:08 -050054 driver = self.driver.admin_driver(controller = controller,tenant='admin')
Scott Baker5bbaa232014-08-14 17:23:15 -070055 ports = driver.shell.quantum.list_ports()["ports"]
56 except:
Tony Mack336e0f92014-11-30 15:53:08 -050057 logger.log_exc("failed to get ports from controller %s" % controller)
Scott Baker5bbaa232014-08-14 17:23:15 -070058 continue
59
60 for port in ports:
61 ports_by_id[port["id"]] = port
62
Scott Bakere75d4412014-08-27 11:21:08 -070063 for port in ports_by_id.values():
Scott Baker378b3222014-08-12 18:00:35 -070064 #logger.info("port %s" % str(port))
Tony Mackdacfb982013-09-24 21:57:16 -040065 if port["id"] in networkSlivers_by_port:
66 # we already have it
Scott Baker378b3222014-08-12 18:00:35 -070067 #logger.info("already accounted for port %s" % port["id"])
Tony Mackdacfb982013-09-24 21:57:16 -040068 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040069
Tony Mackdacfb982013-09-24 21:57:16 -040070 if port["device_owner"] != "compute:nova":
71 # we only want the ports that connect to instances
Scott Baker378b3222014-08-12 18:00:35 -070072 #logger.info("port %s is not a compute port, it is a %s" % (port["id"], port["device_owner"]))
Tony Mackdacfb982013-09-24 21:57:16 -040073 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040074
Scott Baker11a2f502015-01-30 00:02:31 -080075 sliver = slivers_by_instance_uuid.get(port['device_id'], None)
Tony Mackdacfb982013-09-24 21:57:16 -040076 if not sliver:
Scott Baker378b3222014-08-12 18:00:35 -070077 logger.info("no sliver for port %s device_id %s" % (port["id"], port['device_id']))
Tony Mackdacfb982013-09-24 21:57:16 -040078 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040079
Scott Baker378b3222014-08-12 18:00:35 -070080 network = networks_by_id.get(port['network_id'], None)
81 if not network:
82 logger.info("no network for port %s network %s" % (port["id"], port["network_id"]))
83
84 # we know it's associated with a sliver, but we don't know
85 # which network it is part of.
86
87 continue
88
Scott Baker81fa17f2015-01-03 12:03:38 -080089 if network.template.shared_network_name:
Tony Mackdacfb982013-09-24 21:57:16 -040090 # If it's a shared network template, then more than one network
91 # object maps to the quantum network. We have to do a whole bunch
92 # of extra work to find the right one.
93 networks = network.template.network_set.all()
94 network = None
95 for candidate_network in networks:
96 if (candidate_network.owner == sliver.slice):
97 print "found network", candidate_network
98 network = candidate_network
Sapan Bhatia24836f12013-08-27 10:16:05 -040099
Tony Mackdacfb982013-09-24 21:57:16 -0400100 if not network:
Scott Baker378b3222014-08-12 18:00:35 -0700101 logger.info("failed to find the correct network for a shared template for port %s network %s" % (port["id"], port["network_id"]))
Tony Mackdacfb982013-09-24 21:57:16 -0400102 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -0400103
Tony Mackdacfb982013-09-24 21:57:16 -0400104 if not port["fixed_ips"]:
Scott Baker378b3222014-08-12 18:00:35 -0700105 logger.info("port %s has no fixed_ips" % port["id"])
Tony Mackdacfb982013-09-24 21:57:16 -0400106 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -0400107
Scott Baker378b3222014-08-12 18:00:35 -0700108 ip=port["fixed_ips"][0]["ip_address"]
109 logger.info("creating NetworkSliver (%s, %s, %s, %s)" % (str(network), str(sliver), ip, str(port["id"])))
Sapan Bhatia24836f12013-08-27 10:16:05 -0400110
Tony Mackdacfb982013-09-24 21:57:16 -0400111 ns = NetworkSliver(network=network,
112 sliver=sliver,
Scott Baker378b3222014-08-12 18:00:35 -0700113 ip=ip,
Tony Mackdacfb982013-09-24 21:57:16 -0400114 port_id=port["id"])
115 ns.save()
Sapan Bhatia5f4aff22014-07-23 09:48:55 -0400116
Scott Baker5bbaa232014-08-14 17:23:15 -0700117 # Now, handle port forwarding
118 # We get the list of NetworkSlivers again, since we might have just
119 # added a few. Then, for each one of them we find it's quantum port and
120 # make sure quantum's nat:forward_ports argument is the same.
121
122 for networkSliver in NetworkSliver.objects.all():
123 try:
124 nat_list = networkSliver.network.nat_list
125 except (TypeError, ValueError), e:
126 logger.info("Failed to decode nat_list: %s" % str(e))
127 continue
128
129 if not networkSliver.port_id:
130 continue
131
132 neutron_port = ports_by_id.get(networkSliver.port_id, None)
133 if not neutron_port:
134 continue
135
136 neutron_nat_list = neutron_port.get("nat:forward_ports", None)
137 if not neutron_nat_list:
138 # make sure that None and the empty set are treated identically
139 neutron_nat_list = []
140
141 if (neutron_nat_list != nat_list):
142 logger.info("Setting nat:forward_ports for port %s network %s sliver %s to %s" % (str(networkSliver.port_id), str(networkSliver.network.id), str(networkSliver.sliver), str(nat_list)))
143 try:
Tony Mack336e0f92014-11-30 15:53:08 -0500144 driver = self.driver.admin_driver(controller=networkSliver.sliver.node.site_deployment.controller,tenant='admin')
Scott Baker5bbaa232014-08-14 17:23:15 -0700145 driver.shell.quantum.update_port(networkSliver.port_id, {"port": {"nat:forward_ports": nat_list}})
146 except:
147 logger.log_exc("failed to update port with nat_list %s" % str(nat_list))
148 continue
149 else:
150 #logger.info("port %s network %s sliver %s nat %s is already set" % (str(networkSliver.port_id), str(networkSliver.network.id), str(networkSliver.sliver), str(nat_list)))
151 pass
152
Sapan Bhatia5f4aff22014-07-23 09:48:55 -0400153 def delete_record(self, network_sliver):
154 # Nothing to do, this is an OpenCloud object
155 pass
156