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