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.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 | |
| 28 | import com.google.protobuf.GeneratedMessage; |
| 29 | |
| 30 | /** |
| 31 | * Converts for ONOS Link event message to GPB format. |
| 32 | * |
| 33 | */ |
| 34 | class LinkEventConverter implements EventConverter { |
| 35 | |
| 36 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 37 | |
| 38 | @Override |
| 39 | public GeneratedMessage convertToProtoMessage(Event<?, ?> event) { |
| 40 | |
| 41 | LinkEvent linkEvent = (LinkEvent) event; |
| 42 | |
Shravan Ambati | 32770ad | 2016-07-11 21:18:11 -0700 | [diff] [blame^] | 43 | if (!linkEventTypeSupported(linkEvent)) { |
Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 44 | log.error("Unsupported Onos Event {}. There is no matching" |
| 45 | + "proto Event type", linkEvent.type().toString()); |
| 46 | return null; |
| 47 | } |
| 48 | |
| 49 | return buildDeviceProtoMessage(linkEvent); |
| 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 | } |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | private LinkNotification buildDeviceProtoMessage(LinkEvent linkEvent) { |
| 64 | LinkNotification notification = LinkNotification.newBuilder() |
| 65 | .setLinkEventType(getProtoType(linkEvent)) |
| 66 | .setLink(LinkCore.newBuilder() |
| 67 | .setState(LinkState |
| 68 | .valueOf(linkEvent.subject().state().name())) |
| 69 | .setType(LinkType |
| 70 | .valueOf(linkEvent.subject().type().name())) |
| 71 | .setDst(ConnectPoint.newBuilder() |
| 72 | .setDeviceId(linkEvent.subject().dst() |
| 73 | .deviceId().toString()) |
| 74 | .setPortNumber(linkEvent.subject().dst().port() |
| 75 | .toString())) |
| 76 | .setSrc(ConnectPoint.newBuilder() |
| 77 | .setDeviceId(linkEvent.subject().src() |
| 78 | .deviceId().toString()) |
| 79 | .setPortNumber(linkEvent.subject().src().port() |
| 80 | .toString()))) |
| 81 | .build(); |
| 82 | |
| 83 | return notification; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the specific Kafka Device Event Type for the corresponding ONOS |
| 88 | * Device Event Type. |
| 89 | * |
| 90 | * @param event ONOS Device Event |
| 91 | * @return Kafka Device Event Type |
| 92 | */ |
| 93 | private LinkEventType getProtoType(LinkEvent event) { |
| 94 | LinkEventType generatedEventType = null; |
| 95 | LinkEventType[] kafkaEvents = LinkEventType.values(); |
| 96 | for (LinkEventType linkEventType : kafkaEvents) { |
| 97 | if (linkEventType.name().equals(event.type().name())) { |
| 98 | generatedEventType = linkEventType; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return generatedEventType; |
| 103 | } |
| 104 | } |