Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -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 xossynchronizer.steps.syncstep import DeferredException |
| 16 | |
| 17 | class TtHelpers(): |
| 18 | @staticmethod |
| 19 | def validate_onu(model_accessor, log, tt_si): |
| 20 | """ |
| 21 | This method validate an ONU against the whitelist and set the appropriate state. |
| 22 | It's expected that the deferred exception is managed in the caller method, |
| 23 | for example a model_policy or a sync_step. |
| 24 | |
| 25 | :param tt_si: TtWorkflowDriverServiceInstance |
| 26 | :return: [boolean, string] |
| 27 | """ |
| 28 | |
| 29 | oss_service = tt_si.owner.leaf_model |
| 30 | |
| 31 | # See if there is a matching entry in the whitelist. |
| 32 | matching_entries = model_accessor.TtWorkflowDriverWhiteListEntry.objects.filter( |
| 33 | owner_id=oss_service.id, |
| 34 | ) |
| 35 | matching_entries = [e for e in matching_entries if e.serial_number.lower() == tt_si.serial_number.lower()] |
| 36 | |
| 37 | if len(matching_entries) == 0: |
| 38 | log.warn("ONU not found in whitelist", object=str(tt_si), serial_number=tt_si.serial_number, **tt_si.tologdict()) |
| 39 | return [False, "ONU not found in whitelist"] |
| 40 | |
| 41 | whitelisted = matching_entries[0] |
| 42 | try: |
| 43 | pon_port = model_accessor.ONUDevice.objects.get(serial_number=tt_si.serial_number).pon_port |
| 44 | except IndexError: |
| 45 | raise DeferredException("ONU device %s is not know to XOS yet" % tt_si.serial_number) |
| 46 | |
| 47 | if pon_port.port_no != whitelisted.pon_port_id or tt_si.of_dpid != whitelisted.device_id: |
| 48 | log.warn("ONU disable as location don't match", |
| 49 | object=str(tt_si), |
| 50 | serial_number=tt_si.serial_number, |
| 51 | pon_port=pon_port.port_no, |
| 52 | whitelisted_pon_port=whitelisted.pon_port_id, |
| 53 | device_id=tt_si.of_dpid, |
| 54 | whitelisted_device_id=whitelisted.device_id, |
| 55 | **tt_si.tologdict()) |
| 56 | return [False, "ONU activated in wrong location"] |
| 57 | |
| 58 | return [True, "ONU has been validated"] |
| 59 | |
| 60 | @staticmethod |
| 61 | def get_onu_sn(model_accessor, log, event): |
| 62 | olt_service = model_accessor.VOLTService.objects.first() |
| 63 | onu_sn = olt_service.get_onu_sn_from_openflow(event["deviceId"], event["portNumber"]) |
| 64 | if not onu_sn or onu_sn is None: |
| 65 | log.exception("Cannot find onu serial number for this event", kafka_event=event) |
| 66 | raise Exception("Cannot find onu serial number for this event") |
| 67 | |
| 68 | return onu_sn |
| 69 | |
| 70 | @staticmethod |
| 71 | def get_si_by_sn(model_accessor, log, serial_number): |
| 72 | try: |
| 73 | return model_accessor.TtWorkflowDriverServiceInstance.objects.get(serial_number=serial_number) |
| 74 | except IndexError: |
| 75 | log.exception("Cannot find tt-workflow-driver service instance for this serial number", serial_number=serial_number) |
| 76 | raise Exception("Cannot find tt-workflow-driver service instance for this serial number %s", serial_number) |