blob: c878a26ca05e5e9671e9e413fcc552147205b2a3 [file] [log] [blame]
Shravan Ambatibb6b4452016-05-04 13:25:28 -07001/**
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Shravan Ambatibb6b4452016-05-04 13:25:28 -07003 * 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 */
15package org.onosproject.kafkaintegration.converter;
16
17import org.onosproject.event.Event;
18import org.onosproject.grpc.net.Link.ConnectPoint;
19import org.onosproject.grpc.net.Link.LinkCore;
20import org.onosproject.grpc.net.Link.LinkState;
21import org.onosproject.grpc.net.Link.LinkType;
22import org.onosproject.grpc.net.LinkEvent.LinkEventType;
23import org.onosproject.grpc.net.LinkEvent.LinkNotification;
24import org.onosproject.net.link.LinkEvent;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
Yuta HIGUCHI9efba1e2016-07-09 11:07:13 -070028import com.google.protobuf.GeneratedMessageV3;
Shravan Ambatibb6b4452016-05-04 13:25:28 -070029
30/**
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070031 * Converts for ONOS Link event message to protobuf format.
Shravan Ambatibb6b4452016-05-04 13:25:28 -070032 */
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070033public class LinkEventConverter implements EventConverter {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070034
35 private final Logger log = LoggerFactory.getLogger(getClass());
36
37 @Override
Yuta HIGUCHI9efba1e2016-07-09 11:07:13 -070038 public GeneratedMessageV3 convertToProtoMessage(Event<?, ?> event) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070039
40 LinkEvent linkEvent = (LinkEvent) event;
41
Shravan Ambati32770ad2016-07-11 21:18:11 -070042 if (!linkEventTypeSupported(linkEvent)) {
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070043 log.error("Unsupported Onos Event {}. There is no matching "
Shravan Ambatibb6b4452016-05-04 13:25:28 -070044 + "proto Event type", linkEvent.type().toString());
45 return null;
46 }
47
48 return buildDeviceProtoMessage(linkEvent);
49 }
50
Shravan Ambati32770ad2016-07-11 21:18:11 -070051 private boolean linkEventTypeSupported(LinkEvent event) {
52 LinkEventType[] kafkaLinkEvents = LinkEventType.values();
53 for (LinkEventType linkEventType : kafkaLinkEvents) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070054 if (linkEventType.name().equals(event.type().name())) {
55 return true;
56 }
57 }
Shravan Ambatibb6b4452016-05-04 13:25:28 -070058 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 Agarwalcb4a3db2016-07-14 11:42:48 -070067 .setType(LinkType.valueOf(linkEvent.subject().type().name()))
Shravan Ambatibb6b4452016-05-04 13:25:28 -070068 .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}