blob: 4be994b46b669c41d82a2f15d7f1e5d8acbfa26b [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 *
23#from xos.logger import Logger, logging
24
25from requests.auth import HTTPBasicAuth
26#logger = Logger(level=logging.INFO)
27
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
45 onos_url = "http://%s:%s/onos/" % (onos.onos_ip, onos.onos_port)
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"
81 data["vlanId"] = evc.vlanids.split(",")
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)