<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 synchronizers . base . ansible import run_template_ssh <EOL> from core . models import Service <EOL> from services . cord . models import VSGService , VSGTenant , VBNGTenant , VBNGService <EOL> from services . hpc . models import HpcService , CDNPrefix <EOL> from xos . logger import Logger , logging <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> class SyncVBNGTenant ( SyncStep ) : <EOL> provides = [ VSGTenant ] <EOL> observes = VSGTenant <EOL> requested_interval = <NUM_LIT:0> <EOL> def __init__ ( self , ** args ) : <EOL> SyncStep . __init__ ( self , ** args ) <EOL> def fetch_pending ( self , deleted ) : <EOL> if ( not deleted ) : <EOL> objs = VBNGTenant . get_tenant_objects ( ) . filter ( Q ( enacted__lt = F ( '<STR_LIT>' ) ) | Q ( enacted = None ) , Q ( lazy_blocked = False ) ) <EOL> else : <EOL> objs = VBNGTenant . get_deleted_tenant_objects ( ) <EOL> return objs <EOL> def defer_sync ( self , o , reason ) : <EOL> logger . info ( "<STR_LIT>" % ( str ( o ) , reason ) , extra = o . tologdict ( ) ) <EOL> raise Exception ( "<STR_LIT>" % ( str ( o ) , reason ) ) <EOL> def get_vbng_service ( self , o ) : <EOL> if not o . provider_service : <EOL> raise Exception ( "<STR_LIT>" % str ( o . id ) ) <EOL> services = VBNGService . get_service_objects ( ) . filter ( id = o . provider_service . id ) <EOL> if not services : <EOL> raise Exception ( "<STR_LIT>" % str ( o . id ) ) <EOL> return services [ <NUM_LIT:0> ] <EOL> def get_vbng_url ( self , o ) : <EOL> service = self . get_vbng_service ( o ) <EOL> if service . vbng_url : <EOL> return service . vbng_url <EOL> for tenant in service . subscribed_tenants . all ( ) : <EOL> if tenant . provider_service and tenant . provider_service . kind == "<STR_LIT>" : <EOL> onos_service = tenant . provider_service <EOL> if not onos_service . slices . exists ( ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> onos_slice = onos_service . slices . all ( ) [ <NUM_LIT:0> ] <EOL> if not onos_slice . instances . exists ( ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> instance = onos_slice . instances . all ( ) [ <NUM_LIT:0> ] <EOL> if not instance . instance_name : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> ip = instance . get_network_ip ( "<STR_LIT>" ) <EOL> if not ip : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> logger . info ( "<STR_LIT>" % ( ip , instance ) , extra = o . tologdict ( ) ) <EOL> return "<STR_LIT>" % ip <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def get_private_interface ( self , o ) : <EOL> vcpes = VSGTenant . get_tenant_objects ( ) . all ( ) <EOL> vcpes = [ x for x in vcpes if ( x . vbng is not None ) and ( x . vbng . id == o . id ) ] <EOL> if not vcpes : <EOL> raise Exception ( "<STR_LIT>" % str ( o . id ) ) <EOL> if len ( vcpes ) > <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" % str ( o . id ) ) <EOL> vcpe = vcpes [ <NUM_LIT:0> ] <EOL> instance = vcpe . instance <EOL> if not instance : <EOL> raise Exception ( "<STR_LIT>" % str ( o . id ) ) <EOL> if not vcpe . wan_ip : <EOL> self . defer_sync ( o , "<STR_LIT>" ) <EOL> if not vcpe . wan_container_mac : <EOL> self . defer_sync ( o , "<STR_LIT>" ) <EOL> return ( vcpe . wan_ip , vcpe . wan_container_mac , vcpe . instance . node . name ) <EOL> def sync_record ( self , o ) : <EOL> logger . info ( "<STR_LIT>" % str ( o ) , extra = o . tologdict ( ) ) <EOL> if not o . routeable_subnet : <EOL> ( private_ip , private_mac , private_hostname ) = self . get_private_interface ( o ) <EOL> logger . info ( "<STR_LIT>" % ( private_ip , private_mac , private_hostname ) , extra = o . tologdict ( ) ) <EOL> url = self . get_vbng_url ( o ) + "<STR_LIT>" % ( private_ip , private_mac , private_hostname ) <EOL> logger . info ( "<STR_LIT>" % url , extra = o . tologdict ( ) ) <EOL> r = requests . post ( url ) <EOL> if ( r . status_code != <NUM_LIT:200> ) : <EOL> raise Exception ( "<STR_LIT>" % r . status_code ) <EOL> logger . info ( "<STR_LIT>" % ( r . text , private_ip ) , extra = o . tologdict ( ) ) <EOL> if r . text == "<STR_LIT:0>" : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> o . routeable_subnet = r . text <EOL> o . mapped_ip = private_ip <EOL> o . mapped_mac = private_mac <EOL> o . mapped_hostname = private_hostname <EOL> o . save ( ) <EOL> def delete_record ( self , o ) : <EOL> logger . info ( "<STR_LIT>" % str ( o ) , extra = o . tologdict ( ) ) <EOL> if o . mapped_ip : <EOL> private_ip = o . mapped_ip <EOL> logger . info ( "<STR_LIT>" % private_ip , extra = o . tologdict ( ) ) <EOL> r = requests . delete ( self . get_vbng_url ( o ) + "<STR_LIT>" % private_ip , ) <EOL> if ( r . status_code != <NUM_LIT:200> ) : <EOL> raise Exception ( "<STR_LIT>" % r . status_code ) </s>
