Scott Baker | b87b873 | 2015-05-08 10:55:30 -0700 | [diff] [blame] | 1 | import os |
| 2 | import requests |
| 3 | import socket |
| 4 | import sys |
| 5 | import base64 |
| 6 | from django.db.models import F, Q |
| 7 | from xos.config import Config |
| 8 | from observer.syncstep import SyncStep |
| 9 | from observer.ansible import run_template_ssh |
| 10 | from core.models import Service |
| 11 | from cord.models import VCPEService, VCPETenant, VBNGTenant, VBNGService |
| 12 | from hpc.models import HpcService, CDNPrefix |
| 13 | from util.logger import Logger, logging |
| 14 | |
| 15 | VBNG_API = "http://<vnbg-addr>/onos/virtualbng/privateip/" |
| 16 | |
| 17 | # hpclibrary will be in steps/.. |
| 18 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 19 | sys.path.insert(0,parentdir) |
| 20 | |
| 21 | logger = Logger(level=logging.INFO) |
| 22 | |
| 23 | class SyncVBNGTenant(SyncStep): |
| 24 | provides=[VCPETenant] |
| 25 | observes=VCPETenant |
| 26 | requested_interval=0 |
| 27 | |
| 28 | def __init__(self, **args): |
| 29 | SyncStep.__init__(self, **args) |
| 30 | |
| 31 | def fetch_pending(self, deleted): |
| 32 | if (not deleted): |
| 33 | objs = VBNGTenant.get_tenant_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 34 | else: |
| 35 | objs = VBNGTenant.get_deleted_tenant_objects() |
| 36 | |
| 37 | return objs |
| 38 | |
| 39 | def defer_sync(self, o, reason): |
| 40 | o.backend_register="{}" |
| 41 | o.backend_status = "2 - " + reason |
| 42 | o.save(update_fields=['enacted','backend_status','backend_register']) |
| 43 | logger.info("defer object %s due to %s" % (str(o), reason)) |
| 44 | |
| 45 | def sync_record(self, o): |
| 46 | logger.info("sync'ing VBNGTenant %s" % str(o)) |
| 47 | |
| 48 | vcpes = VCPETenant.get_tenant_objects().all() |
| 49 | vcpes = [x for x in vcpes if (x.vbng is not None) and (x.vbng.id == o.id)] |
| 50 | if not vcpes: |
| 51 | raise Exception("No vCPE tenant is associated with vBNG %s" % str(o.id)) |
| 52 | if len(vcpes)>1: |
| 53 | raise Exception("More than one vCPE tenant is associated with vBNG %s" % str(o.id)) |
| 54 | |
| 55 | vcpe = vcpes[0] |
| 56 | sliver = vcpe.sliver |
| 57 | |
| 58 | if not sliver: |
| 59 | raise Exception("No sliver associated with vBNG %s" % str(o.id)) |
| 60 | |
| 61 | external_ns = None |
| 62 | for ns in sliver.networkslivers.all(): |
| 63 | if (ns.ip) and (ns.network.template.visibility=="private") and (ns.network.template.translation=="none"): |
| 64 | # need some logic here to find the right network |
| 65 | external_ns = ns |
| 66 | |
| 67 | if not external_ns: |
| 68 | self.defer_sync(o, "private network is not filled in yet") |
| 69 | return |
| 70 | |
| 71 | private_ip = external_ns.ip |
| 72 | |
| 73 | if not o.routeable_subnet: |
| 74 | print "This is where we would call Pingping's API" |
| 75 | o.routeable_subnet = "placeholder-from-observer" |
| 76 | |
| 77 | # r = requests.post(VBNG_API + "%s" % private_ip, ) |
| 78 | # public_ip = r.json() |
| 79 | # o.routeable_subnet = public_ip |
| 80 | |
| 81 | o.save() |
| 82 | |
| 83 | def delete_record(self, m): |
| 84 | pass |
| 85 | |