Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
| 17 | import json |
| 18 | import time |
| 19 | import os |
| 20 | import sys |
| 21 | from synchronizers.new_base.eventstep import EventStep |
| 22 | from synchronizers.new_base.modelaccessor import AttWorkflowDriverServiceInstance, model_accessor |
| 23 | |
| 24 | class ONUEventStep(EventStep): |
| 25 | topics = ["onu.events"] |
| 26 | technology = "kafka" |
| 27 | |
| 28 | max_onu_retry = 50 |
| 29 | |
| 30 | def __init__(self, *args, **kwargs): |
| 31 | super(ONUEventStep, self).__init__(*args, **kwargs) |
| 32 | |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame^] | 33 | def get_att_si(self, event): |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 34 | try: |
| 35 | att_si = AttWorkflowDriverServiceInstance.objects.get(serial_number=event["serial_number"]) |
| 36 | att_si.no_sync = False; |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame^] | 37 | att_si.uni_port_id = event["uni_port_id"] |
| 38 | att_si.of_dpid = event["of_dpid"] |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 39 | self.log.debug("onu.events: Found existing AttWorkflowDriverServiceInstance", si=att_si) |
| 40 | except IndexError: |
| 41 | # create an AttWorkflowDriverServiceInstance, the validation will be triggered in the corresponding sync step |
| 42 | att_si = AttWorkflowDriverServiceInstance( |
| 43 | serial_number=event["serial_number"], |
Matteo Scandolo | 53da44c | 2018-08-14 16:04:18 -0700 | [diff] [blame] | 44 | of_dpid=event["of_dpid"], |
| 45 | uni_port_id=event["uni_port_id"] |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 46 | ) |
| 47 | self.log.debug("onu.events: Created new AttWorkflowDriverServiceInstance", si=att_si) |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame^] | 48 | return att_si |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 49 | |
| 50 | def process_event(self, event): |
| 51 | value = json.loads(event.value) |
| 52 | self.log.info("onu.events: received event", value=value) |
| 53 | |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame^] | 54 | att_si = self.get_att_si(value) |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 55 | if value["status"] == "activated": |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame^] | 56 | self.log.info("onu.events: activated onu", value=value) |
| 57 | att_si.onu_state = "ACTIVE" |
| 58 | elif value["status"] == "disabled": |
| 59 | self.log.info("onu.events: disabled onu", value=value) |
| 60 | att_si.onu_state = "DISABLED" |
| 61 | att_si.save(always_update_timestamp=True) |
| 62 | |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 63 | |