blob: 4452636c729f739f9540e16338b748716568b3cd [file] [log] [blame]
Scott Baker1ae0dc92018-08-15 16:26:22 -07001
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
17import json
Scott Baker63afcc12019-02-01 15:41:46 -080018from xossynchronizer.event_steps.eventstep import EventStep
19from xossynchronizer.modelaccessor import FabricCrossconnectService, FabricCrossconnectServiceInstance, Service
Scott Baker1ae0dc92018-08-15 16:26:22 -070020from xosconfig import Config
21from multistructlog import create_logger
22
23log = create_logger(Config().get('logging'))
24
Scott Bakere7b55e42019-04-01 17:18:03 -070025
Scott Baker1ae0dc92018-08-15 16:26:22 -070026class KubernetesPodDetailsEventStep(EventStep):
27 topics = ["xos.kubernetes.pod-details"]
28 technology = "kafka"
29
30 def __init__(self, *args, **kwargs):
31 super(KubernetesPodDetailsEventStep, self).__init__(*args, **kwargs)
32
33 @staticmethod
34 def get_fabric_onos(service):
35 service = Service.objects.get(id=service.id)
36
37 # get the onos_fabric service
38 fabric_onos = [s.leaf_model for s in service.provider_services if "onos" in s.name.lower()]
39
40 if len(fabric_onos) == 0:
41 raise Exception('Cannot find ONOS service in provider_services of Fabric-Crossconnect')
42
43 return fabric_onos[0]
44
45 def process_event(self, event):
46 value = json.loads(event.value)
47
48 if (value.get("status") != "created"):
49 return
50
51 if "labels" not in value:
52 return
53
54 xos_service = value["labels"].get("xos_service")
55 if not xos_service:
56 return
57
58 for service in FabricCrossconnectService.objects.all():
59 fabric_onos = KubernetesPodDetailsEventStep.get_fabric_onos(service)
Matteo Scandolo2c991b32018-08-28 17:37:08 -070060 if (fabric_onos.name.lower() != xos_service.lower()):
Scott Baker1ae0dc92018-08-15 16:26:22 -070061 continue
62
63 for service_instance in service.service_instances.all():
64 log.info("Dirtying FabricCrossconnectServiceInstance", service_instance=service_instance)
Scott Bakere7b55e42019-04-01 17:18:03 -070065 service_instance.backend_code = 0
66 service_instance.backend_status = "resynchronize due to kubernetes event"
67 service_instance.save(
68 update_fields=[
69 "updated",
70 "backend_code",
71 "backend_status"],
72 always_update_timestamp=True)