Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame^] | 1 | |
| 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 | |
| 17 | |
| 18 | from xossynchronizer.model_policies.policy import Policy |
| 19 | |
| 20 | import os |
| 21 | import sys |
| 22 | |
| 23 | sync_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) |
| 24 | sys.path.append(sync_path) |
| 25 | |
| 26 | from helpers import TtHelpers |
| 27 | |
| 28 | class DeferredException(Exception): |
| 29 | pass |
| 30 | |
| 31 | class TtWorkflowDriverServiceInstancePolicy(Policy): |
| 32 | model_name = "TtWorkflowDriverServiceInstance" |
| 33 | |
| 34 | def handle_create(self, si): |
| 35 | self.logger.debug("MODEL_POLICY: handle_create for TtWorkflowDriverServiceInstance %s " % si.id) |
| 36 | self.handle_update(si) |
| 37 | |
| 38 | def handle_update(self, si): |
| 39 | self.logger.debug("MODEL_POLICY: handle_update for TtWorkflowDriverServiceInstance %s " % (si.id), onu_state=si.onu_state) |
| 40 | |
| 41 | self.process_onu_state(si) |
| 42 | self.process_dhcp_state(si) |
| 43 | |
| 44 | self.validate_states(si) |
| 45 | |
| 46 | # handling the subscriber status |
| 47 | # It's a combination of all the other states |
| 48 | subscriber = self.get_subscriber(si.serial_number) |
| 49 | if subscriber: |
| 50 | self.update_subscriber(subscriber, si) |
| 51 | |
| 52 | si.save_changed_fields() |
| 53 | |
| 54 | def process_onu_state(self, si): |
| 55 | [valid, message] = TtHelpers.validate_onu(self.model_accessor, self.logger, si) |
| 56 | if si.onu_state == "AWAITING" or si.onu_state == "ENABLED": |
| 57 | si.status_message = message |
| 58 | if valid: |
| 59 | si.onu_state = "ENABLED" |
| 60 | self.update_onu(si.serial_number, "ENABLED") |
| 61 | else: |
| 62 | si.onu_state = "DISABLED" |
| 63 | self.update_onu(si.serial_number, "DISABLED") |
| 64 | else: # DISABLED |
| 65 | if not valid: |
| 66 | si.status_message = message |
| 67 | else: |
| 68 | si.status_message = "ONU has been disabled" |
| 69 | self.update_onu(si.serial_number, "DISABLED") |
| 70 | |
| 71 | |
| 72 | # The DhcpL2Relay ONOS app generates events that update the fields below. |
| 73 | # It only sends events when it processes DHCP packets. It keeps no internal state. |
| 74 | def process_dhcp_state(self, si): |
| 75 | if si.onu_state in ["AWAITING", "DISABLED"]: |
| 76 | si.ip_address = "" |
| 77 | si.mac_address = "" |
| 78 | si.dhcp_state = "AWAITING" |
| 79 | |
| 80 | # Make sure the object is in a legitimate state |
| 81 | # It should be after the above processing steps |
| 82 | # However this might still fail if an event has fired in the meantime |
| 83 | # Valid states: |
| 84 | # ONU | DHCP |
| 85 | # =============================== |
| 86 | # AWAITING | AWAITING |
| 87 | # ENABLED | * |
| 88 | # DISABLED | AWAITING |
| 89 | def validate_states(self, si): |
| 90 | if (si.onu_state == "AWAITING" or si.onu_state == "DISABLED") and si.dhcp_state == "AWAITING": |
| 91 | return |
| 92 | if si.onu_state == "ENABLED": |
| 93 | return |
| 94 | self.logger.warning("MODEL_POLICY (validate_states): invalid state combination", onu_state=si.onu_state, dhcp_state=si.dhcp_state) |
| 95 | |
| 96 | |
| 97 | def update_onu(self, serial_number, admin_state): |
| 98 | onu = [onu for onu in self.model_accessor.ONUDevice.objects.all() if onu.serial_number.lower() == serial_number.lower()][0] |
| 99 | if onu.admin_state == admin_state: |
| 100 | self.logger.debug("MODEL_POLICY: ONUDevice [%s] already has admin_state to %s" % (serial_number, admin_state)) |
| 101 | else: |
| 102 | self.logger.debug("MODEL_POLICY: setting ONUDevice [%s] admin_state to %s" % (serial_number, admin_state)) |
| 103 | onu.admin_state = admin_state |
| 104 | onu.save_changed_fields(always_update_timestamp=True) |
| 105 | |
| 106 | def get_subscriber(self, serial_number): |
| 107 | try: |
| 108 | return [s for s in self.model_accessor.RCORDSubscriber.objects.all() if s.onu_device.lower() == serial_number.lower()][0] |
| 109 | except IndexError: |
| 110 | # If the subscriber doesn't exist we don't do anything |
| 111 | self.logger.debug("MODEL_POLICY: subscriber does not exists for this SI, doing nothing", onu_device=serial_number) |
| 112 | return None |
| 113 | |
| 114 | def update_subscriber_ip(self, subscriber, ip): |
| 115 | # TODO check if the subscriber has an IP and update it, |
| 116 | # or create a new one |
| 117 | try: |
| 118 | ip = self.model_accessor.RCORDIpAddress.objects.filter( |
| 119 | subscriber_id=subscriber.id, |
| 120 | ip=ip |
| 121 | )[0] |
| 122 | self.logger.debug("MODEL_POLICY: found existing RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip) |
| 123 | ip.save_changed_fields() |
| 124 | except IndexError: |
| 125 | self.logger.debug("MODEL_POLICY: Creating new RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip) |
| 126 | ip = self.model_accessor.RCORDIpAddress( |
| 127 | subscriber_id=subscriber.id, |
| 128 | ip=ip, |
| 129 | description="DHCP Assigned IP Address" |
| 130 | ) |
| 131 | ip.save() |
| 132 | |
| 133 | def delete_subscriber_ip(self, subscriber, ip): |
| 134 | try: |
| 135 | ip = self.model_accessor.RCORDIpAddress.objects.filter( |
| 136 | subscriber_id=subscriber.id, |
| 137 | ip=ip |
| 138 | )[0] |
| 139 | self.logger.debug("MODEL_POLICY: delete RCORDIpAddress for subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status, ip=ip) |
| 140 | ip.delete() |
| 141 | except: |
| 142 | self.logger.warning("MODEL_POLICY: no RCORDIpAddress object found, cannot delete", ip=ip) |
| 143 | |
| 144 | def update_subscriber(self, subscriber, si): |
| 145 | cur_status = subscriber.status |
| 146 | # Don't change state if someone has disabled the subscriber |
| 147 | if subscriber.status != "disabled": |
| 148 | if si.onu_state == "ENABLED": |
| 149 | subscriber.status = "enabled" |
| 150 | elif si.onu_state == "DISABLED": |
| 151 | subscriber.status = "awaiting-auth" |
| 152 | |
| 153 | # NOTE we save the subscriber only if: |
| 154 | # - the status has changed |
| 155 | # - we get a DHCPACK event |
| 156 | if cur_status != subscriber.status or si.dhcp_state == "DHCPACK": |
| 157 | self.logger.debug("MODEL_POLICY: updating subscriber", onu_device=subscriber.onu_device, subscriber_status=subscriber.status) |
| 158 | if subscriber.status == "awaiting-auth": |
| 159 | self.delete_subscriber_ip(subscriber, si.ip_address) |
| 160 | subscriber.mac_address = "" |
| 161 | elif si.ip_address and si.mac_address: |
| 162 | self.update_subscriber_ip(subscriber, si.ip_address) |
| 163 | subscriber.mac_address = si.mac_address |
| 164 | subscriber.save_changed_fields(always_update_timestamp=True) |
| 165 | else: |
| 166 | self.logger.debug("MODEL_POLICY: subscriber status has not changed", onu_device=subscriber.onu_device, |
| 167 | subscriber_status=subscriber.status) |
| 168 | |
| 169 | def handle_delete(self, si): |
| 170 | pass |