Matteo Scandolo | b8dec87 | 2018-07-17 16:56:23 -0400 | [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, HippieOSSServiceInstance, 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["device_id"], event["port_number"]) |
| 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 | |
| 39 | def get_hippie_oss_si_by_sn(self, serial_number): |
| 40 | try: |
| 41 | return HippieOSSServiceInstance.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 | |
| 46 | |
| 47 | def activate_subscriber(self, subscriber): |
| 48 | subscriber.status = 'enabled' |
| 49 | subscriber.save() |
| 50 | |
| 51 | def disable_subscriber(self, subscriber): |
| 52 | subscriber.status = 'auth-failed' |
| 53 | subscriber.save() |
| 54 | |
| 55 | def process_event(self, event): |
| 56 | value = json.loads(event.value) |
| 57 | |
| 58 | onu_sn = self.get_onu_sn(value) |
| 59 | si = self.get_hippie_oss_si_by_sn(onu_sn) |
| 60 | if not si: |
| 61 | self.log.exception("authentication.events: Cannot find hippie-oss service instance for this event", kafka_event=value) |
| 62 | raise Exception("authentication.events: Cannot find hippie-oss service instance for this event") |
| 63 | |
| 64 | si.authentication_state = value["authentication_state"]; |
| 65 | si.no_sync = True |
| 66 | si.save(update_fields=["authentication_state", "no_sync", "updated"], always_update_timestamp=True) |