blob: 223247cda1da231d16c91ea5e738ffcb950c714f [file] [log] [blame]
Scott Bakerb2e8aa12018-08-15 16:04:41 -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
Scott Bakere9855012019-04-01 15:01:34 -070015from __future__ import absolute_import
Scott Bakerb2e8aa12018-08-15 16:04:41 -070016
17import json
Scott Baker382366d2019-02-04 10:58:43 -080018from xossynchronizer.event_steps.eventstep import EventStep
19from xossynchronizer.modelaccessor import model_accessor
20from xossynchronizer.modelaccessor import FabricService, Switch, Service
Scott Bakerb2e8aa12018-08-15 16:04:41 -070021from xosconfig import Config
22from multistructlog import create_logger
23
24log = create_logger(Config().get('logging'))
25
Scott Bakere9855012019-04-01 15:01:34 -070026
Scott Bakerb2e8aa12018-08-15 16:04:41 -070027class 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 Bhandari50797df2020-05-02 14:41:55 +053035 def get_fabric_onos(fabric):
36 service = Service.objects.get(id=fabric.id)
Scott Bakerb2e8aa12018-08-15 16:04:41 -070037 # get the onos_fabric service
Himanshu Bhandari50797df2020-05-02 14:41:55 +053038 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 Bakerb2e8aa12018-08-15 16:04:41 -070042
Himanshu Bhandari50797df2020-05-02 14:41:55 +053043 return fabric_onos
Scott Bakerb2e8aa12018-08-15 16:04:41 -070044
Himanshu Bhandari50797df2020-05-02 14:41:55 +053045 @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 Bakerb2e8aa12018-08-15 16:04:41 -070069
70 def process_event(self, event):
Himanshu Bhandari50797df2020-05-02 14:41:55 +053071 log.info("processing event")
Scott Bakerb2e8aa12018-08-15 16:04:41 -070072 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 Scandoloda3fae92018-08-28 17:37:13 -070086 if (onos_service.name.lower() != xos_service.lower()):
Scott Bakerb2e8aa12018-08-15 16:04:41 -070087 continue
88
Himanshu Bhandari50797df2020-05-02 14:41:55 +053089 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)