Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 1 | |
| 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 | import json |
| 17 | import time |
| 18 | import os |
| 19 | import sys |
| 20 | from synchronizers.new_base.eventstep import EventStep |
| 21 | from synchronizers.new_base.modelaccessor import VOLTService, RCORDSubscriber, model_accessor |
| 22 | |
| 23 | class 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 Scandolo | 744effb | 2018-10-02 14:36:19 -0700 | [diff] [blame^] | 46 | self.log.info("dhcp.events: Got event for subscriber", subscriber=subscriber, event_value=value, onu_sn=onu_sn) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 47 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 48 | # 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 Scandolo | 507a2a0 | 2018-09-17 15:08:25 -0700 | [diff] [blame] | 50 | if subscriber.ip_address != value["ipAddress"] or \ |
| 51 | subscriber.mac_address != value["macAddress"]: |
| 52 | |
Matteo Scandolo | 744effb | 2018-10-02 14:36:19 -0700 | [diff] [blame^] | 53 | # FIXME apparently it's always saving |
Matteo Scandolo | 507a2a0 | 2018-09-17 15:08:25 -0700 | [diff] [blame] | 54 | subscriber.ip_address = value["ipAddress"] |
| 55 | subscriber.mac_address = value["macAddress"] |
| 56 | subscriber.save() |