Andrea Campanella | 65e91e6 | 2017-05-22 17:50:37 +0200 | [diff] [blame^] | 1 | import os |
| 2 | import sys |
| 3 | import requests |
| 4 | import json |
| 5 | from synchronizers.new_base.syncstep import SyncStep |
| 6 | from synchronizers.new_base.modelaccessor import * |
| 7 | #from xos.logger import Logger, logging |
| 8 | |
| 9 | from requests.auth import HTTPBasicAuth |
| 10 | #logger = Logger(level=logging.INFO) |
| 11 | |
| 12 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 13 | sys.path.insert(0, parentdir) |
| 14 | |
| 15 | |
| 16 | class SyncMetroNetEline(SyncStep): |
| 17 | provides = [ELine] |
| 18 | |
| 19 | observes = ELine |
| 20 | |
| 21 | requested_interval = 0 |
| 22 | |
| 23 | def __init__(self, *args, **kwargs): |
| 24 | super(SyncMetroNetEline, self).__init__(*args, **kwargs) |
| 25 | |
| 26 | def get_onos_global_addr(self, onos): |
| 27 | #Fetching ip and port from the Global ONOS, append the CE specif API |
| 28 | |
| 29 | onos_url = "http://%s:%s/onos/" % (onos.onos_ip, onos.onos_port) |
| 30 | evc_endpoint = "carrierethernet/evc" |
| 31 | return onos_url + evc_endpoint |
| 32 | |
| 33 | def get_onos_global_auth(self, onos): |
| 34 | #Fetching username and password from the Global ONOS |
| 35 | |
| 36 | return HTTPBasicAuth(onos.onos_username, onos.onos_password) |
| 37 | |
| 38 | def sync_record(self, evc): |
| 39 | print "POST %s " % (evc) |
| 40 | #logger.info("Syncing Edited EVC: %s" % evc.name) |
| 41 | # Fetch the bwp from the DB |
| 42 | bwp = BandwidthProfile.objects.get(name=evc.bwp) |
| 43 | |
| 44 | # json to configure ONOS to start the EVC. |
| 45 | # { |
| 46 | # "evcId": "evc1", |
| 47 | # "evcCfgId": "evpl1", |
| 48 | # "uniList": [ |
| 49 | # "netconf:192.168.56.10:830/0", |
| 50 | # "netconf:192.168.56.20:830/0" |
| 51 | # ], |
| 52 | # "evcType": "POINT_TO_POINT", |
| 53 | # "vlanId": 100, |
| 54 | # "cir": "400", |
| 55 | # "eir": "200", |
| 56 | # "cbs": "3000", |
| 57 | # "ebs": "2000" |
| 58 | # } |
| 59 | |
| 60 | data = {} |
| 61 | data["evcId"] = evc.name |
| 62 | data["evcCfgId"] = evc.name |
| 63 | data["uniList"] = [evc.connect_point_1_id, evc.connect_point_2_id] |
| 64 | data["evcType"] = "POINT_TO_POINT" |
| 65 | data["vlanId"] = evc.vlanids.split(",") |
| 66 | data["cbs"] = bwp.cbs |
| 67 | data["ebs"] = bwp.ebs |
| 68 | data["cir"] = bwp.cir |
| 69 | data["eir"] = bwp.eir |
| 70 | print "data %s" % data |
| 71 | # assuming that the CPEs are controller by the fabric ONOS |
| 72 | onos = OnosModel.objects.get(onos_type="global") |
| 73 | onos_addr = self.get_onos_global_addr(onos) |
| 74 | |
| 75 | # FIXME - hardcoded auth |
| 76 | auth = self.get_onos_global_auth(onos) |
| 77 | |
| 78 | print "POST %s for app %s, data = %s" % (onos_addr, evc.name, data) |
| 79 | |
| 80 | r = requests.post(onos_addr, data=json.dumps(data), auth=auth) |
| 81 | #TODO XOS might fail to connect to ONOS. |
| 82 | if (r.status_code != 200): |
| 83 | print r |
| 84 | raise Exception("Received error from EVC Installation update (%d)" % r.status_code) |
| 85 | |
| 86 | def delete_record(self, evc): |
| 87 | # TODO evaluate what to in this case. |
| 88 | print "Syncing delete EVC: %s" % evc.name |
| 89 | #logger.info("Syncing delete EVC: %s" % evc.name) |