blob: 3d2eea19bedc047c57e356e843dbb34e4e7c69b1 [file] [log] [blame]
Shravan Ambatibb6b4452016-05-04 13:25:28 -07001/**
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Shravan Ambatibb6b4452016-05-04 13:25:28 -07003 * 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 */
15package org.onosproject.kafkaintegration.converter;
16
Yuta HIGUCHI9efba1e2016-07-09 11:07:13 -070017import com.google.protobuf.GeneratedMessageV3;
18
Shravan Ambatibb6b4452016-05-04 13:25:28 -070019import org.onosproject.event.Event;
20import org.onosproject.grpc.net.Device.DeviceCore;
21import org.onosproject.grpc.net.Device.DeviceType;
22import org.onosproject.grpc.net.DeviceEvent.DeviceEventType;
23import org.onosproject.grpc.net.DeviceEvent.DeviceNotification;
24import org.onosproject.grpc.net.Port.PortCore;
25import org.onosproject.grpc.net.Port.PortType;
26import org.onosproject.net.device.DeviceEvent;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
Shravan Ambatibb6b4452016-05-04 13:25:28 -070030/**
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070031 * Converts ONOS Device event message to protobuf format.
Shravan Ambatibb6b4452016-05-04 13:25:28 -070032 */
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070033public class DeviceEventConverter implements EventConverter {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070034
35 private final Logger log = LoggerFactory.getLogger(getClass());
36
37 @Override
Yuta HIGUCHI9efba1e2016-07-09 11:07:13 -070038 public GeneratedMessageV3 convertToProtoMessage(Event<?, ?> event) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070039
40 DeviceEvent deviceEvent = (DeviceEvent) event;
41
Shravan Ambati32770ad2016-07-11 21:18:11 -070042 if (!deviceEventTypeSupported(deviceEvent)) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070043 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 Ambati32770ad2016-07-11 21:18:11 -070057 private boolean deviceEventTypeSupported(DeviceEvent event) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070058 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 Ambati32770ad2016-07-11 21:18:11 -070069 DeviceNotification.Builder notificationBuilder =
70 DeviceNotification.newBuilder();
71
72 DeviceCore deviceCore =
73 DeviceCore.newBuilder()
Shravan Ambatibb6b4452016-05-04 13:25:28 -070074 .setChassisId(deviceEvent.subject().chassisId().id()
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070075 .toString())
Shravan Ambatibb6b4452016-05-04 13:25:28 -070076 .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 Ambati32770ad2016-07-11 21:18:11 -070081 .setType(DeviceType
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070082 .valueOf(deviceEvent.subject().type().name()))
Shravan Ambati32770ad2016-07-11 21:18:11 -070083 .build();
Shravan Ambatibb6b4452016-05-04 13:25:28 -070084
Shravan Ambati32770ad2016-07-11 21:18:11 -070085 PortCore portCore = null;
86 if (deviceEvent.port() != null) {
87 portCore =
88 PortCore.newBuilder()
89 .setIsEnabled(deviceEvent.port().isEnabled())
90 .setPortNumber(deviceEvent.port().number()
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070091 .toString())
Shravan Ambati32770ad2016-07-11 21:18:11 -070092 .setPortSpeed(deviceEvent.port().portSpeed())
93 .setType(PortType
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070094 .valueOf(deviceEvent.port().type().name()))
Shravan Ambati32770ad2016-07-11 21:18:11 -070095 .build();
96
97 notificationBuilder.setPort(portCore);
98 }
99
100 notificationBuilder.setDeviceEventType(getProtoType(deviceEvent))
101 .setDevice(deviceCore);
102
103 return notificationBuilder.build();
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700104 }
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}