Aaron Dunlap | ee83f26 | 2019-01-14 12:32:38 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Foundation |
| 3 | * |
| 4 | * 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * 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 | */ |
| 16 | package org.onosproject.kafkaintegration.converter; |
| 17 | |
| 18 | import com.google.protobuf.GeneratedMessageV3; |
| 19 | |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.event.Event; |
| 22 | import org.onosproject.grpc.net.host.models.HostEnumsProto.HostEventTypeProto; |
| 23 | import org.onosproject.grpc.net.host.models.HostEventProto.HostNotificationProto; |
| 24 | import org.onosproject.grpc.net.models.HostProtoOuterClass.HostProto; |
| 25 | import org.onosproject.incubator.protobuf.models.net.AnnotationsTranslator; |
| 26 | import org.onosproject.incubator.protobuf.models.net.HostIdProtoTranslator; |
| 27 | import org.onosproject.incubator.protobuf.models.net.HostLocationProtoTranslator; |
| 28 | import org.onosproject.net.host.HostEvent; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | import java.util.stream.Collectors; |
| 32 | |
| 33 | /** |
| 34 | * Converts ONOS Host event message to protobuf format. |
| 35 | */ |
| 36 | public class HostEventConverter implements EventConverter { |
| 37 | |
| 38 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 39 | |
| 40 | @Override |
| 41 | public byte[] convertToProtoMessage(Event<?, ?> event) { |
| 42 | |
| 43 | HostEvent hostEvent = (HostEvent) event; |
| 44 | |
| 45 | if (!hostEventTypeSupported(hostEvent)) { |
| 46 | log.error("Unsupported Onos Host Event {}. There is no matching" |
| 47 | + "proto Host Event type", hostEvent.type().toString()); |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | return ((GeneratedMessageV3) buildHostProtoMessage(hostEvent)).toByteArray(); |
| 52 | |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Checks if the ONOS Host Event type is supported. |
| 57 | * |
| 58 | * @param event ONOS Host event |
| 59 | * @return true if there is a match and false otherwise |
| 60 | */ |
| 61 | private boolean hostEventTypeSupported(HostEvent event) { |
| 62 | HostEventTypeProto[] hostEvents = HostEventTypeProto.values(); |
| 63 | for (HostEventTypeProto hostEventType : hostEvents) { |
| 64 | if (hostEventType.name().equals(event.type().name())) { |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | private HostNotificationProto buildHostProtoMessage(HostEvent hostEvent) { |
| 73 | HostNotificationProto.Builder notificationBuilder = |
| 74 | HostNotificationProto.newBuilder(); |
| 75 | |
| 76 | HostProto hostCore = |
| 77 | HostProto.newBuilder() |
| 78 | .setHostId(HostIdProtoTranslator.translate(hostEvent |
| 79 | .subject().id())) |
| 80 | .setConfigured(hostEvent.subject().configured()) |
| 81 | .addAllIpAddresses(hostEvent.subject().ipAddresses() |
| 82 | .stream().map(IpAddress::toString) |
| 83 | .collect(Collectors.toList())) |
| 84 | .setLocation(HostLocationProtoTranslator.translate( |
| 85 | hostEvent.subject().location())) |
| 86 | .setVlan(hostEvent.subject().vlan().toShort()) |
| 87 | .putAllAnnotations(AnnotationsTranslator.asMap( |
| 88 | hostEvent.subject().annotations())) |
| 89 | .build(); |
| 90 | |
| 91 | notificationBuilder.setHostEventType(getProtoType(hostEvent)) |
| 92 | .setHost(hostCore); |
| 93 | |
| 94 | return notificationBuilder.build(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Retrieves the protobuf generated host event type. |
| 99 | * |
| 100 | * @param event ONOS Host Event |
| 101 | * @return generated Host Event Type |
| 102 | */ |
| 103 | private HostEventTypeProto getProtoType(HostEvent event) { |
| 104 | HostEventTypeProto protobufEventType = null; |
| 105 | HostEventTypeProto[] hostEvents = HostEventTypeProto.values(); |
| 106 | for (HostEventTypeProto hostEventType : hostEvents) { |
| 107 | if (hostEventType.name().equals(event.type().name())) { |
| 108 | protobufEventType = hostEventType; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return protobufEventType; |
| 113 | } |
| 114 | } |