Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
| 18 | import org.onosproject.event.Event; |
| 19 | import org.onosproject.grpc.net.Link.ConnectPoint; |
| 20 | import org.onosproject.grpc.net.Link.LinkCore; |
| 21 | import org.onosproject.grpc.net.Link.LinkState; |
| 22 | import org.onosproject.grpc.net.Link.LinkType; |
| 23 | import org.onosproject.grpc.net.LinkEvent.LinkEventType; |
| 24 | import org.onosproject.grpc.net.LinkEvent.LinkNotification; |
| 25 | import org.onosproject.net.link.LinkEvent; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
Yuta HIGUCHI | 9efba1e | 2016-07-09 11:07:13 -0700 | [diff] [blame] | 29 | import com.google.protobuf.GeneratedMessageV3; |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 32 | * Converts for ONOS Link event message to protobuf format. |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 33 | */ |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 34 | public class LinkEventConverter implements EventConverter { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 35 | |
| 36 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 37 | |
| 38 | @Override |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 39 | public byte[] convertToProtoMessage(Event<?, ?> event) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 40 | |
| 41 | LinkEvent linkEvent = (LinkEvent) event; |
| 42 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 43 | if (!linkEventTypeSupported(linkEvent)) { |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 44 | log.error("Unsupported Onos Event {}. There is no matching " |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 45 | + "proto Event type", linkEvent.type().toString()); |
| 46 | return null; |
| 47 | } |
| 48 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 49 | return ((GeneratedMessageV3) buildDeviceProtoMessage(linkEvent)).toByteArray(); |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 52 | private boolean linkEventTypeSupported(LinkEvent event) { |
| 53 | LinkEventType[] kafkaLinkEvents = LinkEventType.values(); |
| 54 | for (LinkEventType linkEventType : kafkaLinkEvents) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 55 | if (linkEventType.name().equals(event.type().name())) { |
| 56 | return true; |
| 57 | } |
| 58 | } |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | |
| 62 | private LinkNotification buildDeviceProtoMessage(LinkEvent linkEvent) { |
| 63 | LinkNotification notification = LinkNotification.newBuilder() |
| 64 | .setLinkEventType(getProtoType(linkEvent)) |
| 65 | .setLink(LinkCore.newBuilder() |
| 66 | .setState(LinkState |
| 67 | .valueOf(linkEvent.subject().state().name())) |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 68 | .setType(LinkType.valueOf(linkEvent.subject().type().name())) |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 69 | .setDst(ConnectPoint.newBuilder() |
| 70 | .setDeviceId(linkEvent.subject().dst() |
| 71 | .deviceId().toString()) |
| 72 | .setPortNumber(linkEvent.subject().dst().port() |
| 73 | .toString())) |
| 74 | .setSrc(ConnectPoint.newBuilder() |
| 75 | .setDeviceId(linkEvent.subject().src() |
| 76 | .deviceId().toString()) |
| 77 | .setPortNumber(linkEvent.subject().src().port() |
| 78 | .toString()))) |
| 79 | .build(); |
| 80 | |
| 81 | return notification; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the specific Kafka Device Event Type for the corresponding ONOS |
| 86 | * Device Event Type. |
| 87 | * |
| 88 | * @param event ONOS Device Event |
| 89 | * @return Kafka Device Event Type |
| 90 | */ |
| 91 | private LinkEventType getProtoType(LinkEvent event) { |
| 92 | LinkEventType generatedEventType = null; |
| 93 | LinkEventType[] kafkaEvents = LinkEventType.values(); |
| 94 | for (LinkEventType linkEventType : kafkaEvents) { |
| 95 | if (linkEventType.name().equals(event.type().name())) { |
| 96 | generatedEventType = linkEventType; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return generatedEventType; |
| 101 | } |
| 102 | } |