Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.kafkaintegration.impl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Service; |
| 23 | import org.onosproject.event.Event; |
| 24 | import org.onosproject.kafkaintegration.api.EventConversionService; |
| 25 | import org.onosproject.kafkaintegration.api.dto.OnosEvent; |
| 26 | import org.onosproject.kafkaintegration.converter.DeviceEventConverter; |
| 27 | import org.onosproject.kafkaintegration.converter.EventConverter; |
| 28 | import org.onosproject.kafkaintegration.converter.LinkEventConverter; |
| 29 | import org.onosproject.net.device.DeviceEvent; |
| 30 | import org.onosproject.net.link.LinkEvent; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import static org.onosproject.kafkaintegration.api.dto.OnosEvent.Type.DEVICE; |
| 35 | import static org.onosproject.kafkaintegration.api.dto.OnosEvent.Type.LINK; |
| 36 | |
| 37 | /** |
| 38 | * Implementation of Event Conversion Service. |
| 39 | * |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | @Service |
| 43 | public class EventConversionManager implements EventConversionService { |
| 44 | |
| 45 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 46 | private EventConverter deviceEventConverter; |
| 47 | private EventConverter linkEventConverter; |
| 48 | |
| 49 | @Activate |
| 50 | protected void activate() { |
| 51 | deviceEventConverter = new DeviceEventConverter(); |
| 52 | linkEventConverter = new LinkEventConverter(); |
| 53 | |
| 54 | log.info("Started"); |
| 55 | } |
| 56 | |
| 57 | @Deactivate |
| 58 | protected void deactivate() { |
| 59 | log.info("Stopped"); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public OnosEvent convertEvent(Event<?, ?> event) { |
| 64 | if (event instanceof DeviceEvent) { |
| 65 | return new OnosEvent(DEVICE, deviceEventConverter.convertToProtoMessage(event)); |
| 66 | } else if (event instanceof LinkEvent) { |
| 67 | return new OnosEvent(LINK, linkEventConverter.convertToProtoMessage(event)); |
| 68 | } else { |
| 69 | throw new IllegalArgumentException("Unsupported event type"); |
| 70 | } |
| 71 | } |
| 72 | } |