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 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 15 | from xossynchronizer.steps.syncstep import DeferredException |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 16 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 17 | class AttHelpers(): |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 18 | @staticmethod |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 19 | def validate_onu(model_accessor, log, att_si): |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 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 att_si: AttWorkflowDriverServiceInstance |
| 26 | :return: [boolean, string] |
| 27 | """ |
| 28 | |
| 29 | oss_service = att_si.owner.leaf_model |
| 30 | |
| 31 | # See if there is a matching entry in the whitelist. |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 32 | matching_entries = model_accessor.AttWorkflowDriverWhiteListEntry.objects.filter( |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 33 | owner_id=oss_service.id, |
| 34 | ) |
| 35 | matching_entries = [e for e in matching_entries if e.serial_number.lower() == att_si.serial_number.lower()] |
| 36 | |
| 37 | if len(matching_entries) == 0: |
| 38 | log.warn("ONU not found in whitelist", object=str(att_si), serial_number=att_si.serial_number, **att_si.tologdict()) |
| 39 | return [False, "ONU not found in whitelist"] |
| 40 | |
| 41 | whitelisted = matching_entries[0] |
| 42 | try: |
Matteo Scandolo | 2d9f40d | 2019-04-19 08:38:10 -0700 | [diff] [blame] | 43 | onu = model_accessor.ONUDevice.objects.get(serial_number=att_si.serial_number) |
| 44 | pon_port = onu.pon_port |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 45 | except IndexError: |
| 46 | raise DeferredException("ONU device %s is not know to XOS yet" % att_si.serial_number) |
| 47 | |
Andy Bavier | e02c07f | 2019-05-15 12:56:34 -0700 | [diff] [blame] | 48 | if onu.admin_state == "ADMIN_DISABLED": |
| 49 | return [False, "ONU has been manually disabled"] |
| 50 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 51 | 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] | 52 | log.warn("ONU disable as location don't match", |
| 53 | object=str(att_si), |
| 54 | serial_number=att_si.serial_number, |
| 55 | pon_port=pon_port.port_no, |
| 56 | whitelisted_pon_port=whitelisted.pon_port_id, |
| 57 | device_id=att_si.of_dpid, |
| 58 | whitelisted_device_id=whitelisted.device_id, |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 59 | **att_si.tologdict()) |
| 60 | return [False, "ONU activated in wrong location"] |
| 61 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 62 | return [True, "ONU has been validated"] |
| 63 | |
| 64 | @staticmethod |
Matteo Scandolo | 59e10fc | 2019-04-18 14:19:52 -0700 | [diff] [blame] | 65 | def find_or_create_att_si(model_accessor, log, event): |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 66 | try: |
Matteo Scandolo | 59e10fc | 2019-04-18 14:19:52 -0700 | [diff] [blame] | 67 | att_si = model_accessor.AttWorkflowDriverServiceInstance.objects.get( |
| 68 | serial_number=event["serialNumber"] |
| 69 | ) |
| 70 | log.debug("AttHelpers: Found existing AttWorkflowDriverServiceInstance", si=att_si) |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 71 | except IndexError: |
Matteo Scandolo | 59e10fc | 2019-04-18 14:19:52 -0700 | [diff] [blame] | 72 | # create an AttWorkflowDriverServiceInstance, the validation will be |
| 73 | # triggered in the corresponding sync step |
| 74 | att_si = model_accessor.AttWorkflowDriverServiceInstance( |
| 75 | serial_number=event["serialNumber"], |
| 76 | of_dpid=event["deviceId"], |
| 77 | uni_port_id=long(event["portNumber"]), |
| 78 | # we assume there is only one AttWorkflowDriverService |
| 79 | owner=model_accessor.AttWorkflowDriverService.objects.first() |
| 80 | ) |
| 81 | log.debug("AttHelpers: Created new AttWorkflowDriverServiceInstance", si=att_si) |
| 82 | return att_si |