blob: aefc2669dad857dd97df5a61bd74f51d9509bf66 [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 # json to configure ONOS to start the EVC.
59 # {
60 # "evcId": "evc1",
61 # "evcCfgId": "evpl1",
62 # "uniList": [
63 # "netconf:192.168.56.10:830/0",
64 # "netconf:192.168.56.20:830/0"
65 # ],
66 # "evcType": "POINT_TO_POINT",
67 # "vlanId": 100,
68 # "cir": "400",
69 # "eir": "200",
70 # "cbs": "3000",
71 # "ebs": "2000"
72 # }
73
74 data = {}
75 data["evcId"] = evc.name
76 data["evcCfgId"] = evc.name
Scott Bakerd76123f2018-01-23 14:26:54 -080077 data["uniList"] = [evc.connect_point_1.cpe_id, evc.connect_point_2.cpe_id]
Andrea Campanella95c02bd2017-09-01 16:51:03 +020078 data["evcType"] = "POINT_TO_POINT"
Scott Baker9bf61aa2017-10-10 14:35:50 -070079 data["vlanId"] = int(evc.vlanids.split(",")[0]) # FIXME - should be list (CORD-2075)
Scott Bakerd76123f2018-01-23 14:26:54 -080080 data["cbs"] = evc.bwp.cbs
81 data["ebs"] = evc.bwp.ebs
82 data["cir"] = evc.bwp.cir
83 data["eir"] = evc.bwp.eir
Scott Bakerf7acc7d2017-10-18 10:55:47 -070084 logger.info("data %s" % data)
Andrea Campanella95c02bd2017-09-01 16:51:03 +020085 # assuming that the CPEs are controller by the fabric ONOS
86 onos = OnosModel.objects.get(onos_type="global")
87 onos_addr = self.get_onos_global_addr(onos)
88
Andrea Campanella95c02bd2017-09-01 16:51:03 +020089 auth = self.get_onos_global_auth(onos)
90
Andrea Campanellaf0339fe2017-11-16 09:37:18 -080091 logger.info("POST %s for evc %s, data = %s" % (onos_addr, evc.name, data))
Andrea Campanella95c02bd2017-09-01 16:51:03 +020092
93 r = requests.post(onos_addr, data=json.dumps(data), auth=auth)
Andrea Campanellaf0339fe2017-11-16 09:37:18 -080094
Andrea Campanella95c02bd2017-09-01 16:51:03 +020095 #TODO XOS might fail to connect to ONOS.
96 if (r.status_code != 200):
Scott Bakerf7acc7d2017-10-18 10:55:47 -070097 logger.info("result = %s" % r)
Andrea Campanella95c02bd2017-09-01 16:51:03 +020098 raise Exception("Received error from EVC Installation update (%d)" % r.status_code)
99
100 def delete_record(self, evc):
Scott Bakerf7acc7d2017-10-18 10:55:47 -0700101 logger.info("Syncing delete EVC: %s" % evc.name)
Andrea Campanellaf0339fe2017-11-16 09:37:18 -0800102
103 onos = OnosModel.objects.get(onos_type="global")
104 onos_addr = self.get_onos_global_addr(onos)
105
106 auth = self.get_onos_global_auth(onos)
107
108 logger.info("Delete %s for evc %s" % (onos_addr, evc.name))
109
110 data = {}
111 data['evcId'] = evc.name
112
113 r = requests.delete(onos_addr + "/" + evc.name, auth=auth)
114
115 # TODO XOS might fail to connect to ONOS.
116 if (r.status_code != 200):
117 logger.info("result = %s" % r)
118 raise Exception("Received error from EVC Removal update (%d)" % r.status_code)