Andrea Campanella | 95c02bd | 2017-09-01 16:51:03 +0200 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
| 17 | import os |
| 18 | import sys |
| 19 | import requests |
| 20 | import json |
| 21 | from synchronizers.new_base.syncstep import SyncStep |
| 22 | from synchronizers.new_base.modelaccessor import * |
| 23 | #from xos.logger import Logger, logging |
| 24 | |
| 25 | from requests.auth import HTTPBasicAuth |
| 26 | #logger = Logger(level=logging.INFO) |
| 27 | |
| 28 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 29 | sys.path.insert(0, parentdir) |
| 30 | |
| 31 | |
| 32 | class SyncvNaaSEline(SyncStep): |
| 33 | provides = [ELine] |
| 34 | |
| 35 | observes = ELine |
| 36 | |
| 37 | requested_interval = 0 |
| 38 | |
| 39 | def __init__(self, *args, **kwargs): |
| 40 | super(SyncvNaaSEline, self).__init__(*args, **kwargs) |
| 41 | |
| 42 | def get_onos_global_addr(self, onos): |
| 43 | #Fetching ip and port from the Global ONOS, append the CE specif API |
| 44 | |
Scott Baker | 9bf61aa | 2017-10-10 14:35:50 -0700 | [diff] [blame^] | 45 | onos_url = "http://%s:%s/" % (onos.onos_ip, onos.onos_port) |
Andrea Campanella | 95c02bd | 2017-09-01 16:51:03 +0200 | [diff] [blame] | 46 | evc_endpoint = "carrierethernet/evc" |
| 47 | return onos_url + evc_endpoint |
| 48 | |
| 49 | def get_onos_global_auth(self, onos): |
| 50 | #Fetching username and password from the Global ONOS |
| 51 | |
| 52 | return HTTPBasicAuth(onos.onos_username, onos.onos_password) |
| 53 | |
| 54 | def sync_record(self, evc): |
| 55 | print "POST %s " % (evc) |
| 56 | #logger.info("Syncing Edited EVC: %s" % evc.name) |
| 57 | # Fetch the bwp from the DB |
| 58 | bwp = BandwidthProfile.objects.get(name=evc.bwp) |
| 59 | |
| 60 | # json to configure ONOS to start the EVC. |
| 61 | # { |
| 62 | # "evcId": "evc1", |
| 63 | # "evcCfgId": "evpl1", |
| 64 | # "uniList": [ |
| 65 | # "netconf:192.168.56.10:830/0", |
| 66 | # "netconf:192.168.56.20:830/0" |
| 67 | # ], |
| 68 | # "evcType": "POINT_TO_POINT", |
| 69 | # "vlanId": 100, |
| 70 | # "cir": "400", |
| 71 | # "eir": "200", |
| 72 | # "cbs": "3000", |
| 73 | # "ebs": "2000" |
| 74 | # } |
| 75 | |
| 76 | data = {} |
| 77 | data["evcId"] = evc.name |
| 78 | data["evcCfgId"] = evc.name |
| 79 | data["uniList"] = [evc.connect_point_1_id, evc.connect_point_2_id] |
| 80 | data["evcType"] = "POINT_TO_POINT" |
Scott Baker | 9bf61aa | 2017-10-10 14:35:50 -0700 | [diff] [blame^] | 81 | data["vlanId"] = int(evc.vlanids.split(",")[0]) # FIXME - should be list (CORD-2075) |
Andrea Campanella | 95c02bd | 2017-09-01 16:51:03 +0200 | [diff] [blame] | 82 | data["cbs"] = bwp.cbs |
| 83 | data["ebs"] = bwp.ebs |
| 84 | data["cir"] = bwp.cir |
| 85 | data["eir"] = bwp.eir |
| 86 | print "data %s" % data |
| 87 | # assuming that the CPEs are controller by the fabric ONOS |
| 88 | onos = OnosModel.objects.get(onos_type="global") |
| 89 | onos_addr = self.get_onos_global_addr(onos) |
| 90 | |
| 91 | # FIXME - hardcoded auth |
| 92 | auth = self.get_onos_global_auth(onos) |
| 93 | |
| 94 | print "POST %s for app %s, data = %s" % (onos_addr, evc.name, data) |
| 95 | |
| 96 | r = requests.post(onos_addr, data=json.dumps(data), auth=auth) |
| 97 | #TODO XOS might fail to connect to ONOS. |
| 98 | if (r.status_code != 200): |
| 99 | print r |
| 100 | raise Exception("Received error from EVC Installation update (%d)" % r.status_code) |
| 101 | |
| 102 | def delete_record(self, evc): |
| 103 | # TODO evaluate what to in this case. |
| 104 | print "Syncing delete EVC: %s" % evc.name |
| 105 | #logger.info("Syncing delete EVC: %s" % evc.name) |