blob: 16bc5a26637bfd4ce1a52823f52dd7144382e5fa [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
Sapan Bhatia24836f12013-08-27 10:16:05 -04004from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04005from observer.openstacksyncstep import OpenStackSyncStep
Tony Mack06c8e472014-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 Bhatia24836f12013-08-27 10:16:05 -040015
Scott Baker378b3222014-08-12 18:00:35 -070016 # The way it works is to enumerate the all of the ports that quantum
17 # has, and then work backward from each port's network-id to determine
18 # which Network is associated from the port.
Scott Baker378b3222014-08-12 18:00:35 -070019
20 def call(self, **args):
Scott Baker78c6b982014-11-05 09:05:14 -080021 logger.info("sync'ing network slivers")
22
Tony Mackdacfb982013-09-24 21:57:16 -040023 networkSlivers = NetworkSliver.objects.all()
24 networkSlivers_by_id = {}
25 networkSlivers_by_port = {}
26 for networkSliver in networkSlivers:
27 networkSlivers_by_id[networkSliver.id] = networkSliver
28 networkSlivers_by_port[networkSliver.port_id] = networkSliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040029
Tony Mackdacfb982013-09-24 21:57:16 -040030 networks = Network.objects.all()
31 networks_by_id = {}
32 for network in networks:
Tony Mack06c8e472014-11-30 15:53:08 -050033 for nd in network.controllernetworks.all():
Scott Baker378b3222014-08-12 18:00:35 -070034 networks_by_id[nd.net_id] = network
35
36 #logger.info("networks_by_id = ")
37 #for (network_id, network) in networks_by_id.items():
38 # logger.info(" %s: %s" % (network_id, network.name))
Sapan Bhatia24836f12013-08-27 10:16:05 -040039
Tony Mackdacfb982013-09-24 21:57:16 -040040 slivers = Sliver.objects.all()
41 slivers_by_instance_id = {}
42 for sliver in slivers:
43 slivers_by_instance_id[sliver.instance_id] = sliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040044
Tony Mack06c8e472014-11-30 15:53:08 -050045 # Get all ports in all controllers
Scott Baker5bbaa232014-08-14 17:23:15 -070046
47 ports_by_id = {}
Tony Mack06c8e472014-11-30 15:53:08 -050048 for controller in Controller.objects.all():
49 if not controller.admin_tenant:
50 logger.info("controller %s has no admin_tenant" % controller)
Scott Baker5bbaa232014-08-14 17:23:15 -070051 continue
52 try:
Tony Mack06c8e472014-11-30 15:53:08 -050053 driver = self.driver.admin_driver(controller = controller,tenant='admin')
Scott Baker5bbaa232014-08-14 17:23:15 -070054 ports = driver.shell.quantum.list_ports()["ports"]
55 except:
Tony Mack06c8e472014-11-30 15:53:08 -050056 logger.log_exc("failed to get ports from controller %s" % controller)
Scott Baker5bbaa232014-08-14 17:23:15 -070057 continue
58
59 for port in ports:
60 ports_by_id[port["id"]] = port
61
Scott Bakere75d4412014-08-27 11:21:08 -070062 for port in ports_by_id.values():
Scott Baker378b3222014-08-12 18:00:35 -070063 #logger.info("port %s" % str(port))
Tony Mackdacfb982013-09-24 21:57:16 -040064 if port["id"] in networkSlivers_by_port:
65 # we already have it
Scott Baker378b3222014-08-12 18:00:35 -070066 #logger.info("already accounted for port %s" % port["id"])
Tony Mackdacfb982013-09-24 21:57:16 -040067 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040068
Tony Mackdacfb982013-09-24 21:57:16 -040069 if port["device_owner"] != "compute:nova":
70 # we only want the ports that connect to instances
Scott Baker378b3222014-08-12 18:00:35 -070071 #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 -040072 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040073
Tony Mackdacfb982013-09-24 21:57:16 -040074 sliver = slivers_by_instance_id.get(port['device_id'], None)
75 if not sliver:
Scott Baker378b3222014-08-12 18:00:35 -070076 logger.info("no sliver for port %s device_id %s" % (port["id"], port['device_id']))
Tony Mackdacfb982013-09-24 21:57:16 -040077 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040078
Scott Baker378b3222014-08-12 18:00:35 -070079 network = networks_by_id.get(port['network_id'], None)
80 if not network:
81 logger.info("no network for port %s network %s" % (port["id"], port["network_id"]))
82
83 # we know it's associated with a sliver, but we don't know
84 # which network it is part of.
85
86 continue
87
Scott Baker369f9b92015-01-03 12:03:38 -080088 if network.template.shared_network_name:
Tony Mackdacfb982013-09-24 21:57:16 -040089 # If it's a shared network template, then more than one network
90 # object maps to the quantum network. We have to do a whole bunch
91 # of extra work to find the right one.
92 networks = network.template.network_set.all()
93 network = None
94 for candidate_network in networks:
95 if (candidate_network.owner == sliver.slice):
96 print "found network", candidate_network
97 network = candidate_network
Sapan Bhatia24836f12013-08-27 10:16:05 -040098
Tony Mackdacfb982013-09-24 21:57:16 -040099 if not network:
Scott Baker378b3222014-08-12 18:00:35 -0700100 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 -0400101 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -0400102
Tony Mackdacfb982013-09-24 21:57:16 -0400103 if not port["fixed_ips"]:
Scott Baker378b3222014-08-12 18:00:35 -0700104 logger.info("port %s has no fixed_ips" % port["id"])
Tony Mackdacfb982013-09-24 21:57:16 -0400105 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -0400106
Scott Baker378b3222014-08-12 18:00:35 -0700107 ip=port["fixed_ips"][0]["ip_address"]
108 logger.info("creating NetworkSliver (%s, %s, %s, %s)" % (str(network), str(sliver), ip, str(port["id"])))
Sapan Bhatia24836f12013-08-27 10:16:05 -0400109
Tony Mackdacfb982013-09-24 21:57:16 -0400110 ns = NetworkSliver(network=network,
111 sliver=sliver,
Scott Baker378b3222014-08-12 18:00:35 -0700112 ip=ip,
Tony Mackdacfb982013-09-24 21:57:16 -0400113 port_id=port["id"])
114 ns.save()
Sapan Bhatia5f4aff22014-07-23 09:48:55 -0400115
Scott Baker5bbaa232014-08-14 17:23:15 -0700116 # Now, handle port forwarding
117 # We get the list of NetworkSlivers again, since we might have just
118 # added a few. Then, for each one of them we find it's quantum port and
119 # make sure quantum's nat:forward_ports argument is the same.
120
121 for networkSliver in NetworkSliver.objects.all():
122 try:
123 nat_list = networkSliver.network.nat_list
124 except (TypeError, ValueError), e:
125 logger.info("Failed to decode nat_list: %s" % str(e))
126 continue
127
128 if not networkSliver.port_id:
129 continue
130
131 neutron_port = ports_by_id.get(networkSliver.port_id, None)
132 if not neutron_port:
133 continue
134
135 neutron_nat_list = neutron_port.get("nat:forward_ports", None)
136 if not neutron_nat_list:
137 # make sure that None and the empty set are treated identically
138 neutron_nat_list = []
139
140 if (neutron_nat_list != nat_list):
141 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)))
142 try:
Tony Mack06c8e472014-11-30 15:53:08 -0500143 driver = self.driver.admin_driver(controller=networkSliver.sliver.node.site_deployment.controller,tenant='admin')
Scott Baker5bbaa232014-08-14 17:23:15 -0700144 driver.shell.quantum.update_port(networkSliver.port_id, {"port": {"nat:forward_ports": nat_list}})
145 except:
146 logger.log_exc("failed to update port with nat_list %s" % str(nat_list))
147 continue
148 else:
149 #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)))
150 pass
151
Sapan Bhatia5f4aff22014-07-23 09:48:55 -0400152 def delete_record(self, network_sliver):
153 # Nothing to do, this is an OpenCloud object
154 pass
155