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