blob: 602aded52352def840452c4a8c7055dc43905faf [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 Scandolo74f63302018-11-01 14:05:01 -070018from synchronizers.new_base.modelaccessor import RCORDSubscriber, RCORDIpAddress, ONUDevice, model_accessor
Matteo Scandoload0c1752018-08-09 15:47:16 -070019from 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:
Matteo Scandoloc5c558f2018-11-02 10:33:40 -070048 # clean the status and verify that the ONU is actually disabled
Matteo Scandolode8cfa82018-10-16 13:49:05 -070049 si.status_message = "ONU has been disabled"
Matteo Scandoloc5c558f2018-11-02 10:33:40 -070050 self.update_onu(si.serial_number, "DISABLED")
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070051
Matteo Scandoloea529092018-09-11 16:36:39 -070052 # handling the subscriber status
53 subscriber = self.get_subscriber(si.serial_number)
Matteo Scandoload0c1752018-08-09 15:47:16 -070054
Matteo Scandoloea529092018-09-11 16:36:39 -070055 if subscriber:
56 self.update_subscriber(subscriber, si)
Matteo Scandoload0c1752018-08-09 15:47:16 -070057
Andy Bavier0d631eb2018-10-17 18:05:04 -070058 si.save_changed_fields()
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070059
Matteo Scandoloea529092018-09-11 16:36:39 -070060 def validate_onu_state(self, si):
Scott Baker0b4ad932018-11-26 15:02:13 -080061 [valid, message] = AttHelpers.validate_onu(self.logger, si)
Matteo Scandolo200dab42018-09-12 13:52:09 -070062 si.status_message = message
Matteo Scandoloea529092018-09-11 16:36:39 -070063 if valid:
64 si.onu_state = "ENABLED"
65 self.update_onu(si.serial_number, "ENABLED")
66 else:
67 si.onu_state = "DISABLED"
68 self.update_onu(si.serial_number, "DISABLED")
Matteo Scandoload0c1752018-08-09 15:47:16 -070069
Matteo Scandoloea529092018-09-11 16:36:39 -070070 def update_onu(self, serial_number, admin_state):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -070071 onu = [onu for onu in ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0]
72 if onu.admin_state == admin_state:
73 self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state))
74 else:
75 self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state))
76 onu.admin_state = admin_state
Andy Bavier0d631eb2018-10-17 18:05:04 -070077 onu.save_changed_fields(always_update_timestamp=True)
Matteo Scandoload0c1752018-08-09 15:47:16 -070078
Matteo Scandoloea529092018-09-11 16:36:39 -070079 def get_subscriber(self, serial_number):
80 try:
81 return [s for s in RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0]
82 except IndexError:
83 # If the subscriber doesn't exist we don't do anything
84 self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number)
85 return None
Matteo Scandoload0c1752018-08-09 15:47:16 -070086
Matteo Scandolo74f63302018-11-01 14:05:01 -070087 def update_subscriber_ip(self, subscriber, ip):
88 # TODO check if the subscriber has an IP and update it,
89 # or create a new one
90 try:
91 ip = RCORDIpAddress.objects.filter(
92 subscriber_id=subscriber.id,
93 ip=ip
94 )[0]
95 self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
96 ip.save_changed_fields()
97 except IndexError:
98 self.logger.debug("MODEL_POLICY: Creating new RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip)
99 ip = RCORDIpAddress(
100 subscriber_id=subscriber.id,
101 ip=ip,
102 description="DHCP Assigned IP Address"
103 )
104 ip.save()
105
Matteo Scandoloea529092018-09-11 16:36:39 -0700106 def update_subscriber(self, subscriber, si):
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700107 cur_status = subscriber.status
Matteo Scandoloea529092018-09-11 16:36:39 -0700108 if si.authentication_state == "AWAITING":
109 subscriber.status = "awaiting-auth"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700110 si.status_message += " - Awaiting Authentication"
Matteo Scandoloea529092018-09-11 16:36:39 -0700111 elif si.authentication_state == "REQUESTED":
112 subscriber.status = "awaiting-auth"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700113 si.status_message += " - Authentication requested"
Matteo Scandoloea529092018-09-11 16:36:39 -0700114 elif si.authentication_state == "STARTED":
115 subscriber.status = "awaiting-auth"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700116 si.status_message += " - Authentication started"
Matteo Scandoloea529092018-09-11 16:36:39 -0700117 elif si.authentication_state == "APPROVED":
118 subscriber.status = "enabled"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700119 si.status_message += " - Authentication succeded"
Matteo Scandoloea529092018-09-11 16:36:39 -0700120 elif si.authentication_state == "DENIED":
121 subscriber.status = "auth-failed"
Matteo Scandolo075ec442018-09-13 14:19:02 -0700122 si.status_message += " - Authentication denied"
Matteo Scandoload0c1752018-08-09 15:47:16 -0700123
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700124 # NOTE we save the subscriber only if:
125 # - the status has changed
126 # - we get a DHCPACK event
127 if cur_status != subscriber.status or si.dhcp_state == "DHCPACK":
128 self.logger.debug("MODEL_POLICY: updating subscriber", onu_device=subscriber.onu_device, authentication_state=si.authentication_state, subscriber_status=subscriber.status)
129 if si.ip_address and si.mac_address:
Matteo Scandolo74f63302018-11-01 14:05:01 -0700130 # subscriber.ip_address = si.ip_address
131 self.update_subscriber_ip(subscriber, si.ip_address)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700132 subscriber.mac_address = si.mac_address
Andy Bavier0d631eb2018-10-17 18:05:04 -0700133 subscriber.save_changed_fields(always_update_timestamp=True)
Matteo Scandolode8cfa82018-10-16 13:49:05 -0700134 else:
Matteo Scandoloc6ac74a2018-09-14 08:14:51 -0700135 self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device,
136 authentication_state=si.authentication_state, subscriber_status=subscriber.status)
Matteo Scandoload0c1752018-08-09 15:47:16 -0700137
138 def handle_delete(self, si):
139 pass