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 |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 22 | from helpers import AttHelpers |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 23 | |
| 24 | class SubscriberDhcpEventStep(EventStep): |
| 25 | topics = ["dhcp.events"] |
| 26 | technology = "kafka" |
| 27 | |
| 28 | def __init__(self, *args, **kwargs): |
| 29 | super(SubscriberDhcpEventStep, self).__init__(*args, **kwargs) |
| 30 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 31 | def process_event(self, event): |
| 32 | value = json.loads(event.value) |
| 33 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 34 | onu_sn = AttHelpers.get_onu_sn(value) |
| 35 | si = AttHelpers.get_si_by_sn(onu_sn) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 36 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 37 | if not si: |
| 38 | self.log.exception("dhcp.events: Cannot find att-workflow-driver service instance for this event", kafka_event=value) |
| 39 | raise Exception("dhcp.events: Cannot find att-workflow-driver service instance for this event") |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 40 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 41 | self.log.info("dhcp.events: Got event for subscriber", event_value=value, onu_sn=onu_sn, si=si) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 42 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 43 | si.dhcp_state = value["messageType"]; |
| 44 | si.ip_address = value["ipAddress"]; |
| 45 | si.mac_address = value["macAddress"]; |
Andy Bavier | 0d631eb | 2018-10-17 18:05:04 -0700 | [diff] [blame] | 46 | si.save_changed_fields(always_update_timestamp=True) |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 47 | # subscriber = RCORDSubscriber.objects.get(onu_device=onu_sn) |
| 48 | # |
| 49 | # |
| 50 | # # NOTE it will be better to update the SI and use the model policy to update the subscriber, |
| 51 | # # if this fails for any reason the event is lost |
| 52 | # |
| 53 | # if value["messageType"] == "DHCPACK": |
| 54 | # |
| 55 | # # FIXME apparently it's always saving |
| 56 | # subscriber.ip_address = value["ipAddress"] |
| 57 | # subscriber.mac_address = value["macAddress"] |
| 58 | # subscriber.save() |