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 | |
| 25 | |
| 26 | # Manually send the event |
| 27 | |
| 28 | # import json |
| 29 | # from kafka import KafkaProducer |
| 30 | |
| 31 | # event = json.dumps({ |
| 32 | # 'status': 'activate', |
| 33 | # 'serial_number': 'BRCM1234', |
| 34 | # 'uni_port_of_id': 'of:00100101', |
| 35 | # 'of_dpid': 'of:109299321' |
| 36 | # }) |
| 37 | # producer = KafkaProducer(bootstrap_servers="cord-kafka-kafka") |
| 38 | # producer.send("onu.events", event) |
| 39 | # producer.flush() |
| 40 | |
| 41 | |
| 42 | class ONUEventStep(EventStep): |
| 43 | topics = ["onu.events"] |
| 44 | technology = "kafka" |
| 45 | |
| 46 | def __init__(self, *args, **kwargs): |
| 47 | super(ONUEventStep, self).__init__(*args, **kwargs) |
| 48 | |
| 49 | def get_oss_service(self, onu_serial_number): |
| 50 | try: |
| 51 | onu = ONUDevice.objects.get(serial_number=onu_serial_number) |
| 52 | except IndexError as e: |
| 53 | raise Exception("No ONUDevice with serial_number %s is present in XOS" % onu_serial_number) |
| 54 | |
| 55 | volt_service = onu.pon_port.olt_device.volt_service |
| 56 | service = Service.objects.get(id=volt_service.id) |
| 57 | osses = [s for s in service.provider_services if s.kind.lower() == "oss"] |
| 58 | |
| 59 | if len(osses) > 1: |
| 60 | self.log.warn("More than one OSS found for %s" % volt_service.name) |
| 61 | try: |
| 62 | return osses[0].leaf_model |
| 63 | except IndexError as e: |
| 64 | return None |
| 65 | |
| 66 | def handle_onu_activate_event(self, event): |
| 67 | oss = self.get_oss_service(event["serial_number"]) |
| 68 | |
| 69 | if not oss: |
| 70 | self.log.info("Not processing events as no OSS service is present (is it a provider of vOLT?") |
| 71 | else: |
| 72 | try: |
| 73 | oss.validate_onu(event) |
| 74 | except Exception, e: |
| 75 | self.log.exception("Failing to validate ONU in OSS Service %s" % oss.name) |
| 76 | raise e |
| 77 | |
| 78 | def process_event(self, event): |
| 79 | value = json.loads(event.value) |
| 80 | self.log.info("onu.events: received event", value=value) |
| 81 | |
| 82 | if value["status"] == "activate": |
| 83 | self.log.info("onu.events: activate onu", value=value) |
| 84 | self.handle_onu_activate_event(value) |
| 85 | |