blob: 8eed0c45d9db5c6a5cd8042fe2e92ba88715b35e [file] [log] [blame]
Matteo Scandolo04287a82018-10-02 15:25:26 -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
18import os
19import sys
Scott Baker47b47302019-01-30 16:55:07 -080020from xossynchronizer.event_steps.eventstep import EventStep
21from xossynchronizer.modelaccessor import VOLTService, VOLTServiceInstance, Service
Matteo Scandolo04287a82018-10-02 15:25:26 -070022from xosconfig import Config
23from multistructlog import create_logger
24
25log = create_logger(Config().get('logging'))
26
27class 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
35 def get_onos(service):
36 service = Service.objects.get(id=service.id)
37
38 # get the onos_fabric service
39 onos = [s.leaf_model for s in service.subscriber_services if "onos" in s.name.lower()]
40
41 if len(onos) == 0:
42 raise Exception('Cannot find ONOS service in provider_services of Fabric-Crossconnect')
43
44 return 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 service in VOLTService.objects.all():
60 onos = KubernetesPodDetailsEventStep.get_onos(service)
61 if (onos.name.lower() != xos_service.lower()):
62 continue
63
64 for service_instance in service.service_instances.all():
65 log.info("Dirtying VOLTServiceInstance", service_instance=service_instance)
66 service_instance.backend_code=0
67 service_instance.backend_status="resynchronize due to kubernetes event"
68 service_instance.save(update_fields=["updated", "backend_code", "backend_status"], always_update_timestamp=True)