blob: e08d6bd800f6a21237ec2e08e597e941ad87963d [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 Scandolob8da43d2018-09-12 15:52:16 -070047 else:
48 # but we still verify that the device is actually down
49 self.update_onu(si.serial_number, "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
Matteo Scandoloea529092018-09-11 16:36:39 -070057 si.save()
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 Scandolob8da43d2018-09-12 15:52:16 -070070 # TODO if the status hasn't changed don't save it again
Matteo Scandoloea529092018-09-11 16:36:39 -070071 self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
72 onu = ONUDevice.objects.get(serial_number=serial_number)
73 onu.admin_state = admin_state
74 onu.save(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -070075
Matteo Scandoloea529092018-09-11 16:36:39 -070076 def get_subscriber(self, serial_number):
77 try:
78 return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0]
79 except IndexError:
80 # If the subscriber doesn't exist we don't do anything
81 self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
82 return None
Matteo Scandoload0c1752018-08-09 15:47:16 -070083
Matteo Scandoloea529092018-09-11 16:36:39 -070084 def update_subscriber(self, subscriber, si):
Matteo Scandolob8da43d2018-09-12 15:52:16 -070085 # TODO if the status hasn't changed don't save it again
Matteo Scandoloea529092018-09-11 16:36:39 -070086 if si.authentication_state == "AWAITING":
87 subscriber.status = "awaiting-auth"
Matteo Scandolo200dab42018-09-12 13:52:09 -070088 si.status_message = "Awaiting Authentication"
Matteo Scandoloea529092018-09-11 16:36:39 -070089 elif si.authentication_state == "REQUESTED":
90 subscriber.status = "awaiting-auth"
Matteo Scandolo200dab42018-09-12 13:52:09 -070091 si.status_message = "Authentication requested"
Matteo Scandoloea529092018-09-11 16:36:39 -070092 elif si.authentication_state == "STARTED":
93 subscriber.status = "awaiting-auth"
Matteo Scandolo200dab42018-09-12 13:52:09 -070094 si.status_message = "Authentication started"
Matteo Scandoloea529092018-09-11 16:36:39 -070095 elif si.authentication_state == "APPROVED":
96 subscriber.status = "enabled"
Matteo Scandolo200dab42018-09-12 13:52:09 -070097 si.status_message = "Authentication succeded"
Matteo Scandoloea529092018-09-11 16:36:39 -070098 elif si.authentication_state == "DENIED":
99 subscriber.status = "auth-failed"
Matteo Scandolo200dab42018-09-12 13:52:09 -0700100 si.status_message = "Authentication denied"
Matteo Scandoloea529092018-09-11 16:36:39 -0700101 self.logger.debug("MODEL_POLICY: handling subscriber", onu_device=subscriber.onu_device, authentication_state=si.authentication_state, subscriber_status=subscriber.status)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700102
Matteo Scandoloea529092018-09-11 16:36:39 -0700103 subscriber.save(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700104
105 def handle_delete(self, si):
106 pass