Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 3 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.kafkaintegration.converter; |
| 17 | |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 18 | import com.google.protobuf.GeneratedMessageV3; |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 19 | import org.onosproject.event.Event; |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 20 | import org.onosproject.grpc.net.link.models.LinkEnumsProto.LinkEventTypeProto; |
| 21 | import org.onosproject.grpc.net.link.models.LinkEnumsProto.LinkStateProto; |
| 22 | import org.onosproject.grpc.net.link.models.LinkEnumsProto.LinkTypeProto; |
| 23 | import org.onosproject.grpc.net.link.models.LinkEventProto.LinkNotificationProto; |
| 24 | import org.onosproject.grpc.net.models.ConnectPointProtoOuterClass.ConnectPointProto; |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 25 | import org.onosproject.grpc.net.models.LinkProtoOuterClass.LinkProto; |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 26 | import org.onosproject.net.link.LinkEvent; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 30 | /** |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 31 | * Converts for ONOS Link event message to protobuf format. |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 32 | */ |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 33 | public class LinkEventConverter implements EventConverter { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 34 | |
| 35 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 36 | |
| 37 | @Override |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 38 | public byte[] convertToProtoMessage(Event<?, ?> event) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 39 | |
| 40 | LinkEvent linkEvent = (LinkEvent) event; |
| 41 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 42 | if (!linkEventTypeSupported(linkEvent)) { |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 43 | log.error("Unsupported Onos Event {}. There is no matching " |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 44 | + "proto Event type", linkEvent.type().toString()); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 45 | return null; |
| 46 | } |
| 47 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 48 | return ((GeneratedMessageV3) buildDeviceProtoMessage(linkEvent)).toByteArray(); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 51 | private boolean linkEventTypeSupported(LinkEvent event) { |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 52 | LinkEventTypeProto[] kafkaLinkEvents = LinkEventTypeProto.values(); |
| 53 | for (LinkEventTypeProto linkEventType : kafkaLinkEvents) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 54 | if (linkEventType.name().equals(event.type().name())) { |
| 55 | return true; |
| 56 | } |
| 57 | } |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 61 | private LinkNotificationProto buildDeviceProtoMessage(LinkEvent linkEvent) { |
| 62 | LinkNotificationProto notification = LinkNotificationProto.newBuilder() |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 63 | .setLinkEventType(getProtoType(linkEvent)) |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 64 | .setLink(LinkProto.newBuilder() |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 65 | .setState(LinkStateProto.ACTIVE |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 66 | .valueOf(linkEvent.subject().state().name())) |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 67 | .setType(LinkTypeProto.valueOf(linkEvent.subject().type().name())) |
| 68 | .setDst(ConnectPointProto.newBuilder() |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 69 | .setDeviceId(linkEvent.subject().dst() |
| 70 | .deviceId().toString()) |
| 71 | .setPortNumber(linkEvent.subject().dst().port() |
| 72 | .toString())) |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 73 | .setSrc(ConnectPointProto.newBuilder() |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 74 | .setDeviceId(linkEvent.subject().src() |
| 75 | .deviceId().toString()) |
| 76 | .setPortNumber(linkEvent.subject().src().port() |
| 77 | .toString()))) |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 78 | .build(); |
| 79 | |
| 80 | return notification; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the specific Kafka Device Event Type for the corresponding ONOS |
| 85 | * Device Event Type. |
| 86 | * |
| 87 | * @param event ONOS Device Event |
| 88 | * @return Kafka Device Event Type |
| 89 | */ |
Jian Li | c9b4bf1 | 2017-06-26 23:50:32 +0900 | [diff] [blame] | 90 | private LinkEventTypeProto getProtoType(LinkEvent event) { |
| 91 | LinkEventTypeProto generatedEventType = null; |
| 92 | LinkEventTypeProto[] kafkaEvents = LinkEventTypeProto.values(); |
| 93 | for (LinkEventTypeProto linkEventType : kafkaEvents) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 94 | if (linkEventType.name().equals(event.type().name())) { |
| 95 | generatedEventType = linkEventType; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return generatedEventType; |
| 100 | } |
| 101 | } |