blob: e9152dcc969f2f4d87e3f54ed127987a93dee448 [file] [log] [blame]
Scott Bakerc3ce3e72016-06-20 17:35:19 -07001import os
2import requests
3import socket
4import sys
5import base64
Scott Baker61b41a92017-03-14 13:04:57 -07006from synchronizers.new_base.syncstep import SyncStep
7from synchronizers.new_base.modelaccessor import *
Scott Bakerc3ce3e72016-06-20 17:35:19 -07008from xos.logger import Logger, logging
Scott Bakerc3ce3e72016-06-20 17:35:19 -07009
10logger = Logger(level=logging.INFO)
11
Scott Baker08a4df32017-03-15 14:13:59 -070012# TODO: This file seems obsolete -- evaluate for deletion.
13
Scott Bakerc3ce3e72016-06-20 17:35:19 -070014# XXX should save and load this
15glo_saved_vtn_maps = []
16
17class SyncPortAddresses(SyncStep):
18 requested_interval = 0 # 3600
19 provides=[Port]
20 observes=Port
21
22 def __init__(self, **args):
23 SyncStep.__init__(self, **args)
24
25 def call(self, **args):
26 global glo_saved_vtn_maps
27
Scott Baker61b41a92017-03-14 13:04:57 -070028 vtn_service = VTNService.objects.all()
Scott Baker855a9df2016-09-09 09:23:50 -070029 if not vtn_service:
30 raise Exception("SyncPortAddresses: No VTN Service")
31 vtn_service = vtn_service[0]
32 if vtn_service.vtnAPIVersion >= 2:
33 logger.info("skipping SyncPortAddresses due to VTN API Version")
34 return
Scott Bakercdbb71e2016-10-26 15:50:03 -070035 else:
36 raise Exception("VTN API Version 1 is no longer supported by VTN Synchronizer")
Scott Baker855a9df2016-09-09 09:23:50 -070037
Scott Bakerc3ce3e72016-06-20 17:35:19 -070038 # build up a dictionary of port-->[wan_addrs] mappings
39 port_addrs = {}
Andy Bavier59012632016-09-14 08:41:56 -040040 try:
41 from services.vsg.models import VSGTenant
Scott Bakerc3ce3e72016-06-20 17:35:19 -070042
Andy Bavier59012632016-09-14 08:41:56 -040043 logger.info("sync'ing vsg tenant to port addresses")
Scott Bakerc3ce3e72016-06-20 17:35:19 -070044
Scott Baker61b41a92017-03-14 13:04:57 -070045 for vsg in VSGTenant.objects.all():
Andy Bavier59012632016-09-14 08:41:56 -040046 if not vsg.instance:
47 logger.info("skipping vsg %s because it has no instance" % vsg)
Scott Bakerc3ce3e72016-06-20 17:35:19 -070048
Andy Bavier59012632016-09-14 08:41:56 -040049 wan_ip = vsg.wan_container_ip
50 if not wan_ip:
51 logger.info("skipping vsg %s because it has no wan_container_ip" % vsg)
Scott Bakerc3ce3e72016-06-20 17:35:19 -070052
Andy Bavier59012632016-09-14 08:41:56 -040053 wan_mac = vsg.wan_container_mac
54 if not wan_mac:
55 logger.info("skipping vsg %s because it has no wan_container_mac" % vsg)
Scott Bakerc3ce3e72016-06-20 17:35:19 -070056
Andy Bavier59012632016-09-14 08:41:56 -040057 lan_network = vsg.get_lan_network(vsg.instance)
58 if not lan_network:
59 logger.info("skipping vsg %s because it has no lan_network" % vsg)
Scott Bakerc3ce3e72016-06-20 17:35:19 -070060
Andy Bavier59012632016-09-14 08:41:56 -040061 lan_port = Port.objects.filter(instance = vsg.instance, network=lan_network)
62 if not lan_port:
63 logger.info("skipping vsg %s because it has no lan_port" % vsg)
64 lan_port = lan_port[0]
Scott Bakerc3ce3e72016-06-20 17:35:19 -070065
Andy Bavier59012632016-09-14 08:41:56 -040066 if not lan_port.port_id:
67 logger.info("skipping vsg %s because its lan_port has no port_id" % vsg)
68
69 if not (lan_port.pk in port_addrs):
70 port_addrs[lan_port.pk] = []
71 entry = {"mac_address": wan_mac, "ip_address": wan_ip}
72 addr_pairs = port_addrs[lan_port.pk]
Scott Bakerc3ce3e72016-06-20 17:35:19 -070073 if not entry in addr_pairs:
74 addr_pairs.append(entry)
75
Andy Bavier59012632016-09-14 08:41:56 -040076 # now do the VM_WAN_IP from the instance
77 if vsg.instance:
78 wan_vm_ip = vsg.wan_vm_ip
79 wan_vm_mac = vsg.wan_vm_mac
80 entry = {"mac_address": wan_vm_mac, "ip_address": wan_vm_ip}
81 if not entry in addr_pairs:
82 addr_pairs.append(entry)
83 except:
84 logger.info("No VSG service present")
85
Scott Bakerc3ce3e72016-06-20 17:35:19 -070086 # Get all ports in all controllers
87 ports_by_id = {}
88 for controller in Controller.objects.all():
89 if not controller.admin_tenant:
90 logger.info("controller %s has no admin_tenant" % controller)
91 continue
92 try:
93 driver = self.driver.admin_driver(controller = controller)
Zack Williamsabe24da2016-06-27 13:09:00 -070094 # FIXME: after LTS merge, use neutron (new name) instead of quantum (old name)
95 # the following if is a workaround
96 if hasattr( driver.shell, "neutron"):
97 ports = driver.shell.neutron.list_ports()["ports"]
98 else:
99 ports = driver.shell.quantum.list_ports()["ports"]
Scott Bakerc3ce3e72016-06-20 17:35:19 -0700100 except:
101 logger.log_exc("failed to get ports from controller %s" % controller)
102 continue
103
104 for port in ports:
105 ports_by_id[port["id"]] = port
106
107 for port_pk in port_addrs.keys():
108 port = Port.objects.get(pk=port_pk)
109 addr_pairs = port_addrs[port_pk]
110 neutron_port = ports_by_id.get(port.port_id,None)
111 if not neutron_port:
112 logger.info("failed to get neutron port for port %s" % port)
113 continue
114
115 ips = [x["ip_address"] for x in addr_pairs]
116
117 changed = False
118
119 # delete addresses in neutron that don't exist in XOS
120 aaps = neutron_port.get("allowed_address_pairs", [])
121 for aap in aaps[:]:
122 if not aap["ip_address"] in ips:
123 logger.info("removing address %s from port %s" % (aap["ip_address"], port))
124 aaps.remove(aap)
125 changed = True
126
127 aaps_ips = [x["ip_address"] for x in aaps]
128
129 # add addresses in XOS that don't exist in neutron
130 for addr in addr_pairs:
131 if not addr["ip_address"] in aaps_ips:
132 logger.info("adding address %s to port %s" % (addr, port))
133 aaps.append( addr )
134 aaps_ips.append(addr["ip_address"])
135 changed = True
136
137 if changed:
138 logger.info("updating port %s" % port)
Zack Williamsabe24da2016-06-27 13:09:00 -0700139 # FIXME: See FIXME above, remove after LTS merge
140 if hasattr( driver.shell, "neutron"):
141 driver.shell.neutron.update_port(port.port_id, {"port": {"allowed_address_pairs": aaps}})
142 else:
143 driver.shell.quantum.update_port(port.port_id, {"port": {"allowed_address_pairs": aaps}})
Scott Bakerc3ce3e72016-06-20 17:35:19 -0700144