blob: dfb9d8dfdb45b99c159cbc5989f6aa25e4f9f03e [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 Mack16f04742013-09-25 08:53:28 -040012 def call(self, failed=[]):
Tony Mackdacfb982013-09-24 21:57:16 -040013 networkSlivers = NetworkSliver.objects.all()
14 networkSlivers_by_id = {}
15 networkSlivers_by_port = {}
16 for networkSliver in networkSlivers:
17 networkSlivers_by_id[networkSliver.id] = networkSliver
18 networkSlivers_by_port[networkSliver.port_id] = networkSliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040019
Tony Mackdacfb982013-09-24 21:57:16 -040020 networks = Network.objects.all()
21 networks_by_id = {}
22 for network in networks:
23 networks_by_id[network.network_id] = network
Sapan Bhatia24836f12013-08-27 10:16:05 -040024
Tony Mackdacfb982013-09-24 21:57:16 -040025 slivers = Sliver.objects.all()
26 slivers_by_instance_id = {}
27 for sliver in slivers:
28 slivers_by_instance_id[sliver.instance_id] = sliver
Sapan Bhatia24836f12013-08-27 10:16:05 -040029
Tony Mackdacfb982013-09-24 21:57:16 -040030 ports = self.driver.shell.quantum.list_ports()["ports"]
31 for port in ports:
32 if port["id"] in networkSlivers_by_port:
33 # we already have it
34 print "already accounted for port", port["id"]
35 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040036
Tony Mackdacfb982013-09-24 21:57:16 -040037 if port["device_owner"] != "compute:nova":
38 # we only want the ports that connect to instances
39 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040040
Tony Mackdacfb982013-09-24 21:57:16 -040041 network = networks_by_id.get(port['network_id'], None)
42 if not network:
43 #print "no network for port", port["id"], "network", port["network_id"]
44 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040045
Tony Mackdacfb982013-09-24 21:57:16 -040046 sliver = slivers_by_instance_id.get(port['device_id'], None)
47 if not sliver:
48 print "no sliver for port", port["id"], "device_id", port['device_id']
49 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040050
Tony Mackdacfb982013-09-24 21:57:16 -040051 if network.template.sharedNetworkId is not None:
52 # If it's a shared network template, then more than one network
53 # object maps to the quantum network. We have to do a whole bunch
54 # of extra work to find the right one.
55 networks = network.template.network_set.all()
56 network = None
57 for candidate_network in networks:
58 if (candidate_network.owner == sliver.slice):
59 print "found network", candidate_network
60 network = candidate_network
Sapan Bhatia24836f12013-08-27 10:16:05 -040061
Tony Mackdacfb982013-09-24 21:57:16 -040062 if not network:
63 print "failed to find the correct network for a shared template for port", port["id"], "network", port["network_id"]
64 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040065
Tony Mackdacfb982013-09-24 21:57:16 -040066 if not port["fixed_ips"]:
67 print "port", port["id"], "has no fixed_ips"
68 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040069
Tony Mackdacfb982013-09-24 21:57:16 -040070# print "XXX", port
Sapan Bhatia24836f12013-08-27 10:16:05 -040071
Tony Mackdacfb982013-09-24 21:57:16 -040072 ns = NetworkSliver(network=network,
73 sliver=sliver,
74 ip=port["fixed_ips"][0]["ip_address"],
75 port_id=port["id"])
76 ns.save()