Illyoung Choi | 5d59ab6 | 2019-06-24 16:15:27 -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 | def validate_onu(model_accessor, logging, att_si): |
| 18 | """ |
| 19 | This method validate an ONU against the whitelist and set the appropriate state. |
| 20 | It's expected that the deferred exception is managed in the caller method, |
| 21 | for example a model_policy or a sync_step. |
| 22 | |
| 23 | :param att_si: AttWorkflowDriverServiceInstance |
| 24 | :return: [boolean, string] |
| 25 | """ |
| 26 | |
| 27 | oss_service = att_si.owner.leaf_model |
| 28 | |
| 29 | # See if there is a matching entry in the whitelist. |
| 30 | matching_entries = model_accessor.AttWorkflowDriverWhiteListEntry.objects.filter( |
| 31 | owner_id=oss_service.id, |
| 32 | ) |
| 33 | matching_entries = [e for e in matching_entries if e.serial_number.lower() == att_si.serial_number.lower()] |
| 34 | |
| 35 | if len(matching_entries) == 0: |
| 36 | logging.warn("ONU not found in whitelist", object=str(att_si), serial_number=att_si.serial_number, **att_si.tologdict()) |
| 37 | return [False, "ONU not found in whitelist"] |
| 38 | |
| 39 | whitelisted = matching_entries[0] |
| 40 | try: |
| 41 | onu = model_accessor.ONUDevice.objects.get(serial_number=att_si.serial_number) |
| 42 | pon_port = onu.pon_port |
| 43 | except IndexError: |
| 44 | raise DeferredException("ONU device %s is not know to XOS yet" % att_si.serial_number) |
| 45 | |
| 46 | if onu.admin_state == "ADMIN_DISABLED": |
| 47 | return [False, "ONU has been manually disabled"] |
| 48 | |
| 49 | if pon_port.port_no != whitelisted.pon_port_id or att_si.of_dpid != whitelisted.device_id: |
| 50 | logging.warn("ONU disable as location don't match", |
| 51 | object=str(att_si), |
| 52 | serial_number=att_si.serial_number, |
| 53 | pon_port=pon_port.port_no, |
| 54 | whitelisted_pon_port=whitelisted.pon_port_id, |
| 55 | device_id=att_si.of_dpid, |
| 56 | whitelisted_device_id=whitelisted.device_id, |
| 57 | **att_si.tologdict()) |
| 58 | return [False, "ONU activated in wrong location"] |
| 59 | |
| 60 | return [True, "ONU has been validated"] |
| 61 | |
| 62 | def find_or_create_att_si(model_accessor, logging, event): |
| 63 | try: |
| 64 | att_si = model_accessor.AttWorkflowDriverServiceInstance.objects.get( |
| 65 | serial_number=event["serialNumber"] |
| 66 | ) |
| 67 | logging.debug("AttHelpers: Found existing AttWorkflowDriverServiceInstance", si=att_si) |
| 68 | except IndexError: |
| 69 | # create an AttWorkflowDriverServiceInstance, the validation will be |
| 70 | # triggered in the corresponding sync step |
| 71 | att_si = model_accessor.AttWorkflowDriverServiceInstance( |
| 72 | serial_number=event["serialNumber"], |
| 73 | of_dpid=event["deviceId"], |
| 74 | uni_port_id=long(event["portNumber"]), |
| 75 | # we assume there is only one AttWorkflowDriverService |
| 76 | owner=model_accessor.AttWorkflowDriverService.objects.first() |
| 77 | ) |
| 78 | logging.debug("AttHelpers: Created new AttWorkflowDriverServiceInstance", si=att_si) |
| 79 | return att_si |