blob: a445b146c31127f9b100e898c93a180bdf689003 [file] [log] [blame]
Ray Milkey2d572dd2017-04-14 10:01:24 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Ray Milkey2d572dd2017-04-14 10:01:24 -07003 *
Shravan Ambatibb6b4452016-05-04 13:25:28 -07004 * 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 Milkey2d572dd2017-04-14 10:01:24 -07007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Shravan Ambatibb6b4452016-05-04 13:25:28 -070010 * 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 */
16package org.onosproject.kafkaintegration.converter;
17
Yuta HIGUCHI9efba1e2016-07-09 11:07:13 -070018import com.google.protobuf.GeneratedMessageV3;
19
Shravan Ambatibb6b4452016-05-04 13:25:28 -070020import org.onosproject.event.Event;
Jian Lic9b4bf12017-06-26 23:50:32 +090021import org.onosproject.grpc.net.device.models.DeviceEnumsProto.DeviceEventTypeProto;
22import org.onosproject.grpc.net.device.models.DeviceEnumsProto.DeviceTypeProto;
23import org.onosproject.grpc.net.device.models.DeviceEventProto.DeviceNotificationProto;
24import org.onosproject.grpc.net.device.models.PortEnumsProto;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090025import org.onosproject.grpc.net.models.DeviceProtoOuterClass.DeviceProto;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090026import org.onosproject.grpc.net.models.PortProtoOuterClass;
Shravan Ambatibb6b4452016-05-04 13:25:28 -070027import org.onosproject.net.device.DeviceEvent;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
Shravan Ambatibb6b4452016-05-04 13:25:28 -070031/**
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070032 * Converts ONOS Device event message to protobuf format.
Shravan Ambatibb6b4452016-05-04 13:25:28 -070033 */
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070034public class DeviceEventConverter implements EventConverter {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070035
36 private final Logger log = LoggerFactory.getLogger(getClass());
37
38 @Override
Shravan Ambatia4875d82017-01-09 13:06:51 -080039 public byte[] convertToProtoMessage(Event<?, ?> event) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070040
41 DeviceEvent deviceEvent = (DeviceEvent) event;
42
Shravan Ambati32770ad2016-07-11 21:18:11 -070043 if (!deviceEventTypeSupported(deviceEvent)) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070044 log.error("Unsupported Onos Device Event {}. There is no matching"
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090045 + "proto Device Event type", deviceEvent.type().toString());
Shravan Ambatibb6b4452016-05-04 13:25:28 -070046 return null;
47 }
48
Shravan Ambatia4875d82017-01-09 13:06:51 -080049 return ((GeneratedMessageV3) buildDeviceProtoMessage(deviceEvent)).toByteArray();
Shravan Ambatibb6b4452016-05-04 13:25:28 -070050 }
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 Ambati32770ad2016-07-11 21:18:11 -070058 private boolean deviceEventTypeSupported(DeviceEvent event) {
Jian Lic9b4bf12017-06-26 23:50:32 +090059 DeviceEventTypeProto[] deviceEvents = DeviceEventTypeProto.values();
60 for (DeviceEventTypeProto deviceEventType : deviceEvents) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070061 if (deviceEventType.name().equals(event.type().name())) {
62 return true;
63 }
64 }
65
66 return false;
67 }
68
Jian Lic9b4bf12017-06-26 23:50:32 +090069 private DeviceNotificationProto buildDeviceProtoMessage(DeviceEvent deviceEvent) {
70 DeviceNotificationProto.Builder notificationBuilder =
71 DeviceNotificationProto.newBuilder();
Shravan Ambati32770ad2016-07-11 21:18:11 -070072
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090073 DeviceProto deviceCore =
74 DeviceProto.newBuilder()
Shravan Ambatibb6b4452016-05-04 13:25:28 -070075 .setChassisId(deviceEvent.subject().chassisId().id()
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070076 .toString())
Shravan Ambatibb6b4452016-05-04 13:25:28 -070077 .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())
Jian Lic9b4bf12017-06-26 23:50:32 +090082 .setType(DeviceTypeProto
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070083 .valueOf(deviceEvent.subject().type().name()))
Shravan Ambati32770ad2016-07-11 21:18:11 -070084 .build();
Shravan Ambatibb6b4452016-05-04 13:25:28 -070085
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090086 PortProtoOuterClass.PortProto portProto = null;
Shravan Ambati32770ad2016-07-11 21:18:11 -070087 if (deviceEvent.port() != null) {
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090088 portProto =
89 PortProtoOuterClass.PortProto.newBuilder()
Shravan Ambati32770ad2016-07-11 21:18:11 -070090 .setIsEnabled(deviceEvent.port().isEnabled())
91 .setPortNumber(deviceEvent.port().number()
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070092 .toString())
Shravan Ambati32770ad2016-07-11 21:18:11 -070093 .setPortSpeed(deviceEvent.port().portSpeed())
Jian Lic9b4bf12017-06-26 23:50:32 +090094 .setType(PortEnumsProto.PortTypeProto
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070095 .valueOf(deviceEvent.port().type().name()))
Shravan Ambati32770ad2016-07-11 21:18:11 -070096 .build();
97
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090098 notificationBuilder.setPort(portProto);
Shravan Ambati32770ad2016-07-11 21:18:11 -070099 }
100
101 notificationBuilder.setDeviceEventType(getProtoType(deviceEvent))
102 .setDevice(deviceCore);
103
104 return notificationBuilder.build();
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700105 }
106
107 /**
108 * Retrieves the protobuf generated device event type.
109 *
110 * @param event ONOS Device Event
111 * @return generated Device Event Type
112 */
Jian Lic9b4bf12017-06-26 23:50:32 +0900113 private DeviceEventTypeProto getProtoType(DeviceEvent event) {
114 DeviceEventTypeProto protobufEventType = null;
115 DeviceEventTypeProto[] deviceEvents = DeviceEventTypeProto.values();
116 for (DeviceEventTypeProto deviceEventType : deviceEvents) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700117 if (deviceEventType.name().equals(event.type().name())) {
118 protobufEventType = deviceEventType;
119 }
120 }
121
122 return protobufEventType;
123 }
124}