blob: 27199f292d94aa3775d02af9f650181825de7e9d [file] [log] [blame]
Scott Baker91bf91b2019-03-15 16:13:51 -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
Scott Bakere9855012019-04-01 15:01:34 -070016from __future__ import absolute_import
Scott Baker91bf91b2019-03-15 16:13:51 -070017
18import json
19from xossynchronizer.event_steps.eventstep import EventStep
20from xosconfig import Config
21from multistructlog import create_logger
22
23log = create_logger(Config().get('logging'))
24
25
26class OnosPortEventStep(EventStep):
27 topics = ["onos.events.port"]
28 technology = "kafka"
29
30 def __init__(self, *args, **kwargs):
31 super(OnosPortEventStep, self).__init__(*args, **kwargs)
32
33 def process_event(self, event):
34 value = json.loads(event.value)
35
36 switch = self.model_accessor.Switch.objects.filter(
37 ofId=value["deviceId"]
38 )
39 if not switch:
40 log.info("Event for unknown switch", deviceId=value["deviceId"])
41 return
42
43 switch = switch[0]
44
45 port = self.model_accessor.SwitchPort.objects.filter(
46 switch_id=switch.id,
47 portId=value["portId"]
48 )
49 if not port:
50 log.info("Event for unknown port",
51 deviceId=value["deviceId"],
52 portId=value["portId"])
53 return
54
55 port = port[0]
56
57 oper_status = "enabled" if value["enabled"] else "disabled"
58 if oper_status != port.oper_status:
59 port.oper_status = oper_status
60 port.save_changed_fields()