blob: d2288908a777d99f23800aee51e4e3f8102c65ed [file] [log] [blame]
Daniele Moro134b8d62020-01-14 11:32:05 -08001# 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
15from xossynchronizer.steps.syncstep import DeferredException
16
17class DtHelpers():
18 @staticmethod
19 def validate_onu(model_accessor, log, dt_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 dt_si: DtWorkflowDriverServiceInstance
26 :return: [boolean, string]
27 """
28
29 oss_service = dt_si.owner.leaf_model
30
31 # See if there is a matching entry in the whitelist.
32 matching_entries = model_accessor.DtWorkflowDriverWhiteListEntry.objects.filter(
33 owner_id=oss_service.id,
34 )
35 matching_entries = [e for e in matching_entries if e.serial_number.lower() == dt_si.serial_number.lower()]
36
37 if len(matching_entries) == 0:
38 log.warn("ONU not found in whitelist", object=str(dt_si), serial_number=dt_si.serial_number, **dt_si.tologdict())
39 return [False, "ONU not found in whitelist"]
40
41 whitelisted = matching_entries[0]
42 try:
Daniele Morof2a7f8e2020-02-19 17:37:34 -080043 onu = model_accessor.ONUDevice.objects.get(serial_number=dt_si.serial_number.split("-")[0])
Daniele Moro134b8d62020-01-14 11:32:05 -080044 pon_port = onu.pon_port
45 except IndexError:
46 raise DeferredException("ONU device %s is not know to XOS yet" % dt_si.serial_number)
47
48 if onu.admin_state == "ADMIN_DISABLED":
49 return [False, "ONU has been manually disabled"]
50
51 if pon_port.port_no != whitelisted.pon_port_id or dt_si.of_dpid != whitelisted.device_id:
52 log.warn("ONU disable as location don't match",
53 object=str(dt_si),
54 serial_number=dt_si.serial_number,
55 pon_port=pon_port.port_no,
56 whitelisted_pon_port=whitelisted.pon_port_id,
57 device_id=dt_si.of_dpid,
58 whitelisted_device_id=whitelisted.device_id,
59 **dt_si.tologdict())
60 return [False, "ONU activated in wrong location"]
61
62 return [True, "ONU has been validated"]
63
64 @staticmethod
65 def find_or_create_dt_si(model_accessor, log, event):
66 try:
67 dt_si = model_accessor.DtWorkflowDriverServiceInstance.objects.get(
Daniele Morof2a7f8e2020-02-19 17:37:34 -080068 # FIXME: in this way we support a single UNI port per ONU
69 serial_number=event["serialNumber"].split("-")[0]
Daniele Moro134b8d62020-01-14 11:32:05 -080070 )
71 log.debug("DtHelpers: Found existing DtWorkflowDriverServiceInstance", si=dt_si)
72 except IndexError:
73 # create an DtWorkflowDriverServiceInstance, the validation will be
74 # triggered in the corresponding sync step
75 dt_si = model_accessor.DtWorkflowDriverServiceInstance(
76 serial_number=event["serialNumber"],
77 of_dpid=event["deviceId"],
78 uni_port_id=long(event["portNumber"]),
79 # we assume there is only one DtWorkflowDriverService
80 owner=model_accessor.DtWorkflowDriverService.objects.first()
81 )
82 log.debug("DtHelpers: Created new DtWorkflowDriverServiceInstance", si=dt_si)
83 return dt_si