Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 1 | /** |
| 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 | */ |
| 15 | package org.onosproject.kafkaintegration.converter; |
| 16 | |
| 17 | import org.onosproject.event.Event; |
| 18 | import org.onosproject.grpc.net.Device.DeviceCore; |
| 19 | import org.onosproject.grpc.net.Device.DeviceType; |
| 20 | import org.onosproject.grpc.net.DeviceEvent.DeviceEventType; |
| 21 | import org.onosproject.grpc.net.DeviceEvent.DeviceNotification; |
| 22 | import org.onosproject.grpc.net.Port.PortCore; |
| 23 | import org.onosproject.grpc.net.Port.PortType; |
| 24 | import org.onosproject.net.device.DeviceEvent; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import com.google.protobuf.GeneratedMessage; |
| 29 | |
| 30 | /** |
| 31 | * Converts ONOS Device event message to GPB format. |
| 32 | * |
| 33 | */ |
| 34 | class 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 | } |