<s> import os <EOL> import requests <EOL> import socket <EOL> import sys <EOL> import base64 <EOL> from django . db . models import F , Q <EOL> from xos . config import Config <EOL> from synchronizers . base . syncstep import SyncStep <EOL> from core . models import Service , Port , Controller , Tag <EOL> from core . models . service import COARSE_KIND <EOL> from services . cord . models import VSGTenant <EOL> from services . cord . models import Tenant <EOL> from xos . logger import Logger , logging <EOL> from requests . auth import HTTPBasicAuth <EOL> parentdir = os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT:..>" ) <EOL> sys . path . insert ( <NUM_LIT:0> , parentdir ) <EOL> logger = Logger ( level = logging . INFO ) <EOL> glo_saved_vtn_maps = [ ] <EOL> class SyncPortAddresses ( SyncStep ) : <EOL> requested_interval = <NUM_LIT:0> <EOL> provides = [ Port ] <EOL> observes = Port <EOL> def __init__ ( self , ** args ) : <EOL> SyncStep . __init__ ( self , ** args ) <EOL> def call ( self , ** args ) : <EOL> global glo_saved_vtn_maps <EOL> logger . info ( "<STR_LIT>" ) <EOL> port_addrs = { } <EOL> for vsg in VSGTenant . get_tenant_objects ( ) . all ( ) : <EOL> if not vsg . instance : <EOL> logger . info ( "<STR_LIT>" % vsg ) <EOL> wan_ip = vsg . wan_container_ip <EOL> if not wan_ip : <EOL> logger . info ( "<STR_LIT>" % vsg ) <EOL> wan_mac = vsg . wan_container_mac <EOL> if not wan_mac : <EOL> logger . info ( "<STR_LIT>" % vsg ) <EOL> lan_network = vsg . get_lan_network ( vsg . instance ) <EOL> if not lan_network : <EOL> logger . info ( "<STR_LIT>" % vsg ) <EOL> lan_port = Port . objects . filter ( instance = vsg . instance , network = lan_network ) <EOL> if not lan_port : <EOL> logger . info ( "<STR_LIT>" % vsg ) <EOL> lan_port = lan_port [ <NUM_LIT:0> ] <EOL> if not lan_port . port_id : <EOL> logger . info ( "<STR_LIT>" % vsg ) <EOL> if not ( lan_port . pk in port_addrs ) : <EOL> port_addrs [ lan_port . pk ] = [ ] <EOL> entry = { "<STR_LIT>" : wan_mac , "<STR_LIT>" : wan_ip } <EOL> addr_pairs = port_addrs [ lan_port . pk ] <EOL> if not entry in addr_pairs : <EOL> addr_pairs . append ( entry ) <EOL> if vsg . instance : <EOL> wan_vm_ip = vsg . wan_vm_ip <EOL> wan_vm_mac = vsg . wan_vm_mac <EOL> entry = { "<STR_LIT>" : wan_vm_mac , "<STR_LIT>" : wan_vm_ip } <EOL> if not entry in addr_pairs : <EOL> addr_pairs . append ( entry ) <EOL> ports_by_id = { } <EOL> for controller in Controller . objects . all ( ) : <EOL> if not controller . admin_tenant : <EOL> logger . info ( "<STR_LIT>" % controller ) <EOL> continue <EOL> try : <EOL> driver = self . driver . admin_driver ( controller = controller ) <EOL> ports = driver . shell . quantum . list_ports ( ) [ "<STR_LIT>" ] <EOL> except : <EOL> logger . log_exc ( "<STR_LIT>" % controller ) <EOL> continue <EOL> for port in ports : <EOL> ports_by_id [ port [ "<STR_LIT:id>" ] ] = port <EOL> for port_pk in port_addrs . keys ( ) : <EOL> port = Port . objects . get ( pk = port_pk ) <EOL> addr_pairs = port_addrs [ port_pk ] <EOL> neutron_port = ports_by_id . get ( port . port_id , None ) <EOL> if not neutron_port : <EOL> logger . info ( "<STR_LIT>" % port ) <EOL> continue <EOL> ips = [ x [ "<STR_LIT>" ] for x in addr_pairs ] <EOL> changed = False <EOL> aaps = neutron_port . get ( "<STR_LIT>" , [ ] ) <EOL> for aap in aaps [ : ] : <EOL> if not aap [ "<STR_LIT>" ] in ips : <EOL> logger . info ( "<STR_LIT>" % ( aap [ "<STR_LIT>" ] , port ) ) <EOL> aaps . remove ( aap ) <EOL> changed = True <EOL> aaps_ips = [ x [ "<STR_LIT>" ] for x in aaps ] <EOL> for addr in addr_pairs : <EOL> if not addr [ "<STR_LIT>" ] in aaps_ips : <EOL> logger . info ( "<STR_LIT>" % ( addr , port ) ) <EOL> aaps . append ( addr ) <EOL> aaps_ips . append ( addr [ "<STR_LIT>" ] ) <EOL> changed = True <EOL> if changed : <EOL> logger . info ( "<STR_LIT>" % port ) <EOL> driver . shell . quantum . update_port ( port . port_id , { "<STR_LIT:port>" : { "<STR_LIT>" : aaps } } ) </s>
