Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from synchronizers.new_base.syncstep import DeferredException |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 16 | from synchronizers.new_base.modelaccessor import AttWorkflowDriverWhiteListEntry, AttWorkflowDriverServiceInstance, ONUDevice, VOLTService, model_accessor |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 17 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 18 | class AttHelpers(): |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 19 | @staticmethod |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 20 | def validate_onu(log, att_si): |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 21 | """ |
| 22 | This method validate an ONU against the whitelist and set the appropriate state. |
| 23 | It's expected that the deferred exception is managed in the caller method, |
| 24 | for example a model_policy or a sync_step. |
| 25 | |
| 26 | :param att_si: AttWorkflowDriverServiceInstance |
| 27 | :return: [boolean, string] |
| 28 | """ |
| 29 | |
| 30 | oss_service = att_si.owner.leaf_model |
| 31 | |
| 32 | # See if there is a matching entry in the whitelist. |
| 33 | matching_entries = AttWorkflowDriverWhiteListEntry.objects.filter( |
| 34 | owner_id=oss_service.id, |
| 35 | ) |
| 36 | matching_entries = [e for e in matching_entries if e.serial_number.lower() == att_si.serial_number.lower()] |
| 37 | |
| 38 | if len(matching_entries) == 0: |
| 39 | log.warn("ONU not found in whitelist", object=str(att_si), serial_number=att_si.serial_number, **att_si.tologdict()) |
| 40 | return [False, "ONU not found in whitelist"] |
| 41 | |
| 42 | whitelisted = matching_entries[0] |
| 43 | try: |
| 44 | pon_port = ONUDevice.objects.get(serial_number=att_si.serial_number).pon_port |
| 45 | except IndexError: |
| 46 | raise DeferredException("ONU device %s is not know to XOS yet" % att_si.serial_number) |
| 47 | |
| 48 | if pon_port.port_no != whitelisted.pon_port_id or att_si.of_dpid != whitelisted.device_id: |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 49 | log.warn("ONU disable as location don't match", |
| 50 | object=str(att_si), |
| 51 | serial_number=att_si.serial_number, |
| 52 | pon_port=pon_port.port_no, |
| 53 | whitelisted_pon_port=whitelisted.pon_port_id, |
| 54 | device_id=att_si.of_dpid, |
| 55 | whitelisted_device_id=whitelisted.device_id, |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 56 | **att_si.tologdict()) |
| 57 | return [False, "ONU activated in wrong location"] |
| 58 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 59 | return [True, "ONU has been validated"] |
| 60 | |
| 61 | @staticmethod |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 62 | def get_onu_sn(log, event): |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 63 | olt_service = VOLTService.objects.first() |
| 64 | onu_sn = olt_service.get_onu_sn_from_openflow(event["deviceId"], event["portNumber"]) |
| 65 | if not onu_sn or onu_sn is None: |
| 66 | log.exception("Cannot find onu serial number for this event", kafka_event=event) |
| 67 | raise Exception("Cannot find onu serial number for this event") |
| 68 | |
| 69 | return onu_sn |
| 70 | |
| 71 | @staticmethod |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 72 | def get_si_by_sn(log, serial_number): |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 73 | try: |
| 74 | return AttWorkflowDriverServiceInstance.objects.get(serial_number=serial_number) |
| 75 | except IndexError: |
| 76 | log.exception("Cannot find att-workflow-driver service instance for this serial number", serial_number=serial_number) |
| 77 | raise Exception("Cannot find att-workflow-driver service instance for this serial number %s", serial_number) |