blob: dffd154dfa867963d2ae5c31b4749c5d58783999 [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
Matteo Scandoloea529092018-09-11 16:36:39 -070035 separator = " // "
36
Matteo Scandoload0c1752018-08-09 15:47:16 -070037 def handle_create(self, si):
38 self.logger.debug("MODEL_POLICY: handle_create for AttWorkflowDriverServiceInstance %s " % si.id)
39 self.handle_update(si)
40
Matteo Scandoload0c1752018-08-09 15:47:16 -070041 def handle_update(self, si):
Matteo Scandoloea529092018-09-11 16:36:39 -070042 self.logger.debug("MODEL_POLICY: handle_update for AttWorkflowDriverServiceInstance %s " % (si.id))
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070043
Matteo Scandoloea529092018-09-11 16:36:39 -070044 # validating ONU
45 if si.onu_state == "AWAITING" or si.onu_state == "ENABLED":
46 # we validate the ONU state only if it is enabled or awaiting,
47 # if it's disabled it means someone has disabled it
48 self.validate_onu_state(si)
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070049
Matteo Scandoloea529092018-09-11 16:36:39 -070050 # handling the subscriber status
51 subscriber = self.get_subscriber(si.serial_number)
Matteo Scandoload0c1752018-08-09 15:47:16 -070052
Matteo Scandoloea529092018-09-11 16:36:39 -070053 if subscriber:
54 self.update_subscriber(subscriber, si)
Matteo Scandoload0c1752018-08-09 15:47:16 -070055
Matteo Scandoloea529092018-09-11 16:36:39 -070056 si.save()
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070057
Matteo Scandoloea529092018-09-11 16:36:39 -070058 def validate_onu_state(self, si):
59 [valid, message] = AttHelpers.validate_onu(si)
60 si.status_message += self.separator + message
61 if valid:
62 si.onu_state = "ENABLED"
63 self.update_onu(si.serial_number, "ENABLED")
64 else:
65 si.onu_state = "DISABLED"
66 self.update_onu(si.serial_number, "DISABLED")
Matteo Scandoload0c1752018-08-09 15:47:16 -070067
Matteo Scandoloea529092018-09-11 16:36:39 -070068 def update_onu(self, serial_number, admin_state):
69 self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
70 onu = ONUDevice.objects.get(serial_number=serial_number)
71 onu.admin_state = admin_state
72 onu.save(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -070073
Matteo Scandoloea529092018-09-11 16:36:39 -070074 def get_subscriber(self, serial_number):
75 try:
76 return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0]
77 except IndexError:
78 # If the subscriber doesn't exist we don't do anything
79 self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
80 return None
Matteo Scandoload0c1752018-08-09 15:47:16 -070081
Matteo Scandoloea529092018-09-11 16:36:39 -070082 def update_subscriber(self, subscriber, si):
83 if si.authentication_state == "AWAITING":
84 subscriber.status = "awaiting-auth"
85 si.status_message += self.separator + "Awaiting Authentication"
86 elif si.authentication_state == "REQUESTED":
87 subscriber.status = "awaiting-auth"
88 si.status_message += self.separator + "Authentication requested"
89 elif si.authentication_state == "STARTED":
90 subscriber.status = "awaiting-auth"
91 si.status_message += self.separator + "Authentication started"
92 elif si.authentication_state == "APPROVED":
93 subscriber.status = "enabled"
94 si.status_message += self.separator + "Authentication succeded"
95 elif si.authentication_state == "DENIED":
96 subscriber.status = "auth-failed"
97 si.status_message += self.separator + "Authentication denied"
98 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 -070099
Matteo Scandoloea529092018-09-11 16:36:39 -0700100 subscriber.save(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700101
102 def handle_delete(self, si):
103 pass