Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 3 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 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 |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 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 | package org.onosproject.kafkaintegration.converter; |
| 17 | |
Yuta HIGUCHI | 9efba1e | 2016-07-09 11:07:13 -0700 | [diff] [blame] | 18 | import com.google.protobuf.GeneratedMessageV3; |
| 19 | |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 20 | import org.onosproject.event.Event; |
| 21 | import org.onosproject.grpc.net.Device.DeviceCore; |
| 22 | import org.onosproject.grpc.net.Device.DeviceType; |
| 23 | import org.onosproject.grpc.net.DeviceEvent.DeviceEventType; |
| 24 | import org.onosproject.grpc.net.DeviceEvent.DeviceNotification; |
| 25 | import org.onosproject.grpc.net.Port.PortCore; |
| 26 | import org.onosproject.grpc.net.Port.PortType; |
| 27 | import org.onosproject.net.device.DeviceEvent; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 31 | /** |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 32 | * Converts ONOS Device event message to protobuf format. |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 33 | */ |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 34 | public class DeviceEventConverter implements EventConverter { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 35 | |
| 36 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 37 | |
| 38 | @Override |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 39 | public byte[] convertToProtoMessage(Event<?, ?> event) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 40 | |
| 41 | DeviceEvent deviceEvent = (DeviceEvent) event; |
| 42 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 43 | if (!deviceEventTypeSupported(deviceEvent)) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 44 | log.error("Unsupported Onos Device Event {}. There is no matching" |
| 45 | + "proto Device Event type", deviceEvent.type().toString()); |
| 46 | return null; |
| 47 | } |
| 48 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 49 | return ((GeneratedMessageV3) buildDeviceProtoMessage(deviceEvent)).toByteArray(); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Checks if the ONOS Device Event type is supported. |
| 54 | * |
| 55 | * @param event ONOS Device event |
| 56 | * @return true if there is a match and false otherwise |
| 57 | */ |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 58 | private boolean deviceEventTypeSupported(DeviceEvent event) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 59 | DeviceEventType[] deviceEvents = DeviceEventType.values(); |
| 60 | for (DeviceEventType deviceEventType : deviceEvents) { |
| 61 | if (deviceEventType.name().equals(event.type().name())) { |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | private DeviceNotification buildDeviceProtoMessage(DeviceEvent deviceEvent) { |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 70 | DeviceNotification.Builder notificationBuilder = |
| 71 | DeviceNotification.newBuilder(); |
| 72 | |
| 73 | DeviceCore deviceCore = |
| 74 | DeviceCore.newBuilder() |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 75 | .setChassisId(deviceEvent.subject().chassisId().id() |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 76 | .toString()) |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 77 | .setDeviceId(deviceEvent.subject().id().toString()) |
| 78 | .setHwVersion(deviceEvent.subject().hwVersion()) |
| 79 | .setManufacturer(deviceEvent.subject().manufacturer()) |
| 80 | .setSerialNumber(deviceEvent.subject().serialNumber()) |
| 81 | .setSwVersion(deviceEvent.subject().swVersion()) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 82 | .setType(DeviceType |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 83 | .valueOf(deviceEvent.subject().type().name())) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 84 | .build(); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 85 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 86 | PortCore portCore = null; |
| 87 | if (deviceEvent.port() != null) { |
| 88 | portCore = |
| 89 | PortCore.newBuilder() |
| 90 | .setIsEnabled(deviceEvent.port().isEnabled()) |
| 91 | .setPortNumber(deviceEvent.port().number() |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 92 | .toString()) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 93 | .setPortSpeed(deviceEvent.port().portSpeed()) |
| 94 | .setType(PortType |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 95 | .valueOf(deviceEvent.port().type().name())) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 96 | .build(); |
| 97 | |
| 98 | notificationBuilder.setPort(portCore); |
| 99 | } |
| 100 | |
| 101 | notificationBuilder.setDeviceEventType(getProtoType(deviceEvent)) |
| 102 | .setDevice(deviceCore); |
| 103 | |
| 104 | return notificationBuilder.build(); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Retrieves the protobuf generated device event type. |
| 109 | * |
| 110 | * @param event ONOS Device Event |
| 111 | * @return generated Device Event Type |
| 112 | */ |
| 113 | private DeviceEventType getProtoType(DeviceEvent event) { |
| 114 | DeviceEventType protobufEventType = null; |
| 115 | DeviceEventType[] deviceEvents = DeviceEventType.values(); |
| 116 | for (DeviceEventType deviceEventType : deviceEvents) { |
| 117 | if (deviceEventType.name().equals(event.type().name())) { |
| 118 | protobufEventType = deviceEventType; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return protobufEventType; |
| 123 | } |
| 124 | } |