Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 1 | # 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 | |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 16 | |
| 17 | import json |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 18 | from xossynchronizer.event_steps.eventstep import EventStep |
| 19 | from xossynchronizer.modelaccessor import model_accessor |
| 20 | from xossynchronizer.modelaccessor import FabricService, Switch, Service |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 21 | from xosconfig import Config |
| 22 | from multistructlog import create_logger |
| 23 | |
| 24 | log = create_logger(Config().get('logging')) |
| 25 | |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 26 | |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 27 | class KubernetesPodDetailsEventStep(EventStep): |
| 28 | topics = ["xos.kubernetes.pod-details"] |
| 29 | technology = "kafka" |
| 30 | |
| 31 | def __init__(self, *args, **kwargs): |
| 32 | super(KubernetesPodDetailsEventStep, self).__init__(*args, **kwargs) |
| 33 | |
| 34 | @staticmethod |
Himanshu Bhandari | 50797df | 2020-05-02 14:41:55 +0530 | [diff] [blame^] | 35 | def get_fabric_onos(fabric): |
| 36 | service = Service.objects.get(id=fabric.id) |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 37 | # get the onos_fabric service |
Himanshu Bhandari | 50797df | 2020-05-02 14:41:55 +0530 | [diff] [blame^] | 38 | fabric_onos = service.provider_services[0].leaf_model |
| 39 | if not fabric_onos: |
| 40 | log.error("ONOS Service is not found in provider_services of Fabric %s" % service.name) |
| 41 | raise Exception('Configuration error. Fabric without ONOS is not possible.') |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 42 | |
Himanshu Bhandari | 50797df | 2020-05-02 14:41:55 +0530 | [diff] [blame^] | 43 | return fabric_onos |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 44 | |
Himanshu Bhandari | 50797df | 2020-05-02 14:41:55 +0530 | [diff] [blame^] | 45 | @staticmethod |
| 46 | def dirty_switch(fabric_switches=None): |
| 47 | switches = None |
| 48 | if fabric_switches: |
| 49 | switches = fabric_switches |
| 50 | else: |
| 51 | switches = Switch.objects.all() |
| 52 | |
| 53 | for switch in switches: |
| 54 | log.info("Dirtying Switch", switch=switch) |
| 55 | switch.backend_code = 0 |
| 56 | switch.backend_status = "resynchronize due to kubernetes event" |
| 57 | switch.save(update_fields=["updated", "backend_code", "backend_status"], always_update_timestamp=True) |
| 58 | |
| 59 | for port in switch.ports.all(): |
| 60 | log.info("Dirtying SwitchPort", port=port) |
| 61 | port.backend_code = 0 |
| 62 | port.backend_status = "resynchronize due to kubernetes event" |
| 63 | port.save( |
| 64 | update_fields=[ |
| 65 | "updated", |
| 66 | "backend_code", |
| 67 | "backend_status"], |
| 68 | always_update_timestamp=True) |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 69 | |
| 70 | def process_event(self, event): |
Himanshu Bhandari | 50797df | 2020-05-02 14:41:55 +0530 | [diff] [blame^] | 71 | log.info("processing event") |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 72 | value = json.loads(event.value) |
| 73 | |
| 74 | if (value.get("status") != "created"): |
| 75 | return |
| 76 | |
| 77 | if "labels" not in value: |
| 78 | return |
| 79 | |
| 80 | xos_service = value["labels"].get("xos_service") |
| 81 | if not xos_service: |
| 82 | return |
| 83 | |
| 84 | for fabric_service in FabricService.objects.all(): |
| 85 | onos_service = KubernetesPodDetailsEventStep.get_fabric_onos(fabric_service) |
Matteo Scandolo | da3fae9 | 2018-08-28 17:37:13 -0700 | [diff] [blame] | 86 | if (onos_service.name.lower() != xos_service.lower()): |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 87 | continue |
| 88 | |
Himanshu Bhandari | 50797df | 2020-05-02 14:41:55 +0530 | [diff] [blame^] | 89 | if(len(FabricService.objects.all()) == 1): |
| 90 | log.info("Dirtying all switches.") |
| 91 | KubernetesPodDetailsEventStep.dirty_switch() |
| 92 | else: |
| 93 | fabric_switches = fabric_service.switch.all() |
| 94 | KubernetesPodDetailsEventStep.dirty_switch(fabric_switches) |