blob: c6e4b7cb3583b071e24699e33d052cd7993dbbf9 [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
6from core.models.network import *
Scott Baker378b3222014-08-12 18:00:35 -07007from util.logger import Logger, logging
8
9logger = Logger(level=logging.INFO)
Sapan Bhatia24836f12013-08-27 10:16:05 -040010
11class SyncNetworkSlivers(OpenStackSyncStep):
Scott Baker378b3222014-08-12 18:00:35 -070012 requested_interval = 0 # 3600
Tony Mackdacfb982013-09-24 21:57:16 -040013 provides=[NetworkSliver]
Sapan Bhatia24836f12013-08-27 10:16:05 -040014
Scott Baker378b3222014-08-12 18:00:35 -070015 # The way it works is to enumerate the all of the ports that quantum
16 # has, and then work backward from each port's network-id to determine
17 # which Network is associated from the port.
Scott Baker378b3222014-08-12 18:00:35 -070018
19 def call(self, **args):
Tony Mackdacfb982013-09-24 21:57:16 -040020 networkSlivers = NetworkSliver.objects.all()
21 networkSlivers_by_id = {}
22 networkSlivers_by_port = {}
23 for networkSliver in networkSlivers:
24 networkSlivers_by_id[networkSliver.id] = networkSliver
25 networkSlivers_by_port[networkSliver.port_id] = networkSliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040026
Tony Mackdacfb982013-09-24 21:57:16 -040027 networks = Network.objects.all()
28 networks_by_id = {}
29 for network in networks:
Scott Baker378b3222014-08-12 18:00:35 -070030 for nd in network.networkdeployments_set.all():
31 networks_by_id[nd.net_id] = network
32
33 #logger.info("networks_by_id = ")
34 #for (network_id, network) in networks_by_id.items():
35 # logger.info(" %s: %s" % (network_id, network.name))
Sapan Bhatia24836f12013-08-27 10:16:05 -040036
Tony Mackdacfb982013-09-24 21:57:16 -040037 slivers = Sliver.objects.all()
38 slivers_by_instance_id = {}
39 for sliver in slivers:
40 slivers_by_instance_id[sliver.instance_id] = sliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040041
Tony Mack9976dfd2014-06-03 21:04:35 -040042 driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.node.deployment.name)
Tony Macke4be32f2014-03-11 20:45:25 -040043 ports = driver.shell.quantum.list_ports()["ports"]
Tony Mackdacfb982013-09-24 21:57:16 -040044 for port in ports:
Scott Baker378b3222014-08-12 18:00:35 -070045 #logger.info("port %s" % str(port))
Tony Mackdacfb982013-09-24 21:57:16 -040046 if port["id"] in networkSlivers_by_port:
47 # we already have it
Scott Baker378b3222014-08-12 18:00:35 -070048 #logger.info("already accounted for port %s" % port["id"])
Tony Mackdacfb982013-09-24 21:57:16 -040049 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040050
Tony Mackdacfb982013-09-24 21:57:16 -040051 if port["device_owner"] != "compute:nova":
52 # we only want the ports that connect to instances
Scott Baker378b3222014-08-12 18:00:35 -070053 #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 -040054 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040055
Tony Mackdacfb982013-09-24 21:57:16 -040056 sliver = slivers_by_instance_id.get(port['device_id'], None)
57 if not sliver:
Scott Baker378b3222014-08-12 18:00:35 -070058 logger.info("no sliver for port %s device_id %s" % (port["id"], port['device_id']))
Tony Mackdacfb982013-09-24 21:57:16 -040059 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040060
Scott Baker378b3222014-08-12 18:00:35 -070061 network = networks_by_id.get(port['network_id'], None)
62 if not network:
63 logger.info("no network for port %s network %s" % (port["id"], port["network_id"]))
64
65 # we know it's associated with a sliver, but we don't know
66 # which network it is part of.
67
68 continue
69
70 if network.template.sharedNetworkName:
Tony Mackdacfb982013-09-24 21:57:16 -040071 # If it's a shared network template, then more than one network
72 # object maps to the quantum network. We have to do a whole bunch
73 # of extra work to find the right one.
74 networks = network.template.network_set.all()
75 network = None
76 for candidate_network in networks:
77 if (candidate_network.owner == sliver.slice):
78 print "found network", candidate_network
79 network = candidate_network
Sapan Bhatia24836f12013-08-27 10:16:05 -040080
Tony Mackdacfb982013-09-24 21:57:16 -040081 if not network:
Scott Baker378b3222014-08-12 18:00:35 -070082 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 -040083 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040084
Tony Mackdacfb982013-09-24 21:57:16 -040085 if not port["fixed_ips"]:
Scott Baker378b3222014-08-12 18:00:35 -070086 logger.info("port %s has no fixed_ips" % port["id"])
Tony Mackdacfb982013-09-24 21:57:16 -040087 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040088
Scott Baker378b3222014-08-12 18:00:35 -070089 ip=port["fixed_ips"][0]["ip_address"]
90 logger.info("creating NetworkSliver (%s, %s, %s, %s)" % (str(network), str(sliver), ip, str(port["id"])))
Sapan Bhatia24836f12013-08-27 10:16:05 -040091
Tony Mackdacfb982013-09-24 21:57:16 -040092 ns = NetworkSliver(network=network,
93 sliver=sliver,
Scott Baker378b3222014-08-12 18:00:35 -070094 ip=ip,
Tony Mackdacfb982013-09-24 21:57:16 -040095 port_id=port["id"])
96 ns.save()
Sapan Bhatia5f4aff22014-07-23 09:48:55 -040097
98 def delete_record(self, network_sliver):
99 # Nothing to do, this is an OpenCloud object
100 pass
101