blob: 10091cf98fa4b4763e3742c77c696169fe104e13 [file] [log] [blame]
Matteo Scandolof6337eb2018-04-05 15:58:37 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Matteo Scandolod44ca992018-05-17 15:02:10 -070015import os, sys
16sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
Matteo Scandolof6337eb2018-04-05 15:58:37 -070017
Matteo Scandolof6337eb2018-04-05 15:58:37 -070018from helpers import Helpers
19
Matteo Scandolod44ca992018-05-17 15:02:10 -070020import requests
21from multistructlog import create_logger
22from requests.auth import HTTPBasicAuth
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070023from synchronizers.new_base.modelaccessor import VOLTService, VOLTServiceInstance, ServiceInstance, model_accessor
Matteo Scandolod44ca992018-05-17 15:02:10 -070024from synchronizers.new_base.syncstep import SyncStep, DeferredException
25from xosconfig import Config
26
Matteo Scandolof6337eb2018-04-05 15:58:37 -070027log = create_logger(Config().get("logging"))
28
29class SyncVOLTServiceInstance(SyncStep):
30 provides = [VOLTServiceInstance]
31
32 observes = VOLTServiceInstance
33
34 def sync_record(self, o):
35
Matteo Scandolo80912942018-07-25 20:51:30 -070036 if o.policy_code != 1:
37 raise DeferredException("Waiting for ModelPolicy to complete")
38
39
Matteo Scandolof6337eb2018-04-05 15:58:37 -070040 volt_service = VOLTService.objects.get(id=o.owner_id)
41
42 si = ServiceInstance.objects.get(id=o.id)
43
Luca Preteca974c82018-05-01 18:06:16 -070044 log.info("Synching OLTServiceInstance", object=str(o), **o.tologdict())
Matteo Scandolof6337eb2018-04-05 15:58:37 -070045
46 c_tag = si.get_westbound_service_instance_properties("c_tag")
Matteo Scandolo80912942018-07-25 20:51:30 -070047 s_tag = si.get_westbound_service_instance_properties("s_tag")
Matteo Scandolo7db85372018-05-22 15:26:55 -070048
Matteo Scandolod8ed60e2018-06-18 17:00:57 -070049 olt_device = o.onu_device.pon_port.olt_device
Matteo Scandolof6337eb2018-04-05 15:58:37 -070050
Matteo Scandolo80912942018-07-25 20:51:30 -070051 try:
52 # NOTE each ONU has only one UNI port!
53 uni_port_id = o.onu_device.uni_ports.first().port_no
54 except AttributeError:
55 # This is because the ONUDevice is set by model_policy
56 raise DeferredException("Waiting for ONUDevice %s " % olt_device.name)
Matteo Scandolof6337eb2018-04-05 15:58:37 -070057
58 if not olt_device.dp_id:
59 raise DeferredException("Waiting for OLTDevice %s to be synchronized" % olt_device.name)
60
61 log.debug("Adding subscriber with info",
Luca Preteca974c82018-05-01 18:06:16 -070062 c_tag = c_tag,
Matteo Scandolo80912942018-07-25 20:51:30 -070063 s_tag = s_tag,
Luca Preteca974c82018-05-01 18:06:16 -070064 uni_port_id = uni_port_id,
65 dp_id = olt_device.dp_id)
Matteo Scandolof6337eb2018-04-05 15:58:37 -070066
Luca Preteca974c82018-05-01 18:06:16 -070067 # Send request to ONOS
68 onos_voltha = Helpers.get_onos_voltha_info(volt_service)
69 onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
Matteo Scandolof6337eb2018-04-05 15:58:37 -070070
Matteo Scandolo80912942018-07-25 20:51:30 -070071 full_url = "%s:%d/onos/olt/oltapp/subscribers" % (onos_voltha['url'], onos_voltha['port'])
Matteo Scandolof6337eb2018-04-05 15:58:37 -070072
Matteo Scandolo80912942018-07-25 20:51:30 -070073 data = {
74 "deviceId" : olt_device.dp_id,
75 "port" : uni_port_id,
76 "sVlan" : s_tag,
77 "cVlan" : c_tag
78 }
Matteo Scandolof6337eb2018-04-05 15:58:37 -070079
Matteo Scandolo80912942018-07-25 20:51:30 -070080 log.info("Sending request to onos-voltha", url=full_url, data=data)
81
82 request = requests.post(full_url, auth=onos_voltha_basic_auth, json=data)
Matteo Scandolof6337eb2018-04-05 15:58:37 -070083
Luca Preteca974c82018-05-01 18:06:16 -070084 if request.status_code != 200:
85 raise Exception("Failed to add subscriber in onos-voltha: %s" % request.text)
Matteo Scandolof6337eb2018-04-05 15:58:37 -070086
Matteo Scandoloe48a4642018-06-26 10:08:34 -070087 log.info("Added Subscriber in onos voltha", response=request.text)
88
89 def delete_record(self, o):
90
91 log.info("Removing OLTServiceInstance", object=str(o), **o.tologdict())
92
93 volt_service = VOLTService.objects.get(id=o.owner_id)
94 onos_voltha = Helpers.get_onos_voltha_info(volt_service)
95 onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
96
97 olt_device = o.onu_device.pon_port.olt_device
98 # NOTE each ONU has only one UNI port!
99 uni_port_id = o.onu_device.uni_ports.first().port_no
100
101 full_url = "%s:%d/onos/olt/oltapp/%s/%s" % (onos_voltha['url'], onos_voltha['port'], olt_device.dp_id, uni_port_id)
102
103 request = requests.delete(full_url, auth=onos_voltha_basic_auth)
104
105 if request.status_code != 204:
106 raise Exception("Failed to remove subscriber from onos-voltha: %s" % request.text)
107
108 log.info("Removed Subscriber from onos voltha", response=request.text)