blob: 9842406460ac6c67488af348ce811ace317aaf1e [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -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
16import json
17import time
18import os
19import sys
20from synchronizers.new_base.eventstep import EventStep
21from synchronizers.new_base.modelaccessor import VOLTService, AttWorkflowDriverServiceInstance, model_accessor
22
23class 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["deviceId"], event["portNumber"])
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
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070039 def get_si_by_sn(self, serial_number):
Matteo Scandoload0c1752018-08-09 15:47:16 -070040 try:
41 return AttWorkflowDriverServiceInstance.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
Matteo Scandoload0c1752018-08-09 15:47:16 -070046 def process_event(self, event):
47 value = json.loads(event.value)
48
49 onu_sn = self.get_onu_sn(value)
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070050 si = self.get_si_by_sn(onu_sn)
Matteo Scandoload0c1752018-08-09 15:47:16 -070051 if not si:
52 self.log.exception("authentication.events: Cannot find hippie-oss service instance for this event", kafka_event=value)
53 raise Exception("authentication.events: Cannot find hippie-oss service instance for this event")
54
55 si.authentication_state = value["authenticationState"];
56 si.no_sync = True
57 si.save(update_fields=["authentication_state", "no_sync", "updated"], always_update_timestamp=True)