blob: c8d4bca868de4f0acd739cdecd1305c1d8dfe99d [file] [log] [blame]
Ray Milkey2d572dd2017-04-14 10:01:24 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Ray Milkey2d572dd2017-04-14 10:01:24 -07003 *
Shravan Ambatibb6b4452016-05-04 13:25:28 -07004 * 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 Milkey2d572dd2017-04-14 10:01:24 -07007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Shravan Ambatibb6b4452016-05-04 13:25:28 -070010 * 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 */
16package org.onosproject.kafkaintegration.converter;
17
18import org.onosproject.event.Event;
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090019import org.onosproject.grpc.net.models.ConnectPointProto.ConnectPoint;
20import org.onosproject.grpc.net.models.LinkProtoOuterClass.LinkProto;
21import org.onosproject.grpc.net.models.LinkEnums.LinkState;
22import org.onosproject.grpc.net.models.LinkEnums.LinkType;
23import org.onosproject.grpc.net.models.LinkEnums.LinkEventType;
24import org.onosproject.grpc.net.models.LinkEventProto.LinkNotification;
Shravan Ambatibb6b4452016-05-04 13:25:28 -070025import org.onosproject.net.link.LinkEvent;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
Yuta HIGUCHI9efba1e2016-07-09 11:07:13 -070029import com.google.protobuf.GeneratedMessageV3;
Shravan Ambatibb6b4452016-05-04 13:25:28 -070030
31/**
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070032 * Converts for ONOS Link event message to protobuf format.
Shravan Ambatibb6b4452016-05-04 13:25:28 -070033 */
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070034public class LinkEventConverter implements EventConverter {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070035
36 private final Logger log = LoggerFactory.getLogger(getClass());
37
38 @Override
Shravan Ambatia4875d82017-01-09 13:06:51 -080039 public byte[] convertToProtoMessage(Event<?, ?> event) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070040
41 LinkEvent linkEvent = (LinkEvent) event;
42
Shravan Ambati32770ad2016-07-11 21:18:11 -070043 if (!linkEventTypeSupported(linkEvent)) {
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070044 log.error("Unsupported Onos Event {}. There is no matching "
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090045 + "proto Event type", linkEvent.type().toString());
Shravan Ambatibb6b4452016-05-04 13:25:28 -070046 return null;
47 }
48
Shravan Ambatia4875d82017-01-09 13:06:51 -080049 return ((GeneratedMessageV3) buildDeviceProtoMessage(linkEvent)).toByteArray();
Shravan Ambatibb6b4452016-05-04 13:25:28 -070050 }
51
Shravan Ambati32770ad2016-07-11 21:18:11 -070052 private boolean linkEventTypeSupported(LinkEvent event) {
53 LinkEventType[] kafkaLinkEvents = LinkEventType.values();
54 for (LinkEventType linkEventType : kafkaLinkEvents) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070055 if (linkEventType.name().equals(event.type().name())) {
56 return true;
57 }
58 }
Shravan Ambatibb6b4452016-05-04 13:25:28 -070059 return false;
60 }
61
62 private LinkNotification buildDeviceProtoMessage(LinkEvent linkEvent) {
63 LinkNotification notification = LinkNotification.newBuilder()
64 .setLinkEventType(getProtoType(linkEvent))
Aaron Kruglikov9f95f992017-06-23 14:15:25 +090065 .setLink(LinkProto.newBuilder()
66 .setState(LinkState
67 .valueOf(linkEvent.subject().state().name()))
68 .setType(LinkType.valueOf(linkEvent.subject().type().name()))
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())))
Shravan Ambatibb6b4452016-05-04 13:25:28 -070079 .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}