blob: a1db209d488427bb2da0cf261926ccf97b0158da [file] [log] [blame]
Shravan Ambatibb6b4452016-05-04 13:25:28 -07001/**
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 */
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
28import com.google.protobuf.GeneratedMessage;
29
30/**
31 * Converts for ONOS Link event message to GPB format.
32 *
33 */
34class 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 Ambati32770ad2016-07-11 21:18:11 -070043 if (!linkEventTypeSupported(linkEvent)) {
Shravan Ambatibb6b4452016-05-04 13:25:28 -070044 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 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 }
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}