blob: ac04040fed9a0b29ab58c60acabda05b9bf56eca [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
17import org.onosproject.event.Event;
18import org.onosproject.grpc.net.Device.DeviceCore;
19import org.onosproject.grpc.net.Device.DeviceType;
20import org.onosproject.grpc.net.DeviceEvent.DeviceEventType;
21import org.onosproject.grpc.net.DeviceEvent.DeviceNotification;
22import org.onosproject.grpc.net.Port.PortCore;
23import org.onosproject.grpc.net.Port.PortType;
24import org.onosproject.net.device.DeviceEvent;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import com.google.protobuf.GeneratedMessage;
29
30/**
31 * Converts ONOS Device event message to GPB format.
32 *
33 */
34class DeviceEventConverter implements EventConverter {
35
36 private final Logger log = LoggerFactory.getLogger(getClass());
37
38 @Override
39 public GeneratedMessage convertToProtoMessage(Event<?, ?> event) {
40
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"
45 + "proto Device Event type", deviceEvent.type().toString());
46 return null;
47 }
48
49 return buildDeviceProtoMessage(deviceEvent);
50 }
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) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070059 DeviceEventType[] deviceEvents = DeviceEventType.values();
60 for (DeviceEventType deviceEventType : deviceEvents) {
61 if (deviceEventType.name().equals(event.type().name())) {
62 return true;
63 }
64 }
65
66 return false;
67 }
68
69 private DeviceNotification buildDeviceProtoMessage(DeviceEvent deviceEvent) {
Shravan Ambati32770ad2016-07-11 21:18:11 -070070 DeviceNotification.Builder notificationBuilder =
71 DeviceNotification.newBuilder();
72
73 DeviceCore deviceCore =
74 DeviceCore.newBuilder()
Shravan Ambatibb6b4452016-05-04 13:25:28 -070075 .setChassisId(deviceEvent.subject().chassisId().id()
76 .toString())
77 .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())
Shravan Ambati32770ad2016-07-11 21:18:11 -070082 .setType(DeviceType
83 .valueOf(deviceEvent.subject().type().name()))
84 .build();
Shravan Ambatibb6b4452016-05-04 13:25:28 -070085
Shravan Ambati32770ad2016-07-11 21:18:11 -070086 PortCore portCore = null;
87 if (deviceEvent.port() != null) {
88 portCore =
89 PortCore.newBuilder()
90 .setIsEnabled(deviceEvent.port().isEnabled())
91 .setPortNumber(deviceEvent.port().number()
92 .toString())
93 .setPortSpeed(deviceEvent.port().portSpeed())
94 .setType(PortType
95 .valueOf(deviceEvent.port().type().name()))
96 .build();
97
98 notificationBuilder.setPort(portCore);
99 }
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 */
113 private DeviceEventType getProtoType(DeviceEvent event) {
114 DeviceEventType protobufEventType = null;
115 DeviceEventType[] deviceEvents = DeviceEventType.values();
116 for (DeviceEventType deviceEventType : deviceEvents) {
117 if (deviceEventType.name().equals(event.type().name())) {
118 protobufEventType = deviceEventType;
119 }
120 }
121
122 return protobufEventType;
123 }
124}