blob: 692306299c3bbdf0b3bbb0affadc9f4e93022ec4 [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -07001# 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
15import json
16from synchronizers.new_base.syncstep import SyncStep, model_accessor
Matteo Scandolo53da44c2018-08-14 16:04:18 -070017from synchronizers.new_base.modelaccessor import AttWorkflowDriverServiceInstance, AttWorkflowDriverWhiteListEntry, ONUDevice
Matteo Scandoload0c1752018-08-09 15:47:16 -070018
19from xosconfig import Config
20from multistructlog import create_logger
21
22log = create_logger(Config().get('logging'))
23
24class SyncAttWorkflowDriverServiceInstance(SyncStep):
25 provides = [AttWorkflowDriverServiceInstance]
26 observes = AttWorkflowDriverServiceInstance
27
Matteo Scandolo53da44c2018-08-14 16:04:18 -070028 def validate_onu(self, si):
Matteo Scandoload0c1752018-08-09 15:47:16 -070029 # This is where you may want to call your OSS Database to verify if this ONU can be activated
30 oss_service = si.owner.leaf_model
31
32 # See if there is a matching entry in the whitelist.
33
34 matching_entries = AttWorkflowDriverWhiteListEntry.objects.filter(owner_id=oss_service.id,
35 serial_number=si.serial_number)
36
Matteo Scandolo53da44c2018-08-14 16:04:18 -070037 # check that it's in the whitelist
38 if len(matching_entries) == 0:
39 log.warn("ONU disable as not in whitelist", object=str(si), serial_number=si.serial_number, **si.tologdict())
40 return False
Matteo Scandoload0c1752018-08-09 15:47:16 -070041
Matteo Scandolo53da44c2018-08-14 16:04:18 -070042 whitelisted = matching_entries[0]
Matteo Scandoloe8c33d62018-08-16 14:37:24 -070043
44 # FIXME if the ONU is not there yet it raise an index error, if that happens raise DeferredException
Matteo Scandolo9d927422018-08-15 14:50:50 -070045 pon_port = ONUDevice.objects.get(serial_number=si.serial_number).pon_port
Matteo Scandolo53da44c2018-08-14 16:04:18 -070046 if pon_port.port_no != whitelisted.pon_port_id or si.of_dpid != whitelisted.device_id:
47 log.warn("ONU disable as location don't match", object=str(si), serial_number=si.serial_number,
48 **si.tologdict())
49 return False
Matteo Scandoload0c1752018-08-09 15:47:16 -070050
Matteo Scandolo53da44c2018-08-14 16:04:18 -070051 return True
Matteo Scandoload0c1752018-08-09 15:47:16 -070052
Matteo Scandolo53da44c2018-08-14 16:04:18 -070053 def sync_record(self, si):
54 log.info("synching AttWorkflowDriverServiceInstance", object=str(si), **si.tologdict())
55
56 if not self.validate_onu(si):
57 log.error("ONU with serial number %s is not valid in the OSS Database" % si.serial_number)
58 si.valid = "invalid"
Matteo Scandoload0c1752018-08-09 15:47:16 -070059 else:
Matteo Scandolo53da44c2018-08-14 16:04:18 -070060 si.valid = "valid"
Matteo Scandoload0c1752018-08-09 15:47:16 -070061
Matteo Scandolo53da44c2018-08-14 16:04:18 -070062 si.save()
Matteo Scandoload0c1752018-08-09 15:47:16 -070063
64 def delete_record(self, o):
65 pass