blob: 14a95ac54ebfef55a574921bbc5543cdec65e21d [file] [log] [blame]
Takahiro Suzuki2b66b942020-12-17 11:58:14 +09001
2# Copyright 2020-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 json
18from xossynchronizer.event_steps.eventstep import EventStep
19from helpers import NttHelpers
20import requests
21from requests.auth import HTTPBasicAuth
22
23class ONUEventStep(EventStep):
24 topics = ["onu.events"]
25 technology = "kafka"
26
27 max_onu_retry = 50
28
29 def __init__(self, *args, **kwargs):
30 super(ONUEventStep, self).__init__(*args, **kwargs)
31
32 def process_event(self, event):
33 value = json.loads(event.value)
34 self.log.info("onu.events: received event", value=value)
35 # This is needed to be compatible with both Voltha 1.7 and Voltha 2.x
36 # It supposes to have only 1 subscriber per ONU and the subscriber is connected to the first port
37 if "-" in value["serialNumber"] and not value["serialNumber"].endswith("-1"):
38 self.log.info("Skip event, only consider [serialNumber]-1 events")
39 return
40
41 ntt_oi = NttHelpers.find_or_create_ntt_oi(self.model_accessor, self.log, value)
42 ntt_oi.no_sync = False
43 ntt_oi.of_dpid = value["deviceId"]
44 ntt_oi.save_changed_fields(always_update_timestamp=True)
45 ntt_si = NttHelpers.find_or_create_ntt_si(self.model_accessor, self.log, value)
46 if value["status"] == "activated":
47 self.log.info("onu.events: activated onu", value=value)
48 ntt_si.no_sync = False
49 ntt_si.uni_port_id = long(value["portNumber"])
50 ntt_si.of_dpid = value["deviceId"]
51 ntt_si.oper_onu_status = "ENABLED"
52 ntt_si.save_changed_fields(always_update_timestamp=True)
53 elif value["status"] == "disabled":
54 self.log.info("onu.events: disabled onu, resetting the subscriber", value=value)
55 ntt_si.oper_onu_status = "DISABLED"
56 ntt_si.save_changed_fields(always_update_timestamp=True)
57
58 log.debug("Removing subscriber with info",
59 uni_port_id = ntt_si.uni_port_id,
60 dp_id = ntt_si.of_dpid
61 )
62
63 onos_voltha_basic_auth = HTTPBasicAuth("karaf", "karaf")
64
65 handle = "%s/%s" % (ntt_si.of_dpid, ntt_si.uni_port_id)
66 # TODO store URL and PORT in the vOLT Service model
67 full_url = "http://129.60.110.180:8181/onos/olt/oltapp/%s" % (handle)
68
69 log.info("Sending request to onos-voltha", url=full_url)
70
71 request = requests.delete(full_url, auth=onos_voltha_basic_auth)
72
73 if request.status_code != 204:
74 raise Exception("Failed to remove subscriber from onos-voltha: %s" % request.text)
75
76 log.info("Removed Subscriber from onos voltha", response=request.text)
77
78 return
79 else:
80 self.log.warn("onu.events: Unknown status value: %s" % value["status"], value=value)
81 return