Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -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 | from xossynchronizer.event_steps.eventstep import EventStep |
| 19 | |
| 20 | class ONUEventStep(EventStep): |
| 21 | topics = ["onu.events"] |
| 22 | technology = "kafka" |
| 23 | |
| 24 | max_onu_retry = 50 |
| 25 | |
| 26 | def __init__(self, *args, **kwargs): |
| 27 | super(ONUEventStep, self).__init__(*args, **kwargs) |
| 28 | |
| 29 | def get_tt_si(self, event): |
| 30 | try: |
| 31 | tt_si = self.model_accessor.TtWorkflowDriverServiceInstance.objects.get(serial_number=event["serial_number"]) |
| 32 | tt_si.no_sync = False; |
| 33 | tt_si.uni_port_id = event["uni_port_id"] |
| 34 | tt_si.of_dpid = event["of_dpid"] |
| 35 | self.log.debug("onu.events: Found existing TtWorkflowDriverServiceInstance", si=tt_si) |
| 36 | except IndexError: |
| 37 | # create an TtWorkflowDriverServiceInstance, the validation will be triggered in the corresponding sync step |
| 38 | tt_si = self.model_accessor.TtWorkflowDriverServiceInstance( |
| 39 | serial_number=event["serial_number"], |
| 40 | of_dpid=event["of_dpid"], |
| 41 | uni_port_id=event["uni_port_id"], |
| 42 | owner=self.model_accessor.TtWorkflowDriverService.objects.first() # we assume there is only one TtWorkflowDriverService |
| 43 | ) |
| 44 | self.log.debug("onu.events: Created new TtWorkflowDriverServiceInstance", si=tt_si) |
| 45 | return tt_si |
| 46 | |
| 47 | def process_event(self, event): |
| 48 | value = json.loads(event.value) |
| 49 | self.log.info("onu.events: received event", value=value) |
| 50 | |
| 51 | if value["status"] == "activated": |
| 52 | self.log.info("onu.events: activated onu", value=value) |
| 53 | tt_si = self.get_tt_si(value) |
| 54 | tt_si.onu_state = "ENABLED" |
| 55 | tt_si.save_changed_fields(always_update_timestamp=True) |
| 56 | elif value["status"] == "disabled": |
| 57 | self.log.info("onu.events: disabled onu, not taking any action", value=value) |
| 58 | return |
| 59 | else: |
| 60 | self.log.warn("onu.events: Unknown status value: %s" % value["status"], value=value) |
| 61 | return |