blob: df73f7d2891487b0518a0f24588b78c077c7e990 [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 Scandoloea529092018-09-11 16:36:39 -070040 self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s " % (si.id))
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 Scandoloe8c33d62018-08-16 14:37:24 -070047
Matteo Scandoloea529092018-09-11 16:36:39 -070048 # handling the subscriber status
49 subscriber = self.get_subscriber(si.serial_number)
Matteo Scandoload0c1752018-08-09 15:47:16 -070050
Matteo Scandoloea529092018-09-11 16:36:39 -070051 if subscriber:
52 self.update_subscriber(subscriber, si)
Matteo Scandoload0c1752018-08-09 15:47:16 -070053
Matteo Scandoloea529092018-09-11 16:36:39 -070054 si.save()
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070055
Matteo Scandoloea529092018-09-11 16:36:39 -070056 def validate_onu_state(self, si):
57 [valid, message] = AttHelpers.validate_onu(si)
Matteo Scandolo200dab42018-09-12 13:52:09 -070058 si.status_message = message
Matteo Scandoloea529092018-09-11 16:36:39 -070059 if valid:
60 si.onu_state = "ENABLED"
61 self.update_onu(si.serial_number, "ENABLED")
62 else:
63 si.onu_state = "DISABLED"
64 self.update_onu(si.serial_number, "DISABLED")
Matteo Scandoload0c1752018-08-09 15:47:16 -070065
Matteo Scandoloea529092018-09-11 16:36:39 -070066 def update_onu(self, serial_number, admin_state):
67 self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
68 onu = ONUDevice.objects.get(serial_number=serial_number)
69 onu.admin_state = admin_state
70 onu.save(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -070071
Matteo Scandoloea529092018-09-11 16:36:39 -070072 def get_subscriber(self, serial_number):
73 try:
74 return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0]
75 except IndexError:
76 # If the subscriber doesn't exist we don't do anything
77 self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
78 return None
Matteo Scandoload0c1752018-08-09 15:47:16 -070079
Matteo Scandoloea529092018-09-11 16:36:39 -070080 def update_subscriber(self, subscriber, si):
81 if si.authentication_state == "AWAITING":
82 subscriber.status = "awaiting-auth"
Matteo Scandolo200dab42018-09-12 13:52:09 -070083 si.status_message = "Awaiting Authentication"
Matteo Scandoloea529092018-09-11 16:36:39 -070084 elif si.authentication_state == "REQUESTED":
85 subscriber.status = "awaiting-auth"
Matteo Scandolo200dab42018-09-12 13:52:09 -070086 si.status_message = "Authentication requested"
Matteo Scandoloea529092018-09-11 16:36:39 -070087 elif si.authentication_state == "STARTED":
88 subscriber.status = "awaiting-auth"
Matteo Scandolo200dab42018-09-12 13:52:09 -070089 si.status_message = "Authentication started"
Matteo Scandoloea529092018-09-11 16:36:39 -070090 elif si.authentication_state == "APPROVED":
91 subscriber.status = "enabled"
Matteo Scandolo200dab42018-09-12 13:52:09 -070092 si.status_message = "Authentication succeded"
Matteo Scandoloea529092018-09-11 16:36:39 -070093 elif si.authentication_state == "DENIED":
94 subscriber.status = "auth-failed"
Matteo Scandolo200dab42018-09-12 13:52:09 -070095 si.status_message = "Authentication denied"
Matteo Scandoloea529092018-09-11 16:36:39 -070096 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 -070097
Matteo Scandoloea529092018-09-11 16:36:39 -070098 subscriber.save(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -070099
100 def handle_delete(self, si):
101 pass