blob: 6b2ee2437ff5fa3142ca23fa21ffdf7879452944 [file] [log] [blame]
Shravan Ambatibb6b4452016-05-04 13:25:28 -07001/**
2 * Copyright 2016 Open Networking Laboratory
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 */
15package org.onosproject.kafkaintegration.converter;
16
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070017import com.google.protobuf.GeneratedMessage;
Shravan Ambatibb6b4452016-05-04 13:25:28 -070018import org.onosproject.event.Event;
19import org.onosproject.grpc.net.Device.DeviceCore;
20import org.onosproject.grpc.net.Device.DeviceType;
21import org.onosproject.grpc.net.DeviceEvent.DeviceEventType;
22import org.onosproject.grpc.net.DeviceEvent.DeviceNotification;
23import org.onosproject.grpc.net.Port.PortCore;
24import org.onosproject.grpc.net.Port.PortType;
25import org.onosproject.net.device.DeviceEvent;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
Shravan Ambatibb6b4452016-05-04 13:25:28 -070029/**
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070030 * Converts ONOS Device event message to protobuf format.
Shravan Ambatibb6b4452016-05-04 13:25:28 -070031 */
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070032public class DeviceEventConverter implements EventConverter {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070033
34 private final Logger log = LoggerFactory.getLogger(getClass());
35
36 @Override
37 public GeneratedMessage convertToProtoMessage(Event<?, ?> event) {
38
39 DeviceEvent deviceEvent = (DeviceEvent) event;
40
Shravan Ambati32770ad2016-07-11 21:18:11 -070041 if (!deviceEventTypeSupported(deviceEvent)) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070042 log.error("Unsupported Onos Device Event {}. There is no matching"
43 + "proto Device Event type", deviceEvent.type().toString());
44 return null;
45 }
46
47 return buildDeviceProtoMessage(deviceEvent);
48 }
49
50 /**
51 * Checks if the ONOS Device Event type is supported.
52 *
53 * @param event ONOS Device event
54 * @return true if there is a match and false otherwise
55 */
Shravan Ambati32770ad2016-07-11 21:18:11 -070056 private boolean deviceEventTypeSupported(DeviceEvent event) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070057 DeviceEventType[] deviceEvents = DeviceEventType.values();
58 for (DeviceEventType deviceEventType : deviceEvents) {
59 if (deviceEventType.name().equals(event.type().name())) {
60 return true;
61 }
62 }
63
64 return false;
65 }
66
67 private DeviceNotification buildDeviceProtoMessage(DeviceEvent deviceEvent) {
Shravan Ambati32770ad2016-07-11 21:18:11 -070068 DeviceNotification.Builder notificationBuilder =
69 DeviceNotification.newBuilder();
70
71 DeviceCore deviceCore =
72 DeviceCore.newBuilder()
Shravan Ambatibb6b4452016-05-04 13:25:28 -070073 .setChassisId(deviceEvent.subject().chassisId().id()
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070074 .toString())
Shravan Ambatibb6b4452016-05-04 13:25:28 -070075 .setDeviceId(deviceEvent.subject().id().toString())
76 .setHwVersion(deviceEvent.subject().hwVersion())
77 .setManufacturer(deviceEvent.subject().manufacturer())
78 .setSerialNumber(deviceEvent.subject().serialNumber())
79 .setSwVersion(deviceEvent.subject().swVersion())
Shravan Ambati32770ad2016-07-11 21:18:11 -070080 .setType(DeviceType
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070081 .valueOf(deviceEvent.subject().type().name()))
Shravan Ambati32770ad2016-07-11 21:18:11 -070082 .build();
Shravan Ambatibb6b4452016-05-04 13:25:28 -070083
Shravan Ambati32770ad2016-07-11 21:18:11 -070084 PortCore portCore = null;
85 if (deviceEvent.port() != null) {
86 portCore =
87 PortCore.newBuilder()
88 .setIsEnabled(deviceEvent.port().isEnabled())
89 .setPortNumber(deviceEvent.port().number()
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070090 .toString())
Shravan Ambati32770ad2016-07-11 21:18:11 -070091 .setPortSpeed(deviceEvent.port().portSpeed())
92 .setType(PortType
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070093 .valueOf(deviceEvent.port().type().name()))
Shravan Ambati32770ad2016-07-11 21:18:11 -070094 .build();
95
96 notificationBuilder.setPort(portCore);
97 }
98
99 notificationBuilder.setDeviceEventType(getProtoType(deviceEvent))
100 .setDevice(deviceCore);
101
102 return notificationBuilder.build();
Shravan Ambatibb6b4452016-05-04 13:25:28 -0700103 }
104
105 /**
106 * Retrieves the protobuf generated device event type.
107 *
108 * @param event ONOS Device Event
109 * @return generated Device Event Type
110 */
111 private DeviceEventType getProtoType(DeviceEvent event) {
112 DeviceEventType protobufEventType = null;
113 DeviceEventType[] deviceEvents = DeviceEventType.values();
114 for (DeviceEventType deviceEventType : deviceEvents) {
115 if (deviceEventType.name().equals(event.type().name())) {
116 protobufEventType = deviceEventType;
117 }
118 }
119
120 return protobufEventType;
121 }
122}