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, AttWorkflowDriverServiceInstance, model_accessor |
| 22 | |
| 23 | class SubscriberAuthEventStep(EventStep): |
| 24 | topics = ["authentication.events"] |
| 25 | technology = "kafka" |
| 26 | |
| 27 | def __init__(self, *args, **kwargs): |
| 28 | super(SubscriberAuthEventStep, 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("authentication.events: Cannot find onu serial number for this event", kafka_event=event) |
| 35 | raise Exception("authentication.events: Cannot find onu serial number for this event") |
| 36 | |
| 37 | return onu_sn |
| 38 | |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 39 | def get_si_by_sn(self, serial_number): |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 40 | try: |
| 41 | return AttWorkflowDriverServiceInstance.objects.get(serial_number=serial_number) |
| 42 | except IndexError: |
| 43 | self.log.exception("authentication.events: Cannot find hippie-oss service instance for this event", kafka_event=value) |
| 44 | raise Exception("authentication.events: Cannot find hippie-oss service instance for this event") |
| 45 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 46 | def process_event(self, event): |
| 47 | value = json.loads(event.value) |
| 48 | |
| 49 | onu_sn = self.get_onu_sn(value) |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 50 | si = self.get_si_by_sn(onu_sn) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 51 | if not si: |
| 52 | self.log.exception("authentication.events: Cannot find hippie-oss service instance for this event", kafka_event=value) |
| 53 | raise Exception("authentication.events: Cannot find hippie-oss service instance for this event") |
| 54 | |
| 55 | si.authentication_state = value["authenticationState"]; |
| 56 | si.no_sync = True |
| 57 | si.save(update_fields=["authentication_state", "no_sync", "updated"], always_update_timestamp=True) |