Takahiro Suzuki | 2b66b94 | 2020-12-17 11:58:14 +0900 | [diff] [blame^] | 1 | # Copyright 2020-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 | import json |
| 16 | from xossynchronizer.steps.syncstep import DeferredException |
| 17 | import time |
| 18 | import requests |
| 19 | from requests.auth import HTTPBasicAuth |
| 20 | |
| 21 | class NttHelpers(): |
| 22 | @staticmethod |
| 23 | def validate_onu(model_accessor, log, ntt_si): |
| 24 | """ |
| 25 | This method validate an ONU against the whitelist and set the appropriate state. |
| 26 | It's expected that the deferred exception is managed in the caller method, |
| 27 | for example a model_policy or a sync_step. |
| 28 | |
| 29 | :param ntt_si: NttWorkflowDriverServiceInstance |
| 30 | :return: [boolean, string] |
| 31 | """ |
| 32 | tech_value = json.loads(model_accessor.TechnologyProfile.objects.get(profile_id=64).profile_value) |
| 33 | if tech_value["profile_type"] == "EPON": |
| 34 | tech = tech_value["epon_attribute"]["package_type"] |
| 35 | else: |
| 36 | tech = tech_value["profile_type"] |
| 37 | |
| 38 | oss_service = ntt_si.owner.leaf_model |
| 39 | |
| 40 | # See if there is a matching entry in the whitelist. |
| 41 | matching_entries = model_accessor.NttWorkflowDriverWhiteListEntry.objects.filter( |
| 42 | owner_id=oss_service.id, |
| 43 | ) |
| 44 | |
| 45 | matching_entries = [e for e in matching_entries if e.mac_address.lower() == ntt_si.mac_address.lower()] |
| 46 | |
| 47 | if len(matching_entries) == 0: |
| 48 | log.warn("ONU not found in whitelist") |
| 49 | return [False, "ONU not found in whitelist"] |
| 50 | |
| 51 | whitelisted = matching_entries[0] |
| 52 | try: |
| 53 | onu = model_accessor.ONUDevice.objects.get(serial_number=ntt_si.serial_number.split("-")[0]) |
| 54 | pon_port = onu.pon_port |
| 55 | except IndexError: |
| 56 | raise DeferredException("ONU device %s is not know to XOS yet" % ntt_si.serial_number) |
| 57 | |
| 58 | if onu.admin_state == "ADMIN_DISABLED": |
| 59 | return [False, "ONU has been manually disabled"] |
| 60 | |
| 61 | if pon_port.port_no < whitelisted.pon_port_from or pon_port.port_no > whitelisted.pon_port_to: |
| 62 | log.warn("PON port is not approved.") |
| 63 | return [False, "PON port is not approved."] |
| 64 | |
| 65 | if tech == "B": |
| 66 | if ntt_si.authentication_state == "DENIED": |
| 67 | return [False, "IEEE802.1X authentication has not been denied."] |
| 68 | elif ntt_si.authentication_state != "APPROVED": |
| 69 | return [True, "IEEE802.1X authentication has not been done yet."] |
| 70 | else: |
| 71 | pass |
| 72 | |
| 73 | log.debug("Adding subscriber with info", |
| 74 | uni_port_id = ntt_si.uni_port_id, |
| 75 | dp_id = ntt_si.of_dpid |
| 76 | ) |
| 77 | |
| 78 | time.sleep(180) |
| 79 | |
| 80 | onos_voltha_basic_auth = HTTPBasicAuth("karaf", "karaf") |
| 81 | |
| 82 | handle = "%s/%s" % (ntt_si.of_dpid, ntt_si.uni_port_id) |
| 83 | # TODO store URL and PORT in the vOLT Service model |
| 84 | full_url = "http://129.60.110.180:8181/onos/olt/oltapp/%s" % (handle) |
| 85 | |
| 86 | log.info("Sending request to onos-voltha", url=full_url) |
| 87 | |
| 88 | request = requests.post(full_url, auth=onos_voltha_basic_auth) |
| 89 | |
| 90 | if request.status_code != 200: |
| 91 | raise Exception("Failed to add subscriber in onos-voltha: %s" % request.text) |
| 92 | log.info("Added Subscriber in onos voltha", response=request.text) |
| 93 | |
| 94 | return [True, "ONU has been validated"] |
| 95 | |
| 96 | @staticmethod |
| 97 | def find_or_create_ntt_si(model_accessor, log, event): |
| 98 | try: |
| 99 | ntt_si = model_accessor.NttWorkflowDriverServiceInstance.objects.get( |
| 100 | serial_number=event["serialNumber"] |
| 101 | ) |
| 102 | try: |
| 103 | onu = model_accessor.ONUDevice.objects.get(serial_number=event["serialNumber"].split("-")[0]) |
| 104 | ntt_si.mac_address = onu.mac_address |
| 105 | except IndexError: |
| 106 | log.debug("NttHelpers: ONU has been deleted", si=ntt_si) |
| 107 | log.debug("NttHelpers: Found existing NttWorkflowDriverServiceInstance", si=ntt_si) |
| 108 | except IndexError: |
| 109 | # create an NttWorkflowDriverServiceInstance, the validation will be |
| 110 | # triggered in the corresponding sync step |
| 111 | while True: |
| 112 | try: |
| 113 | onu = model_accessor.ONUDevice.objects.get(serial_number=event["serialNumber"].split("-")[0]) |
| 114 | break |
| 115 | except IndexError: |
| 116 | time.sleep(1) |
| 117 | continue |
| 118 | |
| 119 | ntt_si = model_accessor.NttWorkflowDriverServiceInstance( |
| 120 | serial_number=event["serialNumber"], |
| 121 | of_dpid=event["deviceId"], |
| 122 | uni_port_id=long(event["portNumber"]), |
| 123 | mac_address=onu.mac_address, |
| 124 | # we assume there is only one NttWorkflowDriverService |
| 125 | owner=model_accessor.NttWorkflowDriverService.objects.first() |
| 126 | ) |
| 127 | log.debug("NttHelpers: Created new NttWorkflowDriverServiceInstance", si=ntt_si) |
| 128 | return ntt_si |
| 129 | |
| 130 | @staticmethod |
| 131 | def find_or_create_ntt_oi(model_accessor, log, event): |
| 132 | try: |
| 133 | ntt_oi = model_accessor.NttWorkflowDriverOltInformation.objects.get( |
| 134 | of_dpid=event["deviceId"] |
| 135 | ) |
| 136 | try: |
| 137 | onu = model_accessor.ONUDevice.objects.get(serial_number=event["serialNumber"].split("-")[0]) |
| 138 | ntt_oi.port_no = onu.pon_port.port_no |
| 139 | tech_value = json.loads(model_accessor.TechnologyProfile.objects.get(profile_id=64).profile_value) |
| 140 | if tech_value["profile_type"] == "EPON": |
| 141 | tech = tech_value["epon_attribute"]["package_type"] |
| 142 | else: |
| 143 | tech = tech_value["profile_type"] |
| 144 | ntt_oi.olt_package = tech |
| 145 | except IndexError: |
| 146 | log.debug("NttHelpers: ONU has been deleted", oi=ntt_oi) |
| 147 | log.debug("NttHelpers: Found existing NttWorkflowDriverOltInformation", oi=ntt_oi) |
| 148 | except IndexError: |
| 149 | while True: |
| 150 | try: |
| 151 | onu = model_accessor.ONUDevice.objects.get(serial_number=event["serialNumber"].split("-")[0]) |
| 152 | break |
| 153 | except IndexError: |
| 154 | time.sleep(1) |
| 155 | continue |
| 156 | |
| 157 | tech_value = json.loads(model_accessor.TechnologyProfile.objects.get(profile_id=64).profile_value) |
| 158 | if tech_value["profile_type"] == "EPON": |
| 159 | tech = tech_value["epon_attribute"]["package_type"] |
| 160 | else: |
| 161 | tech = tech_value["profile_type"] |
| 162 | |
| 163 | pon_port = onu.pon_port |
| 164 | ntt_oi = model_accessor.NttWorkflowDriverOltInformation( |
| 165 | of_dpid=event["deviceId"], |
| 166 | olt_package=tech, |
| 167 | port_no=pon_port.port_no, |
| 168 | owner=model_accessor.NttWorkflowDriverService.objects.first() |
| 169 | ) |
| 170 | log.debug("NttHelpers: Created new NttWorkflowDriverOltInformation", oi=ntt_oi) |
| 171 | return ntt_oi |