blob: e08d6e1ba78bc5dd6fd3b98c71301c8fe2350e97 [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
43 if (!deviceEventSubtypeSupported(deviceEvent)) {
44 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 */
58 private boolean deviceEventSubtypeSupported(DeviceEvent event) {
59 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) {
70 DeviceNotification notification = DeviceNotification.newBuilder()
71 .setDeviceEventType(getProtoType(deviceEvent))
72 .setDevice(DeviceCore.newBuilder()
73 .setChassisId(deviceEvent.subject().chassisId().id()
74 .toString())
75 .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())
80 .setType(DeviceType.valueOf(deviceEvent.type().name()))
81 .build())
82 .setPort(PortCore.newBuilder()
83 .setIsEnabled(deviceEvent.port().isEnabled())
84 .setPortNumber(deviceEvent.port().number().toString())
85 .setPortSpeed(deviceEvent.port().portSpeed())
86 .setType(PortType
87 .valueOf(deviceEvent.port().type().name()))
88 .build())
89 .build();
90
91 return notification;
92 }
93
94 /**
95 * Retrieves the protobuf generated device event type.
96 *
97 * @param event ONOS Device Event
98 * @return generated Device Event Type
99 */
100 private DeviceEventType getProtoType(DeviceEvent event) {
101 DeviceEventType protobufEventType = null;
102 DeviceEventType[] deviceEvents = DeviceEventType.values();
103 for (DeviceEventType deviceEventType : deviceEvents) {
104 if (deviceEventType.name().equals(event.type().name())) {
105 protobufEventType = deviceEventType;
106 }
107 }
108
109 return protobufEventType;
110 }
111}