blob: 70181ed238f506949f03591a9877ef5ac9f01adb [file] [log] [blame]
Matteo Scandolo85f54982018-05-18 11:33:35 -07001
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
17import json
Matteo Scandolo62ca52b2018-06-21 17:03:58 -070018import time
Matteo Scandolo85f54982018-05-18 11:33:35 -070019import os
20import sys
21from synchronizers.new_base.eventstep import EventStep
22from synchronizers.new_base.modelaccessor import VOLTService, ONUDevice, Service, model_accessor
23
24# from xos.exceptions import XOSValidationError
25
Matteo Scandolo85f54982018-05-18 11:33:35 -070026class ONUEventStep(EventStep):
27 topics = ["onu.events"]
28 technology = "kafka"
29
Matteo Scandolo62ca52b2018-06-21 17:03:58 -070030 max_onu_retry = 50
31
Matteo Scandolo85f54982018-05-18 11:33:35 -070032 def __init__(self, *args, **kwargs):
33 super(ONUEventStep, self).__init__(*args, **kwargs)
34
Matteo Scandolo62ca52b2018-06-21 17:03:58 -070035 def get_oss_service(self, onu_serial_number, retry=0):
Matteo Scandolo85f54982018-05-18 11:33:35 -070036 try:
37 onu = ONUDevice.objects.get(serial_number=onu_serial_number)
38 except IndexError as e:
Matteo Scandolo62ca52b2018-06-21 17:03:58 -070039 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 Scandolo85f54982018-05-18 11:33:35 -070046
47 volt_service = onu.pon_port.olt_device.volt_service
48 service = Service.objects.get(id=volt_service.id)
Matteo Scandolo7db85372018-05-22 15:26:55 -070049 osses = [s for s in service.subscriber_services if s.kind.lower() == "oss"]
Matteo Scandolo85f54982018-05-18 11:33:35 -070050
51 if len(osses) > 1:
Matteo Scandolo62ca52b2018-06-21 17:03:58 -070052 self.log.warn("onu.events: More than one OSS found for %s" % volt_service.name)
Matteo Scandolo85f54982018-05-18 11:33:35 -070053 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 Scandolo62ca52b2018-06-21 17:03:58 -070062 self.log.info("onu.events: Not processing events as no OSS service is present (is it a provider of vOLT?")
Matteo Scandolo85f54982018-05-18 11:33:35 -070063 else:
64 try:
Matteo Scandolo62ca52b2018-06-21 17:03:58 -070065 self.log.info("onu.events: Calling OSS for ONUDevice with serial_number %s" % event["serial_number"])
Matteo Scandolo85f54982018-05-18 11:33:35 -070066 oss.validate_onu(event)
67 except Exception, e:
Matteo Scandolo096a3cf2018-06-20 13:56:13 -070068 self.log.exception("onu.events: Failing to validate ONU in OSS Service %s" % oss.name, onu_serial_number=event["serial_number"])
Matteo Scandolo85f54982018-05-18 11:33:35 -070069 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 Scandolo7db85372018-05-22 15:26:55 -070075 if value["status"] == "activated":
Matteo Scandolo85f54982018-05-18 11:33:35 -070076 self.log.info("onu.events: activate onu", value=value)
77 self.handle_onu_activate_event(value)
78