Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -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; |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 21 | import org.onosproject.grpc.net.device.models.DeviceEnumsProto.DeviceEventTypeProto; |
| 22 | import org.onosproject.grpc.net.device.models.DeviceEnumsProto.DeviceTypeProto; |
| 23 | import org.onosproject.grpc.net.device.models.DeviceEventProto.DeviceNotificationProto; |
| 24 | import org.onosproject.grpc.net.device.models.PortEnumsProto; |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 25 | import org.onosproject.grpc.net.models.DeviceProtoOuterClass.DeviceProto; |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 26 | import org.onosproject.grpc.net.models.PortProtoOuterClass; |
Pravin Chaudhary | 31a8c29 | 2019-01-03 17:43:15 +0530 | [diff] [blame] | 27 | import org.onosproject.incubator.protobuf.models.net.AnnotationsTranslator; |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceEvent; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
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 | * Converts ONOS Device event message to protobuf format. |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 34 | */ |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 35 | public class DeviceEventConverter implements EventConverter { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 36 | |
| 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 38 | |
| 39 | @Override |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 40 | public byte[] convertToProtoMessage(Event<?, ?> event) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 41 | |
| 42 | DeviceEvent deviceEvent = (DeviceEvent) event; |
| 43 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 44 | if (!deviceEventTypeSupported(deviceEvent)) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 45 | log.error("Unsupported Onos Device Event {}. There is no matching" |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 46 | + "proto Device Event type", deviceEvent.type().toString()); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 47 | return null; |
| 48 | } |
| 49 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 50 | return ((GeneratedMessageV3) buildDeviceProtoMessage(deviceEvent)).toByteArray(); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Checks if the ONOS Device Event type is supported. |
| 55 | * |
| 56 | * @param event ONOS Device event |
| 57 | * @return true if there is a match and false otherwise |
| 58 | */ |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 59 | private boolean deviceEventTypeSupported(DeviceEvent event) { |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 60 | DeviceEventTypeProto[] deviceEvents = DeviceEventTypeProto.values(); |
| 61 | for (DeviceEventTypeProto deviceEventType : deviceEvents) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 62 | if (deviceEventType.name().equals(event.type().name())) { |
| 63 | return true; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return false; |
| 68 | } |
| 69 | |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 70 | private DeviceNotificationProto buildDeviceProtoMessage(DeviceEvent deviceEvent) { |
| 71 | DeviceNotificationProto.Builder notificationBuilder = |
| 72 | DeviceNotificationProto.newBuilder(); |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 73 | |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 74 | DeviceProto deviceCore = |
| 75 | DeviceProto.newBuilder() |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 76 | .setChassisId(deviceEvent.subject().chassisId().id() |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 77 | .toString()) |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 78 | .setDeviceId(deviceEvent.subject().id().toString()) |
| 79 | .setHwVersion(deviceEvent.subject().hwVersion()) |
| 80 | .setManufacturer(deviceEvent.subject().manufacturer()) |
| 81 | .setSerialNumber(deviceEvent.subject().serialNumber()) |
| 82 | .setSwVersion(deviceEvent.subject().swVersion()) |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 83 | .setType(DeviceTypeProto |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 84 | .valueOf(deviceEvent.subject().type().name())) |
Pravin Chaudhary | 31a8c29 | 2019-01-03 17:43:15 +0530 | [diff] [blame] | 85 | .putAllAnnotations(AnnotationsTranslator.asMap(deviceEvent.subject().annotations())) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 86 | .build(); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 87 | |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 88 | PortProtoOuterClass.PortProto portProto = null; |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 89 | if (deviceEvent.port() != null) { |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 90 | portProto = |
| 91 | PortProtoOuterClass.PortProto.newBuilder() |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 92 | .setIsEnabled(deviceEvent.port().isEnabled()) |
| 93 | .setPortNumber(deviceEvent.port().number() |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 94 | .toString()) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 95 | .setPortSpeed(deviceEvent.port().portSpeed()) |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 96 | .setType(PortEnumsProto.PortTypeProto |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 97 | .valueOf(deviceEvent.port().type().name())) |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 98 | .build(); |
| 99 | |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 100 | notificationBuilder.setPort(portProto); |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | notificationBuilder.setDeviceEventType(getProtoType(deviceEvent)) |
| 104 | .setDevice(deviceCore); |
| 105 | |
| 106 | return notificationBuilder.build(); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Retrieves the protobuf generated device event type. |
| 111 | * |
| 112 | * @param event ONOS Device Event |
| 113 | * @return generated Device Event Type |
| 114 | */ |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 115 | private DeviceEventTypeProto getProtoType(DeviceEvent event) { |
| 116 | DeviceEventTypeProto protobufEventType = null; |
| 117 | DeviceEventTypeProto[] deviceEvents = DeviceEventTypeProto.values(); |
| 118 | for (DeviceEventTypeProto deviceEventType : deviceEvents) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 119 | if (deviceEventType.name().equals(event.type().name())) { |
| 120 | protobufEventType = deviceEventType; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return protobufEventType; |
| 125 | } |
| 126 | } |