blob: 31e5572bad2358675aad05edf61c8cc6f0d6bb9b [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 Mack9976dfd2014-06-03 21:04:35 -040030 driver = self.driver.client_driver(caller=sliver.creator, tenant=sliver.slice.name, deployment=sliver.node.deployment.name)
Tony Macke4be32f2014-03-11 20:45:25 -040031 ports = driver.shell.quantum.list_ports()["ports"]
Tony Mackdacfb982013-09-24 21:57:16 -040032 for port in ports:
33 if port["id"] in networkSlivers_by_port:
34 # we already have it
35 print "already accounted for port", port["id"]
36 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040037
Tony Mackdacfb982013-09-24 21:57:16 -040038 if port["device_owner"] != "compute:nova":
39 # we only want the ports that connect to instances
40 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040041
Tony Mackdacfb982013-09-24 21:57:16 -040042 network = networks_by_id.get(port['network_id'], None)
43 if not network:
44 #print "no network for port", port["id"], "network", port["network_id"]
45 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040046
Tony Mackdacfb982013-09-24 21:57:16 -040047 sliver = slivers_by_instance_id.get(port['device_id'], None)
48 if not sliver:
49 print "no sliver for port", port["id"], "device_id", port['device_id']
50 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040051
Tony Mackdacfb982013-09-24 21:57:16 -040052 if network.template.sharedNetworkId is not None:
53 # If it's a shared network template, then more than one network
54 # object maps to the quantum network. We have to do a whole bunch
55 # of extra work to find the right one.
56 networks = network.template.network_set.all()
57 network = None
58 for candidate_network in networks:
59 if (candidate_network.owner == sliver.slice):
60 print "found network", candidate_network
61 network = candidate_network
Sapan Bhatia24836f12013-08-27 10:16:05 -040062
Tony Mackdacfb982013-09-24 21:57:16 -040063 if not network:
64 print "failed to find the correct network for a shared template for port", port["id"], "network", port["network_id"]
65 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040066
Tony Mackdacfb982013-09-24 21:57:16 -040067 if not port["fixed_ips"]:
68 print "port", port["id"], "has no fixed_ips"
69 continue
Sapan Bhatia24836f12013-08-27 10:16:05 -040070
Tony Mackdacfb982013-09-24 21:57:16 -040071# print "XXX", port
Sapan Bhatia24836f12013-08-27 10:16:05 -040072
Tony Mackdacfb982013-09-24 21:57:16 -040073 ns = NetworkSliver(network=network,
74 sliver=sliver,
75 ip=port["fixed_ips"][0]["ip_address"],
76 port_id=port["id"])
77 ns.save()
Sapan Bhatia5f4aff22014-07-23 09:48:55 -040078
79 def delete_record(self, network_sliver):
80 # Nothing to do, this is an OpenCloud object
81 pass
82