blob: d3195e0b5936bec5138eab280dd55bacf953e0fd [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
16
Matteo Scandoloea529092018-09-11 16:36:39 -070017
Matteo Scandoload0c1752018-08-09 15:47:16 -070018from synchronizers.new_base.modelaccessor import RCORDSubscriber, ONUDevice, model_accessor
19from synchronizers.new_base.policy import Policy
20
Matteo Scandoloea529092018-09-11 16:36:39 -070021import os
22import sys
23
24sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
25sys.path.append(sync_path)
26
27from helpers import AttHelpers
28
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070029class DeferredException(Exception):
30 pass
31
Matteo Scandoload0c1752018-08-09 15:47:16 -070032class AttWorkflowDriverServiceInstancePolicy(Policy):
33 model_name = "AttWorkflowDriverServiceInstance"
34
35 def handle_create(self, si):
36 self.logger.debug("MODEL_POLICY: handle_create for AttWorkflowDriverServiceInstance %s " % si.id)
37 self.handle_update(si)
38
Matteo Scandoload0c1752018-08-09 15:47:16 -070039 def handle_update(self, si):
Matteo Scandolob8da43d2018-09-12 15:52:16 -070040 self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s " % (si.id), onu_state=si.onu_state, authentication_state=si.authentication_state)
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070041
Matteo Scandoloea529092018-09-11 16:36:39 -070042 # validating ONU
43 if si.onu_state == "AWAITING" or si.onu_state == "ENABLED":
44 # we validate the ONU state only if it is enabled or awaiting,
45 # if it's disabled it means someone has disabled it
46 self.validate_onu_state(si)
Matteo Scandolo744effb2018-10-02 14:36:19 -070047 else:
48 # just clean the status
Matteo Scandolode8cfa82018-10-16 13:49:05 -070049 si.status_message = "ONU has been disabled"
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070050
Matteo Scandoloea529092018-09-11 16:36:39 -070051 # handling the subscriber status
52 subscriber = self.get_subscriber(si.serial_number)
Matteo Scandoload0c1752018-08-09 15:47:16 -070053
Matteo Scandoloea529092018-09-11 16:36:39 -070054 if subscriber:
55 self.update_subscriber(subscriber, si)
Matteo Scandoload0c1752018-08-09 15:47:16 -070056
Andy Bavier0d631eb2018-10-17 18:05:04 -070057 si.save_changed_fields()
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070058
Matteo Scandoloea529092018-09-11 16:36:39 -070059 def validate_onu_state(self, si):
60 [valid, message] = AttHelpers.validate_onu(si)
Matteo Scandolo200dab42018-09-12 13:52:09 -070061 si.status_message = message
Matteo Scandoloea529092018-09-11 16:36:39 -070062 if valid:
63 si.onu_state = "ENABLED"
64 self.update_onu(si.serial_number, "ENABLED")
65 else:
66 si.onu_state = "DISABLED"
67 self.update_onu(si.serial_number, "DISABLED")
Matteo Scandoload0c1752018-08-09 15:47:16 -070068
Matteo Scandoloea529092018-09-11 16:36:39 -070069 def update_onu(self, serial_number, admin_state):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -070070 onu = [onu for onu in ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0]
71 if onu.admin_state == admin_state:
72 self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state))
73 else:
74 self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
75 onu.admin_state = admin_state
Andy Bavier0d631eb2018-10-17 18:05:04 -070076 onu.save_changed_fields(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -070077
Matteo Scandoloea529092018-09-11 16:36:39 -070078 def get_subscriber(self, serial_number):
79 try:
80 return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0]
81 except IndexError:
82 # If the subscriber doesn't exist we don't do anything
83 self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
84 return None
Matteo Scandoload0c1752018-08-09 15:47:16 -070085
Matteo Scandoloea529092018-09-11 16:36:39 -070086 def update_subscriber(self, subscriber, si):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -070087 cur_status = subscriber.status
Matteo Scandoloea529092018-09-11 16:36:39 -070088 if si.authentication_state == "AWAITING":
89 subscriber.status = "awaiting-auth"
Matteo Scandolo075ec442018-09-13 14:19:02 -070090 si.status_message += " - Awaiting Authentication"
Matteo Scandoloea529092018-09-11 16:36:39 -070091 elif si.authentication_state == "REQUESTED":
92 subscriber.status = "awaiting-auth"
Matteo Scandolo075ec442018-09-13 14:19:02 -070093 si.status_message += " - Authentication requested"
Matteo Scandoloea529092018-09-11 16:36:39 -070094 elif si.authentication_state == "STARTED":
95 subscriber.status = "awaiting-auth"
Matteo Scandolo075ec442018-09-13 14:19:02 -070096 si.status_message += " - Authentication started"
Matteo Scandoloea529092018-09-11 16:36:39 -070097 elif si.authentication_state == "APPROVED":
98 subscriber.status = "enabled"
Matteo Scandolo075ec442018-09-13 14:19:02 -070099 si.status_message += " - Authentication succeded"
Matteo Scandoloea529092018-09-11 16:36:39 -0700100 elif si.authentication_state == "DENIED":
101 subscriber.status = "auth-failed"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700102 si.status_message += " - Authentication denied"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700103
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700104 # NOTE we save the subscriber only if:
105 # - the status has changed
106 # - we get a DHCPACK event
107 if cur_status != subscriber.status or si.dhcp_state == "DHCPACK":
108 self.logger.debug("MODEL_POLICY: updating subscriber", onu_device=subscriber.onu_device, authentication_state=si.authentication_state, subscriber_status=subscriber.status)
109 if si.ip_address and si.mac_address:
110 subscriber.ip_address = si.ip_address
111 subscriber.mac_address = si.mac_address
Andy Bavier0d631eb2018-10-17 18:05:04 -0700112 subscriber.save_changed_fields(always_update_timestamp=True)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700113 else:
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700114 self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device,
115 authentication_state=si.authentication_state, subscriber_status=subscriber.status)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700116
117 def handle_delete(self, si):
118 pass