Murat Parlakisik | b224cc9 | 2017-02-16 16:27:12 -0800 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import requests |
| 4 | import json |
| 5 | from django.db.models import Q, F |
| 6 | from services.progran.models import * |
| 7 | from synchronizers.base.syncstep import SyncStep |
| 8 | from xos.logger import Logger, logging |
| 9 | |
| 10 | # from core.models import Service |
| 11 | from requests.auth import HTTPBasicAuth |
| 12 | |
| 13 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 14 | sys.path.insert(0, parentdir) |
| 15 | |
| 16 | logger = Logger(level=logging.INFO) |
| 17 | |
| 18 | |
| 19 | class SyncVProfileApp(SyncStep): |
| 20 | provides = [VProgranProfile] |
| 21 | |
| 22 | observes = VProgranProfile |
| 23 | |
| 24 | requested_interval = 0 |
| 25 | |
| 26 | def __init__(self, *args, **kwargs): |
| 27 | super(SyncVProfileApp, self).__init__(*args, **kwargs) |
| 28 | |
| 29 | def get_onos_progran_addr(self): |
| 30 | |
| 31 | return "http://%s:%s/onos/" % ("10.6.0.1", "8183") |
| 32 | |
| 33 | def get_onos_progran_auth(self): |
| 34 | |
| 35 | return HTTPBasicAuth("onos", "rocks") |
| 36 | |
| 37 | def sync_record(self, app): |
| 38 | |
| 39 | logger.info("Sync'ing Edited vProgran Profile ") |
| 40 | |
| 41 | onos_addr = self.get_onos_progran_addr() |
| 42 | |
| 43 | data = {} |
| 44 | data["profile"] = app.profile |
| 45 | data["dlrate"] = app.dlrate |
| 46 | data["ulrate"] = app.ulrate |
| 47 | |
| 48 | |
| 49 | url = onos_addr + "progran/mwc/profile" |
| 50 | |
| 51 | print "POST %s for app %s" % (url, "Progran Imsi") |
| 52 | |
| 53 | auth = self.get_onos_progran_auth() |
| 54 | r = requests.post(url, data=json.dumps(data), auth=auth) |
| 55 | if (r.status_code != 200): |
| 56 | print r |
| 57 | raise Exception("Received error from progran app update (%d)" % r.status_code) |
| 58 | |
| 59 | def delete_record(self, app): |
| 60 | logger.info("Deletion is not supported yet") |
| 61 | |