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 |
| 16 | from synchronizers.new_base.modelaccessor import AttWorkflowDriverWhiteListEntry, ONUDevice, model_accessor |
| 17 | |
| 18 | from xosconfig import Config |
| 19 | from multistructlog import create_logger |
| 20 | |
| 21 | log = create_logger(Config().get('logging')) |
| 22 | |
| 23 | class AttHelpers(): |
| 24 | |
| 25 | @staticmethod |
| 26 | def validate_onu(att_si): |
| 27 | """ |
| 28 | This method validate an ONU against the whitelist and set the appropriate state. |
| 29 | It's expected that the deferred exception is managed in the caller method, |
| 30 | for example a model_policy or a sync_step. |
| 31 | |
| 32 | :param att_si: AttWorkflowDriverServiceInstance |
| 33 | :return: [boolean, string] |
| 34 | """ |
| 35 | |
| 36 | oss_service = att_si.owner.leaf_model |
| 37 | |
| 38 | # See if there is a matching entry in the whitelist. |
| 39 | matching_entries = AttWorkflowDriverWhiteListEntry.objects.filter( |
| 40 | owner_id=oss_service.id, |
| 41 | ) |
| 42 | matching_entries = [e for e in matching_entries if e.serial_number.lower() == att_si.serial_number.lower()] |
| 43 | |
| 44 | if len(matching_entries) == 0: |
| 45 | log.warn("ONU not found in whitelist", object=str(att_si), serial_number=att_si.serial_number, **att_si.tologdict()) |
| 46 | return [False, "ONU not found in whitelist"] |
| 47 | |
| 48 | whitelisted = matching_entries[0] |
| 49 | try: |
| 50 | pon_port = ONUDevice.objects.get(serial_number=att_si.serial_number).pon_port |
| 51 | except IndexError: |
| 52 | raise DeferredException("ONU device %s is not know to XOS yet" % att_si.serial_number) |
| 53 | |
| 54 | if pon_port.port_no != whitelisted.pon_port_id or att_si.of_dpid != whitelisted.device_id: |
| 55 | log.warn("ONU disable as location don't match", object=str(att_si), serial_number=att_si.serial_number, |
| 56 | **att_si.tologdict()) |
| 57 | return [False, "ONU activated in wrong location"] |
| 58 | |
| 59 | return [True, "ONU has been validated"] |