Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 1 | /** |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 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.Link.ConnectPoint; |
| 19 | import org.onosproject.grpc.net.Link.LinkCore; |
| 20 | import org.onosproject.grpc.net.Link.LinkState; |
| 21 | import org.onosproject.grpc.net.Link.LinkType; |
| 22 | import org.onosproject.grpc.net.LinkEvent.LinkEventType; |
| 23 | import org.onosproject.grpc.net.LinkEvent.LinkNotification; |
| 24 | import org.onosproject.net.link.LinkEvent; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
Yuta HIGUCHI | 9efba1e | 2016-07-09 11:07:13 -0700 | [diff] [blame] | 28 | import com.google.protobuf.GeneratedMessageV3; |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 29 | |
| 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 |
Yuta HIGUCHI | 9efba1e | 2016-07-09 11:07:13 -0700 | [diff] [blame] | 38 | public GeneratedMessageV3 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 " |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 44 | + "proto Event type", linkEvent.type().toString()); |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | return buildDeviceProtoMessage(linkEvent); |
| 49 | } |
| 50 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame] | 51 | private boolean linkEventTypeSupported(LinkEvent event) { |
| 52 | LinkEventType[] kafkaLinkEvents = LinkEventType.values(); |
| 53 | for (LinkEventType 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 | |
| 61 | private LinkNotification buildDeviceProtoMessage(LinkEvent linkEvent) { |
| 62 | LinkNotification notification = LinkNotification.newBuilder() |
| 63 | .setLinkEventType(getProtoType(linkEvent)) |
| 64 | .setLink(LinkCore.newBuilder() |
| 65 | .setState(LinkState |
| 66 | .valueOf(linkEvent.subject().state().name())) |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 67 | .setType(LinkType.valueOf(linkEvent.subject().type().name())) |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 68 | .setDst(ConnectPoint.newBuilder() |
| 69 | .setDeviceId(linkEvent.subject().dst() |
| 70 | .deviceId().toString()) |
| 71 | .setPortNumber(linkEvent.subject().dst().port() |
| 72 | .toString())) |
| 73 | .setSrc(ConnectPoint.newBuilder() |
| 74 | .setDeviceId(linkEvent.subject().src() |
| 75 | .deviceId().toString()) |
| 76 | .setPortNumber(linkEvent.subject().src().port() |
| 77 | .toString()))) |
| 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 | */ |
| 90 | private LinkEventType getProtoType(LinkEvent event) { |
| 91 | LinkEventType generatedEventType = null; |
| 92 | LinkEventType[] kafkaEvents = LinkEventType.values(); |
| 93 | for (LinkEventType linkEventType : kafkaEvents) { |
| 94 | if (linkEventType.name().equals(event.type().name())) { |
| 95 | generatedEventType = linkEventType; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return generatedEventType; |
| 100 | } |
| 101 | } |