Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 1 | /** |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | package org.onosproject.kafkaintegration.converter; |
| 16 | |
Yuta HIGUCHI | 9efba1e | 2016-07-09 11:07:13 -0700 | [diff] [blame] | 17 | import com.google.protobuf.GeneratedMessageV3; |
| 18 | |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 19 | import org.onosproject.event.Event; |
| 20 | import org.onosproject.grpc.net.Device.DeviceCore; |
| 21 | import org.onosproject.grpc.net.Device.DeviceType; |
| 22 | import org.onosproject.grpc.net.DeviceEvent.DeviceEventType; |
| 23 | import org.onosproject.grpc.net.DeviceEvent.DeviceNotification; |
| 24 | import org.onosproject.grpc.net.Port.PortCore; |
| 25 | import org.onosproject.grpc.net.Port.PortType; |
| 26 | import org.onosproject.net.device.DeviceEvent; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 30 | /** |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 31 | * Converts ONOS Device event message to protobuf format. |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 32 | */ |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 33 | public class DeviceEventConverter implements EventConverter { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 34 | |
| 35 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 36 | |
| 37 | @Override |
Yuta HIGUCHI | 9efba1e | 2016-07-09 11:07:13 -0700 | [diff] [blame] | 38 | public GeneratedMessageV3 convertToProtoMessage(Event<?, ?> event) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 39 | |
| 40 | DeviceEvent deviceEvent = (DeviceEvent) event; |
| 41 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 42 | if (!deviceEventTypeSupported(deviceEvent)) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 43 | log.error("Unsupported Onos Device Event {}. There is no matching" |
| 44 | + "proto Device Event type", deviceEvent.type().toString()); |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | return buildDeviceProtoMessage(deviceEvent); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Checks if the ONOS Device Event type is supported. |
| 53 | * |
| 54 | * @param event ONOS Device event |
| 55 | * @return true if there is a match and false otherwise |
| 56 | */ |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 57 | private boolean deviceEventTypeSupported(DeviceEvent event) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 58 | DeviceEventType[] deviceEvents = DeviceEventType.values(); |
| 59 | for (DeviceEventType deviceEventType : deviceEvents) { |
| 60 | if (deviceEventType.name().equals(event.type().name())) { |
| 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | private DeviceNotification buildDeviceProtoMessage(DeviceEvent deviceEvent) { |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 69 | DeviceNotification.Builder notificationBuilder = |
| 70 | DeviceNotification.newBuilder(); |
| 71 | |
| 72 | DeviceCore deviceCore = |
| 73 | DeviceCore.newBuilder() |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 74 | .setChassisId(deviceEvent.subject().chassisId().id() |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 75 | .toString()) |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 76 | .setDeviceId(deviceEvent.subject().id().toString()) |
| 77 | .setHwVersion(deviceEvent.subject().hwVersion()) |
| 78 | .setManufacturer(deviceEvent.subject().manufacturer()) |
| 79 | .setSerialNumber(deviceEvent.subject().serialNumber()) |
| 80 | .setSwVersion(deviceEvent.subject().swVersion()) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 81 | .setType(DeviceType |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 82 | .valueOf(deviceEvent.subject().type().name())) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 83 | .build(); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 84 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 85 | PortCore portCore = null; |
| 86 | if (deviceEvent.port() != null) { |
| 87 | portCore = |
| 88 | PortCore.newBuilder() |
| 89 | .setIsEnabled(deviceEvent.port().isEnabled()) |
| 90 | .setPortNumber(deviceEvent.port().number() |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 91 | .toString()) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 92 | .setPortSpeed(deviceEvent.port().portSpeed()) |
| 93 | .setType(PortType |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 94 | .valueOf(deviceEvent.port().type().name())) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 95 | .build(); |
| 96 | |
| 97 | notificationBuilder.setPort(portCore); |
| 98 | } |
| 99 | |
| 100 | notificationBuilder.setDeviceEventType(getProtoType(deviceEvent)) |
| 101 | .setDevice(deviceCore); |
| 102 | |
| 103 | return notificationBuilder.build(); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Retrieves the protobuf generated device event type. |
| 108 | * |
| 109 | * @param event ONOS Device Event |
| 110 | * @return generated Device Event Type |
| 111 | */ |
| 112 | private DeviceEventType getProtoType(DeviceEvent event) { |
| 113 | DeviceEventType protobufEventType = null; |
| 114 | DeviceEventType[] deviceEvents = DeviceEventType.values(); |
| 115 | for (DeviceEventType deviceEventType : deviceEvents) { |
| 116 | if (deviceEventType.name().equals(event.type().name())) { |
| 117 | protobufEventType = deviceEventType; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return protobufEventType; |
| 122 | } |
| 123 | } |