blob: e11d15d32828cff3026dfc4e6b3061d470e17691 [file] [log] [blame]
Andrea Campanella95c02bd2017-09-01 16:51:03 +02001
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
17import os
18import sys
19import requests
20import json
21from synchronizers.new_base.syncstep import SyncStep
22from synchronizers.new_base.modelaccessor import *
Scott Bakerf7acc7d2017-10-18 10:55:47 -070023from xos.logger import Logger, logging
Andrea Campanella95c02bd2017-09-01 16:51:03 +020024
25from requests.auth import HTTPBasicAuth
Scott Bakerf7acc7d2017-10-18 10:55:47 -070026logger = Logger(level=logging.INFO)
Andrea Campanella95c02bd2017-09-01 16:51:03 +020027
28parentdir = os.path.join(os.path.dirname(__file__), "..")
29sys.path.insert(0, parentdir)
30
31
32class 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 Baker9bf61aa2017-10-10 14:35:50 -070045 onos_url = "http://%s:%s/" % (onos.onos_ip, onos.onos_port)
Andrea Campanella95c02bd2017-09-01 16:51:03 +020046 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):
Scott Bakerf7acc7d2017-10-18 10:55:47 -070055 logger.info("Syncing Edited EVC: %s" % evc.name)
56 logger.info("POST %s " % (evc))
57
Andrea Campanella95c02bd2017-09-01 16:51:03 +020058 # Fetch the bwp from the DB
59 bwp = BandwidthProfile.objects.get(name=evc.bwp)
60
61 # json to configure ONOS to start the EVC.
62 # {
63 # "evcId": "evc1",
64 # "evcCfgId": "evpl1",
65 # "uniList": [
66 # "netconf:192.168.56.10:830/0",
67 # "netconf:192.168.56.20:830/0"
68 # ],
69 # "evcType": "POINT_TO_POINT",
70 # "vlanId": 100,
71 # "cir": "400",
72 # "eir": "200",
73 # "cbs": "3000",
74 # "ebs": "2000"
75 # }
76
77 data = {}
78 data["evcId"] = evc.name
79 data["evcCfgId"] = evc.name
80 data["uniList"] = [evc.connect_point_1_id, evc.connect_point_2_id]
81 data["evcType"] = "POINT_TO_POINT"
Scott Baker9bf61aa2017-10-10 14:35:50 -070082 data["vlanId"] = int(evc.vlanids.split(",")[0]) # FIXME - should be list (CORD-2075)
Andrea Campanella95c02bd2017-09-01 16:51:03 +020083 data["cbs"] = bwp.cbs
84 data["ebs"] = bwp.ebs
85 data["cir"] = bwp.cir
86 data["eir"] = bwp.eir
Scott Bakerf7acc7d2017-10-18 10:55:47 -070087 logger.info("data %s" % data)
Andrea Campanella95c02bd2017-09-01 16:51:03 +020088 # assuming that the CPEs are controller by the fabric ONOS
89 onos = OnosModel.objects.get(onos_type="global")
90 onos_addr = self.get_onos_global_addr(onos)
91
Andrea Campanella95c02bd2017-09-01 16:51:03 +020092 auth = self.get_onos_global_auth(onos)
93
Andrea Campanellaf0339fe2017-11-16 09:37:18 -080094 logger.info("POST %s for evc %s, data = %s" % (onos_addr, evc.name, data))
Andrea Campanella95c02bd2017-09-01 16:51:03 +020095
96 r = requests.post(onos_addr, data=json.dumps(data), auth=auth)
Andrea Campanellaf0339fe2017-11-16 09:37:18 -080097
Andrea Campanella95c02bd2017-09-01 16:51:03 +020098 #TODO XOS might fail to connect to ONOS.
99 if (r.status_code != 200):
Scott Bakerf7acc7d2017-10-18 10:55:47 -0700100 logger.info("result = %s" % r)
Andrea Campanella95c02bd2017-09-01 16:51:03 +0200101 raise Exception("Received error from EVC Installation update (%d)" % r.status_code)
102
103 def delete_record(self, evc):
Scott Bakerf7acc7d2017-10-18 10:55:47 -0700104 logger.info("Syncing delete EVC: %s" % evc.name)
Andrea Campanellaf0339fe2017-11-16 09:37:18 -0800105
106 onos = OnosModel.objects.get(onos_type="global")
107 onos_addr = self.get_onos_global_addr(onos)
108
109 auth = self.get_onos_global_auth(onos)
110
111 logger.info("Delete %s for evc %s" % (onos_addr, evc.name))
112
113 data = {}
114 data['evcId'] = evc.name
115
116 r = requests.delete(onos_addr + "/" + evc.name, auth=auth)
117
118 # TODO XOS might fail to connect to ONOS.
119 if (r.status_code != 200):
120 logger.info("result = %s" % r)
121 raise Exception("Received error from EVC Removal update (%d)" % r.status_code)