Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -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 os |
| 19 | import sys |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 20 | from xossynchronizer.event_steps.eventstep import EventStep |
| 21 | from xossynchronizer.modelaccessor import model_accessor |
| 22 | from xossynchronizer.modelaccessor import FabricService, Switch, Service |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 23 | from xosconfig import Config |
| 24 | from multistructlog import create_logger |
| 25 | |
| 26 | log = create_logger(Config().get('logging')) |
| 27 | |
| 28 | class KubernetesPodDetailsEventStep(EventStep): |
| 29 | topics = ["xos.kubernetes.pod-details"] |
| 30 | technology = "kafka" |
| 31 | |
| 32 | def __init__(self, *args, **kwargs): |
| 33 | super(KubernetesPodDetailsEventStep, self).__init__(*args, **kwargs) |
| 34 | |
| 35 | @staticmethod |
| 36 | def get_fabric_onos(service): |
| 37 | service = Service.objects.get(id=service.id) |
| 38 | # get the onos_fabric service |
| 39 | fabric_onos = [s.leaf_model for s in service.provider_services if "onos" in s.name.lower()] |
| 40 | |
| 41 | if len(fabric_onos) == 0: |
| 42 | raise Exception('Cannot find ONOS service in provider_services of Fabric') |
| 43 | |
| 44 | return fabric_onos[0] |
| 45 | |
| 46 | def process_event(self, event): |
| 47 | value = json.loads(event.value) |
| 48 | |
| 49 | if (value.get("status") != "created"): |
| 50 | return |
| 51 | |
| 52 | if "labels" not in value: |
| 53 | return |
| 54 | |
| 55 | xos_service = value["labels"].get("xos_service") |
| 56 | if not xos_service: |
| 57 | return |
| 58 | |
| 59 | for fabric_service in FabricService.objects.all(): |
| 60 | onos_service = KubernetesPodDetailsEventStep.get_fabric_onos(fabric_service) |
Matteo Scandolo | da3fae9 | 2018-08-28 17:37:13 -0700 | [diff] [blame] | 61 | if (onos_service.name.lower() != xos_service.lower()): |
Scott Baker | b2e8aa1 | 2018-08-15 16:04:41 -0700 | [diff] [blame] | 62 | continue |
| 63 | |
| 64 | for switch in Switch.objects.all(): |
| 65 | log.info("Dirtying Switch", switch=switch) |
| 66 | switch.backend_code=0 |
| 67 | switch.backend_status="resynchronize due to kubernetes event" |
| 68 | switch.save(update_fields=["updated", "backend_code", "backend_status"], always_update_timestamp=True) |
| 69 | |
| 70 | for port in switch.ports.all(): |
| 71 | log.info("Dirtying SwitchPort", port=port) |
| 72 | port.backend_code=0 |
| 73 | port.backend_status="resynchronize due to kubernetes event" |
| 74 | port.save(update_fields=["updated", "backend_code", "backend_status"], always_update_timestamp=True) |