blob: 92485a5e3b286486444d142dea9502a02803da89 [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 *
Sapan Bhatia24836f12013-08-27 10:16:05 -04007
8class SyncNetworkSlivers(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -04009 requested_interval = 3600
10 provides=[NetworkSliver]
Sapan Bhatia24836f12013-08-27 10:16:05 -040011
Tony Mack31078dd2013-10-02 12:46:21 -040012 def fetch_pending(self):
13 return NetworkSliver.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
14
Tony Mack16f04742013-09-25 08:53:28 -040015 def call(self, failed=[]):
Tony Mackdacfb982013-09-24 21:57:16 -040016 networkSlivers = NetworkSliver.objects.all()
17 networkSlivers_by_id = {}
18 networkSlivers_by_port = {}
19 for networkSliver in networkSlivers:
20 networkSlivers_by_id[networkSliver.id] = networkSliver
21 networkSlivers_by_port[networkSliver.port_id] = networkSliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040022
Tony Mackdacfb982013-09-24 21:57:16 -040023 networks = Network.objects.all()
24 networks_by_id = {}
25 for network in networks:
26 networks_by_id[network.network_id] = network
Sapan Bhatia24836f12013-08-27 10:16:05 -040027
Tony Mackdacfb982013-09-24 21:57:16 -040028 slivers = Sliver.objects.all()
29 slivers_by_instance_id = {}
30 for sliver in slivers:
31 slivers_by_instance_id[sliver.instance_id] = sliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040032
Tony Mack9976dfd2014-06-03 21:04:35 -040033 driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.node.deployment.name)
Tony Macke4be32f2014-03-11 20:45:25 -040034 ports = driver.shell.quantum.list_ports()["ports"]
Tony Mackdacfb982013-09-24 21:57:16 -040035 for port in ports:
36 if port["id"] in networkSlivers_by_port:
37 # we already have it
38 print "already accounted for port", port["id"]
39 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040040
Tony Mackdacfb982013-09-24 21:57:16 -040041 if port["device_owner"] != "compute:nova":
42 # we only want the ports that connect to instances
43 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040044
Tony Mackdacfb982013-09-24 21:57:16 -040045 network = networks_by_id.get(port['network_id'], None)
46 if not network:
47 #print "no network for port", port["id"], "network", port["network_id"]
48 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040049
Tony Mackdacfb982013-09-24 21:57:16 -040050 sliver = slivers_by_instance_id.get(port['device_id'], None)
51 if not sliver:
52 print "no sliver for port", port["id"], "device_id", port['device_id']
53 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040054
Tony Mackdacfb982013-09-24 21:57:16 -040055 if network.template.sharedNetworkId is not None:
56 # If it's a shared network template, then more than one network
57 # object maps to the quantum network. We have to do a whole bunch
58 # of extra work to find the right one.
59 networks = network.template.network_set.all()
60 network = None
61 for candidate_network in networks:
62 if (candidate_network.owner == sliver.slice):
63 print "found network", candidate_network
64 network = candidate_network
Sapan Bhatia24836f12013-08-27 10:16:05 -040065
Tony Mackdacfb982013-09-24 21:57:16 -040066 if not network:
67 print "failed to find the correct network for a shared template for port", port["id"], "network", port["network_id"]
68 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040069
Tony Mackdacfb982013-09-24 21:57:16 -040070 if not port["fixed_ips"]:
71 print "port", port["id"], "has no fixed_ips"
72 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040073
Tony Mackdacfb982013-09-24 21:57:16 -040074# print "XXX", port
Sapan Bhatia24836f12013-08-27 10:16:05 -040075
Tony Mackdacfb982013-09-24 21:57:16 -040076 ns = NetworkSliver(network=network,
77 sliver=sliver,
78 ip=port["fixed_ips"][0]["ip_address"],
79 port_id=port["id"])
80 ns.save()