Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -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 | package org.onosproject.kafkaintegration; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.Map; |
| 21 | import java.util.UUID; |
| 22 | |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.kafkaintegration.api.EventExporterService; |
| 32 | import org.onosproject.kafkaintegration.api.dto.EventSubscriber; |
| 33 | import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId; |
| 34 | import org.onosproject.kafkaintegration.errors.InvalidApplicationException; |
| 35 | import org.onosproject.kafkaintegration.errors.InvalidGroupIdException; |
| 36 | import org.onosproject.kafkaintegration.errors.UnsupportedEventException; |
| 37 | import org.onosproject.net.device.DeviceService; |
| 38 | import org.onosproject.net.link.LinkService; |
| 39 | import org.onosproject.store.serializers.KryoNamespaces; |
| 40 | import org.onosproject.store.service.Serializer; |
| 41 | import org.onosproject.store.service.StorageService; |
| 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | /** |
| 46 | * Implementation of Event Exporter Service. |
| 47 | * |
| 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | @Service |
| 51 | public class EventExporterManager implements EventExporterService { |
| 52 | |
| 53 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 54 | |
| 55 | // Stores the currently registered applications for event export service. |
| 56 | // Map of Appname to groupId |
| 57 | private Map<ApplicationId, EventSubscriberGroupId> registeredApps; |
| 58 | |
| 59 | private static final String REGISTERED_APPS = "registered-applications"; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected DeviceService deviceService; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected LinkService linkService; |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected CoreService coreService; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected StorageService storageService; |
| 72 | |
| 73 | private static final String NOT_YET_SUPPORTED = "Not yet supported."; |
| 74 | |
| 75 | private ApplicationId appId; |
| 76 | |
| 77 | @Activate |
| 78 | protected void activate() { |
| 79 | appId = coreService |
| 80 | .registerApplication("org.onosproject.kafkaintegration"); |
| 81 | |
| 82 | registeredApps = storageService |
| 83 | .<ApplicationId, EventSubscriberGroupId>consistentMapBuilder() |
| 84 | .withName(REGISTERED_APPS) |
| 85 | .withSerializer(Serializer.using(KryoNamespaces.API, |
| 86 | EventSubscriberGroupId.class, |
| 87 | UUID.class)) |
| 88 | .build().asJavaMap(); |
| 89 | |
| 90 | log.info("Started"); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | protected void deactivate() { |
| 95 | log.info("Stopped"); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public EventSubscriberGroupId registerListener(String appName) { |
| 100 | |
| 101 | // TODO: Remove it once ONOS provides a mechanism for external apps |
| 102 | // to register with the core service. See Jira - 4409 |
| 103 | ApplicationId externalAppId = coreService.registerApplication(appName); |
| 104 | |
| 105 | return registeredApps.computeIfAbsent(externalAppId, |
| 106 | (key) -> new EventSubscriberGroupId(UUID |
| 107 | .randomUUID())); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void unregisterListener(String appName) { |
| 113 | ApplicationId externalAppId = |
| 114 | checkNotNull(coreService.getAppId(appName)); |
| 115 | registeredApps.remove(externalAppId); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void subscribe(EventSubscriber subscriber) |
| 120 | throws UnsupportedEventException, InvalidGroupIdException, |
| 121 | InvalidApplicationException { |
| 122 | |
| 123 | throw new UnsupportedOperationException(NOT_YET_SUPPORTED); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void unsubscribe(EventSubscriber subscriber) |
| 128 | throws InvalidGroupIdException, InvalidApplicationException { |
| 129 | |
| 130 | throw new UnsupportedOperationException(NOT_YET_SUPPORTED); |
| 131 | } |
| 132 | } |