Matteo Scandolo | 85f5498 | 2018-05-18 11:33:35 -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 | |
| 17 | import json |
| 18 | import os |
| 19 | import sys |
| 20 | from synchronizers.new_base.eventstep import EventStep |
| 21 | from synchronizers.new_base.modelaccessor import VOLTService, ONUDevice, Service, model_accessor |
| 22 | |
| 23 | # from xos.exceptions import XOSValidationError |
| 24 | |
Matteo Scandolo | 85f5498 | 2018-05-18 11:33:35 -0700 | [diff] [blame] | 25 | class ONUEventStep(EventStep): |
| 26 | topics = ["onu.events"] |
| 27 | technology = "kafka" |
| 28 | |
| 29 | def __init__(self, *args, **kwargs): |
| 30 | super(ONUEventStep, self).__init__(*args, **kwargs) |
| 31 | |
| 32 | def get_oss_service(self, onu_serial_number): |
| 33 | try: |
| 34 | onu = ONUDevice.objects.get(serial_number=onu_serial_number) |
| 35 | except IndexError as e: |
Matteo Scandolo | 7db8537 | 2018-05-22 15:26:55 -0700 | [diff] [blame] | 36 | # TODO create ONU if it does not exists |
Matteo Scandolo | 85f5498 | 2018-05-18 11:33:35 -0700 | [diff] [blame] | 37 | raise Exception("No ONUDevice with serial_number %s is present in XOS" % onu_serial_number) |
| 38 | |
| 39 | volt_service = onu.pon_port.olt_device.volt_service |
| 40 | service = Service.objects.get(id=volt_service.id) |
Matteo Scandolo | 7db8537 | 2018-05-22 15:26:55 -0700 | [diff] [blame] | 41 | osses = [s for s in service.subscriber_services if s.kind.lower() == "oss"] |
Matteo Scandolo | 85f5498 | 2018-05-18 11:33:35 -0700 | [diff] [blame] | 42 | |
| 43 | if len(osses) > 1: |
| 44 | self.log.warn("More than one OSS found for %s" % volt_service.name) |
| 45 | try: |
| 46 | return osses[0].leaf_model |
| 47 | except IndexError as e: |
| 48 | return None |
| 49 | |
| 50 | def handle_onu_activate_event(self, event): |
| 51 | oss = self.get_oss_service(event["serial_number"]) |
| 52 | |
| 53 | if not oss: |
| 54 | self.log.info("Not processing events as no OSS service is present (is it a provider of vOLT?") |
| 55 | else: |
| 56 | try: |
| 57 | oss.validate_onu(event) |
| 58 | except Exception, e: |
| 59 | self.log.exception("Failing to validate ONU in OSS Service %s" % oss.name) |
| 60 | raise e |
| 61 | |
| 62 | def process_event(self, event): |
| 63 | value = json.loads(event.value) |
| 64 | self.log.info("onu.events: received event", value=value) |
| 65 | |
Matteo Scandolo | 7db8537 | 2018-05-22 15:26:55 -0700 | [diff] [blame] | 66 | if value["status"] == "activated": |
Matteo Scandolo | 85f5498 | 2018-05-18 11:33:35 -0700 | [diff] [blame] | 67 | self.log.info("onu.events: activate onu", value=value) |
| 68 | self.handle_onu_activate_event(value) |
| 69 | |