blob: b41f80328e3740fb5327aee892ff34c9b710f17c [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -07001
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
16import json
17import time
18import os
19import sys
20from synchronizers.new_base.eventstep import EventStep
21from synchronizers.new_base.modelaccessor import VOLTService, RCORDSubscriber, model_accessor
22
23class SubscriberDhcpEventStep(EventStep):
24 topics = ["dhcp.events"]
25 technology = "kafka"
26
27 def __init__(self, *args, **kwargs):
28 super(SubscriberDhcpEventStep, self).__init__(*args, **kwargs)
29
30 def get_onu_sn(self, event):
31 olt_service = VOLTService.objects.first()
32 onu_sn = olt_service.get_onu_sn_from_openflow(event["deviceId"], event["portNumber"])
33 if not onu_sn or onu_sn is None:
34 self.log.exception("dhcp.events: Cannot find onu serial number for this event", kafka_event=event)
35 raise Exception("dhcp.events: Cannot find onu serial number for this event")
36
37 return onu_sn
38
39 def process_event(self, event):
40 value = json.loads(event.value)
41
42 onu_sn = self.get_onu_sn(value)
43
44 subscriber = RCORDSubscriber.objects.get(onu_device=onu_sn)
45
Matteo Scandolo744effb2018-10-02 14:36:19 -070046 self.log.info("dhcp.events: Got event for subscriber", subscriber=subscriber, event_value=value, onu_sn=onu_sn)
Matteo Scandoload0c1752018-08-09 15:47:16 -070047
Matteo Scandoloea529092018-09-11 16:36:39 -070048 # NOTE it will be better to update the SI and use the model policy to update the subscriber,
49 # if this fails for any reason the event is lost
Matteo Scandolo507a2a02018-09-17 15:08:25 -070050 if subscriber.ip_address != value["ipAddress"] or \
51 subscriber.mac_address != value["macAddress"]:
52
Matteo Scandolo744effb2018-10-02 14:36:19 -070053 # FIXME apparently it's always saving
Matteo Scandolo507a2a02018-09-17 15:08:25 -070054 subscriber.ip_address = value["ipAddress"]
55 subscriber.mac_address = value["macAddress"]
56 subscriber.save()